Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 Kruskal’s and Dijkstra’s Algorithm

2  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph.  If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component).  Kruskal's algorithm is an example of a greedy algorithm.  You can look at all of the edges at once  You can hold all of the vertices at once 1 Kruskal’s Algorithm

3 2 ALGORITHM Kruskal(G) //Kruskal’s Algorithm for constructing a minimum spanning tree //Input: A weighted connected graph G=(V,E) //Output: E T – the set of edges composing a minimum spanning tree of G. Sort E in the non-decreasing order of the edges weights w(e i1 )≤ …≤ w(e i|E| ) E T ← 0; encounter ← 0; k ← 0 While encounter < |v|-1 do k ← k+1 if E T U {e ik } is acyclic then E T ← E T U e ik ; encounter ← encounter +1 return E T

4 3 Example: Step 1. In the graph, the Edge(g, h) is shortest. Either vertex g or vertex h could be representative. Lets choose vertex g arbitrarily. Step 2. The edge (c, i) creates the second tree. Choose vertex c as representative for second tree. Step by Step operation of Kurskal's algorithm.

5 4 Step 3. Edge (g, f) is the next shortest edge. Add this edge and choose vertex g as representative. Step 4. Edge (a, b) creates a third tree. Step 5. Add edge (c, f) and merge two trees. Vertex c is chosen as the representative.

6 5 Step 6. Edge (g, i) is the next next cheapest, but if we add this edge a cycle would be created. Vertex c is the representative of both. Step 7. Instead, add edge (c, d). Step 8. If we add edge (h, i), edge(h, i) would make a cycle.

7 6 Step 9. Instead of adding edge (h, i) add edge (a, h). Step 10. Again, if we add edge (b, c), it would create a cycle. Add edge (d, e) instead to complete the spanning tree. In this spanning tree all trees joined and vertex c is a sole representative.

8 7  Dijkstra's algorithm, conceived by Dutch computer scientist Edsger Dijkstra in 1959.  This algorithm is often used in routing.  In particular, this algorithm can be used to illustrate the deployment of successive approximation methods by dynamic programming. Dijkstra’s Algorithm

9 8 ALGORITHM Dijkstra (G, w, s) // Dijkstra’s Algorithm for constructing a minimum spanning tree INITIALIZE SINGLE-SOURCE (G, s) S ← { } // S will ultimately contains vertices of final shortest-path weights from s Initialize priority queue Q i.e., Q ← V[G] while priority queue Q is not empty do u ← EXTRACT_MIN(Q) // Pull out new vertex S ← S È {u} // Perform relaxation for each vertex v adjacent to u for each vertex v in Adj[u] do Relax (u, v, w) Analysis Dijkstra's algorithm runs in O(|E|lg|V|) time.

10 9 Step by Step operation of Dijkstra algorithm. Step1. Given initial graph G=(V, E). All nodes nodes have infinite cost except the source node, s, which has 0 cost. Step 2. First we choose the node, which is closest to the source node, s. We initialize d[s] to 0. Add it to S. Relax all nodes adjacent to source, s. Update predecessor (see red arrow in diagram below) for all nodes updated.

11 10 Step 3. Choose the closest node, x. Relax all nodes adjacent to node x. Update predecessors for nodes u, v and y (again notice red arrows in diagram below). Step 4. Now, node y is the closest node, so add it to S. Relax node v and adjust its predecessor (red arrows remember!).

12 11 Step 5. Now we have node u that is closest. Choose this node and adjust its neighbor node v. Step 6. Finally, add node v. The predecessor list now defines the shortest path from each node to the source node s.

13 12 Conclusion oBasically, Kruskal's method is more time-saving (you can order the edges by weight and burn through them fast). oIt is a graph search algorithm that solves the single-source shortest path problem for a graph with nonnegative edge path costs, producing a shortest path tree.

14 13 Dantzig, G.B. (1960), On the shortest path route through a network, Management Science, 6, 187-190. Dreyfus, S. (1969), An appraisal of some shortest-path algorithms Operations Research, 17, 395-412. Website :-  Microsoft Shortest Path Algorithms Project: research.microsoft.com/research/sv/SPA/ex.html. research.microsoft.com/research/sv/SPA/ex.html BIBLIOGRAPHY


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

Similar presentations


Ads by Google