1 Topological sort From a given partial order, produce a - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 Topological sort From a given partial order, produce a - - PDF document

Chair of Softw are Engineering Einfhrung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer October 2006 February 2007 Lecture 19: Topological sort Part 1: Problem and math basis Intro. to Programming, lecture 19:


slide-1
SLIDE 1

1

  • Intro. to Programming, lecture 19: Topological Sort I 1

Einführung in die Programmierung Introduction to Programming

  • Prof. Dr. Bertrand Meyer

October 2006 – February 2007

Chair of Softw are Engineering

Lecture 19: Topological sort Part 1: Problem and math basis

  • Intro. to Programming, lecture 19: Topological Sort I 2

3

by Caran d’Ache

slide-2
SLIDE 2

2

  • Intro. to Programming, lecture 19: Topological Sort I 4

“Topological sort”

From a given partial order, produce a compatible total order

  • Intro. to Programming, lecture 19: Topological Sort I 5

The problem

Partial order: ordering constraints between elements of a set, e.g.

“Remove the dishes before discussing politics” “Walk to Üetliberg before lunch” “Take your medicine before lunch” “Finish lunch before removing dishes”

Total order: sequence including all elements of set Compatible: the sequence respects all ordering constraints

Üetliberg, Medicine, Lunch, Dishes, Politics : OK Medicine, Üetliberg, Lunch, Dishes, Politics : OK Politics, Medicine, Lunch, Dishes, Üetliberg : not OK

From a given partial order, produce a compatible total order

  • Intro. to Programming, lecture 19: Topological Sort I 6

Why we are doing this!

Very common problem in many different areas Interesting, efficient, non-trivial algorithm Illustration of many algorithmic techniques Illustration of data structures, complexity (big-Oh

notation), and other topics of last lecture

Illustration of software engineering techniques: from

algorithm to component with useful API

Opportunity to learn or rediscover important

mathematical concepts: binary relations (order relations in particular) and their properties

It’s just beautiful!

Today: problem and math basis Next time: detailed algorithm and component

slide-3
SLIDE 3

3

  • Intro. to Programming, lecture 19: Topological Sort I 7

Reading assignment for next Monday

Touch of Class, chapter on topological sort: 17

  • Intro. to Programming, lecture 19: Topological Sort I 8

Topological sort: example uses

From a dictionary, produce a list of definitions such that no word

  • ccurs prior to its definition

Produce a complete schedule for carrying out a set of tasks, subject to ordering constraints (This is done for scheduling maintenance tasks for industrial tasks,

  • ften with thousands of constraints)

Produce a version of a class with the features reordered so that no feature call appears before the feature’s declaration

  • Intro. to Programming, lecture 19: Topological Sort I 9

Rectangles with overlap constraints

B D A C E Constraints: [B, A], [D, A], [A, C], [B, D], [D, C]

slide-4
SLIDE 4

4

  • Intro. to Programming, lecture 19: Topological Sort I 10

Rectangles with overlap constraints

B D A C E B D E A C Constraints: [B, A], [D, A], [A, C], [B, D], [D, C] Possible solution:

  • Intro. to Programming, lecture 19: Topological Sort I 11

The problem

Partial order: ordering constraints between elements of a set, e.g.

“Remove the dishes before discussing politics” “Walk to Üetliberg before lunch” “Take your medicine before lunch” “Finish lunch before removing dishes”

Total order: sequence including all elements of set Compatible: the sequence respects all ordering constraints

Üetliberg, Medicine, Lunch, Dishes, Politics : OK Medicine, Üetliberg, Lunch, Dishes, Politics : OK Politics, Medicine, Lunch, Dishes, Üetliberg : not OK

From a given partial order, produce a compatible total order

  • Intro. to Programming, lecture 19: Topological Sort I 12

Pictured as a graph

“Remove the dishes before discussing politics” “Walk to Üetliberg before lunch” “Take your medicine before lunch” “Finish lunch before removing dishes”

