0% found this document useful (0 votes)
6 views21 pages

Kruskals Algorithm

Uploaded by

reemhamdy914
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views21 pages

Kruskals Algorithm

Uploaded by

reemhamdy914
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

“Kruskal’s Algorithm ”

1
Contents

• Spanning trees
• Finding a spanning tree
• Minimum-cost spanning trees
• Kruskal’s algorithm And example

2
Spanning Trees
Spanning trees
• Suppose you have a connected undirected graph
– Connected: every node is reachable from every other node
– Undirected: edges do not have an associated direction
• ...then a spanning tree of the graph is a connected subgraph
in which there are no cycles

A connected, Four of the spanning trees of the graph


undirected graph

4
Finding a spanning tree
• To find a spanning tree of a graph,
pick an initial node and call it part of the spanning tree
do a search from the initial node:
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

An undirected graph
5
Minimizing costs
• Suppose you want to supply a set of houses (say, in a new
subdivision) with:
– electric power
– water
– sewage lines
– telephone lines
• To keep costs down, you could connect these houses with
a spanning tree (of, for example, power lines)
– However, the houses are not all equal distances apart
• To reduce costs even further, you could connect the houses
with a minimum-cost spanning tree

6
Minimum-cost spanning trees
• Suppose you have a connected undirected graph with a
weight (or cost) associated with each edge
• The cost of a spanning tree would be the sum of the costs
of its edges
• A minimum-cost spanning tree is a spanning tree that has
the lowest cost
16 16
A B A B
21 11 6 11 6

19 5 5
F C F C
33 14
10
E 18 D E 18 D
A connected, undirected graph A minimum-cost spanning tree
7
Kruskal algorithm(E,cost,n,t)
//E is set of edges in G .G has n vertices.Cost[u,v] is
//cost of edge(u,v).t is the set of edges in minimum-cost
//spanning tree.the final cost is returned.

{
//construct a heap out of the edge costs using heapify

For i=:1 to n do parent[i]:=-1;


//each vertex is in a different set.
i:=0;mincost:=0.0;

While((i<n-1) and (heap not empty)) do


{
//delete a minimum cost edje(u,v) from the heap and reheapify using
Adjust;
j:=Find(u);
k:=Find(v);

8
If (j!=k) then
{
i:=i+1;
t[i,1]:=u;
t[i,2]:=v;
mincost:=mincost+cost[u,v];
union(j,k);
}
}

if(i!=n-1) then
write(“no spanning tree”);

else return mincost;

}
9
Example graph

10
11
12
13
14
15
16
17
18
Minimum cost spanning tree we
obtained finally

19
Any queries???????????

20
Thank U………………

21

You might also like