Lecture 19: Topological sort Part 1: Problem and math basis by - - PowerPoint PPT Presentation

lecture 19 topological sort part 1 problem and math basis
SMART_READER_LITE
LIVE PREVIEW

Lecture 19: Topological sort Part 1: Problem and math basis by - - PowerPoint PPT Presentation

Chair of Softw are Engineering Chair of Softw are Engineering Einfhrung in die Programmierung Einfhrung in die Programmierung Introduction to Programming P Prof. Dr. Bertrand Meyer f D B t d M Lecture 19: Topological sort Part 1:


slide-1
SLIDE 1

Chair of Softw are Engineering Chair of Softw are Engineering

Einführung in die Programmierung Einführung in die Programmierung Introduction to Programming

P f D B t d M

  • Prof. Dr. Bertrand Meyer

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

slide-2
SLIDE 2

by Caran d’Ache

Introduction to Programming, lecture 19: Topological sort

2

2

slide-3
SLIDE 3

“Topological sort”

From a given partial order, produce a compatible total order compatible total order

Introduction to Programming, lecture 19: Topological sort

3

slide-4
SLIDE 4

The problem

From a given partial order, produce a compatible total order

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

produce a compatible total order

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

“Remove the dishes before discussing politics” “Walk to Üetliberg before lunch”

g

“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

P l M d L h D h Ü l b K

Introduction to Programming, lecture 19: Topological sort

4

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

slide-5
SLIDE 5

Why we are doing this!

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

notation), and other topics of last lecture

Illustration of software engineering techniques: Illustration of software engineering techniques:

from algorithm to component with useful API

Opportunity to learn or rediscover important

Opportun ty to learn or red scover mportant mathematical concepts: binary relations (order relations in particular) and their properties

It’s just beautiful!

Today: problem and math basis

Introduction to Programming, lecture 19: Topological sort

5

Next time: detailed algorithm and component

slide-6
SLIDE 6

Reading assignment for next Monday

Touch of Class, chapter on topological sort: 17

Introduction to Programming, lecture 19: Topological sort

6

slide-7
SLIDE 7

Topological sort: example uses

From a dictionary, produce a list of definitions such that no word From a d ct onary, produce a l st of def n t ons such that no word

  • ccurs prior to its definition

P d l h d l f i f k 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

Introduction to Programming, lecture 19: Topological sort

7

slide-8
SLIDE 8

Rectangles with overlap constraints

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

Introduction to Programming, lecture 19: Topological sort

8

slide-9
SLIDE 9

Rectangles with overlap constraints

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

Introduction to Programming, lecture 19: Topological sort

9

slide-10
SLIDE 10

The problem

From a given partial order, produce a compatible total order

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

produce a compatible total order

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

“Remove the dishes before discussing politics” “Walk to Üetliberg before lunch”

g

“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

P l M d L h D h Ü l b K

Introduction to Programming, lecture 19: Topological sort

10

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

slide-11
SLIDE 11

Pictured as a graph

Üetliberg Lunch Dishes Politics Medicine

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

Introduction to Programming, lecture 19: Topological sort

11

slide-12
SLIDE 12

Sometimes there is no solution

“Introducing recursion requires that

Introduc ng recurs on requ res that students know about stacks”

“You must discuss abstract data

types before introducing stacks types before introducing stacks

“Abstract data types rely on

recursion”

The constraints introduce a cycle

Introduction to Programming, lecture 19: Topological sort

12

The constraints introduce a cycle

slide-13
SLIDE 13

Overall structure (1)

Given: class ORDERABLE [G] feature A type G A set of elements class ORDERABLE [G] feature elements: LIST [G ]

  • f type G

constraints: LIST [TUPLE [G, G ]] A set of constraints between these elements [ ] R i d topsort : LIST [G ] is between these elements Required: An enumeration of the elements, in an order l h h ... ensure compatible (Result, constraints) compatible with the constraints end

Introduction to Programming, lecture 19: Topological sort

13

slide-14
SLIDE 14

Some mathematical background...

Introduction to Programming, lecture 19: Topological sort

14

slide-15
SLIDE 15

Binary relation on a set

Any property that either holds or doesn’t hold between two elements of a set two elements of a set On a set PERSON of persons example relations are: 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)

Note: relation names will i

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

appear in green

Introduction to Programming, lecture 19: Topological sort

15

slide-16
SLIDE 16

Example: the before relation

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

The set of interest:

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

The constraining relation:

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

Introduction to Programming, lecture 19: Topological sort

16

slide-17
SLIDE 17

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

Introduction to Programming, lecture 19: Topological sort

17

slide-18
SLIDE 18

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 Then x r y simply means that [x, y] ∈ r

See examples on next slide

Introduction to Programming, lecture 19: Topological sort

18

slide-19
SLIDE 19

Example: the before relation

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

The set of interest:

F n sh lunch before remov ng d shes

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

Introduction to Programming, lecture 19: Topological sort

19

slide-20
SLIDE 20

Using ordinary set operators

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

Introduction to Programming, lecture 19: Topological sort

20

slide-21
SLIDE 21

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)) R fl i Reflexive: a r a Irreflexive: not (a r a) S t i b b Symmetric: a r b ⇒ b r a Antisymmetric: (a r b) ∧ (b r a) ⇒ a = b A t i t (( b) (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

Introduction to Programming, lecture 19: Topological sort

21

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

slide-22
SLIDE 22

Examples (on a set of persons)

sibling

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

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

Asymmetric, irreflexive

Reflexive: a r a Irreflexive: not (a r a) Symmetric: a r b ⇒ b 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))

Introduction to Programming, lecture 19: Topological sort

22

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

