Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topological Sort CSE 373 Data Structures Lecture 19.

Similar presentations


Presentation on theme: "Topological Sort CSE 373 Data Structures Lecture 19."— Presentation transcript:

1 Topological Sort CSE 373 Data Structures Lecture 19

2 Topological Sort Problem: Find an order in which all these be taken.
142 322 143 321 Problem: Find an order in which all these be taken. courses can 326 370 341 Example: 370 326 142 321 421 143 341 401 378 322 378 421 In order to take a course, you must 401 take all of its prerequisites first 11/27/02 Topological Sort - Lecture 19 3

3 Topological Sort Given a digraph G = (V, its vertices such that:
E), find a linear ordering of for any edge (v, w) in E, v precedes w in the ordering B C A F D E 11/27/02 Topological Sort - Lecture 19 4

4 Topo sort - good example B C B C Any linear ordering
all the arrows go to in which A the right F is a valid solution D E A B F C D E Note that F can go anywhere in this list because it is not connected. 11/27/02 Topological Sort - Lecture 19 5

5 Topo sort - bad example B C 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 Topological Sort - Lecture 19 6

6 Not all can be Sorted • A directed graph with a cycle cannot be
topologically sorted. B C A F D E 11/27/02 Topological Sort - Lecture 19 7

7 vertices v1,v2, …,vk Cycles • Given a digraph G = (V,E), a cycle is a
sequence of that vertices v1,v2, …,vk such G k < 1 v1 = vk (vi,vi+1) in E for 1 < i < k. is acyclic if it has no cycles. 11/27/02 Topological Sort - Lecture 19 8

8 Topo sort algorithm - 1 Step 1: Identify vertices that have no incoming edges The “in-degree” of these vertices is zero B C A F D E 11/27/02 Topological Sort - Lecture 19 9

9 Topo sort algorithm - 1a Step 1: Identify vertices that have no incoming edges If no such vertices, graph has only cycle(s) (cyclic graph) Topological sort not possible – Halt. B C A Example of a cyclic graph D 11/27/02 Topological Sort - Lecture 19 10

10 Topo sort algorithm - 1b Step 1: Identify vertices that have no
incoming edges Select one such vertex Select B C A F D E 11/27/02 Topological Sort - Lecture 19 11

11 Topo sort algorithm - 2 Step 2: Delete this
vertex of in-degree 0 and all its outgoing edges output. B A from the graph. Place it in the C A F D E 11/27/02 Topological Sort - Lecture 19 12

12 Continue until done Repeat Step 1 and Step 2 until graph is empty
Select B C A F D E 11/27/02 Topological Sort - Lecture 19 13

13 B Select B. Copy to sorted list. Delete B and its edges. B C F A B D E
11/27/02 Topological Sort - Lecture 19 14

14 C Select C. Copy to sorted list. Delete C and its edges. C F A B C D E
11/27/02 Topological Sort - Lecture 19 15

15 D Select D. Copy to sorted list. Delete D and its edges. F A B C D D E
11/27/02 Topological Sort - Lecture 19 16

16 E, F Select E. F. Copy to sorted list. Delete E and its edges. F and
B C D E F E 11/27/02 Topological Sort - Lecture 19 17

17 Done B C A F Remove from algorithm and serve. D E A B C D E F 11/27/02
Topological Sort - Lecture 19 18

18 Topological Ordering Algorithm: Example
v2 v3 v6 v5 v4 v7 v1 v1 Topological order:

19 Topological Ordering Algorithm: Example
v2 v2 v3 v6 v5 v4 v7 Topological order: v1

20 Topological Ordering Algorithm: Example
v3 v3 v6 v5 v4 v7 Topological order: v1, v2

21 Topological Ordering Algorithm: Example
v6 v5 v4 v4 v7 Topological order: v1, v2, v3

22 Topological Ordering Algorithm: Example
v6 v5 v5 v7 Topological order: v1, v2, v3, v4

23 Topological Ordering Algorithm: Example
v6 v6 v7 Topological order: v1, v2, v3, v4, v5

24 Topological Ordering Algorithm: Example
v7 v7 Topological order: v1, v2, v3, v4, v5, v6

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

26 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 otherwise there is a cycle. 11/27/02 Topological Sort - Lecture 19 24

27 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: to a vertex O(|E|) For input graph G=(V,E) run time = O(|V| + |E|) 11/ 27/ 02 Linear time! Topological Sort - Lecture 19 26


Download ppt "Topological Sort CSE 373 Data Structures Lecture 19."

Similar presentations


Ads by Google