Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices.

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Chapter 9: Graphs Topological Sort
Some Graph Algorithms.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Topological Sort Example This job consists of 10 tasks with the following precedence rules: Must start with 7, 5, 4 or 9. Task 1 must follow 7. Tasks 3.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Topological Sort.
Topological Sort and Hashing
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
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.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Algorithms and Data Structures
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
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.
What is the next line of the proof? a). Let G be a graph with k vertices. b). Assume the theorem holds for all graphs with k+1 vertices. c). Let G be a.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
CSE 421 Algorithms Richard Anderson Lecture 4. What does it mean for an algorithm to be efficient?
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Section 13  Questions  Graphs. Graphs  A graph representation: –Adjacency matrix. Pros:? Cons:? –Neighbor lists. Pros:? Cons:?
Topological Sort: Definition
CSC2100B Tutorial 10 Graph Jianye Hao.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
SSSP in DAGs (directed acyclic graphs). DFS (depth first search) DFS(vertex v) { v.visited = TRUE; for each w adjacent to v do if(!w.visited) then dfs(w);
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Topological Sort. Sorting technique over DAGs (Directed Acyclic Graphs) It creates a linear sequence (ordering) for the nodes such that: –If u has an.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Topological Sorting.
Chapter 22 Elementary Graph Algorithms
Introduction to Algorithms
Lecture 12 Graph Algorithms
CS120 Graphs.
More Graph Algorithms.
Topological Sort.
Topological Sort.
Paul Beame in lieu of Richard Anderson
"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
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Richard Anderson Winter 2009 Lecture 6
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Richard Anderson Lecture 5 Graph Theory
Lecture 11 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Assignment #2 (Assignment due: Nov. 06, 2018) v1 v2 v3 v4 v5
Presentation transcript:

Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. A B C F D E

Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. A B C F D EAD E F BC

Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. A B C F D EAD E F BC Any linear ordering in which all the arrows go to the right.

Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. A B C F D FAD E E BC Any linear ordering in which all the arrows go to the right. This is not a topological ordering.

Graph Algorithms: Topological Sort The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. (In general, this subset must be nonempty—why?) A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. (In general, this subset must be nonempty—because the graph is acyclic.) A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. Select one of them. A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Remove it, and its outgoing edges, and add it to the output. A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge,... A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them,... A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. AB C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. AB C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. ABCF D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. ABCF D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. ABCFD E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. ABCFD E

Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. ABCFDE

Graph Algorithms: Topological Sort The topological sorting algorithm: finished! ABCFDE A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound? ABCFDE A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: ? Remove edges: ? Place vertices in output: ? ABCFDE A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: ? Remove edges: O(|E|) Place vertices in output: ? ABCFDE A B C F D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: ? Remove edges: O(|E|) Place vertices in output: O(|V|) ABCFDE A B C F D E

Graph Algorithms: Topological Sort Find vertices with no predecessors: ? A B C F D E ABCDEFABCDEF B D E E D C Assume an adjacency list representation:

Graph Algorithms: Topological Sort The topological sorting algorithm: …and initialize and maintain for each vertex its no. of predecessors. A B C F D E ABCDEFABCDEF B D E E D C

Graph Algorithms: Topological Sort Find vertices with no predecessors: ? A B C F D E Time for each vertex: O(|V|) ABCDEFABCDEF B D E E D C

Graph Algorithms: Topological Sort Find vertices with no predecessors: ? A B C F D E Total time: O(|V| ) 2 ABCDEFABCDEF B D E E D C

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|) 2

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|) 2 Total: O(|V| + |E|) 2

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|) 2 Total: O(|V| + |E|) 2 Too much!

Graph Algorithms: Topological Sort The topological sorting algorithm: We need a faster way to do this step: Find vertices with no predecessors.

Graph Algorithms: Topological Sort The topological sorting algorithm: Key idea: initialize and maintain a queue (or stack) holding pointers to the vertices with 0 predecessors A B C F D E ABCDEFABCDEF B D E E D C

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C F D E ABCDEFABCDEF B D E E D C

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. B C D E ABCDEFABCDEF E E D C F Output: A No scan is required, so O(1).

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. B C D E ABCDEFABCDEF E E D C Output: A F

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. C D E ABCDEFABCDEF E E D Output: A F B

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. D E ABCDEFABCDEF E Output: A F B C

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. E ABCDEFABCDEF Output: A F B C D

Graph Algorithms: Topological Sort The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. Finished! ABCDEFABCDEF Output: A F B C D E

Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound: Now the time for each part is Find vertices with no predecessors: O(|V|) Remove edges: O(|E|) Place vertices in output: O(|V|) Total: O(|V|+|E|) Linear in |V|+|E|. Much better!