You are on page 1of 22

Design and Analysis of Algorithm

Unit 4: Greedy algorithms

Minimum Spanning Tree(II)

Compiled By
Prof. Dharmesh R. Tank
CE\IT Department, LDRP-ITR
Outline

Graphs: Minimum Spanning trees

Kruskal’s Algorithm

Prim’s Algorithm
Minimum Spanning Tree
• Prim’s algorithm
Prim’s Algorithm
1. Choose some vertex v € V
2. Let T = Փ
3. While S ≠ V
I. Choose a least cost edge e with one endpoint in S
and one endpoint in V-S.
II. Add e to T.
III. Add both the endpoints of e to S.
Prim’s Algorithm
1. Generate a set of A of edges.

2. Incrementally add edges to A such that they will


belong to MST concept { Safe Edge}

3. An edge (U,V) is safe for A if and only if

A U {(U,V)} is also a subset of same MST.


Prim’s Algorithm
• Pre-processing before apply Prim Algorithm
– (i)Remove all the loops

A 5
6 5
1 Any edge that
B 5 D start and ends at
5 the same vertex is
3 C 2 a loop.
6 4
E 6 F
Prim’s Algorithm
• Pre-processing before apply Prim Algorithm
– (ii)Remove all the parallel edges between two vertex
except the one with least weight

A
6 5
In this graph vertex E
1 and Vertex F
B 5 D
Parallel 5 connected with the
Edges
3 C 2
parallel edges having
6 4 weight 6 and 15
respectively. So we
E 6 F remove 15 and keep 6.

15
Prim’s Algorithm
• Pre-processing before apply Prim Algorithm
– Now we can start applying Prim algorithm

A
6 5
1
B 5 D
5
3 C 2
6 4
E 6 F
Prim’s Algorithm
• Step 1: Create table (6 * 6, because vertices are 6)
– Make matrix of weight between edges. Where only
direct path are applicable. Diagonal matrix value will be
0 and where there is no direct path contain ∞.

A B C D E F
A 0 6 1 5 ∞ ∞
B 6 0 5 ∞ 3 ∞
C 1 5 0 5 6 4
D 5 ∞ 5 0 ∞ 2
E ∞ 3 6 ∞ 0 6
F ∞ ∞ 4 2 6 0
Prim’s Algorithm
• Step 2: Find the MST using Prim’s Algorithm.

A B C D E F
A A 0 6 1 5 ∞ ∞
6 5
1 B 6 0 5 ∞ 3 ∞
B 5 D
5 C 1 5 0 5 6 4
3 C 2 D 5 ∞ 5 0 ∞ 2
6 4
F E ∞ 3 6 ∞ 0 6
E 6
F ∞ ∞ 4 2 6 0
Prim’s Algorithm
• Step 3: Find the minimum value in A-row.
– Note here we will not consider 0 as minimum,
because it will be correspond to same vertex.

A A B C D E F
6 5
A 0 6 1 5 ∞ ∞
1
B 5 D B 6 0 5 ∞ 3 ∞
5
C 2 C 1 5 0 5 6 4
3 6 4
D 5 ∞ 5 0 ∞ 2
E 6 F
E ∞ 3 6 ∞ 0 6
1 is smallest unmarked value F ∞ ∞ 4 2 6 0
and edge AC, So we will mark
the edge A to C. Also Tick into
AC and CA both.
Prim’s Algorithm
• Step 4: Now next finding the minimum value in
A-row & B-row.
– Note here we will not consider 0 as minimum, because
it will be correspond to same vertex.
A
6 5 A B C D E F
1 A 0 6 1 5 ∞ ∞
B 5 D
5 B 6 0 5 ∞ 3 ∞
3 C 2
6 4 C 1 5 0 5 6 4
E 6 F D 5 ∞ 5 0 ∞ 2
3 is smallest unmarked value E ∞ 3 6 ∞ 0 6
and edge BE, So we will mark
the edge B to E. Also Tick into
F ∞ ∞ 4 2 6 0
BE and EB both.
Prim’s Algorithm
• Step 5: Find the minimum value in A, B, C-rows.
– Note here we will not consider 0 as minimum, because
it will be correspond to same vertex.

