1 / 16

Topological Sort ( topological order )

Let G = (V, E) be a directed graph with |V| = n . Topological Sort ( topological order ). A Topological Sort is a sequence v 1 , v 2 ,.... v n , such that . { v 1 , v 2 ,.... v n } = V, and for every i and j with 1  i < j  n , ( v j , v i )  E. 7. 1. 6. 0. 5. 3.

nicki
Télécharger la présentation

Topological Sort ( topological order )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Let G = (V, E) be a directed graph with |V| = n. Topological Sort (topological order) 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 0 5 3 2 4 0 7 5 There are more than one such order in this graph!! 2 4 3 1 6 ITK 279

  2. If the graph is not acyclic, then there is no topological sort for the graph. 0 Not an acyclic Graph 2 4 7 9 1 3 5 8 No way to arrange vertices in this cycle without pointing backwards 6 ITK 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 vwith in-degree 0; If can’t find such vand V is not empty then stop the algorithm; this graph is not acyclic 2. Remove vfrom V and update E 3. Process v ITK 279

  4. Topological Sort (topological order) of an acyclic graph 7 1 6 0 5 3 2 4 0 7 5 2 4 3 1 6 ITK 279

  5. Fail to find a topological sort (topological order) 1 2 8 0 2 4 7 9 1 3 5 8 the graph is not acyclic 6 ITK 279

  6. Topological Sort (topological order) of an acyclic graph 0 2 8 5 4 3 7 9 6 1 0 2 4 7 9 1 3 5 8 6 This graph is acyclic ITK 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); } return topo; } ITK 279

  8. 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. ITK 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 ITK 279

  10. Greedy Algorithms  Dynamic Programming Problem: Minimized the number of coins for 66¢ { 25¢, 12¢, 5¢, 1¢ } { 25¢, 10¢, 5¢, 1¢ } 16¢ 16¢ 6¢ 4¢ 1¢ 4¢ 0¢ 0¢ Greedy method Greedy method Dynamic method ITK 279

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

  12. Construct a shortest path Map Starting Vertex Starting Vertex 0 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 ITK 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; } ITK 279

  14. Construct a shortest path Map for weighted graph  Starting Vertex Starting Vertex  0 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 ITK 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 theirdistance. candidates.insert(make_pair(0,s)); // start from s; ..... ..... } ITK 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; } ITK 279

More Related