Upcoming SlideShare
×

# KRUSKAL'S algorithm from chaitra

9,485 views
9,262 views

Published on

2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
9,485
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
393
0
Likes
2
Embeds 0
No embeds

No notes for slide

### KRUSKAL'S algorithm from chaitra

1. 1. KUVEMPU UNIVERSITY Department of Computer Science Jnana Sahyadri Shankarghatta Seminar on “ Kruskal’s Algorithm ” Presented by, Chaitra.M.S 3 rd sem , M.Sc, Dept. Of Computer Science, Shankarghatta. Under the guidance of, Suresh.M, Dept. Of Computer Science, Shankarghatta.
2. 2. Contents <ul><li>Spanning trees </li></ul><ul><li>Finding a spanning tree </li></ul><ul><li>Minimum-cost spanning trees </li></ul><ul><li>Kruskal’s algorithm And example </li></ul>
3. 3. Spanning Trees
4. 4. Spanning trees <ul><li>Suppose you have a connected undirected graph </li></ul><ul><ul><li>Connected: every node is reachable from every other node </li></ul></ul><ul><ul><li>Undirected: edges do not have an associated direction </li></ul></ul><ul><li>...then a spanning tree of the graph is a connected subgraph in which there are no cycles </li></ul>A connected, undirected graph Four of the spanning trees of the graph
5. 5. Finding a spanning tree <ul><li>To find a spanning tree of a graph, </li></ul><ul><ul><li>pick an initial node and call it part of the spanning tree </li></ul></ul><ul><ul><li>do a search from the initial node: </li></ul></ul><ul><ul><ul><li>each time you find a node that is not in the spanning tree, add to the spanning tree both the new node and the edge you followed to get to it </li></ul></ul></ul>An undirected graph
6. 6. Minimizing costs <ul><li>Suppose you want to supply a set of houses (say, in a new subdivision) with: </li></ul><ul><ul><li>electric power </li></ul></ul><ul><ul><li>water </li></ul></ul><ul><ul><li>sewage lines </li></ul></ul><ul><ul><li>telephone lines </li></ul></ul><ul><li>To keep costs down, you could connect these houses with a spanning tree (of, for example, power lines) </li></ul><ul><ul><li>However, the houses are not all equal distances apart </li></ul></ul><ul><li>To reduce costs even further, you could connect the houses with a minimum-cost spanning tree </li></ul>
7. 7. Minimum-cost spanning trees <ul><li>Suppose you have a connected undirected graph with a weight (or cost ) associated with each edge </li></ul><ul><li>The cost of a spanning tree would be the sum of the costs of its edges </li></ul><ul><li>A minimum-cost spanning tree is a spanning tree that has the lowest cost </li></ul>A B E D F C 16 19 21 11 33 14 18 10 6 5 A connected, undirected graph A B E D F C 16 11 18 6 5 A minimum-cost spanning tree A B E D F C 16 19 21 11 33 14 18 10 6 5 A connected, undirected graph
8. 8. Kruskal algorithm(E,cost,n,t) <ul><li>//E is set of edges in G .G has n vertices.Cost[u,v] is </li></ul><ul><li>//cost of edge(u,v).t is the set of edges in minimum-cost </li></ul><ul><li>//spanning tree.the final cost is returned. </li></ul><ul><li>{ </li></ul><ul><li>//construct a heap out of the edge costs using heapify </li></ul><ul><li>For i=:1 to n do parent[i]:=-1; </li></ul><ul><li>//each vertex is in a different set. </li></ul><ul><li>i:=0;mincost:=0.0; </li></ul><ul><li>While((i<n-1) and (heap not empty)) do </li></ul><ul><li>{ </li></ul><ul><li>//delete a minimum cost edje(u,v) from the heap and reheapify using Adjust; </li></ul><ul><li>j:=Find(u); </li></ul><ul><li>k:=Find(v); </li></ul>
9. 9. <ul><li>If (j!=k) then </li></ul><ul><li>{ </li></ul><ul><li>i:=i+1; </li></ul><ul><li>t[i,1]:=u; </li></ul><ul><li>t[i,2]:=v; </li></ul><ul><li>mincost:=mincost+cost[u,v]; </li></ul><ul><li>union(j,k); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>if(i!=n-1) then </li></ul><ul><li>write(“no spanning tree”); </li></ul><ul><li>else return mincost; </li></ul><ul><li>} </li></ul>
10. 10. Example graph
11. 19. Minimum cost spanning tree we obtained finally
12. 20. Any queries???????????
13. 21. <ul><li>Thank U……………… </li></ul>