Kruskal and prim's algorithm
Kruskal and prim's algorithm
Algorithm:
1. Sort all edges in the graph in ascending order of their weights.
2. Initialize an empty spanning tree (T) and set the cost to 0.
3. For each edge in sorted order:
o Find the root of both vertices of the edge using the Find() function.
o If they belong to different sets (i.e., adding the edge does not form a cycle):
▪ Include this edge in the MST.
▪ Merge the sets using the Union() function.
▪ Add the edge weight to the total cost.
4. Repeat until (n - 1) edges are added to the spanning tree (where n is the number of
vertices).
5. Output the MST and its total cost.
KRUSKAL(G): 1. Sort all edges in increasing order of weight. 2. Initialize an empty set MST = {} and
minCost = 0. 3. For each edge (u, v) in sorted order: a. If FIND(u) ≠ FIND(v): // If u and v belong to
different sets i. Add (u, v) to MST. ii. UNION(u, v). iii. minCost += weight of (u, v). 4. If the number of
edges in MST = (n - 1), stop. 5. Return MST and minCost.
Functions Used:
1. Find(u): Determines the root of the set containing u (using path compression for efficiency).
2. Union(u, v): Merges two sets (by rank or size to keep the tree balanced).
Time Complexity Analysis
• Sorting edges: O(ElogE)O(E \log E)O(ElogE)
• Find & Union operations (with path compression): O(α(E))O(\alpha(E))O(α(E)), where
α\alphaα is the inverse Ackermann function (almost constant time).
• Overall complexity: O(ElogE)O(E \log E)O(ElogE), where E is the number of edges.
• #include <stdio.h>
•
• int i, j, u, v, n, ne = 1;
• int min, mincost = 0, cost[10][10], parent[10];
•
• int find(int i);
• int uni(int i, int j);
•
• int main() {
• printf("\n\tImplementation of Kruskal's Algorithm\n");
•
• // Input the number of vertices
• printf("\nEnter the number of vertices: ");
• scanf("%d", &n);
•
• // Input the cost adjacency matrix
• printf("\nEnter the cost adjacency matrix:\n");
• for (i = 1; i <= n; i++) {
• for (j = 1; j <= n; j++) {
• scanf("%d", &cost[i][j]);
• if (cost[i][j] == 0)
• cost[i][j] = 999; // Set zero edges to "infinity"
(except diagonal)
• }
• parent[i] = 0; // Initialize parent array
• }
•
• printf("\nThe edges of the Minimum Cost Spanning Tree are:\n");
•
• while (ne < n) { // Runs until we include (n-1) edges
• min = 999; // Reset min before each iteration
•
• // Find the minimum cost edge
• for (i = 1; i <= n; i++) {
• for (j = 1; j <= n; j++) {
• if (cost[i][j] < min) {
• min = cost[i][j];
• u = i;
• v = j;
• }
• }
• }
•
• int rootU = find(u);
• int rootV = find(v);
•
• if (uni(rootU, rootV)) { // If it does not form a cycle,
include it
• printf("%d edge (%d, %d) = %d\n", ne++, u, v, min);
• mincost += min;
• }
•
• // Remove the edge from consideration
• cost[u][v] = cost[v][u] = 999;
• }
•
• printf("\nMinimum Cost = %d\n", mincost);
• return 0;
• }
•
• // Find function with path compression
• int find(int i) {
• while (parent[i] != 0)
• i = parent[i];
• return i;
• }
•
• // Union function to join sets
• int uni(int i, int j) {
• if (i != j) {
• parent[j] = i; // Union operation
• return 1;
• }
• return 0;
• }
int prims();
int main() {
printf("********* PRIM'S ALGORITHM *********\n");
printf("Enter the number of nodes: ");
scanf("%d", &n);
return 0;
}
int prims() {
int nearV[10];
int t[10][3]; // Stores the MST edges
int u = 1, v = 1;
int min, i, j, k;
// Initialize nearV[]
for (i = 1; i <= n; i++)
nearV[i] = 0;
return mincost;
}
Algorithm:
1. Initialize:
o Choose an arbitrary vertex as the starting point.
o Mark it as visited and initialize an array to track the nearest vertices.
2. Iterate until all vertices are included in MST:
o Select the minimum-cost edge that connects a visited vertex to an unvisited vertex.
o Add the selected edge to the MST.
o Update the nearest vertices for the remaining unvisited vertices.
3. Stop when the MST contains (n - 1) edges.
4. Output the MST and its total cost.
Final Answer:
The Minimum Spanning Tree (MST) found using Prim’s Algorithm consists of edges:
1. (A, B) with weight 1
2. (B, C) with weight 2
3. (C, D) with weight 3
with a Total Cost = 6.