Skip to content

Commit

Permalink
Merge pull request #34 from riya0522/master
Browse files Browse the repository at this point in the history
Krushkals c++
  • Loading branch information
jaspreetkaur96 authored Oct 4, 2019
2 parents 0ad44c0 + b284cc9 commit fc682c8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Riya_kruskals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include<bits/stdc++.h>
using namespace std;

string str="";
ifstream inf;
ofstream outf;

class Edge{
public:
int beg;
int end;
int weight;
};
Edge edges[100];

int fpar(int v,int *parent){
if(parent[v]==v)
return v;
return fpar(parent[v],parent);
}
bool compare(Edge e1,Edge e2){
return e1.weight<e2.weight;
}
void krus(Edge*edges,int E,int V){
sort(edges,edges+E,compare);
Edge*output=new Edge[V-1];
int *parent=new int[V];
int parin=0;
while(parin<V){
parent[parin]=parin;
parin++;
}
for(int count=0,i=0; count!=V-1 ; i++){
Edge curred=edges[i];
int bg=fpar(edges[i].beg,parent);
int en=fpar(edges[i].end,parent);
if(bg!=en){
output[count]=curred;
count++;
parent[bg]=en;
}
}
int tem=0;
while(tem<V-1){
outf<<char(output[tem].beg+97)<<","<<char(output[tem].end+97)<<endl;
tem++;
}

}

int main(int argc,char **argv)
{
char *f1=argv[1];
char *f2=argv[2];

inf.open(f1);
outf.open(f2);

inf>>str;
int V=str[0]-48;
inf>>str;
int e=0;
while(inf)
{
string str;
inf>>str;
edges[e].beg=str[0]-97;
edges[e].end=str[2]-97;
int i=4;
int strng=str[i]-48;
i++;
for( ; str[i]!='\0' ; i++){
strng=(str[i]-48)+strng*10;
}
edges[e].weight=strng;
e++;
}
krus(edges,e-1,V);
inf.close();
outf.close();
return 1;

}
Binary file added test.pdf
Binary file not shown.

0 comments on commit fc682c8

Please sign in to comment.