Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Minimum Spanning Trees Algorithm #409

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions Graphs/Minimum_Spanning _Trees/Kruskal'sAlgo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define PI 3.14159265358979323
#define debug(x) cout<<"Case "<<x<<": "
#define For(i,n) for(long long i=0;i<n;i++)
#define Frabs(i,a,b) for(long long i = a; i < b; i++)
#define Frabr(i,a,b) for(long long i = a; i >=b; i--)
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector < pii > vpii;
typedef vector < pll > vpll;
typedef vector <string> vs;

//Handle:cyber_rajat
class Edge
{
public:
int source;
int destination;
int weight;
};

bool compare(Edge a,Edge b) //sorts on basis of weight
{
return a.weight<b.weight;
}

int findParent(int vertix , int*parent)
{
if(parent[vertix]==vertix) //if parent[vertix]==vertix means it is parent itself else call further
return vertix;
return findParent(parent[vertix],parent);
} //Union Find Algorithm is involved in it

void Kruskals(Edge*input,int N,int E)
{
sort(input,input+E,compare); //sorting the input on basis of weight as we need to start from minimum weight
Edge*output=new Edge[N-1]; //out MST will contain N-1 edges
int*parent=new int[N];
for(int i=0;i<N;i++)
parent[i]=i; //initial parent vertix to be itself

int count=0; //edges should be N-1
int i=0; //starting from input no 0
while(count!=N-1)
{
int sourceParent=findParent(input[i].source,parent); //finding Parent of sourse and destination
int destParent=findParent(input[i].destination,parent);
if(sourceParent!=destParent) //If parent of source and destination is same it
{ // means already path exists and introducing this
output[count]=input[i]; //edge will cause cyclisation,which should not happen
count++; //so leave it and move further if parents are differnt
parent[sourceParent]=destParent; //then insert them to MST,increse counte of edges inMST
} //and make parent of both sourse and destination same
i++; //by allocating one as parent of the other
}
for(int i=0;i<N-1;i++)
{
if(output[i].source<=output[i].destination) //printing small vertix first
cout<<output[i].source<<" "<<output[i].destination<<" "<<output[i].weight<<endl;
else
cout<<output[i].destination<<" "<<output[i].source<<" "<<output[i].weight<<endl;
}

}

int main(int argc, char const *argv[])
{
int N,E;
cin>>N>>E;
Edge*input=new Edge[E];
for(int i=0;i<E;i++) //inputting source,destination and weight of edge
{
cin>>input[i].source;
cin>>input[i].destination;
cin>>input[i].weight;
}

Kruskals(input,N,E);
return 0;
}
94 changes: 94 additions & 0 deletions Graphs/Minimum_Spanning _Trees/PrimsAlgo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define PI 3.14159265358979323
#define debug(x) cout<<"Case "<<x<<": "
#define For(i,n) for(long long i=0;i<n;i++)
#define Frabs(i,a,b) for(long long i = a; i < b; i++)
#define Frabr(i,a,b) for(long long i = a; i >=b; i--)
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector < pii > vpii;
typedef vector < pll > vpll;
typedef vector <string> vs;

//Handle:cyber_rajat
int findMinVerix(int*weights,bool*visited,int N)
{
int minVertix=-1;
for(int i=0;i<N;i++)
{
if(!visited[i] && (minVertix==-1 || weights[i]<weights[minVertix]))
minVertix=i;
}
return minVertix;
}

void Prims(int**Edges,int N)
{
int*parents=new int[N];
int*weights=new int[N];
bool*visited=new bool[N];
for(int i=0;i<N;i++)
{
visited[i]=false; //initialising each vertix with INT_MAAX & as not visited
weights[i]=INT_MAX;
}
parents[0]=-1;
weights[0]=0;
for(int i=0;i<N;i++) //Going to all N vertices
{
// Find Min Vertex
int minVertix=findMinVerix(weights,visited,N); //finding minimum vetix from the unvisited vertix
visited[minVertix]=true;
// Explore un visted neighbours
for(int j=0;j<N;j++)
{
if(Edges[minVertix][j]!=0 && !visited[j]) //if there is edge and is not visited
{
if(Edges[minVertix][j]<weights[j]){ //if weight of edge is better than the weight of the vertix
weights[j]=Edges[minVertix][j]; //then update else not , if we update then in parent of that
parents[j]=minVertix; //neighbour vertix as minVertix
}
}
}
}
for(int i=1;i<N;i++) //Then our MST->
{ //index parent
if(parents[i]<=i) // 0 -1
cout<<parents[i]<<" "<<i<<" "<<weights[i]<<endl; // 1 -
else // 2 -
cout<<i<<" "<<parents[i]<<" "<<weights[i]<<endl; // n-1 -
} // print from 1 to n-1
}

int main(int argc, char const *argv[])
{
int N,E,a,b,w;
cin>>N>>E;
int**Edges=new int*[N];
for(int i=0;i<N;i++){
Edges[i]=new int[N];
for(int j=0;j<N;j++)
Edges[i][j]=0;
}
for(int i=0;i<E;i++)
{
cin>>a>>b>>w;
Edges[a][b]=w;
Edges[b][a]=w;
}
Prims(Edges,N);
return 0;
}