Üetliberg Medicine Lunch Dishes Politics

slide-5
SLIDE 5

5

  • Intro. to Programming, lecture 19: Topological Sort I 13

Sometimes there is no solution

“Introducing recursion requires that

students know about stacks”

“You must discuss abstract data types

before introducing stacks

“Abstract data types rely on recursion”

The constraints introduce a cycle

  • Intro. to Programming, lecture 19: Topological Sort I 14

Overall structure (1)

Given: Required: An enumeration of the elements, in an order compatible with the constraints A type G A set of elements

  • f type G

constraints: LIST [TUPLE [G, G ]] topsort : LIST [G ] is ... ensure compatible (Result, constraints) class ORDERABLE [G] feature end A set of constraints between these elements elements: LIST [G ]

  • Intro. to Programming, lecture 19: Topological Sort I 15

Some mathematical background...

slide-6
SLIDE 6

6

  • Intro. to Programming, lecture 19: Topological Sort I 16

Binary relation on a set

Any property that either holds or doesn’t hold between two elements of a set On a set PERSON of persons, example relations are:

mother : a mother b holds if and only if a is the

mother of b

father : child : sister

:

sibling

: (brother or sister)

Notation: a r b to express that r holds of a and b.

Note: relation names will appear in green

  • Intro. to Programming, lecture 19: Topological Sort I 17

Example: the before relation

The set of interest:

Tasks = {Politics, Lunch, Medicine, Dishes, Üetliberg}

The constraining relation:

Dishes before Politics Üetliberg before Lunch Medicine before Lunch Lunch before Dishes

“Remove the dishes before discussing politics” “Walk to Üetliberg before lunch” “Take your medicine before lunch” “Finish lunch before removing dishes”

  • Intro. to Programming, lecture 19: Topological Sort I 18

Some special relations on a set X

universal [X ]: holds between any two elements of X id [X ]: holds between every element of X and itself empty [X ]: holds between no elements of X

slide-7
SLIDE 7

7

  • Intro. to Programming, lecture 19: Topological Sort I 19

Relations: a more precise mathematical view

We consider a relation r on a set P as: A set of pairs in P x P, containing all the pairs [x, y] such that x r y.

Then x r y simply means that [x, y] ∈ r

See examples on next slide

  • Intro. to Programming, lecture 19: Topological Sort I 20

Example: the before relation

The set of interest: elements = {Politics, Lunch, Medicine, Dishes, Üetliberg} The constraining relation: before = {[Dishes, Politics], [Üetliberg, Lunch], [Medicine, Lunch], [Lunch, Dishes]}

“Remove dishes before discussing politics” “Walk to Üetliberg before lunch” “Take your medicine before lunch” “Finish lunch before removing dishes”

  • Intro. to Programming, lecture 19: Topological Sort I 21

Using ordinary set operators

spouse = wife ∪ husband sibling = sister ∪ brother ∪ id [Person] sister ⊆ sibling father ⊆ ancestor universal [X ] = X x X (cartesian product) empty [X ] = ∅

slide-8
SLIDE 8

8

  • Intro. to Programming, lecture 19: Topological Sort I 22

Possible properties of a relation

(On a set X. All definitions must hold for every a, b, c... ∈ X.) Total*: (a ≠ b) ⇒ ((a r b) ∨ (b r a)) Reflexive: a r a Irreflexive: not (a r a) Symmetric: a r b ⇒ b r a Antisymmetric: (a r b) ∧ (b r a) ⇒ a = b Asymmetric: not ((a r b) ∧ (b r a)) Transitive: (a r b) ∧ (b r c) ⇒ a r c

*Definition of “total” is specific to this discussion (there is no standard definition). The other terms are standard.

  • Intro. to Programming, lecture 19: Topological Sort I 23

Examples (on a set of persons)

