1
play

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:


  1. Chair of Softw are Engineering Einführung 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: Topological Sort I 1 Intro. to Programming, lecture 19: Topological Sort I 2 by Caran d’Ache 3 1

  2. “Topological sort” From a given partial order, produce a compatible total order Intro. to Programming, lecture 19: Topological Sort I 4 The problem From a given partial order, 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” � “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 Intro. to Programming, lecture 19: Topological Sort I 5 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 Intro. to Programming, lecture 19: Topological Sort I 6 2

  3. Reading assignment for next Monday Touch of Class, chapter on topological sort: 17 Intro. to Programming, lecture 19: Topological Sort I 7 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 scheduling maintenance tasks for industrial tasks, often 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 8 Rectangles with overlap constraints Constraints: [B, A], [D, A], [A, C], [B, D], [D, C] C E D A B Intro. to Programming, lecture 19: Topological Sort I 9 3

  4. Rectangles with overlap constraints Constraints: [B, A], [D, A], [A, C], [B, D], [D, C] Possible solution: B D E A C C E D A B Intro. to Programming, lecture 19: Topological Sort I 10 The problem From a given partial order, 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” � “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 Intro. to Programming, lecture 19: Topological Sort I 11 Pictured as a graph Üetliberg Lunch Dishes Politics Medicine “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 12 4

  5. 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 13 Overall structure (1) Given: class ORDERABLE [ G ] feature A type G A set of elements elements: LIST [ G ] of type G A set of constraints constraints: LIST [ TUPLE [ G, G ]] between these elements topsort : LIST [ G ] is ... Required: ensure An enumeration of the compatible ( Result , constraints ) elements, in an order compatible with the constraints end Intro. to Programming, lecture 19: Topological Sort I 14 Some mathematical background... Intro. to Programming, lecture 19: Topological Sort I 15 5

  6. 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 Note: relation : � sibling (brother or sister) names will : appear in green Notation: a r b to express that r holds of a and b . Intro. to Programming, lecture 19: Topological Sort I 16 Example: the before relation “Remove the dishes before discussing politics” “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 } The constraining relation: Dishes before Politics Üetliberg before Lunch Medicine before Lunch Lunch before Dishes Intro. to Programming, lecture 19: Topological Sort I 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 Intro. to Programming, lecture 19: Topological Sort I 18 6

  7. 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 19 Example: the before relation “Remove dishes before discussing politics” “Walk to Üetliberg before lunch” “Take your medicine before lunch” “Finish lunch before removing dishes” The set of interest: elements = { Politics , Lunch , Medicine , Dishes , Üetliberg } The constraining relation: before = {[ Dishes, Politics ], [ Üetliberg, Lunch ], [ Medicine, Lunch ], [ Lunch, Dishes ]} Intro. to Programming, lecture 19: Topological Sort I 20 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 21 7

  8. 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 22 Examples (on a set of persons) sibling Reflexive, symmetric, transitive sister Symmetric, irreflexive family_head Reflexive, antisymmetric ( a family_head b means a is the head of b ’s family, with one head per family) mother Asymmetric, irreflexive 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 ⇒ Intro. to Programming, lecture 19: Topological Sort I 23 Total order relation (strict) Relation is strict total order if: Total: ( a ≠ b ) ⇒ (( a r b ) ∨ ( b r a )) Irreflexive: not ( a r a ) Total � Symmetric: a r b ⇒ b r a Irreflexive � 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 2 3 4 5 0 < 1 0 < 2, 0 < 3, 0 < 4, ... 1 < 2 1 < 3 1 < 4, ... 2 < 3 2 < 4, ... ... Intro. to Programming, lecture 19: Topological Sort I 24 8

  9. Theorem A strict order is asymmetric (total) Intro. to Programming, lecture 19: Topological Sort I 25 Total order relation (strict) Relation is strict total order if: Total: ( a ≠ b ) ⇒ (( a r b ) ∨ ( b r a )) Irreflexive: not ( a r a ) � Total Symmetric: a r b ⇒ b r a � Irreflexive 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 Intro. to Programming, lecture 19: Topological Sort I 26 Total order relation (non-strict) Relation is non-strict total order if: Total: ( a ≠ b ) ⇒ (( a r b ) ∨ ( b r a )) Total Irreflexive: not (a r a) Reflexive Symmetric: a r b ⇒ b r a Transitive Antisymmetric: ( a r b ) ∧ ( b r a ) ⇒ a = b Transitive:( a r b ) ∧ ( b r c ) ⇒ a r c Antisymmetric Example: “less than or equal” ≤ on natural numbers 0 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, 1 ≤ 3, 1 ≤ 4, ... 2 ≤ 2 2 ≤ 3, 2 ≤ 4, ... ... Intro. to Programming, lecture 19: Topological Sort I 27 9

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend