Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Código
// C++ program for Kruskal's algorithm to find Minimum Spanning Tree
// of a given connected, undirected and weighted graph
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
return graph;
}
return subsets[i].parent;
}
for (e ; e != E; e++)
{
for (int i = 0; i < V; ++i)
{
Matriz[e][i]=Matriz[e-1][i];
}
}
return Matriz;
}
return Matriz;
}
// A function that does union of two sets of x and y
// (uses union by rank)
int **Union(struct subset subsets[], int x, int y,int e, int **Matriz,int V)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);
subsets[yroot].parent = xroot;
Matriz[e][yroot]=xroot;
for (int i = 0; i < V; ++i)
{
if (Matriz[e][i]==yroot)
{
Matriz[e][i]=xroot;
}
}
}
subsets[yroot].parent = xroot;
Matriz[e][yroot]=xroot;
for (int i = 0; i < V; ++i)
{
if (Matriz[e][i]==yroot)
{
Matriz[e][i]=xroot;
}
}
subsets[xroot].rank++;
}
return Matriz;
}
printf("\nArista\t|");
for (int i = 0; i < V; ++i)
{
printf("\t%d|",i );
}
Matriz=ActualizaRestantes(Matriz,e+1,V,graph->E);
// print the contents of result[] to display the
// built MST
printf("Following are the edges in the constructed MST\n");
for (i = 0; i < e; ++i)
printf("%d -- %d == %d\n", result[i].src, result[i].dest,
result[i].weight);
return Matriz;
}
graph->edge[5].src = 2;
graph->edge[5].dest = 5;
graph->edge[5].weight =5 ;
graph->edge[6].src = 2;
graph->edge[6].dest = 4;
graph->edge[6].weight = 6;
graph->edge[7].src = 3;
graph->edge[7].dest = 4;
graph->edge[7].weight =4 ;
graph->edge[8].src = 3;
graph->edge[8].dest = 6;
graph->edge[8].weight = 4;
graph->edge[9].src = 4;
graph->edge[9].dest = 5;
graph->edge[9].weight = 3;
graph->edge[10].src = 4;
graph->edge[10].dest = 6;
graph->edge[10].weight =6 ;
graph->edge[11].src = 4;
graph->edge[11].dest = 7;
graph->edge[11].weight =2 ;
graph->edge[12].src = 4;
graph->edge[12].dest = 8;
graph->edge[12].weight = 6;
graph->edge[13].src = 5;
graph->edge[13].dest = 8;
graph->edge[14].src = 6;
graph->edge[14].dest = 7;
graph->edge[14].weight =8 ;
graph->edge[15].src = 6;
graph->edge[15].dest = 9;
graph->edge[15].weight =3 ;
graph->edge[16].src = 7;
graph->edge[16].dest = 8;
graph->edge[16].weight =8 ;
graph->edge[17].src = 7;
graph->edge[17].dest = 9;
graph->edge[17].weight = 3;
graph->edge[18].src = 7;
graph->edge[18].dest = 10;
graph->edge[18].weight = 5;
graph->edge[19].src = 7;
graph->edge[19].dest = 11;
graph->edge[19].weight = 7;
graph->edge[20].src = 8;
graph->edge[20].dest = 11;
graph->edge[20].weight = 4;
graph->edge[21].src = 9;
graph->edge[21].dest = 10;
graph->edge[21].weight = 5;
graph->edge[22].src = 10;
graph->edge[22].dest = 11;
graph->edge[22].weight = 2;
Matriz=KruskalMST(graph);
Imprime(Matriz,V,E);
return 0;
}