0% found this document useful (0 votes)
4 views94 pages

Minimum Spanning Tree

The document discusses greedy algorithms, particularly focusing on their application to optimization problems like Minimum Spanning Trees (MST). It explains the principles of greedy methods, their advantages, and specific algorithms such as Kruskal's and Prim's for finding MSTs. The document also outlines the steps for implementing Kruskal's algorithm and provides examples to illustrate the process.

Uploaded by

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

Minimum Spanning Tree

The document discusses greedy algorithms, particularly focusing on their application to optimization problems like Minimum Spanning Trees (MST). It explains the principles of greedy methods, their advantages, and specific algorithms such as Kruskal's and Prim's for finding MSTs. The document also outlines the steps for implementing Kruskal's algorithm and provides examples to illustrate the process.

Uploaded by

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

Design and Analysis of

Algorithm

Greedy Methods
(Minimum Spanning Tree)

Lecture – 45 -
53
Overview

• A greedy algorithm always makes the


choice that looks best at the moment. (i.e.
it makes a locally optimal choice in the
hope that this choice will lead to a globally
optimal solution).
• The objective of this section is to explores
optimization problems that are solvable by
greedy algorithms.
Greedy Algorithm
• In mathematics, computer science and
economics, an optimization problem is the
problem of finding the best solution from
all feasible solutions.
• Algorithms for optimization problems
typically go through a sequence of steps,
with a set of choices at each step.
• Many optimization problems can be solved
using a greedy approach.
• Greedy algorithms are simple and
straightforward.
Greedy Algorithm
• A greedy algorithm always makes the
choice that looks best at the moment.
• That is, it makes a locally optimal choice in
the hope that this choice will lead to a
globally optimal solution.
• Greedy algorithms do not always yield
optimal solutions, but for many problems
they do.
• This algorithms are easy to invent, easy to
implement and most of the time provides
best and optimized solution.
Greedy Algorithm
• Application of Greedy Algorithm:
• A simple but nontrivial problem, the activity-
selection problem, for which a greedy
algorithm efficiently computes a solution.
• In combinatorics,(a branch of mathematics),
a ‘matroid’ is a structure that abstracts and
generalizes the notion of linear
independence in vector spaces. Greedy
algorithm always produces an optimal
solution for such problems. Scheduling unit-
time tasks with deadlines and penalties is
an example of such problem.
Greedy Algorithm
• Application of Greedy Algorithm:
• An important application of greedy
techniques is the design of data-
compression codes (i.e. Huffman code) .
• The greedy method is quite powerful and
works well for a wide range of problems.
They are:
• Minimum-spanning-tree algorithms
(Example: Prims and Kruskal algorithm)
• Single Source Shortest Path.
(Example: Dijkstra's and Bellman ford
algorithm)
Greedy Algorithm
• Application of Greedy Algorithm:
• A problem exhibits optimal substructure if
an optimal solution to the problem contains
within it optimal solutions to subproblems.
• This property is a key ingredient of
assessing the applicability of dynamic
programming as well as greedy algorithms.
• The subtleties between the above two
techniques are illustrated with the help of
two variants of a classical optimization
problem known as knapsack problem. These
variants are:
• 0-1 knapsack problem (Dynamic Programming)
• Fractional knapsack problem (Greedy Algorithm)
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Spanning Tree
• A tree (i.e., connected, acyclic graph) which
contains all the vertices of the graph
• Minimum Spanning Tree
• Spanning tree with the minimum sum of weights
8 7 8 7
b c d b c d
4 9 4 9
2 2
a 11 i 14 e a i 4
e
4
7 6
8 10
h g f h g f
1 2 1 2
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Problem Definition
• A town has a set of houses and a set of roads.
• A road connects 2 and only 2 houses.
• A road connecting houses and has a repair
cost
• Goal:
• Repair enough roads such that
1. everyone stays connected: can reach
every house from all other houses, and
2. total repair cost is minimum.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Graph model for MST
• Undirected graph
• Weight on each edge .
• Find T E such that
1. T connects all vertices (T is a spanning
tree), and
2. is minimized.
• Properties of an MST:
• It has edges.
• It has no cycles.
• It might not be unique
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Generic MST Algorithm
GENERIC-MST
;
while A is not a spanning tree
find an edge that is safe for A

return A
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Generic MST Algorithm
GENERIC-MST
;
while A is not a spanning tree
find an edge that is safe for A

return A
Use the loop invariant to show that this generic algorithm
works.
• Initialization: The empty set trivially satisfies the loop
invariant.
• Maintenance: Since we add only safe edges, A remains a
subset of some MST.
• Termination: All edges added to A are in an MST, so when we
stop, A is a spanning tree that is also an MST.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem

