Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topological Sort: Definition

Similar presentations


Presentation on theme: "Topological Sort: Definition"— Presentation transcript:

1 Topological Sort: Definition
Consider the following graph of course prerequisities 201 306 427 111 123 213 304 446 205 Problem: Find an order in which all these courses can be taken. 220 302 402 To take a course, all of its prerequisites must be taken first Example: 111, 123, 201, 213, 304, 306, 427, 205, 446, 220, 302, 402

2 Topological Sorting Problem
Given digraph G = (V, E), find a linear ordering of its vertices such that: for any edge (u, v) in E, u precedes v in the ordering How would you topo-sort this graph given an adjacency list representation of G=(V, E)? A B C D F E

3 Topological Sorting Problem
Given digraph G = (V, E), find a linear ordering of its vertices such that: for any edge (u, v) in E, u precedes v in the ordering Any linear ordering in which all arrows go to the right is a valid ordering B C F A D E A B F C D E

4 Topological Sort Given digraph G = (V, E), find a linear ordering of its vertices such that: for any edge (u, v) in E, u precedes v in the ordering B C F A Not a valid topological sort D E A B F D C E

5 Topological Sort Algorithm
Step1: Identify vertices that have no incoming edge in-degree of these vertices is 0 B C A F D E

6 Topological Sort Algorithm
Step1: Identify vertices that have no incoming edge If no such vertices, the graph has cycles (cyclic) Cyclic graphs cannot be topo-sort Only Directed Acyclic Graphs (DAG) can be topo-sort B C A An example cyclic graph: No vertex with in-degree = 0 D

7 Topological Sort Algorithm
Step1: Identify vertices that have no incoming edge Select one such vertex B C Select A F D E

8 Topological Sort Algorithm
Step 2: Output the vertex Delete this vertex and all of its outgoing edges from the graph B C A F B C D F E D E A Output:

9 Topological Sort Algorithm
Repeat Steps 1 & 2 until the graph is empty Select Delete B & all its outgoing edges B C C D F E F D E A Output: B

10 Topological Sort Algorithm
Repeat Steps 1 & 2 until the graph is empty Select Delete F & all its outgoing edges C C D E F D E A Output: B F

11 Topological Sort Algorithm
Repeat Steps 1 & 2 until the graph is empty Select Delete C & all its outgoing edges C D E D E A Output: B F C

12 Topological Sort Algorithm
Repeat Steps 1 & 2 until the graph is empty Delete D & all its outgoing edges Select D E E A Output: B F C D

13 Topological Sort Algorithm
Repeat Steps 1 & 2 until the graph is empty Delete E & all its outgoing edges Select Empty Graph E A Output: B F C D E