sibling Total: (a ≠ b) ⇒ (a r b) ∨ (b r a) Reflexive: a r a Irreflexive: not (a r a) Symmetric: a r b ⇒ b r a Antisymmetric: (a r b) ∧ (b r a) ⇒ a = b Asymmetric: not ((a r b) ∧ (b r a)) Transitive: (a r b) ∧ (b r c) ⇒ a r c

Reflexive, symmetric, transitive

sister

Symmetric, irreflexive Reflexive, antisymmetric

family_head

(a family_head b means a is the head

  • f b’s family, with one head per family)

mother

Asymmetric, irreflexive

  • Intro. to Programming, lecture 19: Topological Sort I 24

Total order relation (strict)

Relation is strict total order if:

  • Total
  • Irreflexive
  • Transitive

Example: “less than” < on natural numbers

0 < 1 0 < 2, 0 < 3, 0 < 4, ... 1 < 2 1 < 3 1 < 4, ... 2 < 3 2 < 4, ...

...

1 2 3 4 5

Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a)) Irreflexive: not (a r a) Symmetric: a r b ⇒ b r a Asymmetric: not ((a r b) ∧ (b r a)) Transitive: (a r b) ∧ (b r c) ⇒ a r c

slide-9
SLIDE 9

9

  • Intro. to Programming, lecture 19: Topological Sort I 25

Theorem

A strict order is asymmetric (total)

  • Intro. to Programming, lecture 19: Topological Sort I 26

Total order relation (strict)

Relation is strict total order if:

Total Irreflexive Transitive Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a)) Irreflexive: not (a r a) Symmetric: a r b ⇒ b r a Asymmetric: not ((a r b) ∧ (b r a)) Transitive: (a r b) ∧ (b r c) ⇒ a r c

Theorem: A strict order is asymmetric total

  • Intro. to Programming, lecture 19: Topological Sort I 27

Total order relation (non-strict)

Relation is non-strict total order if: Total Reflexive Transitive Antisymmetric

Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a)) Irreflexive: not (a r a) Symmetric: a r b ⇒ b r a Antisymmetric: (a r b) ∧ (b r a) ⇒ a = b Transitive:(a r b) ∧ (b r c) ⇒ a r c

Example: “less than or equal” ≤ on natural numbers

0 ≤ 0 0 ≤ 1, 0 ≤ 2, 0 ≤ 3, 0 ≤ 4, ... 1 ≤ 1 0 ≤ 2, 0 ≤ 3, 0 ≤ 4, ... 1 ≤ 2, 1 ≤ 3, 1 ≤ 4, ... 2 ≤ 2 2 ≤ 3, 2 ≤ 4, ...

...

1 2 3 4 5

slide-10
SLIDE 10

10

  • Intro. to Programming, lecture 19: Topological Sort I 28

Total order relation (strict)

Relation is strict total order if:

Total Irreflexive Transitive Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a)) Irreflexive: not (a r a) Symmetric: a r b ⇒ b r a Antisymmetric: (a r b) ∧ (b r a) ⇒ a = b Transitive:(a r b) ∧ (b r c) ⇒ a r c

  • Intro. to Programming, lecture 19: Topological Sort I 29

Partial order relation (strict)

[1, 2]

1 2

b a

[0, 1]

y x

1 2 3

[3, 0]

c

[4, 2]

d Example: relation between points in a plane: p q if both xp < xq yp < yq <

Relation is strict partial order if:

Total Irreflexive Transitive Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a)) Irreflexive: not (a r a) Symmetric: a r b ⇒ b r a Antisymmetric: (a r b) ∧ (b r a) ⇒ a = b Transitive:(a r b) ∧ (b r c) ⇒ a r c

  • Intro. to Programming, lecture 19: Topological Sort I 30

Theorems

A strict order is asymmetric (total) partial A total order is a partial order (“partial” order really means possibly partial)

slide-11
SLIDE 11

11

  • Intro. to Programming, lecture 19: Topological Sort I 31

