Topological Sort (topological order)

Slides:



Advertisements
Similar presentations
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
Data Structures Using C++
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
Data Structures Using C++
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
Data Structures Using Java1 Chapter 11 Graphs. Data Structures Using Java2 Chapter Objectives Learn about graphs Become familiar with the basic terminology.
1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices,
Chapter 23 Minimum Spanning Trees
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CS 146: Data Structures and Algorithms July 21 Class Meeting
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
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.
Graphs, BFS, DFS and More…
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.
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Topological Sorting.
Chapter 13: The Graph Abstract Data Type
CSC317 Shortest path algorithms
Chapter 7: Greedy Algorithms
Introduction to Graphs
Shortest Path Problems
SINGLE-SOURCE SHORTEST PATHS IN DAGs
Graphs: As a mathematics branch
Topological Sort (topological order)
Shortest Path Graph represents highway system Edges have weights
More Graph Algorithms.
CHP-7 GRAPH.
Graph Algorithm.
Graphs Graph transversals.
Single-Source All-Destinations Shortest Paths With Negative Costs
Graphs Chapter 13.
CSE 373 Data Structures and Algorithms
Graphs: As a mathematics branch
A* Path Finding Ref: A-star tutorial.
Search Related Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Graphs.
Shortest Path Problems
Shortest Path Algorithms
Directed Graph Algorithms
Directed Graph Algorithms
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
Topological Sort Algorithm
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCI2100 Data Structures Tutorial
Single-Source All-Destinations Shortest Paths With Negative Costs
Shortest Path Problems
CE 221 Data Structures and Algorithms
Chapter 16 1 – Graphs Graph Categories Strong Components
Fundamental Structures of Computer Science II
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Single-Source All-Destinations Shortest Paths With Negative Costs
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Topological Sort (topological order) Let G = (V, E) be a directed graph with |V| = n. A Topological Sort is a sequence v1, v2,.... vn, such that { v1, v2,.... vn } = V, and for every i and j with 1  i < j  n, (vj , vi)  E. 7 1 6 5 3 2 4 7 5 There are more than one such order in this graph!! 2 4 3 1 6 11/13/2018 IT 279

If the graph is not acyclic, then there is no topological sort for the graph. Not an acyclic Graph 2 4 7 9 1 3 5 8 No way to arrange vertices in this cycle without pointing backwards 6 11/13/2018 IT 279

Algorithm for finding Topological Sort Principle: vertex with in-degree 0 should be listed first. 2 1 7 2 7 Algorithm: 1 Input G=(V,E) Repeat the following steps until no more vertex left: Find a vertex v with in-degree 0; If can’t find such v and V is not empty then stop the algorithm; this graph is not acyclic 2. Remove v from V and update E 3. Process v 11/13/2018 IT 279

Topological Sort (topological order) of an acyclic graph 7 1 6 5 3 2 4 7 5 2 4 3 1 6 11/13/2018 IT 279

Fail to find a topological sort (topological order) 1 2 8 2 4 7 9 1 3 5 8 the graph is not acyclic 6 11/13/2018 IT 279

Topological Sort (topological order) of an acyclic graph 2 8 5 4 3 7 9 6 1 2 4 7 9 1 3 5 8 6 This graph is acyclic 11/13/2018 IT 279

Topological Sort in C++ // return an array where the first entry is the size of g // or 0 if g is not acyclic; int * graph::topsort(const Graph & g) { int *topo; Graph tempG = g; topo = new int[g.size()+1]; topo[0] = g.size(); for (int i=1; i<=topo[0]; i++) { int v = findVertexwithZeroIn(tempG); if (v == -1) { // g is not acyclic delete [] topo; topo = new int[1]; topo[0] = 0; return topo; } topo[i] = v; remove_v(tempG, v); 11/13/2018 IT 279

Programming languages Operating systems Distributed processing Edsger Wybe Dijkstra 1930-2002 Algorithm design Programming languages Operating systems Distributed processing Formal specification and verification 1972 Turing Award Dijkstra Algorithm: To find the shortest path between two vertices in a weighted graph. 11/13/2018 IT 279

Common Algorithm Techniques Back Tracking.... Greedy Algorithms: Algorithms that make decisions based on the current information; and once a decision is made, the decision will not be revised Divide and Conquer.... Dynamic Programming..... Problem: Minimized the number of coins for change. In real life, we can use a greedy algorithm to obtain the minimum number of coins. But in general, we need dynamic programming to do the job 11/13/2018 IT 279

