Professional Documents
Culture Documents
PRN: 2042208
Aim: Implement a minimum cost spanning tree for a given undirected graph
using Prim’s algorithm or kruskal’s algorithm.
Details:
1. In computer science, Prim's algorithm (also known as Jarník's algorithm) is a greedy
algorithm that finds a minimum spanning tree for a weighted undirectedgraph.
2. This means it finds a subset of the edges that forms a tree that includes every vertex,
where the total weight of all the edges in the tree isminimized.
3. The algorithm operates by building this tree one vertex at a time, from an arbitrary
starting vertex, at each step adding the cheapest possible connection from the tree to
anothervertex.
4. Thealgorithmwasdevelopedin1930byCzechmathematicianVojtěchJarníkandlater
rediscoveredandrepublishedbycomputerscientistsRobertC.Primin1957andEdsger
W.Dijkstrain1959.Therefore,itisalsosometimescalledtheJarník'salgorithm,Prim–
Jarník algorithm, Prim–Dijkstra algorithm or the DJPalgorithm.
5. Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree
from a graph. Prim's algorithm finds the subset of edges that includes every vertex of
the graph such that the sum of the weights of the edges can beminimized.
6. Prim'salgorithmstartswiththesinglenodeandexploresalltheadjacentnodeswithall the
connecting edges at every step. The edges with the minimal weights causing no cycles
in the graph gotselected.
7. Prim's algorithm can be simply implemented by using the adjacency matrix or
adjacency list graph representation, and to add the edge with the minimum weight
requires the linearly searching of an array ofweights.
8. ItrequiresO(|V|2)runningtime.Itcanbeimprovedfurtherbyusingtheimplementation of
heap to find the minimum weight edges in the inner loop of thealgorithm.
9. Thetimecomplexityoftheprim'salgorithmisO(ElogV)orO(VlogV),whereEisthe no. of
edges, and V is the no. of vertices.
Fig: Example
Pseudo Code:
T = ∅;
U = { 1 };
while (U ≠ V)
{(u,v)}
U = U ∪ {v}
Program:
import java.util.Arrays;
class Pr6 {
9999999;
g.Prim(G, V);
}
}
/*
Output :
Edge : Weight 0
-1 : 9
1 -3 : 19
3 -4 : 31
3 -2 : 51
*/
Questions & Answers:
1. What is Prim’s Algorithm?
Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses
thegreedyapproach.Prim'salgorithmsharesasimilaritywiththeshortestpathfirst
algorithms.
Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single
tree and keeps on adding new nodes to the spanning tree from the given graph.
Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning
tree from a graph. Prim's algorithm finds the subset of edges that includes every
vertexofthegraphsuchthatthesumoftheweightsoftheedgescanbeminimized.
Conclusion:
In this practical we learnt about Prim’s Algorithm also have implemented Java program
to demonstrate working of Prim’s Algorithm.
Sign of Teacher