19 topological sort - ETH Zse.inf.ethz.ch/.../0001/slides/19_topological_sort_6up.pdf3 Intro. to...

8
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 Software 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 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

Transcript of 19 topological sort - ETH Zse.inf.ethz.ch/.../0001/slides/19_topological_sort_6up.pdf3 Intro. to...

Page 1: 19 topological sort - ETH Zse.inf.ethz.ch/.../0001/slides/19_topological_sort_6up.pdf3 Intro. to Programming, lecture 19: Topological Sort I 13 Sometimes there is no solution ¾“Introducing

1

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

Einführung in die ProgrammierungIntroduction to Programming

Prof. Dr. Bertrand MeyerOctober 2006 – February 2007

Chair of Software Engineering

Lecture 19: Topological sortPart 1: Problem and math basis

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

3

by Caran d’Ache

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 setCompatible: the sequence respects all ordering constraints

Üetliberg, Medicine, Lunch, Dishes, Politics : OKMedicine, Üetliberg, Lunch, Dishes, Politics : OKPolitics, 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 areasInteresting, efficient, non-trivial algorithmIllustration of many algorithmic techniquesIllustration of data structures, complexity (big-Oh

notation), and other topics of last lectureIllustration of software engineering techniques: from

algorithm to component with useful APIOpportunity to learn or rediscover important

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

It’s just beautiful!Today: problem and math basisNext time: detailed algorithm and component

Page 2: 19 topological sort - ETH Zse.inf.ethz.ch/.../0001/slides/19_topological_sort_6up.pdf3 Intro. to Programming, lecture 19: Topological Sort I 13 Sometimes there is no solution ¾“Introducing

2

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 occurs prior to its definition

Produce a complete schedule for carrying out a set of tasks, subject to ordering constraints

(This is done for schedulingmaintenance tasks for industrial tasks, often with thousands of constraints)

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

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

Rectangles with overlap constraints

BD

A

CE

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

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

Rectangles with overlap constraints

BD

A

CE

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 setCompatible: the sequence respects all ordering constraints

Üetliberg, Medicine, Lunch, Dishes, Politics : OKMedicine, Üetliberg, Lunch, Dishes, Politics : OKPolitics, 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

MedicineLunch Dishes Politics

Page 3: 19 topological sort - ETH Zse.inf.ethz.ch/.../0001/slides/19_topological_sort_6up.pdf3 Intro. to Programming, lecture 19: Topological Sort I 13 Sometimes there is no solution ¾“Introducing

3

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 typesbefore 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 elementsof 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...

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 bfather :

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 LunchMedicine before LunchLunch 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

Page 4: 19 topological sort - ETH Zse.inf.ethz.ch/.../0001/slides/19_topological_sort_6up.pdf3 Intro. to Programming, lecture 19: Topological Sort I 13 Sometimes there is no solution ¾“Introducing

4

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 ] = ∅

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 aIrreflexive: not (a r a)Symmetric: a r b ⇒ b r aAntisymmetric: (a r b) ∧ (b r a) ⇒ a = bAsymmetric: 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 aIrreflexive: not (a r a)Symmetric: a r b ⇒ b r aAntisymmetric: (a r b) ∧ (b r a) ⇒ a = bAsymmetric: not ((a r b) ∧ (b r a))Transitive: (a r b) ∧ (b r c) ⇒ a r c

Reflexive, symmetric, transitive

sister Symmetric, irreflexiveReflexive, antisymmetricfamily_head

(a family_head b means a is the headof 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:TotalIrreflexiveTransitive

Example: “less than” < on natural numbers

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

2 < 3 2 < 4, ......

0 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 aAsymmetric: not ((a r b) ∧ (b r a))

Transitive: (a r b) ∧ (b r c) ⇒ a r c

Page 5: 19 topological sort - ETH Zse.inf.ethz.ch/.../0001/slides/19_topological_sort_6up.pdf3 Intro. to Programming, lecture 19: Topological Sort I 13 Sometimes there is no solution ¾“Introducing

5

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:TotalIrreflexiveTransitive

Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a))Irreflexive: not (a r a)Symmetric: a r b ⇒ b r aAsymmetric: not ((a r b) ∧ (b r a))

Transitive: (a r b) ∧ (b r c) ⇒ a r c

Theorem:

A strict order is asymmetrictotal

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

Total order relation (non-strict)

Relation is non-strict total order if:TotalReflexiveTransitiveAntisymmetric

Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a))Irreflexive: not (a r a)Symmetric: a r b ⇒ b r aAntisymmetric: (a r b) ∧ (b r a) ⇒ a = bTransitive:(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, ...

...

0 1 2 3 4 5

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

Total order relation (strict)

Relation is strict total order if:TotalIrreflexiveTransitive

Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a))

Irreflexive: not (a r a)Symmetric: a r b ⇒ b r aAntisymmetric: (a r b) ∧ (b r a) ⇒ a = bTransitive:(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

x0

1 2 3

[3, 0]

c

[4, 2]

dExample: relation between points in a plane:

p q if bothxp < xqyp < yq

<

Relation is strict partial order if:TotalIrreflexiveTransitive

Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a))

Irreflexive: not (a r a)Symmetric: a r b ⇒ b r aAntisymmetric: (a r b) ∧ (b r a) ⇒ a = bTransitive:(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)

Page 6: 19 topological sort - ETH Zse.inf.ethz.ch/.../0001/slides/19_topological_sort_6up.pdf3 Intro. to Programming, lecture 19: Topological Sort I 13 Sometimes there is no solution ¾“Introducing

6

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 nore.g. neither

[1, 2]

1

2

b

a[0, 1]

y

01 2 3

[3, 0]

c

[4, 2]

dp q if both

xp < xqyp < yq

a < ba < d

c < d < <

<

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

Possible topological sorts

1

2

b

a

y

01 2 3 c

d

a < ba < 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

01 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 < ba < d

c < d

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 asconstraints =

{[Dishes, Politics], [Üetliberg, Lunch],[Medicine, Lunch], [Lunch, Dishes]}

always a partial order?

Üetliberg

MedicineLunch 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 closurer + = r 1 ∪ r 2 ∪ ... always transitive

Ü

M L DP

r 1

r 2

r 3

Page 7: 19 topological sort - ETH Zse.inf.ethz.ch/.../0001/slides/19_topological_sort_6up.pdf3 Intro. to Programming, lecture 19: Topological Sort I 13 Sometimes there is no solution ¾“Introducing

7

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

Reflexive transitive closure

r i +1 = r i ; r where ; is composition

Transitive closurer + = r 1 ∪ r 2 ∪ ... always transitive

Ü

M L DP

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 DP

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 transitiveclosure is a (strict) order.

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

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

MedicineLunch 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

Page 8: 19 topological sort - ETH Zse.inf.ethz.ch/.../0001/slides/19_topological_sort_6up.pdf3 Intro. to Programming, lecture 19: Topological Sort I 13 Sometimes there is no solution ¾“Introducing

8

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

What we have seen

The topological sort problem and its applicationsMathematical background:

Relations as sets of pairsProperties of relationsOrder relations: partial/total, strict/nonstrictTransitive, reflexive-transitive closuresThe relation between acyclic and order relationsThe 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