Lab2
Lab2
Title: WAP to find the minimum spanning tree using Prim’s Algorithm.
Theory:
Prim's Algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of a
connected, weighted, and undirected graph. The MST connects all vertices of the graph while
minimizing the total edge weight. The algorithm starts with an arbitrary vertex and grows the
MST by selecting the smallest edge that connects a vertex in the MST to a vertex outside it.
Algorithm:
Step 1: Initialize a set of vertices that are included in the MST.
Step 2: Choose an arbitrary vertex and set its key value to 0.
Step 3: Select the vertex with the smallest key value that is not yet included in the MST.
Step 4: Update the key values of the adjacent vertices.
Step 5: Repeat steps 3 and 4 until all vertices are included in the MST.
Step 6: Return the MST edges and the total weight.
Characteristics:
Time Complexity: O(V^2), where V is the number of vertices (for a simple
implementation without priority queues).
Space Complexity: O(V), for storing the MST and key values of vertices.
Source Code:
#include <stdio.h>
#include <limits.h>
#define MAX 30
void primsAlgo()
{
int cost = 0;
int key[MAX], mstSet[MAX];
for (int i = 0; i < n; i++)
{
key[i] = INT_MAX;
mstSet[i] = 0;
}
key[0] = 0;
parent[0] = -1;
mstSet[u] = 1;
printf("\t--------------------------\n");
printf("\tPrim's Algorithm\n");
printf("\t--------------------------\n");
printf("Edges in the Minimum Spanning Tree (MST):\n");
for (int i = 1; i < n; i++)
{
printf("%d - %d : %d\n", parent[i], i, Graph[i][parent[i]]);
cost += Graph[i][parent[i]];
}
int main()
{
n = 5;
int G[MAX][MAX] = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
primsAlgo();
Step 2: Initialize a disjoint-set (or union-find) data structure to keep track of the connected
components.
Step 3: Pick the smallest edge from the sorted edges list. Check if it forms a cycle by
checking if the two vertices of the edge belong to the same set.
Step 4: If it does not form a cycle, add the edge to the MST and union the two sets of the
two vertices.
Step 5: Repeat step 3 and 4 until there are V-1 edges in the MST (where V is the number
of vertices).
Characteristics:
Time Complexity: O(E log E), where E is the number of edges.
Space Complexity: O(V + E) ,where V is the number of nodes.
Source Code:
#include <stdio.h>
#define MAX 30
struct Edge {
int u, v, w;
};
void kruskalAlgo()
{
int i, j, count = 0, mstEdges = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < i; j++)
{
if (Graph[i][j] != 0)
{
edges[count++] = (struct Edge){i, j, Graph[i][j]};
}
}
}
for (i = 0; i < count - 1; i++)
{
for (j = 0; j < count - i - 1; j++)
{
if (edges[j].w > edges[j + 1].w)
{
struct Edge temp = edges[j];
edges[j] = edges[j + 1];
edges[j + 1] = temp;
}
}
}
for (i = 0; i < n; i++)
parent[i] = i;
if (rootU != rootV)
{
mst[mstEdges++] = edges[i];
parent[rootV] = rootU;
}
}
int cost = 0;
printf("\t--------------------------\n");
printf("\tKruskal's Algorithm\n");
printf("\t--------------------------\n");
int main()
{
n = 5;
int G[MAX][MAX] = {
{0, 7, 0, 5, 0},
{7, 0, 8, 9, 7},
{0, 8, 0, 7, 5},
{5, 9, 7, 0, 6},
{0, 7, 5, 6, 0}
};
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Graph[i][j] = G[i][j];
}
kruskalAlgo();
printf("\n\nCompiled By: DEVRAJ SILWAL\n");
return 0;
}
Output: