Skip to content

Commit d7c3c07

Browse files
committed
prism algo
1 parent 2d949ce commit d7c3c07

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
/*
5+
======================================================
6+
Prim's Algorithm: Minimum Spanning Tree (MST)
7+
======================================================
8+
Description:
9+
Prim's algorithm finds the Minimum Spanning Tree (MST) of a weighted
10+
undirected graph. The MST is a subset of edges connecting all vertices
11+
with the minimum total edge weight, without forming cycles.
12+
13+
Approach:
14+
1. Start with a single vertex (we start with vertex 0).
15+
2. Pick the smallest edge connecting a vertex in MST to a vertex outside MST.
16+
3. Include that edge and repeat until all vertices are included in MST.
17+
18+
Time Complexity:
19+
- O(V^2), where V is the number of vertices (using adjacency matrix).
20+
- Using priority queues (min-heap) can reduce it to O(E log V).
21+
22+
Space Complexity:
23+
- O(V^2) for adjacency matrix.
24+
======================================================
25+
*/
26+
27+
const int INF = 1e9; // Infinity value for initialization
28+
29+
int main() {
30+
int n; // Number of vertices
31+
cout << "Enter number of vertices: ";
32+
cin >> n;
33+
34+
// Input adjacency matrix
35+
vector<vector<int>> graph(n, vector<int>(n));
36+
cout << "Enter adjacency matrix (0 for no edge):\n";
37+
for (int i = 0; i < n; i++) {
38+
for (int j = 0; j < n; j++) {
39+
cin >> graph[i][j];
40+
}
41+
}
42+
43+
// key[i] -> Minimum weight edge to include vertex i in MST
44+
vector<int> key(n, INF);
45+
46+
// inMST[i] -> True if vertex i is included in MST
47+
vector<bool> inMST(n, false);
48+
49+
// parent[i] -> Stores the parent of vertex i in MST
50+
vector<int> parent(n, -1);
51+
52+
// Start from vertex 0
53+
key[0] = 0;
54+
55+
// MST will have n vertices
56+
for (int count = 0; count < n - 1; count++) {
57+
// Step 1: Pick the minimum key vertex from the set of vertices not yet included in MST
58+
int u = -1;
59+
int minKey = INF;
60+
61+
for (int v = 0; v < n; v++) {
62+
if (!inMST[v] && key[v] < minKey) {
63+
minKey = key[v];
64+
u = v;
65+
}
66+
}
67+
68+
// Step 2: Include this vertex in MST
69+
inMST[u] = true;
70+
71+
// Step 3: Update key and parent for adjacent vertices
72+
for (int v = 0; v < n; v++) {
73+
// If there is an edge u-v and v is not in MST and weight is smaller
74+
if (graph[u][v] != 0 && !inMST[v] && graph[u][v] < key[v]) {
75+
key[v] = graph[u][v];
76+
parent[v] = u;
77+
}
78+
}
79+
}
80+
81+
// Print the MST
82+
cout << "\nEdges in the Minimum Spanning Tree (MST):\n";
83+
cout << "Edge \tWeight\n";
84+
for (int i = 1; i < n; i++) {
85+
cout << parent[i] << " - " << i << "\t" << graph[i][parent[i]] << "\n";
86+
}
87+
88+
return 0;
89+
}

0 commit comments

Comments
 (0)