270 likes | 284 Views
Topo l og ic a l. S o rt. CSE 373 Data Structures Lecture 19. Topo l og ic a l. S o rt. 14 2. 32 2. 14 3. 32 1. P r o b l e m : F i n d a n o r de r i n. which all these be taken. c ou rs e s. c a n. 32 6. 37 0. 34 1. Example: 370 326. 142 321 421. 143 341 401. 378 322.
E N D
Topological Sort CSE 373 Data Structures Lecture 19
Topological Sort 142 322 143 321 Problem:Findanorderin which all these be taken. courses can 326 370 341 Example: 370 326 142 321 421 143 341 401 378 322 378 421 401 Inorder totake acourse,youmust takeallofitsprerequisitesfirst 11/27/02 TopologicalSort-Lecture19 3
Topological Sort Given a digraph G = (V, its vertices such that: E),findalinearorderingof for any edge (v, w) in E, v precedes win the ordering B C A F D E 11/27/02 TopologicalSort-Lecture19 4
Topo sort - good example B C Any linear ordering all the arrows go to inwhich A the right is avalidsolution F D E A B F C D E Note thatFcangoanywhere inthis list because itisnotconnected. 11/27/02 TopologicalSort-Lecture19 5
Topo sort - bad example B C Any linear ordering in which an arrow goes to the left A F is not a valid solution D E A B F C E D NO! 11/27/02 TopologicalSort-Lecture19 6
Not all can be Sorted • Adirectedgraphwith a cycle cannot be topologically sorted. B C A F D E 11/27/02 TopologicalSort-Lecture19 7
Cycles • GivenadigraphG=(V,E), acycleisa sequence of that verticesv1,v2, …,vk such › › › G k < 1 v1 = vk (vi,vi+1) in E for1<i <k. • isacyclicifithasnocycles. 11/27/02 TopologicalSort-Lecture19 8
Topo sortalgorithm - 1 Step1:Identifyverticesthat havenoincoming edges • The “in-degree” of these vertices iszero B C A F D E 11/27/02 Topological Sort-Lecture19 9
Topo sort algorithm - 1a Step1:Identifyverticesthat havenoincomingedges • • Ifnosuchvertices,graphhasonlycycle(s) (cyclic graph) Topological sort not possible–Halt. B C A Example ofacyclicgraph D 11/27/02 TopologicalSort-Lecture19 10
Topo sort algorithm - 1b Step1:Identifyvertices that haveno incoming edges • Select onesuch vertex Select B C A F D E 11/27/02 TopologicalSort-Lecture19 11
Toposort algorithm - 2 Step2: Deletethis vertexofin-degree0andall its outgoing edges output. B A from the graph. Place it in the C A F D E 11/27/02 TopologicalSort-Lecture19 12
Continue until done RepeatStep1 andStep2 until graph is empty Select B C A F D E 11/27/02 TopologicalSort-Lecture19 13
B SelectB. Copy to sorted list. Delete B and its edges. B C A B F D E 11/27/02 TopologicalSort-Lecture19 14
C Select C. Copy to sorted list. Delete C and its edges. C A B C F D E 11/27/02 TopologicalSort-Lecture19 15
D Select D. Copy to sorted list. Delete D and its edges. D A B C F D E 11/27/02 Topological Sort-Lecture19 16
E, F Select Select E. F. Copy Copy to to sorted sorted list. list. Delete Delete Eanditsedges. F and itsedges. D A B C E F F E 11/27/02 TopologicalSort-Lecture19 17
Done B C A F Removefrom algorithm and serve. D E D A B C E F 11/27/02 TopologicalSort-Lecture19 18
Topological Ordering Algorithm: Example v2 v3 v6 v5 v4 v7 v1 v1 Topological order:
Topological Ordering Algorithm: Example v2 v2 v3 v6 v5 v4 v7 Topological order: v1
Topological Ordering Algorithm: Example v3 v3 v6 v5 v4 v7 Topological order: v1, v2
Topological Ordering Algorithm: Example v6 v5 v4 v4 v7 Topological order: v1, v2, v3
Topological Ordering Algorithm: Example v6 v5 v5 v7 Topological order: v1, v2, v3, v4
Topological Ordering Algorithm: Example v6 v6 v7 Topological order: v1, v2, v3, v4, v5
Topological Ordering Algorithm: Example v7 v7 Topological order: v1, v2, v3, v4, v5, v6
Topological Ordering Algorithm: Example v2 v3 v6 v5 v4 v1 v2 v3 v4 v5 v6 v7 v7 v1 Topological order: v1, v2, v3, v4, v5, v6, v7.
Topological Sort Algorithm 1. 2. 3. Store each vertex’s In-Degree in an array D Initialize queue with all “in-degree=0” vertices While there are vertices remaining in the queue: (a) (b) (c) Dequeue and output a vertex Reduce In-Degree of all vertices adjacent to it by 1 Enqueue any of these vertices whose In-Degree became zero all vertices are output then success, 4. If otherwisethereis acycle. 11/27/02 TopologicalSort-Lecture19 24
Topological Sort Analysis • • • Initialize In-Degree array: O(|V| + |E|) Initialize Queue with In-Degree 0 vertices: O(|V|) Dequeue and output vertex: › |V| vertices, each takes only O(1) to dequeue and output: O(|V|) • Reduce In-Degree of all vertices adjacent and Enqueue any In-Degree 0 vertices: toavertex › O(|E|) • For inputgraphG=(V,E) runtime = O(|V| +|E|) › 11/27/02 Linear time! TopologicalSort-Lecture19 26