• Kruskal’s Algorithm
• Concept and Examples

• Prim’s Algorithm
• Concept and Examples
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem

• Kruskal’s Algorithm
• Concept and Examples

• Prim’s Algorithm
• Concept and Examples
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Kruskal’s Algorithm is a famous greedy
algorithm.
• It is used for finding the Minimum
Spanning Tree (MST) of a given graph.
• To apply Kruskal’s algorithm, the given
graph must be weighted, connected and
undirected.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Implementation of algorithm.
• The implementation of Kruskal’s Algorithm is
explained in the following steps-
• Step-01:
• Sort all the edges from low weight to high
weight.
• Step-02:
• Take the edge with the lowest weight and use it
to connect the vertices of graph.
• If adding an edge creates a cycle, then reject
that edge and go for the next least weight edge.
• Step-03:
• Keep adding edges until all the vertices are
connected and a Minimum Spanning Tree (MST)
is obtained.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Kruskal’s Algorithm-
8 7
b c d
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f
1 2
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm:
Example 1:
Solution: Read the edges 8 7
b c d
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f
1 2
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
ab 4
Example 1: bc 8
Solution: Read the edges cd 7
8 7 de 9
b c d ef 10
4 9
2 df 14
11 cf 4
a i 4
14 e
ci 2
7 6
8 10 ih 7
h g f ig 6
1 2 gf 2
gh 1
ah 8
bh 11
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Implementation of algorithm.
• The implementation of Kruskal’s Algorithm is
explained in the following steps-
• Step-01:
• Sort all the edges from low weight to high
weight.
• Step-02:
• Take the edge with the lowest weight and use it
to connect the vertices of graph.
• If adding an edge creates a cycle, then reject
that edge and go for the next least weight edge.
• Step-03:
• Keep adding edges until all the vertices are
connected and a Minimum Spanning Tree (MST)
is obtained.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 1 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Implementation of algorithm.
• The implementation of Kruskal’s Algorithm is
explained in the following steps-
• Step-01:
• Sort all the edges from low weight to high
weight.
• Step-02:
• Take the edge with the lowest weight and use it
to connect the vertices of graph.
• If adding an edge creates a cycle, then reject
that edge and go for the next least weight edge.
• Step-03:
• Keep adding edges until all the vertices are
connected and a Minimum Spanning Tree (MST)
is obtained.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8
7 X6 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
X

h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8
7 X6 bc 8
X

10
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8
7 X6 bc 8
X

10
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8X
7 X6 bc 8
X

10
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8X
7 X6 bc 8
X

10
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8X
7 X6 X bc 8
X

10
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
a 11 X i 14 e
cd 7
4 ih 7
8X
7 X6 X bc 8
X

10
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
a 11 X i X14 e
cd 7
4 ih 7
8X
7 X6 X bc 8
X

10
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
MST-KRUSKAL(G, w)
1 A←Ø
2 for each vertex v V[G]
3 do MAKE-SET(v)
4 sort the edges of E into nondecreasing order by weight w
5 for each edge (u, v) E, taken in nondecreasing order by
weight
6 do if FIND-SET(u) ≠ FIND-SET(v)
7 then A ← A U {(u, v)}
8 UNION(u, v)
9 return A
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm(Analysis)
• The running time of Kruskal's algorithm for a graph
depends on the implementation of the disjoint-set
data structure.
• Initializing the set A in line 1 takes time, and the
time to sort the edges in line 4 is O(E lg E).
• The for loop of lines 5-8 performs FIND-SET and
UNION operations on the disjoint-set forest. Along
with the |V| MAKE-SET operations, these take a
total of time.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm (Analysis)
• the total running time of Kruskal's algorithm is
• Observing that
• Apply log both side
• Hence the running time of Kruskal's algorithm as
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm:
Example 2: Construct the minimum spanning tree (MST) for the
given graph using Kruskal’s Algorithm- Self
28
b c Practice
10 14 16

a e d
24 18
25 12
f g
22
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem

• Kruskal’s Algorithm
• Concept and Examples

