C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Topological Sort.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Simple Graph Warmup. Cycles in Simple Graphs A cycle in a simple graph is a sequence of vertices v 0, …, v n for some n>0, where v 0, ….v n-1 are distinct,
Chapter 9: Graphs Topological Sort
Some Graph Algorithms.
CSE115/ENGR160 Discrete Mathematics 04/26/12 Ming-Hsuan Yang UC Merced 1.
CMSC 341 Graphs 3. Adjacency Table Implementation Uses table of size |V|  |V| where each entry (i,j) is boolean –TRUE if there is an edge from vertex.
Data Structures Using C++
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.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Data Structures Using C++ 2E1 Topic of this lecture: Topological Order OU CS prerequisite bubble chart
Algorithms and Data Structures
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Data Structures Using C++
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Data Structures Using Java1 Chapter 11 Graphs. Data Structures Using Java2 Chapter Objectives Learn about graphs Become familiar with the basic terminology.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Topological Sort Introduction. Definition of Topological Sort.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Data Structures Using C++ 2E
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest.
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Topological Sorting.
Graphs Chapter 20.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
CSE 373 Topological Sort Graph Traversals
Graph Traversals Some algorithms require that every vertex of a graph be visited exactly once. The order in which the vertices are visited may be important,
Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of a finite set V called.
More Graph Algorithms.
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.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
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
Topological Sort Introduction. Definition of Topological Sort.
Richard Anderson Lecture 5 Graph Theory
Topological Sort Introduction. Definition of Topological Sort.
Presentation transcript:

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Topological Sort

Objectives In this section you will: Learn about topological sort

In college, before taking a particular course, students usually must take all applicable prerequisite courses. For example, before taking the Programming II course, students must take the Programming I course. However, certain courses can be taken independently of each other. The courses within a department can be represented as a directed graph. A directed edge from, say, vertex u to vertex v means that the course represented by the vertex u is a prerequisite of the course represented by the vertex v. It would be helpful for students to know, before starting a major, the sequence in which they can take courses so that before taking a particular course they take all its prerequisite courses. This section describes an algorithm that can be used to output the vertices of a directed graph in such a sequence.

Let G be a directed graph and V(G) = {v 1, v 2,..., v n }, where n > 0. A topological ordering of V(G) is a linear ordering v i1, v i2,..., v in of the vertices such that if v ij is a predecessor of v ik, j  k, 1  j  n, and 1  k  n, then v ij precedes v ik, that is, j < k in this linear ordering. We assume that the graph has no cycles. Because the graph has no cycles: There exists a vertex u in G such that u has no predecessor. There exists a vertex v in G such that v has no successor.

Suppose that the array topologicalOrder (of size n, the number of vertices) is used to store the vertices of G in topological order. Thus, if a vertex, say u, is a successor of the vertex v and topologicalOrder[j]= v and topologicalOrder[k] = u, then j < k. The topological sort algorithm can be implemented using either the depth first traversal or the breadth first traversal. This section discusses how to implement topological ordering using the breadth first traversal.

In the breadth first topological ordering we first find a vertex that has no predecessor vertex and place it first in the topological ordering. We next find the vertex, say v, all of whose predecessors have been placed in the topological ordering and place v next in the topological ordering. To keep track of the number of vertices of a vertex we use the array predCount. Initially, predCount[j] is the number of predecessors of the vertex v j. The queue used to guide the breadth first traversal is initialized to those vertices v k such that predCount[k] is zero.

The general algorithm is:

The vertices of G 3 in breadth first topological ordering are We illustrate the breadth first topological ordering of the graph G 3