slide-23
SLIDE 23

Total order relation (strict)

Relation is strict total order if: T l

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

  • Total
  • Irreflexive
  • Transitive

Irreflexive: not (a r a) Symmetric: a r b ⇒ b r a Asymmetric: not ((a r b) ∧ (b r a))

  • Transitive

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

Example: “less than” < on natural numbers

0 < 1 0 < 2 0 < 3 0 < 4

1 2 3 4 5

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

Introduction to Programming, lecture 19: Topological sort

23

,

...

slide-24
SLIDE 24

Theorem

A strict order is asymmetric (total)

Introduction to Programming, lecture 19: Topological sort

24

slide-25
SLIDE 25

Total order relation (strict)

Relation is strict total order if: T l

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

Theorem: A strict order is asymmetric total

Introduction to Programming, lecture 19: Topological sort

25

slide-26
SLIDE 26

Total order relation (non-strict)

Relation is non-strict total order if: Total

Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a)) I fl i t ( )

Total Reflexive Transitive A i i

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

Antisymmetric

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

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

1 2 3 4 5

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

1 2 3 4 5

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

Introduction to Programming, lecture 19: Topological sort

26

2 ≤ 2 2 ≤ 3, 2 ≤ 4, ...

...

slide-27
SLIDE 27

Total order relation (strict)

Relation is strict total order if: T l

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

Introduction to Programming, lecture 19: Topological sort

27

slide-28
SLIDE 28

Partial order relation (strict)

Relation is strict partial order if: T l

Total: (a ≠ b) ⇒ ((a r b) ∨ (b r a)) Total Irreflexive Transitive Irreflexive: not (a r a) Symmetric: a r b ⇒ b r a Antisymmetric: (a r b) ∧ (b r a) ⇒ a = b Transitive Antisymmetric: (a r b) ∧ (b r a) ⇒ a = b Transitive:(a r b) ∧ (b r c) ⇒ a r c [1, 2]

2

b

y

[4, 2]

d Example: relation between

1

b

[0, 1]

d p points in a plane: p q if both < a

x

[3, 0]

xp < xq yp < yq

Introduction to Programming, lecture 19: Topological sort

28

x

1 2 3 c

slide-29
SLIDE 29

Theorems

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

Introduction to Programming, lecture 19: Topological sort

29

slide-30
SLIDE 30

Example partial order

[1, 2]

2

y

[4, 2]

p q if both

<

1

b

[0, 1]

d

p q if both xp < xq y < y

<

1

a

[3, 0]

yp < yq

Here the following hold: No link between a and c b and c:

1 2 3 c

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

a < b c < d < < a < d

Introduction to Programming, lecture 19: Topological sort

30

slide-31
SLIDE 31

Possible topological sorts

2

y

1

b d

1

a

1 2 3 c

a < b c < d a < d

Introduction to Programming, lecture 19: Topological sort

31

slide-32
SLIDE 32

Topological sort understood

2

y

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

1

b d {[a, b], [a, d], [c, d]}

1

a One of the solutions is: b d

1 2 3 c

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

⊆ t

< a < b c < d relation t such that

⊆ t

< a < d

Introduction to Programming, lecture 19: Topological sort

32

slide-33
SLIDE 33

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

Introduction to Programming, lecture 19: Topological sort

33

slide-34
SLIDE 34

From constraints to partial orders

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

Üetliberg Medicine Lunch Dishes Politics

Introduction to Programming, lecture 19: Topological sort

34

slide-35
SLIDE 35

Powers and transitive closure of a relation

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

Ü P M L D P

Transitive closure r + = r 1 ∪ r 2 ∪ always transitive

r 1

r r ∪ r ∪ ... always transitive

r r 2 r 3

Introduction to Programming, lecture 19: Topological sort

35

slide-36
SLIDE 36

Reflexive transitive closure

r i +1 = r i ; r where ; is composition r 0 = id [X ] where X is the underlying set p

Ü P M L D P

Transitive closure r + = r 1 ∪ r 2 ∪ always transitive

r 1 r 0

r r ∪ r ∪ ... always transitive

r r 2 r 3

Reflexive transitive closure:

Introduction to Programming, lecture 19: Topological sort

36

r ∗ = r 0 ∪ r 1 ∪ r 2 ∪ ... always transitive and reflexive

slide-37
SLIDE 37

Acyclic relation

r + ∩ id [X ] = ∅ A relation r on a set X is acyclic if and only if:

Ü P M L D P

before + before + id [X]

Introduction to Programming, lecture 19: Topological sort

37

slide-38
SLIDE 38

Acyclic relations and partial orders

Theorems:

Any (strict) order relation is acyclic.

A l i i li if d l if i i i

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) p )

Introduction to Programming, lecture 19: Topological sort

38

slide-39
SLIDE 39

From constraints to partial orders

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

Introduction to Programming, lecture 19: Topological sort

39

slide-40
SLIDE 40

Back to software...

Introduction to Programming, lecture 19: Topological sort

40

slide-41
SLIDE 41

The basic algorithm idea

p r t v

  • q

s u w

  • u
  • topsort

topsort

Introduction to Programming, lecture 19: Topological sort

41

slide-42
SLIDE 42

What we have seen

The topological sort problem and its applications Mathematical background: Mathematical background:

Relations as sets of pairs Properties of relations 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

Introduction to Programming, lecture 19: Topological sort

42

Good software engineering: effective API

slide-43
SLIDE 43

Reading assignment for next Monday

Touch of Class, chapter on topological sort: 17

Introduction to Programming, lecture 19: Topological sort

43

slide-44
SLIDE 44

End of lecture 19

Introduction to Programming, lecture 19: Topological sort

44