0% found this document useful (0 votes)
5 views5 pages

Kruskal Alg

The document discusses the Kruskal's algorithm for finding the minimum spanning tree of a graph. It explains the steps of the algorithm and analyzes its time complexity as O(Elogn) where E is the number of edges. It then discusses the single source shortest path problem and provides an example to illustrate finding the shortest paths from a source vertex to all other vertices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Kruskal Alg

The document discusses the Kruskal's algorithm for finding the minimum spanning tree of a graph. It explains the steps of the algorithm and analyzes its time complexity as O(Elogn) where E is the number of edges. It then discusses the single source shortest path problem and provides an example to illustrate finding the shortest paths from a source vertex to all other vertices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithm

Algorithm Kruskal (G, cost, n, tree) There are total


total n
array of edges of the given
graph.
//G be an

vertices.
// cost is an array storing edge (p,g).
tree.
/tree contains the edges of minimum-cost spanning
holdss the minimum cost of spanning tree
/min

while (i<n-1)

delete minimum cost edge (p,q) from heap and eheapify.


S:= Find (p);
t : Find (q); //finds the minimum cost edge.
if (s! =t) then // checks whether i t creates cycle or not

i:=i+1
tree [i, 1]:=p;
tree [i, 2] :=q:
min:=min+cost [p,q);
Union (s,t); //merge S and t

return min;// returning minimum cost spanning tree


reedy Method
Analysis
We will consider the
algorithm
Algorithm Krus kal (G, cost, n, tree)
once again for analyzing the time
complexity
G be an array ot
edges of the given graph. There
vertices.
are
total n

// cost is an
array storing edge (p,
/ tree contains the edges of
q).
minimum-cost spanning tree
min holds the minimum cost of
spanning tree.

while (isn-1)

delete minimum cOst edge (p,


q) from heap and reheapifY
. Ollogn)
S: Find (p);
t : Find (q) //£inds the minimum cost
edgee
i f(s!t) then // checks whether it creates cycle or not

i:=i+l;
tree[i, 1] :=p;
tree[i, 2] :=q;
min:=min+cost [p, ql;
Union (s,t); //merge s and t

return min;// returning minimum cost spanning tree

Hence the computing time of Kruskal's algorithm is O(Elogn) where E is the


number of edges.

3.5 Single Source Shortest Path Problem


Many times, Graph is used to represent the distances between two cities.
Everybody is often interested in moving from one city to other as quickly as
possible.The single source shortest path is based on this interest.
In single source shortest path problem the shortest distance from a single vertex
called source is obtained.
Let G(V,E) be a graph ,then in single source shortest path the shortest paths from
vertex v to all remaining vertex is determined. The vertex vo is then called as source
and the last vertex is called destination.
It is assumed that all the distances are positive.
esign & Analysis of Algorithms
beloW.
Consider a graph
G as given
Example:

30
-O
3
25
10
20
(4
2 12
15
20

5
5

Fig. 3.6 Directed graph for single source shortest path

We will start from source vertex 1. Hence set S[1] = 1.


Now shortest distance from vertex 1 is 10. i.e. 1-2 10. Hence {1,2
min = 10.

From vertex 2 the next vertex chosen is 3.

1,2=10
1 ,31 =

1,5=
1 ,61= 0

1 , 71 =

Now
1, 2 31 = 30

1, 2, 41
1,2, 71 =

1,2, 5} =

1,2, 6} = oo
Desig

Hence select 3.

S[3= 1

Now
1, 2, 3, 4 = 45

1, 2, 3, 5 = 35

11, 2, 3, 6) =

1, 2, 3, 71 = o

Hence select next vertex as 5.

S[5 = 1

Now

1, 2, 3, 5, 6} =

1, 2, 3, 5, 71 = 42

Hence vertex 7 will be selected. In single source shortest path if destination vertex
is7 then we have achieved shortest path 1 2 3 5 - 7 with path length 42. The
single source shortest path from each vertex is summerised below

1, 2 10

1, 2, 3 30

1, 2. 3, 4 45

1, 2, 3, 5 35

1, 2. 3, 4,7 65

1. 2, 3, 5, 7 42

1, 6 30

1.67 65

3.5.1 Algorithm
The algorithm for single source shortest path is given as
Algorithm Sh _Fath ( P,cost, Dist,n)

San aiacency matrix storing cost of each edge i.e.


ti:n,l:n. Given graph can be represented by cost.
St1s a set that stores the shortest path from the sOurce
OYtex 'p any vertex in the graph.
Design & AnalysIs or /
methc
Methç
/ 7 s S stores all the visited vertices of graph. It is of Boolean

/7type arraY.
//initially
for i : =1 to n do

Sil:=0;
Dist:= Cost Ip, i];

th
SIp]:=l // set vertex to true in array S and 1.e. put p ir
Dist [p) : =0.0;
for val:=2 to n-2 doo

// obtain n-1 paths from p


Choose q from the vertices that are not visited (not in S) ar
with minimum distance.
Dist [ql =
min{Dist [i]}
Slq]=1// put q in S
/*update the Distance values of the other nodes*
for (all node r adjacent to
qwitEh S[r] =
0) do
if Dist [r]> (Dist [q] +cost [p, q])) then
Dist [r] :=Dist [q] +Dist
[p, q];

3.5.2 Analysis
Time Complexity of the
algorithm:
The 1st for-loop clearly takes O(n) time
Choosing q takes O(n) time, because it involves
array.
finding a minimum in an

The innermost for-loop for updating Dist iterates at most n times, thus takes
O(n) time.
Therefore, the for-loop iterating over val takes
Thus, single source shortest
O(n*n) time
path takes O(n) time.

You might also like