14 Summary of Top-Sort Algorithm
Store each vertex’s In Degree (# of incoming edges) in an array while (there are vertices remaining) { Find a vertex with In-Degree zero and output it Reduce in-degree of all vertices adjacent to it by 1 Mark this vertex deleted (in-degree = -1) } /* end=while */ A B C D F E In- degree 1 2 A B D B C C D E D E E F

15 Running Time Analysis Can we do better than this?
For input graph G = (V,E), Running Time = ? Break down into total time required to: Initialize In-Degree array: O(n+e) Find vertex with in-degree 0: O(n) N vertices, each takes O(n) to search In-Degree array. Total time = O(n2) Reduce In-Degree of all vertices adjacent to a vertex: O(e) Output and mark vertex: O(n) Total time = O(n2 + e) - Quadratic time! Can we do better than this? Problem: Need a faster way to find a vertex with in-degree = 0

16 Making Top-Sort Faster
Key idea: Initialize and maintain a queue (or stack) of vertices with In-Degree 0 In- degree Queue: A F 1 2 A B D B C A B C D F E C D E D E E F

17 Making Top-Sort Faster
After each vertex is output, update In-Degree array, and enqueue any vertex whose In-Degree has become zero Enqueue Queue: F B In- degree Dequeue 1 2 A B D Output: A B C A B C D F E C D E D E E F

18 Fast Top-Sort Algorithm
Store each vertex’s In-Degree in an array 2. Initialize a queue with all in-degree zero vertices 3. while (there are vertices remaining in the queue) { 3.1. Dequeue and output a vertex 3.2. Reduce In-Degree of all vertices adjacent to it by 1 3.3. Enqueue any of these vertices whose In-Degree became zero } //end-while Running Time Analysis: Step 1 – Initialization - O(n+e) Step 2 – Initialize Q – O(n) Step 3.1 – O(1) – n times – O(n) Step – O(e) Total Running Time – O(n+e) - Linear

19 Topological Sort – Using DFS
There is another O(n+e) algorithm for topological sort that is based on the DFS Think about how DFS works We start from a vertex, and go all the way down the graph until we can go no further This means that the node deepest down the tree must come last in topological order, followed by its ancestors and so on To state this in another way, for every edge (u, v) in a DAG, the finish time of u is greater than the finish time of v. Thus it suffices to output the vertices in reverse order of finishing time.

20 Topological Sort – based on DFS
TopSort(G){ for each u in V { // Initialization color[u] = white; } //end-for L = new linked_list; // L is an empty linked list for each u in V if (color[u] == white) TopVisit(u); return L; // L gives the final order } // end-TopSort TopVisit(u){ // Start a new search at u color[u] = gray; // Mark u visited for each v in Adj[u] { if (color[v] == white){ // if neighbor v undiscovered TopVisit(v); // …visit v } //end-if color[u] = black; // we are done with u Put u to the front of L; // on finishing add u to the list } //end-while } //end-TopVisit

21 Topological Sort - Example
Example: Professor Bumstead’s order of dressing shorts pants belt shirt tie jacket socks shoes shorts (1/10) pants (2/9) shirt (11/14) socks (15/16) belt (3/6) shoes (7/8) jacket (4/5) tie (12/13) Final order: socks, shirt, tie, shorts, pants, shoes, belt, jacket Total Running Time: O(n+e)

22 Articulation Point (or Cut Vertex)
Let G = (V, E) be a connected undirected graph An articulation point or cut vertex is a vertex whose removal (together with the removal of any incident edges) results in a disconnected graph. A bridge is an edge whose removal results in a disconnected graph a e i b f j c g d h Cut Vertex Bridge

23 Articulation Point - Applications
Articulation points are of great interest in the design of network algorithm Because these are “critical” points, whose failure will result in the network becoming disconnected

24 Finding Articulation Points
To find the articulation points of an undirected graph, we will call DFS and use the DFS tree structure to aid us in finding the articulation points Question: If “u” is an articulation point, how would we know it by its structure in the DFS tree?

25 Finding Articulation Points
G = (V, E) a a e i b e b f j c i c g DFS(G) d j d h h f Only tree edges and back edges g

26 Finding Articulation Points
Question 1: Can a leaf node be an articulation point? NO! Since a leaf node will not have any sub-trees in the DFS tree, deleting a leaf will not make the tree disconnected. b e c i d j h f g

27 Finding Articulation Points
Question 2: When is the root of the DFS tree an articulation point? If the root has 2 or more children b e c i d j h f g

28 Finding Articulation Points
Question 3: When is an internal node “u” of the DFS tree an articulation point? For any subtree of “u”, if there is NO back edge from a node in this sub-tree to a proper ancestor of “u”, then u is b e c i d j h f g

29 Finding Articulation Points
An internal node “u” is an articulation point only if for any sub-tree of “u”, there is no back edge from a node in this sub-tree to a proper ancestor of “u” In this example, “u” is NOT an articulation point v u

30 Finding Articulation Points
An internal node “u” is an articulation point only if for any sub-tree of “u”, there is no back edge from a node in this sub-tree to a proper ancestor of “u” In this example, “u” is an articulation point v u

31 Finding Articulation Points
Obs1: No leaf is an articulation point Easy to check Obs2: Root is an articulation point only if it has 2 or more children Obs3: An internal node “u” is an articulation point only if for any sub-tree of “u”, there is no back edge from a node in this sub-tree to a proper ancestor of “u” How to check?

32 Checking for back edges
How to check for back edges? Observe that a proper ancestor “v” of an internal node “u” will have a SMALLER discovery time That is, d[v] < d[u] Define Low[u] to be minimum of d[u] and {d[w]| w is a descendant of u and (w, v) is a back edge} Then “u” is an articulation point only if d[u] <= Low[u] v u w

33 Computing Low[u] During DFS, we compute Low[u] as follows:
Initialization Low[u] = d[u] Back edge (u, v) Low[u] = min{Low[u], d[v]} Tree edge (u, v) Low[u] = min{Low[u], Low[v]} Finally: if d[u] <= Low[u], then “u” is an articulation point v u

34 Finding Articulation Points
d= Low=1 a b e c i d j h f g

35 Articulation Point Algorithm
ArtPt(u){ color[u] = gray; Low[u] = d[u] = ++time; for each (v in Adj[u]) { if (color[v] == white) { // (u, v) is a tree edge pred[v] = u; ArtPt(v); // …visit v Low[u] = min{Low[u], Low[v]}; if (pred[u] == NULL) { // u is root if (this is u’s second child) { Add “u” to the list of articulation points } //end-if } else if (d[u] <= Low[u] ) { } //end-else } else if (v != pred[u]) { // (u, v) is a back edge Low[u] = min(Low[u], d[v]); } //end-for color[u] = black; // we are done with u } //end-ArtPt

36 Euler Tours and Circuits
Consider the problem of drawing the following shapes without lifting your pen, drawing each line only once Euler Tour: The start and end points may be different Euler Circuit: Must start and end at the same point

37 Graph Representation of the Puzzle
Junctions: Vertices Line Segments: Edges Euler Circuit Problem: Can you find a cycle that traverses all edges exactly once, starting and ending at the same vertex? Euler Tour Problem: A path that traverses all edges exactly once A B C G D E F

38 Euler Circuit and Tour: Observations
Observation 1: An Euler Circuit exists only if the graph is connected, and each vertex has even degree Why? At every vertex, we need one edge to get out and one edge to get in Observation 2: A graph has an Euler tour only if it is connected and all vertices except two have even degrees, and exactly two has odd degrees A B C G D E F

39 How to find Euler Circuits?
Given a graph G = (V, E), find an Euler Circuit in G Can check if one exists in O(n+e) time How? Simply go over the adjacency list of each vertex, and see if each vertex has even degree If all vertices have even degree, an Euler Circuit exists Otherwise, an Euler circuit does not exist How to compute the Euler circuit?

40 Computing the Euler Circuit
(1) Do a DFS from a vertex until you are back at this vertex Instead of coloring vertices as done in regular DFS, we will color edges so that the same edge is not visited again (Alternatively, the visited edge is deleted) (2) Pick another vertex having an unmarked edge and redo step (1) (3) Combine all cycles together to get the final Euler circuit Running time: O(n+e)

41 Example DFS(A): Delete these edges from the graph ABDFECA A B C D E G

42 Example (continued) DFS(B): Delete these edges from the graph BGCB A B

43 Example (continued) DFS(D): Delete these edges from the graph DEGD A B

44 Example (continued) We have 3 cycles DFS(A): ABDFECA DFS(B): BGCB
DFS(D): DEGD Combine these together Step 1: ABGCBDFECA Step 2: ABGCBDEGDFECA A B C G D E F

45 Final Euler Circuit ABGCBDEGDFECA A 1 12 B 4 C 2 3 G 11 5 8 7 6 D E 9
10 F ABGCBDEGDFECA

46 Hamiltonian Cycle Euler Circuit: A cycle that goes through each edge exactly once Hamiltonian cycle: A cycle that goes through each vertex exactly once B C B C G G D E D E Which of these graphs have An Euler cycle? A Hamiltonian cycle?

47 How to find an Hamiltonian Cycle?
Is there a simple way to check if the graph contains a Hamiltonian cycle? No known easy algorithm to find a Hamiltonian Cycle (HC) The best known solution is to enumerate ALL cycles in the graph and see if one of the cycles is a HC How many cycles in a graph? Exponential! If each vertex has degree “k”, then we have up to O(kn) cycles B C G D E


Download ppt "Topological Sort: Definition"

Similar presentations


Ads by Google