Topological Sort: Definition

Slides:



Advertisements
Similar presentations
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Advertisements

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Topological Sort and Hashing
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
Graphs – Depth First Search ORD DFW SFO LAX
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Applications of graph traversals
Applications of DFS: Articulation Points and Biconnected Components
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Chapter 2 Graph Algorithms.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
Graphs Upon completion you will be able to:
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
Chapter 05 Introduction to Graph And Search Algorithms.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
Graph Search Applications, Minimum Spanning Tree
Introduction to Algorithms
Topological Sorting.
Chapter 22 Elementary Graph Algorithms
More Graph Algorithms.
Graph.
Graph Search Applications
CS 3343: Analysis of Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Advanced Algorithms Analysis and Design
"Learning how to learn is life's most important skill. " - Tony Buzan
CSE 373 Data Structures and Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Directed Graph Algorithms
Directed Graph Algorithms
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
Richard Anderson Autumn 2016 Lecture 5
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Applications of DFS: Articulation Points and Biconnected Components
CSE 373: Data Structures and Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Elementary Graph Algorithms
Chapter 22: Elementary Graph Algorithms III
GRAPH TRAVERSAL.
Presentation transcript:

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

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

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

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

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

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

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

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:

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

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

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

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

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

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

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

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

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

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 3.2 + 3.3 – O(e) Total Running Time – O(n+e) - Linear

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.

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

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)

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

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

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?

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

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

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

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

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

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

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?

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

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

Finding Articulation Points d=1 Low=1 a 2 1 b 8 8 e 3 1 c 9 8 i 4 3 10 8 d j 5 3 h 7 1 f 6 3 g

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

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

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

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

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?

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)

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

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

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

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

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

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?

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