Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 n } = V, and 2.for every i and j with 1  i < j  n, (v j, v i )  E. 17650342 A Topological Sort is a sequence v 1, v 2,.... v n, such that 1 7 6 5 0 3 4 2 There are more than one such order in this graph!!

2 10/13/2015IT 328, review graph algorithms2 If the graph is not acyclic, then there is no topological sort for the graph. 24 35 7 1 6 9 0 8 Not an acyclic Graph No way to arrange vertices in this cycle without pointing backwards

3 10/13/2015IT 328, review graph algorithms3 Algorithm for finding Topological Sort Principle: vertex with in-degree 0 should be listed first. Algorithm: Repeat the following steps until no more vertex left: 1.Find a vertex v with in-degree 0; Input G=(V,E) 2. Remove v from V and update E 3. Add v to the end of the sort If can’t find such v and V is not empty then stop the algorithm; this graph is not acyclic 35 27 1 3 5

4 10/13/2015IT 328, review graph algorithms4 Topological Sort ( topological order ) of an acyclic graph 17650342 1 7 6 0 3 4 5 2

5 10/13/2015IT 328, review graph algorithms5 24 35 7 1 6 9 0 8 the graph is not acyclic Fail to find a topological sort ( topological order ) 028

6 10/13/2015IT 328, review graph algorithms6 24 35 7 1 6 9 0 8 This graph is acyclic Topological Sort ( topological order ) of an acyclic graph 0285437961

7 10/13/2015IT 328, review graph algorithms7 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; }

8 10/13/2015IT 328, review graph algorithms8 Edsger Wybe Dijkstra 1930-2002 Dijkstra Algorithm: To find the shortest path between two vertices in a weighted graph. Algorithm design Programming languages Operating systems Distributed processing Formal specification and verification 1972 Turing Award

9 10/13/2015IT 328, review graph algorithms9 1.Back Tracking.... 2.Greedy Algorithms: Algorithms that make decisions based on the current information; and once a decision is made, the decision will not be revised 3.Divide and Conquer.... 4.Dynamic Programming..... Common Algorithm Techniques 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

10 10/13/2015IT 328, review graph algorithms10 Greedy Algorithms  Dynamic Programming Problem: Minimized the number of coins for 66 ¢ { 25¢, 10¢, 5¢, 1¢ } { 25¢, 12¢, 5¢, 1¢ } 25¢250¢ 10¢1 5¢1 1¢1 566¢ 25¢250¢ 12¢1 5¢00¢ 1¢44¢ 766¢ 25¢250¢ 12¢00¢ 5¢315¢ 1¢1 666¢ Greedy method Dynamic method 16¢ 6¢ 1¢ 0¢ 16¢ 4¢ 0¢

11 10/13/2015IT 328, review graph algorithms11 Unweighted Graphs  Weighted Graphs Starting Vertex w Final Vertex Starting Vertex w Final Vertex 1 52 15 9 7 1 Both are using the greedy algorithm. 0 1 2 2 2 1 0 2523 373 3 12 7 1088 Finding the shortest path between two vertices

12 10/13/2015IT 328, review graph algorithms12 Shortest paths of unweighted graphs 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 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; } // > typedef map > PathMap;

13 10/13/2015IT 328, review graph algorithms13 Construct a shortest path Map for weighted graph 12 34 Starting Vertex 5 n Final Vertex 1 52 15 9 7 1 0 Starting Vertex Final Vertex 0,-1 5,0 7,13,1 10,2 2,0 w-1,y w,x  : decided    3,1    8,4 

14 10/13/2015IT 328, review graph algorithms14 Shortest paths of weighted graphs (I) // A Dijkstra's algorithm PathMap graph::ShortestPath(Graph & g, int s) { PathMap SPMap; map > 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 candidates ; // next possible vertices //sorted by their distance. candidates.insert(make_pair(0,s)); // start from s;..... }

15 10/13/2015IT 328, review graph algorithms15 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; }

16 10/13/2015IT 328, review graph algorithms16 Minimum Spanning Tree of an undirected graph: The lowest cost to connect all vertices Since a cycle is not necessary, such a connection must be a tree. A spanning is a walk such that every vertex is included 1 12 345 67 1 4 2 2 6 43 10 7 8 5

17 10/13/2015IT 328, review graph algorithms17 Prim’s algorithm:grow the minimum spanning tree from a root 1 12 345 67 1 4 2 2 6 43 10 7 8 5 1 12 345 67 1 4 2 2 6

18 10/13/2015IT 328, review graph algorithms18 Prim’s algorithm:grow the minimum spanning tree from a different root 1 12 345 67 1 4 2 2 6 43 10 7 8 5 1 12 345 67 1 4 2 2 6

19 10/13/2015IT 328, review graph algorithms19 Kruskal’s Algorithm: construct the minimum spanning from edges 1 12 345 67 1 4 2 2 6 43 10 7 8 5 1 12 345 67 1 4 2 2 6

20 10/13/2015IT 328, review graph algorithms20 A Hamiltonian cycle is a cycle that contains every vertex; A graph that contains a Hamiltonian cycle is called Hamiltonian 1 2 9 3 5 10 4 6 7 8 11 12 14 16 15 13

21 10/13/2015IT 328, review graph algorithms21 A non-Hamiltonian graph 1 2 3 4 5 6 7 8 9


Download ppt "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."

Similar presentations


Ads by Google