Example partial order

Here the following hold: c a No link between a and c, b and c: a c nor e.g. neither

[1, 2]

1 2

b a

[0, 1]

y

1 2 3

[3, 0]

c

[4, 2]

d

p q if both xp < xq yp < yq

a < b a < d c < d < < <

  • Intro. to Programming, lecture 19: Topological Sort I 32

Possible topological sorts

1 2

b a

y

1 2 3 c

d a < b a < d c < d

  • Intro. to Programming, lecture 19: Topological Sort I 33

Topological sort understood

< Here the relation is: {[a, b], [a, d], [c, d]}

1 2

b a

y

1 2 3 c

d We are looking for a total order relation t such that

⊆ t

< One of the solutions is: a, b, c, d a < b a < d c < d

slide-12
SLIDE 12

12

  • Intro. to Programming, lecture 19: Topological Sort I 34

Final statement of topological sort problem From a given partial order, produce a compatible total order where: A partial order p is compatible with a total order t if and only if p ⊆ t

  • Intro. to Programming, lecture 19: Topological Sort I 35

From constraints to partial orders

Is a relation defined by a set of constraints, such as constraints = {[Dishes, Politics], [Üetliberg, Lunch], [Medicine, Lunch], [Lunch, Dishes]} always a partial order?

Üetliberg Medicine Lunch Dishes Politics

  • Intro. to Programming, lecture 19: Topological Sort I 36

Powers and transitive closure of a relation

r i +1 = r i ; r where ; is composition Transitive closure r + = r 1 ∪ r 2 ∪ ... always transitive

Ü M L D P r 1 r 2 r 3

slide-13
SLIDE 13

13

  • Intro. to Programming, lecture 19: Topological Sort I 37

Reflexive transitive closure

r i +1 = r i ; r where ; is composition Transitive closure r + = r 1 ∪ r 2 ∪ ... always transitive

Ü M L D P r 1 r 2 r 3 r 0

Reflexive transitive closure: r ∗ = r 0 ∪ r 1 ∪ r 2 ∪ ... always transitive and reflexive r 0 = id [X ] where X is the underlying set

  • Intro. to Programming, lecture 19: Topological Sort I 38

Acyclic relation

r + ∩ id [X ] = ∅ before + id [X]

Ü M L D P

A relation r on a set X is acyclic if and only if:

  • Intro. to Programming, lecture 19: Topological Sort I 39

Acyclic relations and partial orders

Theorems:

Any (strict) order relation is acyclic. A relation is acyclic if and only if its transitive

closure is a (strict) order.

(Also: if and only if its reflexive transitive closure is a nonstrict partial order)

slide-14
SLIDE 14

14

  • Intro. to Programming, lecture 19: Topological Sort I 40

From constraints to partial orders

The partial order of interest is before + before = {[Dishes, Politics], [Üetliberg, Lunch], [Medicine, Lunch], [Lunch, Dishes]} Üetliberg Medicine Lunch Dishes Politics

  • Intro. to Programming, lecture 19: Topological Sort I 41

Back to software...

  • Intro. to Programming, lecture 19: Topological Sort I 42

The basic algorithm idea

topsort

p q r s t u v w

slide-15
SLIDE 15

15

  • Intro. to Programming, lecture 19: Topological Sort I 43

What we have seen

The topological sort problem and its applications Mathematical background:

Relations as sets of pairs Properties of relations Order relations: partial/total, strict/nonstrict Transitive, reflexive-transitive closures The relation between acyclic and order relations The basic idea of topological sort

Next: how to do it for

Efficient operation (O (m+n) for m constraints & n items) Good software engineering: effective API

  • Intro. to Programming, lecture 19: Topological Sort I 44

Reading assignment for next Monday

Touch of Class, chapter on topological sort: 17

  • Intro. to Programming, lecture 19: Topological Sort I 45

End of lecture 19