Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation.

Slides:



Advertisements
Similar presentations
ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort algorithm) 8/25/20091 ALG0183 Algorithms & Data Structures.
Advertisements

Single Source Shortest Paths
Chapter 9: Graphs Topological Sort
Some Graph Algorithms.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Topological Sort.
Topological Sort and Hashing
Graph Algorithms: Topological Sort The topological sorting problem: given a directed, acyclic graph G = (V, E), find a linear ordering of the vertices.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
Topological Sort Introduction. Definition of Topological Sort.
Testing for Connectedness and Cycles
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Connected Components, Directed Graphs, Topological Sort COMP171.
1 Graph Introduction Definitions. 2 Definitions I zDirected Graph (or Di-Graph) is an ordered pair G=(V,E) such that yV is a finite, non-empty set (of.
Graph Traversals Depth-First Traversal. The Algorithm.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graph Traversals Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.
1 Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithms. An Example. Implementation.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Testing for Connectedness & Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
All leaves are on the bottom level. All internal nodes (except perhaps the root node) have at least ceil(m / 2) (nonempty) children. The root node can.
U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1.
10/13/2015IT 328, review graph algorithms1 Topological Sort ( topological order ) Let G = (V, E) be a directed graph with |V| = n. 1.{ v 1, v 2,.... v.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Topological Sort: Definition
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Topological Sort. Sorting technique over DAGs (Directed Acyclic Graphs) It creates a linear sequence (ordering) for the nodes such that: –If u has an.
Topological Sorting.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
CSE 373 Topological Sort Graph Traversals
Topological Sort In this topic, we will discuss: Motivations
Depth-First Search.
SINGLE-SOURCE SHORTEST PATHS IN DAGs
CS202 - Fundamental Structures of Computer Science II
Graph Traversals Depth-First Traversals. Algorithms. Example.
More Graph Algorithms.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Topological Sort.
Topological Sort.
Testing for Connectedness and Cycles
Topological Sort Introduction. Definition of Topological Sort.
"Learning how to learn is life's most important skill. " - Tony Buzan
Topological Sort CSE 373 Data Structures Lecture 19.
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.
Richard Anderson Winter 2009 Lecture 6
Topological Sort Introduction. Definition of Topological Sort.
Topological Sort Introduction. Definition of Topological Sort.
Richard Anderson Lecture 5 Graph Theory
Topological Sort Introduction. Definition of Topological Sort.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Presentation transcript:

Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation. Review Questions.

Introduction There are many problems involving a set of tasks in which some of the tasks must be done before others. For example, consider the problem of dressing up for school in the morning. Is there any systematic way of linearly arranging the items in the order that they should be worn? Yes! - Topological sort.

Definition of Topological Sort Topological sort is a method of arranging the vertices in a directed acyclic graph (DAG), as a sequence, such that no vertex appear in the sequence before its predecessor. The graph in (a) can be topologically sorted as in (b) (a) (b)

Topological Sort is not unique Topological sort is not unique. The following are all topological sort of the graph below: s1 = {a, b, c, d, e, f, g, h, i} s2 = {a, c, b, f, e, d, h, g, i} s3 = {a, b, d, c, e, g, f, h, i} s4 = {a, c, f, b, e, h, d, g, i} etc.

Topological Sort Algorithm One way to find a topological sort is to consider in-degrees of the vertices. The first vertex must have in-degree zero -- every DAG must have at least one vertex with in-degree zero. The full Algorithm goes as follows: 1 Repeat the following until the graph is empty: 2 Select a vertex that has in-degree zero. 3 Add the vertex to the sort. 4 Delete the vertex and all the edges emanating from it.

Topological Sort Example Demonstrating Topological Sort. A F B G C H D I E J D G A B F H J E I C

Implementation of Topological Sort The algorithm is implemented as a traversal method that visits the vertices in a topological sort order. An array of length |V| is used to record the in-degrees of the vertices. Hence no need to remove vertices or edges. A queue is used to keep track of vertices with in-degree zero but not yet visited. 1 public void topologicalOrderTraversal(Visitor visitor) { 2 int[] inDegree = new int [numberOfVertices]; 3 for (int v = 0; v < numberOfVertices; ++v) 4 inDegree [v] = 0; 5 Enumeration p = getEdges (); 6 while (p.hasMoreElements ()){ 7 Edge edge = (Edge) p.nextElement (); 8 Vertex to = edge.getV1 (); 9 ++inDegree [to.getNumber ()]; 10 }

Implementation of Topological Sort 11 Queue queue = new QueueAsLinkedList (); 12 for (int v = 0; v < numberOfVertices; ++v) 13 if (inDegree [v] == 0) 14 queue.enqueue (vertex [v]); 15 while (!queue.isEmpty () && !visitor.isDone ()){ 16 Vertex v = (Vertex) queue.dequeue (); 17 visitor.visit (v); 18 Enumeration q = v.getSuccessors (); 19 while (q.hasMoreElements ()) { 20 Vertex to = (Vertex) q.nextElement (); 21 if (--inDegree [to.getNumber ()] == 0) 22 queue.enqueue (to); 23 } 24 } 25 } 26 //… 27 }

Review Questions 1. List the order in which the nodes of the directed graph GB are visited by a topological order traversal that starts from vertex a. 2. What kind of DAG has a unique topological sort? 3. Generate a directed graph using the required courses for your major. Now apply topological sort on the directed graph you obtained.