0% found this document useful (0 votes)
7 views16 pages

Vennela - Tandra - 2403032010058 - Prims Algorithm

Prim's Algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a graph by growing a tree from an arbitrary starting vertex, adding the smallest edge connecting the tree to a vertex outside it at each step. The algorithm operates similarly to Dijkstra's algorithm, utilizing a priority queue to manage edges and vertices efficiently. The complexity of Prim's Algorithm varies based on the data structure used, with different performance characteristics for dense and sparse graphs.

Uploaded by

tandra vennela
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)
7 views16 pages

Vennela - Tandra - 2403032010058 - Prims Algorithm

Prim's Algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a graph by growing a tree from an arbitrary starting vertex, adding the smallest edge connecting the tree to a vertex outside it at each step. The algorithm operates similarly to Dijkstra's algorithm, utilizing a priority queue to manage edges and vertices efficiently. The complexity of Prim's Algorithm varies based on the data structure used, with different performance characteristics for dense and sparse graphs.

Uploaded by

tandra vennela
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/ 16

Prim’s Algorithm

Presented By:
Tandra Vennela
2403032010058
Prim's Algorithm
Prim's algorithm grows one SCC (X and S) as a single tree
• X (edges) and S (vertices) are the sets of intermediate edges and vertices in
this partial MST
• On each iteration, X grows by one edge, and S by one vertex
 The smallest edge between a vertex in S and a vertex outside S (V - S)
 All vertices S and V-1 edges must eventually move into S and X
 Cannot create a cycle since new vertex not currently in S and we never add a new
edge between two nodes already in S

Prims Algorithm
 What is an efficient data structure to retrieve that edge?
• The algorithm is basically Dijkstra's algorithm except that the PQ key value for
each node outside S is its smallest edge into S

2
Prim's Algorithm – An Intuitive Version
Consider each node as wanting to get into club S
All nodes must join the club before we finish
Each non-member (V-S) has an entry key which is the smallest edge
length from itself to any current member of the club
At each iteration the non-member with the smallest key (edge length)
joins the club
Whenever a new member joins the club, all non-members with edges

CS 312 – Greedy Algorithms


to the new member have their key updated if their edge to the new
member is smaller than their current smallest edge into the club

3
Prim's Algorithm

CS 312 – Greedy Algorithms


• Decreasekey does not update path length, but updates the key with the
decreased edge cost between v and z
4
• Almost the same as Dijkstra's Algorithm, same complexity values
Prim’s Algorithm
Choose arbitrary starting vertex,
set to 0 and deletemin

1 2
1 2 3 S = {5}
1:

CS 312 – Greedy Algorithms


6 5
4 4 6
2:
3 8 ∞
4 5 6 3:

8 4:
4 3 ∞
5: 0
7 6:
5

7:

Prim’s Algorithm
Choose arbitrary starting vertex,
set to 0 and deletemin

1 2
1 2 3 S = {5}
1:

CS 312 – Greedy Algorithms


6 5
4 4 6
2: 4
3 8 3: 5
4 5 6 4: 3
6: 8
8 7: 8
4 3

7
6
Prim’s Algorithm
deletemin returns node
with shortest edge into S

1 2
1 2 3 X: S = {5}
1:
{4,5} S = {4,5} ∞

CS 312 – Greedy Algorithms


6 5
4 4 6
2: 4
3 8 3: 5
4 5 6 4: 3
6: 8
8 7: 8
4 3

7
7
Prim’s Algorithm
Don’t actually store S or X.
Final S is all V and we get MST using
prevs which are fixed once node dequeued.
PQ contains nodes not yet in MST (V – S)
1 2
1 2 3 X: S = {5}
1:
{4,5} S = {4,5} ∞

CS 312 – Greedy Algorithms


6 5
4 4 6
2: 4
3 8 3: 5
4 5 6 4: 3
6: 8
8 7: 8
4 3

7
8
Prim’s Algorithm
Update then decreases keys in PQ
(shortest edge into S) where possible,
based on new node just added to S
1 2
1 2 3 X: S = {5}
1: 4
{4,5} S = {4,5} 2: 4

CS 312 – Greedy Algorithms


6 5
4 4 6
3: 5
3 8 6: 8
4 5 6 7: 4

8
4 3

7
9
Prim’s Algorithm

Repeat until PQ is empty


1 2
1 2 3 X: S = {5}
2: 1
{4,5} S = {4,5} 3: 5

CS 312 – Greedy Algorithms


6 5
4 4 6 {1,4} S = {1,4,5} 6: 8
3 8 7: 4
4 5 6

8
4 3

7
10
Prim’s Algorithm

Repeat until PQ is empty


1 2
1 2 3 X: S = {5}
3: 2
{4,5} S = {4,5} 6: 8

CS 312 – Greedy Algorithms


6 5
4 4 6 {1,4} S = {1,4,5} 7: 4
3 8 {1,2} S = {1,2,4,5}
4 5 6

8
4 3

7
11
Prim’s Algorithm

Repeat until PQ is empty


1 2
1 2 3 X: S = {5}
6: 6
{4,5} S = {4,5} 7: 4

CS 312 – Greedy Algorithms


6 5
4 4 6 {1,4} S = {1,4,5}
3 8 {1,2} S = {1,2,4,5}
4 5 6 {2,3} S = {1,2,3,4,5}
8
4 3

7
12
Prim’s Algorithm

Repeat until PQ is empty


1 2
1 2 3 X: S = {5}
6: 3
{4,5} S = {4,5}

CS 312 – Greedy Algorithms


6 5
4 4 6 {1,4} S = {1,4,5}
3 8 {1,2} S = {1,2,4,5}
4 5 6 {2,3} S = {1,2,3,4,5}
{4,7} S = {1,2,3,4,5,7}
8
4 3

7
13
Prim’s Algorithm

Repeat until PQ is empty


1 2
1 2 3 X: S = {5}
{4,5} S = {4,5}

CS 312 – Greedy Algorithms


6 5
4 4 6 {1,4} S = {1,4,5}
3 8 {1,2} S = {1,2,4,5}
4 5 6 {2,3} S = {1,2,3,4,5}
{4,7} S = {1,2,3,4,5,7}
8
4 3 {6,7} S = {1,2,3,4,5,6,7}
7
14
Complexity
• Dense Graph: |E| = O(|V|2)
• Kruskal’s: (|E| log |V|) = (|V|2 log |V|)
• Prim’s (w/ Array PQ): (|V|2)
• Prim’s (w/ Binary Heap PQ): (|E| log |V|) = (|V|2 log |V|)

• Sparse Graph: |E| = O(|V|)


• Kruskal’s: (|E| log |V|)
• Prim’s (w/ Array PQ): (|V|2)

CS 312 – Greedy Algorithms


• Prim’s (w/ Binary Heap PQ): (|E| log |V|)

• Punch-lines
• Prim’s with Array PQ is best for a dense graph
• Kruskal’s with sets OR Prim’s with Heap PQ are both about the same for
sparser graphs
• Kruskal’s gives more control when choosing between edges with ties in
cost and some consider Kruskal’s as easier to implement

15
THANK YOU

You might also like