lecture 19 topological sort part 1 problem and math basis
play

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:


  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 Prof. Dr. Bertrand Meyer f D B t d M Lecture 19: Topological sort Part 1: Problem and math basis

  2. by Caran d’Ache Introduction to Programming, lecture 19: Topological sort 2 2

  3. “Topological sort” From a given partial order, produce a compatible total order compatible total order Introduction to Programming, lecture 19: Topological sort 3

  4. The problem From a given partial order, produce a compatible total order produce a compatible total order Partial order: ordering constraints between elements of a set, e.g. 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 P l M d L h D h Ü l b K Introduction to Programming, lecture 19: Topological sort 4

  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 Next time: detailed algorithm and component Introduction to Programming, lecture 19: Topological sort 5

  6. Reading assignment for next Monday Touch of Class, chapter on topological sort: 17 Introduction to Programming, lecture 19: Topological sort 6

  7. Topological sort: example uses From a d ct onary, produce a l st of def n t ons such that no word 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, P d l h d l f i f k 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 Introduction to Programming, lecture 19: Topological sort 7

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

  9. 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

  10. The problem From a given partial order, produce a compatible total order produce a compatible total order Partial order: ordering constraints between elements of a set, e.g. 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 P l M d L h D h Ü l b K Introduction to Programming, lecture 19: Topological sort 10

  11. Pictured as a graph Üetliberg Politics Dishes Lunch 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

  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 The constraints introduce a cycle Introduction to Programming, lecture 19: Topological sort 12

  13. Overall structure (1) Given: class ORDERABLE [ G ] feature 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 ... R Required: i d ensure An enumeration of the compatible ( Result , constraints ) elements, in an order compatible with the l h h constraints end Introduction to Programming, lecture 19: Topological sort 13

  14. Some mathematical background... Introduction to Programming, lecture 19: Topological sort 14

  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 : Note: relation � sibling : (brother or sister) names will appear in green i Notation: a r b to express that r holds of a and b . Introduction to Programming, lecture 19: Topological sort 15

  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

  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

  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

  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” F n sh lunch before remov ng d shes 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

  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) empty [ X ] = ∅ [ X ] Introduction to Programming, lecture 19: Topological sort 20

  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 Reflexive: a r a i 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 ⇒ A Asymmetric: not (( a r b ) ∧ t i t (( b ) ( b ( b r a )) )) Transitive: ( a r b ) ∧ ( b r c ) a r c ⇒ *Definition of “total” is specific to this discussion (there is 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

  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 mother Asymmetric, irreflexive Asymmetric irreflexive Total: ( a ≠ b ) ⇒ ( a r b ) ∨ ( b r a ) Reflexive: a r a Reflexive: a r a Irreflexive: not ( a r a ) Symmetric: a r b ⇒ Symmetric: a r b ⇒ b r a b r a Antisymmetric: ( a r b ) ∧ ( b r a ) a = b ⇒ Asymmetric: not (( a r b ) ∧ y (( ) ( b r a )) ( )) Transitive: ( a r b ) ∧ ( b r c ) a r c ⇒ Introduction to Programming, lecture 19: Topological sort 22

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