A A B C D E F
6 5
A 0 6 1 5 ∞ ∞
1
B 5 D B 6 0 5 ∞ 3 ∞
5
C 2 C 1 5 0 5 6 4
3 6 4
D 5 ∞ 5 0 ∞ 2
E 6 F
E ∞ 3 6 ∞ 0 6
1 is smallest but it is already checked So
finding new smallest unmarked value 4
F ∞ ∞ 4 2 6 0
and edge CF, So we will mark the edge
C to F. Also Tick into CF and FC both.
Prim’s Algorithm
• Step 6: Find the minimum value in A,B,C,D-rows
– Note here we will not consider 0 as minimum, because
it will be correspond to same vertex.

A A B C D E F
6 5
A 0 6 1 5 ∞ ∞
1
B 5 D B 6 0 5 ∞ 3 ∞
5
C 2 C 1 5 0 5 6 4
3 6 4
D 5 ∞ 5 0 ∞ 2
E 6 F
E ∞ 3 6 ∞ 0 6
2 is smallest unmarked value and
edge DF, So we will mark the edge D
F ∞ ∞ 4 2 6 0
to F. Also Tick into DF and FD both.
Prim’s Algorithm
• Step 7: Find the minimum value in A,B,C,D,E-row.
– Note here we will not consider 0 as minimum, because
it will be correspond to same vertex.

A A B C D E F
6 5
A 0 6 1 5 ∞ ∞
1
B 5 D B 6 0 5 ∞ 3 ∞
5
C 2 C 1 5 0 5 6 4
3 6 4
D 5 ∞ 5 0 ∞ 2
E 6 F
E ∞ 3 6 ∞ 0 6
5 is smallest unmarked value (AD) but
it generate the close circuit. So
F ∞ ∞ 4 2 6 0
Ignoring it check next minimum value.
Prim’s Algorithm
• Step 8: Find the minimum value in A,B,C,D,E-row.
– Note here we will not consider 0 as minimum, because
it will be correspond to same vertex.

A A B C D E F
6 5
A 0 6 1 5 ∞ ∞
1
B 5 D B 6 0 5 ∞ 3 ∞
5
C 2 C 1 5 0 5 6 4
3 6 4
D 5 ∞ 5 0 ∞ 2
E 6 F
E ∞ 3 6 ∞ 0 6
In next row, again 5 is smallest F ∞ ∞ 4 2 6 0
unmarked value and edge BC, So we
will mark the edge B to C. Also Tick
into BC and CB both.
Prim’s Algorithm
• Step 9: Now all the vertices are visited hence we
will stop over here.

A A B C D E F
A 0 6 1 5 ∞ ∞
1
B D B 6 0 5 ∞ 3 ∞
5
C 2 C 1 5 0 5 6 4
3 4
D 5 ∞ 5 0 ∞ 2
E F
E ∞ 3 6 ∞ 0 6
Travel Path = {AC, CF, DF, BE, BC} F ∞ ∞ 4 2 6 0
Total Cost = 1+2+3+4+5 = 15
Comparison

Prim’s Algorithm Kruskal Algorithms


Initialize with node. Initialize with edge.

Span from one node to another Select the edges in a way that
Node. the position of edge in not
placed previously.

Here, Graph must be connected Here both disconnected and


connected

Time Complexity: O(𝑉 2 ) Time Complexity: O(log 𝑉)


Comparison

Dynamic Programming Greedy Algorithms


At each step, the choice is At each step, we quickly make a
determined based on choice that currently looks best.
solutions of subproblems. --A local optimal (greedy) choice.

Sub-problems are solved first. Greedy choice can be made first


before solving further sub-
problems.
Bottom-up approach Top-down approach

Can be slower, more complex Usually faster, simpler


Exercise
• Solve given undirected graph using Kruskal and Prims
Algorithm.
(i) (ii)

A
6 5
1
B 5 D
5
3 C 2
6 4
E 6 F

Ans = {(A,C), (D,F), (B,E), (C,F), (B,C)} Ans


Exercise
(iii) (iv)

(v) (v)
Reference

• Greedy algorithm, My T. Thai, University of


Florida
• Introduction of Algorithm by Thomas H. Cormen

You might also like