-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmst.cpp
More file actions
146 lines (121 loc) · 4.82 KB
/
mst.cpp
File metadata and controls
146 lines (121 loc) · 4.82 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
146
// Minimum Spanning Tree (MST) using Prim's Algorithm
// Problem Link: https://www.geeksforgeeks.org/problems/minimum-spanning-tree/1
/*
Problem Statement:
Given a weighted, undirected, and connected graph with V vertices and E edges, find the sum of the
weights of the edges in the Minimum Spanning Tree (MST) of the graph. The graph is provided as a
list of edges, where each edge is represented as [u, v, w], indicating an edge between vertex u
and vertex v with edge weight w.
Examples:
Example 1:
Input: V = 3, E = 3, Edges = [[0, 1, 5], [1, 2, 3], [0, 2, 1]]
Output: 4
Explanation: The MST includes edges (0-2) with weight 1 and (1-2) with weight 3, totaling 4.
Example 2:
Input: V = 2, E = 1, Edges = [[0, 1, 5]]
Output: 5
Explanation: Only one Spanning Tree is possible which has a weight of 5.
Constraints:
- 2 ≤ V ≤ 1000
- V-1 ≤ E ≤ (V*(V-1))/2
- 1 ≤ w ≤ 1000
- The graph is connected and doesn't contain self-loops & multiple edges.
Approach (Prim's Algorithm):
1. Build an adjacency list representation of the graph
2. Use a priority queue (min-heap) to select the minimum weight edge at each step
3. Start from any vertex (here, vertex 0)
4. For each unvisited neighbor of the current vertex, add it to the priority queue
5. Select the edge with minimum weight that doesn't form a cycle
6. Repeat until all vertices are included in the MST
Time Complexity: O(E log V)
- Building adjacency list: O(E)
- Priority queue operations: Each edge can be added to the queue once, and each pop takes O(log E) time
- Total: O(E log E), which is equivalent to O(E log V) since E ≤ V²
Space Complexity: O(V + E)
- Adjacency list: O(E)
- Priority queue: O(E) in worst case
- Visited array: O(V)
*/
#include <vector>
#include <queue>
#include <climits>
#include <iostream>
using namespace std;
class Solution {
public:
// Function to find sum of weights of edges of the Minimum Spanning Tree
int spanningTree(int V, vector<vector<int>>& edges) {
// Build adjacency list where adj[u] contains pairs {v, weight}
vector<vector<pair<int,int>>> adj(V);
for (auto &edge : edges) {
int u = edge[0];
int v = edge[1];
int wt = edge[2];
adj[u].push_back({v, wt});
adj[v].push_back({u, wt}); // Since the graph is undirected
}
// Min-Heap to get the minimum weight edge at each step
// Each element is {weight, node}
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> minHeap;
// Track vertices included in MST
vector<bool> inMST(V, false);
// Start from vertex 0
minHeap.push({0, 0}); // {weight, vertex}
int totalWeight = 0; // Sum of weights in MST
// Prim's algorithm main loop
while (!minHeap.empty()) {
// Extract the minimum weight edge
auto [wt, node] = minHeap.top();
minHeap.pop();
// If node already in MST, skip
if (inMST[node]) continue;
// Include vertex in MST
inMST[node] = true;
totalWeight += wt; // Add edge weight to total
// Explore all adjacent vertices of the current vertex
for (auto &[neigh, edgeWt] : adj[node]) {
// If neighbor not yet in MST, add edge to priority queue
if (!inMST[neigh]) {
minHeap.push({edgeWt, neigh});
}
}
}
// Return total weight of MST
return totalWeight;
}
};
// Test function to verify the implementation
void testMST() {
Solution sol;
// Test Case 1: Standard example from problem statement
vector<vector<int>> edges1 = {{0, 1, 5}, {1, 2, 3}, {0, 2, 1}};
int V1 = 3;
int result1 = sol.spanningTree(V1, edges1);
cout << "Test Case 1: MST Weight = " << result1 << " (Expected: 4)" << endl;
// Test Case 2: Single edge graph
vector<vector<int>> edges2 = {{0, 1, 5}};
int V2 = 2;
int result2 = sol.spanningTree(V2, edges2);
cout << "Test Case 2: MST Weight = " << result2 << " (Expected: 5)" << endl;
// Test Case 3: Complete graph with 4 vertices
vector<vector<int>> edges3 = {
{0, 1, 10}, {0, 2, 6}, {0, 3, 5},
{1, 2, 15}, {1, 3, 20},
{2, 3, 4}
};
int V3 = 4;
int result3 = sol.spanningTree(V3, edges3);
cout << "Test Case 3: MST Weight = " << result3 << " (Expected: 19)" << endl;
// Test Case 4: Star-shaped graph
vector<vector<int>> edges4 = {
{0, 1, 2}, {0, 2, 3}, {0, 3, 4}, {0, 4, 5}
};
int V4 = 5;
int result4 = sol.spanningTree(V4, edges4);
cout << "Test Case 4: MST Weight = " << result4 << " (Expected: 14)" << endl;
}
int main() {
cout << "Testing Minimum Spanning Tree Implementation (Prim's Algorithm):" << endl;
testMST();
return 0;
}