• Prim’s Algorithm
• Concept and Examples
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm
• Prim’s Algorithm is a famous greedy
algorithm.
• It is used for finding the Minimum
Spanning Tree (MST) of a given graph.
• To apply Prim’s algorithm, the given graph
must be weighted, connected and
undirected.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm (Implimentation)
• The implementation of Prim’s Algorithm is
explained in the following steps-
• Step-1:
• Randomly choose any vertex.
• The vertex connecting to the edge having
least weight is usually selected.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm (Implementation)
• Step-2:
• Find all the edges that connect the tree to new
vertices.
• Find the least weight edge among those edges
and include it in the existing tree.
• If including that edge creates a cycle, then
reject that edge and look for the next least
weight edge.
• Step-03:
• Keep repeating step-2 until all the vertices are
included and Minimum Spanning Tree (MST) is
obtained.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm (Implementation)
• Step-2:
• Find all the edges that connect the tree to new
vertices.
• Find the least weight edge among those edges
and include it in the existing tree.
• If including that edge creates a cycle, then
reject that edge and look for the next least
weight edge.
• Step-03:
• Keep repeating step-2 until all the vertices are
included and Minimum Spanning Tree (MST) is
obtained.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm: (Algorithm)
MST-PRIM(G, w, r)
1 for each u V[G]
2 do key[u] ← ∞
3 [u] ← NIL
4 key[r] ← 0
5 Q ← V [G]
6 while Q ≠ Ø
7 do u ← EXTRACT-MIN(Q)
8 for each v Adj[u]
9 do if v Q and w(u, v) < key[v]
10 then π[v] ← u
11 key[v] ← w(u, v)
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f
1 2
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f
1 2

a b c d e f g h i
key
/ / / / / / / / /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 1
4 9 Assume that ‘a’ is the
2 root.
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ / / / / / / / /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 1
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a / / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given
Mi n
graph using Prim’s Algorithm-
8 7
b c d Use step 1
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a / / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 1
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a / / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Kruskal’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a / / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Constructn the minimum spanning tree (MST) for the
Mi
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c / c / a c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1:inConstruct the minimum spanning tree (MST) for the
M
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c / c / a c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1:inConstruct the minimum spanning tree (MST) for the
M
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c / c / a c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Kruskal’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c / c i i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c / c i i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c / c i i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c f c f i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i

Mi n key
/ a b c f c f i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c f c f i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c f c f g c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
Mi n key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
cycle key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8 10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
cycle key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8 10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8 10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
cycle key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
cycle key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum
Mi n
spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
cycle h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
cycle h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Mi n Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X

8
X

10
h g f Q a b c d e f g h i
1 2

a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
8 7
given graph
b usingcPrim’s Algorithm-
d
4 9 Use step 2 and 3
2 until MST form
a 11 i 4
14 e
7 6
8
X

10
X
X

h g f
1 2

Since all the vertices have been included in the MST, so stop the
process.
Now, Cost of Minimum Spanning Tree
= Sum of all edge weights
= w[ab]+w[bc]+w[cd]+w[ci]+w[cf]+w[de]+w[gh]+w[fg]
=4+8+7+2+4+9+2+1+=37
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm: (Analysis)
MST-PRIM(G, w, r)
1 for each u V[G]
Ο (𝑉 )
2 do key[u] ← ∞
3 [u] ← NIL
Ο (1)
4 key[r] ← 0
by the help of Fibonacci Heap
5 Q ← V [G]
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem Ο (𝑉 lg 𝑉 )
• Prim’s Algorithm: (Analysis)
MST-PRIM(G, w, r)
6 while Q ≠ Ø Ο(lg 𝑉 )
7 do u ← EXTRACT-MIN(Q) Ο (𝑉 )
8 for each v Adj[u]
9 do if v Q and w(u, v) < ( 𝐸)
Οkey[v]
10 then π[v] ← u
11 𝐷𝑒𝑐𝑟𝑒𝑎𝑠𝑒 𝐾𝑒𝑦𝑜𝑓
key[v] ←𝐹𝑖𝑏𝑜𝑛𝑎𝑐𝑐𝑖
w(u, v) 𝐻𝑒𝑎𝑝𝑤𝑖𝑙𝑙 𝑡𝑎𝑘𝑒 Ο(1)
Time Complexity of Prim’s Algorithm
=Build max Heap()+Extract Min()+Decrease Key()
={In case of Sparse graph}
=
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem Ο (𝑉 lg 𝑉 )
• Prim’s Algorithm: (Analysis)
MST-PRIM(G, w, r)
6 while Q ≠ Ø Ο(lg 𝑉 )
7 do u ← EXTRACT-MIN(Q) Ο (𝑉 )
8 for each v Adj[u]
9 do if v Q and w(u, v) < ( 𝐸)
Οkey[v]
10 then π[v] ← u
11 𝐷𝑒𝑐𝑟𝑒𝑎𝑠𝑒 𝐾𝑒𝑦𝑜𝑓
key[v] 𝐹𝑖𝑏𝑜𝑛𝑎𝑐𝑐𝑖
← w(u, v) 𝐻𝑒𝑎𝑝𝑤𝑖𝑙𝑙 𝑡𝑎𝑘𝑒 Ο(1)

Time Complexity of Prim’s Algorithm


=Build max Heap()+Extract Min()+Decrease Key()
={In case of Dense graph}
=)
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
28
b c
10 16
14

a g d
24 18

25 12
f e
22

You might also like