Skip to content

Commit

Permalink
Add prims algorithm and magic square implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nandwalritik committed May 3, 2021
1 parent b0a4d4b commit 50e43bb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
65 changes: 65 additions & 0 deletions graph/prims.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <bits/stdc++.h>
using namespace std;
/*
LOGIC
1.) Maintain two sets
i.) First set contains vertices already included in MST
ii.) Second set contains vertices not yet included.
2.) At every step consider all edges that connects two sets
pick the minm weight edge from these edges.
*/
int main()
{
int N, m;
cin >> N >> m;
vector<pair<int, int>> adj[N];
int a, b, wt;
for (int i = 0; i < m; i++)
{
cin >> a >> b >> wt;
adj[a].push_back({b, wt});
adj[b].push_back({a, wt});
}
int parent[N];
int key[N];
bool mstSet[N];

for (int i = 0; i < N; i++)
{
key[i] = INT_MAX, mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
int cost = 0;
for (int i = 0; i < N - 1; i++)
{
int minI = INT_MAX, u;
for (int v = 0; v < N; v++)
{
if (mstSet[v] == false && key[v] < minI)
{
minI = key[v], u = v;
}
}
mstSet[u] = true;
cost += minI;
for (auto it : adj[u])
{
int v = it.first;
int weight = it.second;
if (mstSet[v] == false && weight < key[v])
{
parent[v] = u, key[v] = weight;
}
}
}
cout << cost << endl;
for (int i = 1; i < N; i++)
{
cout << parent[i] << " " << i << "\n";
}
return 0;
}
6 changes: 6 additions & 0 deletions rbtr/magicSquare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


n = 3
s = (n*((n**2) + 1))/2
ans = [[0 for i in range(3)] for i in range(3)]
used = [i for i in range(1, n**2 + 1)]

0 comments on commit 50e43bb

Please sign in to comment.