Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

PRACTICAL - 04

NAME : Vedant Gawade


PRN : 202101040090 ROLL NO : 61
BATCH : A-3
SUBJECT : DAA[LAB]
PROBLEM STATEMENT :
Design Implement Prims Algorithm using Greedy Approach. Calculate the time
complexity of the algorithm.

Theory:

Prim's algorithm is a greedy algorithm used to find the minimum spanning tree
(MST) for a connected, undirected graph. The minimum spanning tree is a subset of
the edges of the graph that forms a tree that includes every vertex, where the sum
of the weights of the edges is minimized.

Prim's algorithm works by starting from an arbitrary vertex and iteratively growing
the MST by adding the cheapest edge that connects a vertex in the MST to a vertex
outside the MST.

Example :

Starting from vertex 0, the steps of Prim's algorithm would be:


1. Start from vertex 0.
2. Add vertex 0 to the MST.
3. Add edges (0, 1) and (0, 3) to the priority queue.
4. Remove edge (0, 1) from the priority queue, add vertex 1 to MST, and add
edge (1, 2) to the priority queue.
5. Remove edge (0, 3) from the priority queue, add vertex 3 to MST.
6. Remove edge (1, 2) from the priority queue, add vertex 2 to MST.

The minimum spanning tree is formed by edges (0, 1), (0, 3), and (1, 2), with a total
weight of 6 + 1 + 3 = 10.

Algorithm:

1. Initialize an empty set MST to store the minimum spanning tree and a priority
queue pq to store edges.
2. Choose an arbitrary starting vertex start.
3. Add start to MST.
4. For each edge (start, v) connected to start, add v and the edge weight to pq.
5. While MST does not contain all vertices:
• Remove the edge with the minimum weight from pq.
• If the edge connects a vertex v already in MST to a vertex u outside MST, add
u to MST and the edge to the MST.
• If the edge connects two vertices outside MST, discard the edge.
6. Repeat step 5 until MST contains all vertices.

CODE :
#include <stdio.h>
#include <limits.h>
#define vertices 5

int main() {
int g[vertices][vertices] = {
{0, 0, 3, 0, 0},
{0, 0, 10, 4, 0},
{3, 10, 0, 2, 6},
{0, 4, 2, 0, 1},
{0, 0, 6, 1, 0},
};

int parent[vertices];
int k[vertices];
int mst[vertices];
int i, count, edge, v;

for (i = 0; i < vertices; i++) {


k[i] = INT_MAX;
mst[i] = 0;
}

k[0] = 0;
parent[0] = -1;

for (count = 0; count < vertices - 1; count++) {


int minimum = INT_MAX;
int min;

for (i = 0; i < vertices; i++)


if (mst[i] == 0 && k[i] < minimum )
minimum = k[i], min = i;

edge = min;
mst[edge] = 1;

for (v = 0; v < vertices; v++) {


if (g[edge][v] && mst[v] == 0 && g[edge][v] < k[v]) {
parent[v] = edge;
k[v] = g[edge][v];
}
}
}

printf("\n Edge \t Weight\n");


for (i = 1; i < vertices; i++)
printf(" %d <-> %d %d \n", parent[i], i, g[i][parent[i]]);

return 0;
}
OUTPUT:

TIME COMPLEXITY:

The time complexity of Prim's algorithm implemented with a priority queue is O(E log
V), where E is the number of edges and V is the number of vertices. This is because, in
each iteration, we extract the minimum edge from the priority queue, which takes
O(log V) time, and we iterate through all edges and vertices.

You might also like