Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions greedy_methods/Prim's_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Prim's Algorithm - Minimum Spanning Tree (MST)

Check failure on line 1 in greedy_methods/Prim's_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

greedy_methods/Prim's_algorithm.py:1:8: invalid-syntax: missing closing quote in string literal

Check failure on line 1 in greedy_methods/Prim's_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

greedy_methods/Prim's_algorithm.py:1:1: N999 Invalid module name: 'Prim's_algorithm'

Check failure on line 1 in greedy_methods/Prim's_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

greedy_methods/Prim's_algorithm.py:1:1: invalid-syntax: Expected a statement
// Language: C++

Check failure on line 2 in greedy_methods/Prim's_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

greedy_methods/Prim's_algorithm.py:2:1: invalid-syntax: Expected a statement

Check failure on line 2 in greedy_methods/Prim's_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

greedy_methods/Prim's_algorithm.py:1:50: invalid-syntax: Expected a statement
// Category: Greedy Algorithms

Check failure on line 3 in greedy_methods/Prim's_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

greedy_methods/Prim's_algorithm.py:3:21: invalid-syntax: Simple statements must be separated by newlines or semicolons

Check failure on line 3 in greedy_methods/Prim's_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

greedy_methods/Prim's_algorithm.py:3:1: invalid-syntax: Expected a statement

Check failure on line 3 in greedy_methods/Prim's_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

greedy_methods/Prim's_algorithm.py:2:17: invalid-syntax: Expected an expression

#include <iostream>
#include <vector>
#include <queue>
#include <utility>

using namespace std;

Check failure on line 10 in greedy_methods/Prim's_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

greedy_methods/Prim's_algorithm.py:10:17: invalid-syntax: Simple statements must be separated by newlines or semicolons

Check failure on line 10 in greedy_methods/Prim's_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (invalid-syntax)

greedy_methods/Prim's_algorithm.py:10:7: invalid-syntax: Simple statements must be separated by newlines or semicolons

void primMST(int V, vector<vector<pair<int, int>>> &adj) {
vector<int> key(V, INT_MAX);
vector<int> parent(V, -1);
vector<bool> inMST(V, false);

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;

// Start from vertex 0
key[0] = 0;
pq.push({0, 0});

while (!pq.empty()) {
int u = pq.top().second;
pq.pop();

inMST[u] = true;

for (auto &[v, weight] : adj[u]) {
if (!inMST[v] && weight < key[v]) {
key[v] = weight;
pq.push({key[v], v});
parent[v] = u;
}
}
}

cout << "Edges in MST:\n";
int totalWeight = 0;
for (int i = 1; i < V; ++i) {
cout << parent[i] << " - " << i << " (" << key[i] << ")\n";
totalWeight += key[i];
}
cout << "Total Weight = " << totalWeight << endl;
}

int main() {
int V, E;
cout << "Enter number of vertices and edges: ";
cin >> V >> E;

vector<vector<pair<int, int>>> adj(V);
cout << "Enter edges (u v w):\n";
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});
}

primMST(V, adj);

return 0;
}
Loading