Greedy Algorithms  Dynamic Programming Problem: Minimized the number of coins for 66¢ { 25¢, 12¢, 5¢, 1¢ } { 25¢, 10¢, 5¢, 1¢ } 25¢ 2 50¢ 10¢ 1 5¢ 1¢ 5 66¢ 25¢ 2 50¢ 12¢ 1 5¢ 0¢ 1¢ 4 4¢ 7 66¢ 25¢ 2 50¢ 12¢ 0¢ 5¢ 3 15¢ 1¢ 1 6 66¢ 16¢ 16¢ 6¢ 4¢ 1¢ 4¢ 0¢ 0¢ Greedy method Greedy method Dynamic method 11/13/2018 IT 279

Finding the shortest path between two vertices Starting Vertex Starting Vertex Unweighted Graphs  Weighted Graphs 2 5 1 2 2 3 3 5 1 1 1 5 3 3 7 7 7 2 2 1 9 10 12 8 8 2 Both are using the greedy algorithm. w w Final Vertex Final Vertex 11/13/2018 IT 279

Construct a shortest path Map Starting Vertex Starting Vertex 0,-1 1 2 1,0 1,0 3 4 2,1 2,1 5 2,2 x w-1,y n w,x Final Vertex Final Vertex 11/13/2018 IT 279

Shortest paths of unweighted graphs // <vertex, <distance, previous vertex in the path>> typedef map<int,pair<double, int> > PathMap; PathMap graph::UnweightedShortestPath(Graph & g, int s) { PathMap SPMap; // create a bookkeeper; for (Graph::iterator itr = g.begin(); itr != g.end(); itr++) { SPMap[itr->first] = make_pair(inft,-1); // -1 mean no previous } SPMap[s] = make_pair(0,-1); // s is not done yet. deque<int> candidates; candidates.push_back(s); while (!candidates.empty()) { int v = candidates[0]; candidates.pop_front(); AdjacencyList ADJ = g[v]; for (AdjacencyList::iterator w=ADJ.begin(); w != ADJ.end(); w++) { if (SPMap[w->first].first == inft) { SPMap[w->first].first = SPMap[v].first+1; SPMap[w->first].second=v; candidates.push_back(w->first); } // end enqueue next v } // end ADJ interation; all nextvs's next into the queue. return SPMap; 11/13/2018 IT 279

Construct a shortest path Map for weighted graph  Starting Vertex Starting Vertex  0,-1 2 5   1 1 2 2,0 3,1 5,0 1 5   3 4 7 3,1 7,1 1 9  5 10,2 8,4 w-1,y : decided n w,x Final Vertex Final Vertex 11/13/2018 IT 279

Shortest paths of weighted graphs (I) // A Dijkstra's algorithm PathMap graph::ShortestPath(Graph & g, int s) { PathMap SPMap; map<int,pair<double, bool> > dist_map; // A distance map // for book keeping; for (Graph::iterator itr = g.begin(); itr != g.end(); itr++) { dist_map[itr->first] = make_pair(inft,false); SPMap[itr->first] = make_pair(inft,-1); } dist_map[s] = make_pair(0,false); // s is not done yet. multimap<double,int> candidates ; // next possible vertices //sorted by their distance. candidates.insert(make_pair(0,s)); // start from s; ..... 11/13/2018 IT 279

Shortest paths (II) // A Dijkstra's algorithm PathMap graph::ShortestPath(Graph & g, int s) { ...... while (! candidates.empty()) { int v = candidates.begin()->second; double costs2v = candidates.begin()->first; candidates.erase(candidates.begin()); if (dist_map[v].second) continue; // v is done after pair // (weight v) is inserted; dist_map[v] = make_pair(costs2v,true); AdjacencyList ADJ = g[v]; // all not done adjacent vertices // should be candidates for (AdjacencyList::iterator itr=ADJ.begin(); itr != ADJ.end(); itr++) { int w = itr->first; if (dist_map[w].second) continue; // this w is done; double cost_via_v = costs2v + itr->second; if (cost_via_v < dist_map[w].first) { dist_map[w] = make_pair(cost_via_v ,false); SPMap[w] = make_pair(cost_via_v,v); } candidates.insert(make_pair(cost_via_v,w)); } // end for ADJ iteration; } // end while !candidates.empty() return SPMap; Shortest paths (II) 11/13/2018 IT 279