Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Prim Algorithm from Analysis of Algorithms, Exercises of Design and Analysis of Algorithms

This is an example of Prim Algorithm that is worked out in detail

Typology: Exercises

2024/2025

Uploaded on 03/05/2025

anita-kingsley-1
anita-kingsley-1 🇺🇸

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1 of 2
CS 483 Analysis of Algorithms (Spring 2025)
Prim’s Algorithm Example
Given an undirected graph with five nodes {A, B, C, D, E} and an edge list (source, destination, weight)
of the graph below, find a minimum spanning tree using Prim’s algorithm by choosing A as a start
node.
(A, B, 2)
(A, C, 3)
(B, C, 1)
(B, D, 4)
(C, D, 5)
(C, E, 6)
(D, E, 7)
Answer:
Init Steps:
Start at node A and put it in MST: {A}
Add all edges to neighbors of A in order by weight into Priority Queue: [(A, B, 2), (A, C, 3)]
1st iteration:
Choose the smallest edge: (A, B, 2)
Add B to the MST: {A, B}
Add all edges to neighbors of B in order by weight into Priority Queue: [(B, C, 1), (A, C, 3), (B, D, 4)]
2nd iteration:
Choose the smallest edge: (B, C, 1)
Add C to the MST: {A, B, C}
Add all edges to neighbors of C in order by weight into Priority Queue: [(A, C, 3), (B, D, 4), (C, D, 5), (C,
E, 6)]
3rd iteration:
Choose the smallest edge: (A, C, 3)
C is already in the MST.
NO Update to the MST: {A, B, C}
NO Update to Priority Queue: [(B, D, 4), (C, D, 5), (C, E, 6)]
4th iteration:
Choose the smallest edge: (B, D, 4)
Add D to the MST: {A, B, C, D}
Add all edges to neighbors of D in order by weight into Priority Queue: [(C, D, 5), (C, E, 6), (D, E, 7)]
5th iteration:
Choose the smallest edge: (C, D, 5)
D is already in the MST.
NO Update to the MST: {A, B, C, D}
NO Update to Priority Queue: [(C, E, 6), (D, E, 7)]
6th iteration:
Choose the smallest edge: (C, E, 6)
Add E to the MST: {A, B, C, D, E}
Add all edges to neighbors of E in order by weight into Priority Queue: [(D, E, 7)] no new neighbors
pf2

Partial preview of the text

Download Prim Algorithm from Analysis of Algorithms and more Exercises Design and Analysis of Algorithms in PDF only on Docsity!

Page 1 of 2 CS 483 – Analysis of Algorithms (Spring 2025) Prim’s Algorithm Example Given an undirected graph with five nodes {A, B, C, D, E} and an edge list (source, destination, weight) of the graph below, find a minimum spanning tree using Prim’s algorithm by choosing A as a start node. (A, B, 2 ) (A, C, 3 ) (B, C, 1 ) (B, D, 4 ) (C, D, 5 ) (C, E, 6 ) (D, E, 7 ) Answer: Init Steps: Start at node A and put it in MST: {A} Add all edges to neighbors of A in order by weight into Priority Queue: [(A, B, 2 ), (A, C, 3 )] 1 st^ iteration: Choose the smallest edge: (A, B, 2 ) Add B to the MST: {A, B} Add all edges to neighbors of B in order by weight into Priority Queue: [(B, C, 1 ), (A, C, 3), (B, D, 4)] 2 nd^ iteration: Choose the smallest edge: (B, C, 1 ) Add C to the MST: {A, B, C} Add all edges to neighbors of C in order by weight into Priority Queue: [(A, C, 3), (B, D, 4), (C, D, 5 ), (C, E, 6 )] 3 rd^ iteration: Choose the smallest edge: (A, C, 3) C is already in the MST. NO Update to the MST: {A, B, C} NO Update to Priority Queue: [(B, D, 4), (C, D, 5 ), (C, E, 6 )] 4 th^ iteration: Choose the smallest edge: (B, D, 4 ) Add D to the MST: {A, B, C, D} Add all edges to neighbors of D in order by weight into Priority Queue: [(C, D, 5 ), (C, E, 6 ), (D, E, 7 )] 5 th^ iteration: Choose the smallest edge: (C, D, 5 ) D is already in the MST. NO Update to the MST: {A, B, C, D} NO Update to Priority Queue: [(C, E, 6 ), (D, E, 7 )] 6 th^ iteration: Choose the smallest edge: (C, E, 6 ) Add E to the MST: {A, B, C, D, E} Add all edges to neighbors of E in order by weight into Priority Queue: [(D, E, 7 )] – no new neighbors

Page 2 of 2 7 th^ iteration: All nodes are in the MST. Done. Selected edges are as follows: (A, B, 2 ) (B, C, 1 ) (B, D, 4 ) (C, E, 6 ) The total weight of this MST is: 2 + 1 + 4 + 6 = 13.