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 A Topological Sort is a sequence v 1, v 2,.... v n, such that There are more than one such order in this graph!!
10/13/2015IT 328, review graph algorithms2 If the graph is not acyclic, then there is no topological sort for the graph Not an acyclic Graph No way to arrange vertices in this cycle without pointing backwards
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
10/13/2015IT 328, review graph algorithms4 Topological Sort ( topological order ) of an acyclic graph
10/13/2015IT 328, review graph algorithms the graph is not acyclic Fail to find a topological sort ( topological order ) 028
10/13/2015IT 328, review graph algorithms This graph is acyclic Topological Sort ( topological order ) of an acyclic graph
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; }
10/13/2015IT 328, review graph algorithms8 Edsger Wybe Dijkstra 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
10/13/2015IT 328, review graph algorithms9 1.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 3.Divide and Conquer 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/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¢
10/13/2015IT 328, review graph algorithms11 Unweighted Graphs Weighted Graphs Starting Vertex w Final Vertex Starting Vertex w Final Vertex Both are using the greedy algorithm Finding the shortest path between two vertices
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;
10/13/2015IT 328, review graph algorithms13 Construct a shortest path Map for weighted graph Starting Vertex 5 n Final Vertex 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
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;..... }
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; }
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
10/13/2015IT 328, review graph algorithms17 Prim’s algorithm:grow the minimum spanning tree from a root
10/13/2015IT 328, review graph algorithms18 Prim’s algorithm:grow the minimum spanning tree from a different root
10/13/2015IT 328, review graph algorithms19 Kruskal’s Algorithm: construct the minimum spanning from edges
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
10/13/2015IT 328, review graph algorithms21 A non-Hamiltonian graph