-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathprims_algorithm.cpp
More file actions
145 lines (124 loc) · 2.96 KB
/
prims_algorithm.cpp
File metadata and controls
145 lines (124 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Prim’s Algorithm — Minimum Spanning Tree (MST)
// Problem
// Given a weighted, undirected graph, find a subset of edges
// that connects all vertices with minimum total weight without cycles.
//
// Approach
// 1. Start from any vertex (usually 0), mark it as part of MST.
// 2. Use a priority queue (min-heap) to pick the smallest edge
// connecting MST to a new vertex.
// 3. Add the chosen vertex to MST and repeat until all vertices included.
//
// Complexity
// Time : O((V + E) log V) — using priority queue
// Space : O(V + E) — adjacency list + key array + heap
//
// Input
// - Number of vertices (V)
// - Number of edges (E)
// - Edge list {u, v, w} (u ↔ v with weight w)
//
// Output
// - Total weight of MST
// - Edges included in MST
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int V, E;
cin >> V >> E;
vector<vector<pair<int, int>>> adj(V);
for (int i = 0; i < E; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w}); // undirected graph
}
const int INF = numeric_limits<int>::max();
vector<int> key(V, INF); // minimum edge weight to MST
vector<int> parent(V, -1); // store MST edges
vector<bool> inMST(V, false); // track vertices included
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
int start = 0;
key[start] = 0;
pq.push({0, start});
while (!pq.empty())
{
int w = pq.top().first;
int u = pq.top().second;
pq.pop();
if (inMST[u])
continue;
inMST[u] = true;
for (auto &edge : adj[u])
{
int v = edge.first, weight = edge.second;
if (!inMST[v] && weight < key[v])
{
key[v] = weight;
parent[v] = u;
pq.push({key[v], v});
}
}
}
int totalWeight = 0;
cout << "Edges in MST:\n";
for (int v = 0; v < V; v++)
{
if (parent[v] != -1)
{
cout << parent[v] << " - " << v << " (weight " << key[v] << ")\n";
totalWeight += key[v];
}
}
cout << "Total MST weight: " << totalWeight << "\n";
return 0;
}
/*
Example Input:
5 7
0 1 2
0 3 6
1 2 3
1 3 8
1 4 5
2 4 7
3 4 9
Visualization:
Graph:
(2)
0 ------- 1
\ /|\
(6)\ (3)(5)
\ / \
3 4
(9) (7)
Step-by-step Execution:
Start from 0:
- Include 0 → candidate edges: 0→1(2), 0→3(6)
- Pick min edge 0→1(2) → add 1
- Candidate edges: 1→2(3), 1→3(8), 1→4(5), 0→3(6)
- Pick min edge 1→2(3) → add 2
- Candidate edges: 1→3(8), 1→4(5), 2→4(7), 0→3(6)
- Pick min edge 1→4(5) → add 4
- Candidate edges: 0→3(6), 1→3(8), 2→4(7), 3→4(9)
- Pick min edge 0→3(6) → add 3
All vertices included → MST complete
MST edges:
0 - 1 (2)
1 - 2 (3)
1 - 4 (5)
0 - 3 (6)
Total MST weight: 16
Time : O((V+E) log V)
Space : O(V+E)
*/
// add: Prim PR
// prepare Prim PR
// prepare PR