1
play

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


  1. Chair of Softw are Engineering Chair of Softw are Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer 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 compatible total order Introduction to Programming, lecture 19: Topological sort 3 1

  2. 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” 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 Introduction to Programming, lecture 19: Topological sort 4 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: � 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 Introduction to Programming, lecture 19: Topological sort 5 Reading assignment for next Monday Touch of Class, chapter on topological sort: 17 Introduction to Programming, lecture 19: Topological sort 6 2

  3. 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 g 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 Introduction to Programming, lecture 19: Topological sort 7 Rectangles with overlap constraints Constraints: [B, A], [D, A], [A, C], [B, D], [D, C] C E D D A B Introduction to Programming, lecture 19: Topological sort 8 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 D A B Introduction to Programming, lecture 19: Topological sort 9 3

  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” 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 Introduction to Programming, lecture 19: Topological sort 10 Pictured as a graph Üetliberg Dishes Politics Lunch Medicine “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 11 Sometimes there is no solution � “ Introducing recursion requires 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 4

  5. 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 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 Introduction to Programming, lecture 19: Topological sort 13 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 � 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 . Introduction to Programming, lecture 19: Topological sort 15 5

  6. 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 } Tasks { Politics , Lunch , Medicine , Dishes , Üetliberg } The constraining relation: Dishes before Politics Üetliberg before Lunch Medicine before Lunch Lunch before Dishes Introduction to Programming, lecture 19: Topological sort 16 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 . Then x r y simply means that [ x , y ] ∈ r See examples on next slide Introduction to Programming, lecture 19: Topological sort 18 6

  7. 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 ]} Introduction to Programming, lecture 19: Topological sort 19 Using ordinary set operators spouse = wife ∪ husband sibling = sister ∪ brother ∪ id [ Person ] sister ⊆ sibling g father ⊆ ancestor universal [ X ] = X x X (cartesian product) empty [ X ] = ∅ Introduction to Programming, lecture 19: Topological sort 20 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 Symmetric: a r b ⇒ t i b 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. Introduction to Programming, lecture 19: Topological sort 21 7

  8. 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 mother Asymmetric, irreflexive 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 ⇒ Introduction to Programming, lecture 19: Topological sort 22 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, ... ... Introduction to Programming, lecture 19: Topological sort 23 Theorem A strict order is asymmetric (total) Introduction to Programming, lecture 19: Topological sort 24 8

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