Topological Sort Introduction. Definition of Topological Sort.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
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?
Data Structures Using C++ 2E1 Topic of this lecture: Topological Order OU CS prerequisite bubble chart
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
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.
1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation.
Introduction to Graphs
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.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
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.
Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
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.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
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.
Topological Sort: Definition
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
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.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Week 11 - Wednesday.  What did we talk about last time?  Exam 2  And before that:  Graph representations  Depth first search.
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
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
Search Related Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Chapter 11 Graphs.
CSCI2100 Data Structures Tutorial
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
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 taking a course only after taking its prerequisites. Is there any systematic way of linearly arranging the courses in the order that they should be taken? 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 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 Topological sort algorithm is: int topologicalOrderTraversal( ){ int numVisitedVertices = 0; while(there are more vertices to be visited){ if(there is no vertex with in-degree 0) break; else{ select a vertex v that has in-degree 0; visit v; numVisitedVertices++; delete v and all its emanating edges; } return numVisitedVertices;

Topological Sort Example Demonstrating Topological Sort. A F B G C H D I E J 1 2 3 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 priority queue is used to keep track of vertices with in-degree zero that are not yet visited. public int topologicalOrderTraversal(Visitor visitor){ int numVerticesVisited = 0; int[] inDegree = new int[numberOfVertices]; for(int i = 0; i < numberOfVertices; i++) inDegree[i] = 0; Iterator p = getEdges(); while (p.hasNext()) { Edge edge = (Edge) p.next(); Vertex to = edge.getToVertex(); inDegree[getIndex(to)]++; }

Implementation of Topological Sort BinaryHeap queue = new BinaryHeap(numberOfVertices); p = getVertices(); while(p.hasNext()){ Vertex v = (Vertex)p.next(); if(inDegree[getIndex(v)] == 0) queue.enqueue(v); } while(!queue.isEmpty() && !visitor.isDone()){ Vertex v = (Vertex)queue.dequeueMin(); visitor.visit(v); numVerticesVisited++; p = v.getSuccessors(); while (p.hasNext()){ Vertex to = (Vertex) p.next(); if(--inDegree[getIndex(to)] == 0) queue.enqueue(to); return numVerticesVisited;

Review Questions 1. List the order in which the nodes of the directed graph GB are visited by 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.