Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct list{
- int u, v;
- int w;
- list *next;
- };
- struct comp{
- int parent;
- };
- void make_set(comp set, int v){
- set[v].parent=v;
- }
- int find(comp set, int v){
- if(set[v].parent==v) return v;
- else return find(set, set[v].parent);
- }
- void union(comp set, int u, int v){
- int a=find(set, u);
- int b=find(set, v);
- if(a!=b){
- set[a].parent=b;
- }
- }
- list *pop(list *L){
- list *tmp=L;
- L=L->next;
- return tmp;
- }
- List *sort{List *L;}
- int MST(Graph **G, int n){
- list *Edges=new list;
- Edges=NULL;
- list *G=new list;
- G->next=Edges;
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- if(G[j][i]!=-1?){
- list *nowy=new list;
- nowy->u=j;
- nowy->v=i;
- nowy->w=G[j][i];
- if(Edges==NULL){
- nowy=Edges;
- Edges->next=NULL;}
- else{
- Edges->next=nowy;
- Edges=Edges->next;
- nowy->next=NULL;
- }
- }
- }
- }
- Edges=G->next;
- List *complete=List *sort(Edges);
- comp set[n];
- int counter=0;
- int total_weight=0;
- while(counter!=n){
- list *act=pop(complete);
- if(find(set, act->u)!=find(set, act->v)){
- counter++;
- total_weight+=act->w;
- union(set, act->u, act->v);
- }
- }
- return total_weight;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement