You are on page 1of 6

MINI-PROJECT

Name: Tejas Vilas Pawar

PRN: 2042208

Class: Third Year

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)

let(u,v)bethelowestcostedgesuchthatu∈ Uandv∈ V-U; T = T ∪

{(u,v)}

U = U ∪ {v}
Program:

import java.util.Arrays;

class Pr6 {

public void Prim(int

G[][], int V) { int INF =

9999999;

int no_edge; // number of edge

// create a array to track selected vertex


// selected will become true otherwise false
boolean[] selected = new boolean[V];

// set selected false initially


Arrays.fill(selected, false);

// set number of edge to 0


no_edge = 0;

// the number of egde in minimum spanning tree will be


// always less than (V -1), where V is number of vertices in
// graph

// choose 0th vertex and make it true


selected[0] = true;

// print for edge and weight


System.out.println("Edge : Weight");

while (no_edge < V - 1) {


// For every vertex in the set S, find the all adjacent vertices
// , calculate the distance from the vertex selected at step 1.
// if the vertex is already in the set S, discard it otherwise
// choose another vertex nearest to selected vertex at step 1.

int min = INF;


int x = 0; // row number
int y = 0; // col number

for (int i = 0; i < V; i++) {


if (selected[i] == true) {
for (int j = 0; j < V; j++) {
// not in selected and there is an edge if
(!selected[j] && G[i][j] != 0) {
if (min > G[i][j]) {
min = G[i][j];
x = i;
y = j;
}
}
}
}
}
System.out.println(x + " - " + y + " : " + G[x][y]); selected[y] =
true;
no_edge++;
}
}

public static void main(String[] args) {


Pr6 g = new Pr6();

// number of vertices in grapj


int V = 5;

// create a 2d array of size 5x5


// for adjacency matrix to represent graph
int[][] G =
{
{ 0, 9, 75, 0, 0 },
{ 9, 0, 95, 19, 42 },
{ 75, 95, 0, 51, 66 },
{ 0, 19, 51, 0, 31 },
{ 0, 42, 66, 31, 0 }
};

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.

2. Write down algorithm for Prim’s Algorithm?


Step 1: Select a starting vertex
Step 2: Repeat Steps 3 and 4 until there are fringe vertices
Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has
minimum weight
Step 4: Add the selected edge and the vertex to the minimum spanning tree T
Step 5: [END OF LOOP]
Step 6: EXIT

3. Write down pseudo code for Prim’s Algorithm?


T = ∅;
U = { 1 };
while (U ≠ V)
let(u,v)bethelowestcostedgesuchthatu∈ Uandv∈ V-U; T = T ∪
{(u,v)}
U = U ∪ {v}

4. Write down applications of Prim’s Algorithm?


 Laying cables of electricalwiring
 In networkdesigned
 To make protocols in networkcycles

5. Write down Time Complexity of Prim’sAlgorithm?


6. Differentiate Prim’s Algorithm and Kruskal’sAlgorithm?

Prim’s Algorithm Kruskal’s Algorithm

It starts to build the Minimum


It starts to build the Minimum
Spanning Tree from the vertex
Spanning Tree from any vertex in
carrying minimum weight in the
the graph.
graph.
It traverses one node more than
one time to get the minimum It traverses one node only once.
distance.
Prim’s algorithm has a time
complexity of O(V2), V being the Kruskal’s algorithm’s time
number of vertices and can be complexity is O(E log V), V being
improved up to O(E log V) using the number of vertices.
Fibonacci heaps.
Kruskal’s algorithm can generate
Prim’s algorithm gives connected
forest(disconnected components)
component as well as it works
at any instant as well as it can
only on connected graph.
work on disconnected components
Prim’s algorithm runs faster in Kruskal’s algorithm runs faster in
dense graphs. sparse graphs.

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

You might also like