1 The problem From a given partial order, produce a compatible - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 The problem From a given partial order, produce a compatible - - PDF document

Chair of Softw are Engineering Chair of Softw are Engineering Einfhrung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 19: Topological sort Part 1: Problem and math basis by Caran dAche Introduction


slide-1
SLIDE 1

1

Einführung in die Programmierung Introduction to Programming

  • Prof. Dr. Bertrand Meyer

Chair of Softw are Engineering Chair of Softw are Engineering

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

by Caran d’Ache

Introduction to Programming, lecture 19: Topological sort

2

2

“Topological sort”

From a given partial order, produce a compatible total order

Introduction to Programming, lecture 19: Topological sort

3

compatible total order

slide-2
SLIDE 2

2

The problem

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

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

From a given partial order, produce a compatible total order

Introduction to Programming, lecture 19: Topological sort

4

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

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:

Introduction to Programming, lecture 19: Topological sort

5

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

Reading assignment for next Monday

Touch of Class, chapter on topological sort: 17

Introduction to Programming, lecture 19: Topological sort

6

slide-3
SLIDE 3

3

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

Introduction to Programming, lecture 19: Topological sort

7

g 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

Rectangles with overlap constraints

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

Introduction to Programming, lecture 19: Topological sort

8

B D A

Rectangles with overlap constraints

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

Introduction to Programming, lecture 19: Topological sort

9

B D A

slide-4
SLIDE 4

4

The problem

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

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

From a given partial order, produce a compatible total order

Introduction to Programming, lecture 19: Topological sort

10

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

Pictured as a graph

Üetliberg Lunch Dishes Politics

Introduction to Programming, lecture 19: Topological sort

11

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

Medicine

Sometimes there is no solution

“Introducing recursion requires that

students know about stacks”

“You must discuss abstract data

types before introducing stacks

Introduction to Programming, lecture 19: Topological sort

12

types before introducing stacks

“Abstract data types rely on

recursion”

The constraints introduce a cycle

slide-5
SLIDE 5

5

Overall structure (1)

Given: A type G A set of elements

  • f type G

constraints: LIST [TUPLE [G, G ]] class ORDERABLE [G] feature A set of constraints between these elements elements: LIST [G ]

Introduction to Programming, lecture 19: Topological sort

13

Required: An enumeration of the elements, in an order compatible with the constraints topsort : LIST [G ] is ... ensure compatible (Result, constraints) end between these elements

Some mathematical background...

Introduction to Programming, lecture 19: Topological sort

14

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

Introduction to Programming, lecture 19: Topological sort

15

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

slide-6
SLIDE 6

6

Example: the before relation

The set of interest:

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

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

Introduction to Programming, lecture 19: Topological sort

16

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

The constraining relation:

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

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

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.

Introduction to Programming, lecture 19: Topological sort

18

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

See examples on next slide

slide-7
SLIDE 7

7

Example: the before relation

The set of interest:

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

Introduction to Programming, lecture 19: Topological sort

19

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

Using ordinary set operators

spouse = wife ∪ husband sibling = sister ∪ brother ∪ id [Person] sister ⊆ sibling

Introduction to Programming, lecture 19: Topological sort

20

g father ⊆ ancestor universal [X ] = X x X (cartesian product) empty [X ] = ∅

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) S t i b b

Introduction to Programming, lecture 19: Topological sort

21

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.

slide-8
SLIDE 8

8

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

Introduction to Programming, lecture 19: Topological sort

22

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 mother

Asymmetric, irreflexive

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

Introduction to Programming, lecture 19: Topological sort

23

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

Theorem

A strict order is asymmetric (total)

Introduction to Programming, lecture 19: Topological sort

24

slide-9
SLIDE 9

9

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

Introduction to Programming, lecture 19: Topological sort

25

Theorem: A strict order is asymmetric total

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

Introduction to Programming, lecture 19: Topological sort

26

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

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

Introduction to Programming, lecture 19: Topological sort

27

slide-10
SLIDE 10

10

Partial order relation (strict)

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

Introduction to Programming, lecture 19: Topological sort

28

[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 <

Theorems

A strict order is asymmetric (total) partial

Introduction to Programming, lecture 19: Topological sort

29

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

Example partial order

[1, 2]

1 2

b a

[0, 1]

y

[3, 0] [4, 2]

d

p q if both xp < xq yp < yq

<

Introduction to Programming, lecture 19: Topological sort

30

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

1 2 3 c

a < b a < d c < d < <

slide-11
SLIDE 11

11

Possible topological sorts

1 2

b a

y

d

Introduction to Programming, lecture 19: Topological sort

31 1 2 3 c

a < b a < d c < d

Topological sort understood

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

1 2

b a

y

d One of the solutions is: b d

Introduction to Programming, lecture 19: Topological sort

32 1 2 3 c

We are looking for a total order relation t such that

⊆ t

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

Final statement of topological sort problem From a given partial order, produce a compatible total order where:

Introduction to Programming, lecture 19: Topological sort

33

A partial order p is compatible with a total order t if and only if p ⊆ t

slide-12
SLIDE 12

12

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]}

Introduction to Programming, lecture 19: Topological sort

34

always a partial order?

Üetliberg Medicine Lunch Dishes Politics

Powers and transitive closure of a relation

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

Ü P

Introduction to Programming, lecture 19: Topological sort

35

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

M L D P r 1 r 2 r 3

Reflexive transitive closure

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

Ü P

r 0 = id [X ] where X is the underlying set

Introduction to Programming, lecture 19: Topological sort

36

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

slide-13
SLIDE 13

13

Acyclic relation

r + ∩ id [X ] = ∅

Ü P

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

Introduction to Programming, lecture 19: Topological sort

37

before + id [X]

M L D P

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.

Introduction to Programming, lecture 19: Topological sort

38

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

From constraints to partial orders

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

Introduction to Programming, lecture 19: Topological sort

39

Üetliberg Medicine Lunch Dishes Politics

slide-14
SLIDE 14

14

Back to software...

Introduction to Programming, lecture 19: Topological sort

40

The basic algorithm idea

p q r s t u v w

  • Introduction to Programming, lecture 19: Topological sort

41

topsort

u

  • 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

Introduction to Programming, lecture 19: Topological sort

42

,

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

slide-15
SLIDE 15

15

Reading assignment for next Monday

Touch of Class, chapter on topological sort: 17

Introduction to Programming, lecture 19: Topological sort

43

End of lecture 19

Introduction to Programming, lecture 19: Topological sort

44