Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
Programming languages Operating systems Distributed processing
Edsger Wybe Dijkstra 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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.