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 MP1 HS 2007 Lecture 20: Topological Sort Algorithm Algorithm
Slide 1 MP1 Decide on footnote ("Info1" or "Intro-course" or whatever) Michela Pedroni, 9/16/2003
Back to software... 2 Intro. to Programming, lecture 20: Topological sort algorithm
Overall structure (original) Given: class A type G A type G TOPOLOGICAL SORTABLE [ G ] TOPOLOGICAL_SORTABLE [ G ] A set of elements of feature type G constraints : LINKED_LIST [ TUPLE [ G, G ]] A relation constraints l i i elements : LINKED_LIST [ G ] on these elements Required: topologically_sorted : LINKED_LIST [ G ] An enumeration of the require require elements in an order no_cycle ( constraints ) compatible with do constraints ... ensure compatible ( Result , constraints ) end end end 3 Intro. to Programming, lecture 20: Topological sort algorithm
Overall structure (improved) class TOPOLOGICAL SORT ED [ G ] TOPOLOGICAL_SORT ED [ G ] feature constraints : LINKED_LIST [ TUPLE [ G, G ]] Instead of a function elements : LINKED_LIST [ G ] topologically_sorted , use: sorted : LINKED_LIST [ G ] • A procedure process . process p • An attribute sorted n attr ut sort require (set by process ), to no_cycle ( constraints ) hold the result. do ... ensure compatible ( sorted , constraints ) end end d 4 Intro. to Programming, lecture 20: Topological sort algorithm
Non-uniqueness In general there are several possible solutions y 2 2 d b 1 1 a 3 c 0 1 2 In practice topological sort uses an optimization criterion to choose between possible solutions. 5 Intro. to Programming, lecture 20: Topological sort algorithm
A partial order is acyclic The The relation: relation: < < � Must be a partial order: no cycle in the transitive � Must be a partial order: no cycle in the transitive closure of constraints � This means there is no circular chain of the form � This means there is no circular chain of the form e 0 e 1 … e n e 0 < < < < If there is such a cycle there exists no solution to the If there is such a cycle, there exists no solution to the topological sort problem! 6 Intro. to Programming, lecture 20: Topological sort algorithm
Cycles In topological sort, we are not given the actual relation , < but a relation constraints, through a set of pairs such as but a relation constraints, through a set of pairs such as { [ Dishes, Out ], [ Museum, Lunch ], [ Medicine, Lunch ], [ Lunch, Dishes ]} Th The relation of interest is: l ti f i t t i constraints + = < Partial Partial Acyclic order is acyclic if and only if constraints contains no set of pairs is acyclic if and only if constraints contains no set of pairs < {[ f 0 , f 1 ], [ f 1 , f 2 ], …, [ f m , f 0 ]} 0 1 1 2 m 0 When such a cycle exists, there can be no total order compatible with constraints . 7 Intro. to Programming, lecture 20: Topological sort algorithm
Overall structure (reminder) class TOPOLOGICAL SORTED [ G ] TOPOLOGICAL_SORTED [ G ] feature constraints : LINKED_LIST [ TUPLE [ G, G ]] elements : LINKED_LIST [ G ] sorted : LINKED_LIST [ G ] process p require no_cycle ( constraints ) do ... ensure compatible ( sorted , constraints ) end end d 8 Intro. to Programming, lecture 20: Topological sort algorithm
Original assumption process require require no_cycle ( constraints ) do ... ensure compatible ( sorted constraints ) compatible ( sorted , constraints ) end This assumes there are no cycles in the input. Such an assumption is not enforceable in practice. In particular: finding cycles is essentially as hard as topological sort. 9 Intro. to Programming, lecture 20: Topological sort algorithm
Dealing with cycles Don’t assume anything; find cycles as byproduct of attempt to do topological sort attempt to do topological sort The scheme for process becomes: The scheme for process becomes: “Attempt to do topological sort, “Att t t d t l i l t accounting for possible cycles” if “Cycles found” then “Report cycles” Report cycles end 10 Intro. to Programming, lecture 20: Topological sort algorithm
Overall structure (as previously improved) class class TOPOLOGICAL_SORTED [ G ] TOPOLOGICAL_SORTED [ G ] [ [ ] ] feature feature constraints : LINKED_LIST [ TUPLE [ G, G ]] constraints : LINKED_LIST [ TUPLE [ G, G ]] elements : LINKED_LIST [ G ] elements : LINKED_LIST [ G ] sorted : LINKED_LIST [ G ] sorted : LINKED_LIST [ G ] process process require no_cycle ( constraints ) do ... ensure compatible ( sorted , constraints ) end end end 11 Intro. to Programming, lecture 20: Topological sort algorithm
Overall structure (final) class TOPOLOGICAL_SORTED [ G ] f feature t constraints : LINKED_LIST [ TUPLE [ G, G ]] elements : LINKED_LIST [ G ] sorted : LINKED LIST [ G ] sorted : LINKED_LIST [ G ] process process require -- No precondition in this version do ... ensure compatible ( sorted , constraints ) “ sorted contains all elements not initially involved in a cycle” end end d 12 Intro. to Programming, lecture 20: Topological sort algorithm
The basic algorithm idea p v t r � � w q s u u � � sorted sorted 13 Intro. to Programming, lecture 20: Topological sort algorithm
The basic loop scheme … loop “Find a member next of elements for which constraints contains no pair of the form [ x , next ]” sorted . extend ( next ) “Remove next from elements , and remove from constraints any pairs of the form [ next , y ]” end 14 Intro. to Programming, lecture 20: Topological sort algorithm
The loop invariant Original architecture: “ constraints + has no cycles” Revised architecture: “ constraints + has no cycles other than any that were present originally” 15 Intro. to Programming, lecture 20: Topological sort algorithm
Overall structure (as previously improved) class class TOPOLOGICAL_SORTED [ G ] TOPOLOGICAL_SORTED [ G ] [ [ ] ] feature feature constraints : LINKED_LIST [ TUPLE [ G, G ]] constraints : LINKED_LIST [ TUPLE [ G, G ]] elements : LINKED_LIST [ G ] elements : LINKED_LIST [ G ] sorted : LINKED_LIST [ G ] sorted : LINKED_LIST [ G ] process process require no_cycle ( constraints ) do ... ensure compatible ( sorted , constraints ) end end end 16 Intro. to Programming, lecture 20: Topological sort algorithm
Overall structure (final) class TOPOLOGICAL_SORTED [ G ] f feature t constraints : LINKED_LIST [ TUPLE [ G, G ]] elements : LINKED_LIST [ G ] sorted : LINKED LIST [ G ] sorted : LINKED_LIST [ G ] process process require -- No precondition in this version do ... ensure compatible ( sorted , constraints ) “ sorted contains all elements not initially involved in a cycle” end end d 17 Intro. to Programming, lecture 20: Topological sort algorithm
The loop invariant Original architecture: “ constraints + has no cycles” Revised architecture: “ constraints + has no cycles other than any that were present originally” 18 Intro. to Programming, lecture 20: Topological sort algorithm
Terminology If constraints has a pair [ x, y ], we say that � x is a predecessor of y � y is a successor of x y 19 Intro. to Programming, lecture 20: Topological sort algorithm
Algorithm scheme process do from create {...} sorted . make invariant from create {...} sorted . make invariant “constraints includes no cycles other than original ones” and “sorted is compatible with constraints” and “ All original elements are in either sorted or elements” variant i t “ Size of elements” until “ Every member of elements has a predecessor ” Every member of elements has a predecessor loop next := “A member of elements with no predecessor ” sorted . extend ( next ) ( ) “Remove next from elements ” “Remove from constraints all pairs [ next , y ]” end if “No more elements” then “Report that topological sort is complete” else “Report cycle in remaining constraints and elements ” Report cycle in remaining constraints and elements end end 20 Intro. to Programming, lecture 20: Topological sort algorithm
Implementing the algorithm We start with these data structures, directly reflecting input data: (Number of elements: n elements : LINKED_LIST [ G ] Number of constraints: m ) constraints : LINKED_LIST [ TUPLE [ G, G ]] y 2 b d Example: p elements = { a, b, c, d } 1 constraints = a {[ a, b ], [ a, d ], [ b, d ], [ c, d ]} {[ a, b ], [ a, d ], [ b, d ], [ c, d ]} 3 c 0 1 2 21 Intro. to Programming, lecture 20: Topological sort algorithm
Data structures 1: original elements = { a, b, c, d } constraints = {[ a b ] [ a d ] [ b d ] [ c d ]} constraints = {[ a, b ], [ a, d ], [ b, d ], [ c, d ]} elements a b c d n elements constraints m constraints a b a d b d c d Efficiency: The best we can hope for: O ( m + n ) 22 Intro. to Programming, lecture 20: Topological sort algorithm
Recommend
More recommend