Lecture 7

MCA 202: Discrete Structures
Instructor
Neelima Gupta
[email protected]
Table of Contents
Prim’s MST Algorithm
Kruskal MST Algorithm
Minimum Spanning Tree
Minimum Spanning Tree
οƒ˜ Definition: Given a weighted undirected graph
G=(V,E,W) , π‘Š: 𝐸 β†’ ℝ+ βˆͺ {0}, the spanning tree of
that graph with total minimum weight is called a
minimum spanning tree.
THANKS
Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36)
(MCA 2012)
Prim’s Algorithm for Minimum Spanning
Tree
1. Let 𝑇 denote the vertices currently in the minimum
spanning tree, start with any arbitrary vertex and
include it in 𝑇.
2. Pick an edge (u,v) with minimum weight where u ∈
𝑇 and v ∈ 𝑉 βˆ’ 𝑇 .
3. Include v in 𝑇.
4. Repeat steps 2-3 till 𝑉 – 𝑇 = {βˆ…}
THANKS
Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36)
(MCA 2012)
Example
T
Edges from T to V-T
c
Selected edge
8
6
b
d
2
2
a
3
4
e
f
2
5
3
7
h
g
THANKS
Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36)
(MCA 2012)
Idea of Correctness
β€’ Let T be the set containing the vertices currently in
the minimum spanning tree. Initially T= {βˆ…}
β€’ Claim: At any point of time, the partially constructed
tree with 𝑉 ∈ 𝑇 is a minimum spanning tree.
β€’ Exercise: Prove claim by induction.
THANKS
Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36)
(MCA 2012)
Cut
β€’ A cut is defined as a collection of edges which
separates one collection of vertices from another.
T
V-T
THANKS
Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36)
(MCA 2012)
Kruskal’s algorithm
Step 1: Sort the edges in the increasing order of
weights.
Step 2: Pick the minimum weighted edge and
include it in your partial constructed
forest if it does not form a cycle.
Step 3: Repeat step 2 until you include all the
vertices (n) or exactly (n-1) edges.
Thanks: Shammi-37 and Shivangi-38
(MCA 202)
Minimum Spanning Tree
c
Traversing edges:
6
8
be
df
b
4
1
3
d
3
e
ah
ef , de
2
f
a
fg
cd , bc
5
2
7
h
gh, ab
g
TheNow
edge
 we
Similarly,
β€˜de’
traversed
β€˜bc’
is traversed
butthe
included
butwe
not
included.
because
it weights.
forms
cycle.
The
edge
β€˜ab’
isisnot
traversed
atnot
all
as
have
included
n-1 edges.

have
completed
tree,
with
minimized
Thanks: Shammi-37 and Shivangi-38
(MCA 202)
 How to check cycle: If both the vertices are
of same component it will form cycle.
 Data structure used: Linked list. How much
time to check for cycles? How much time to
merge two components?
 Only constant amount of time is needed to
perform step 2, as edges are already sorted
before step 1.
Thanks: Shammi-37 and Shivangi-38
(MCA 202)