Este documento contiene el código C para implementar el algoritmo de Kruskal para encontrar el árbol de expansión mínimo de un grafo no dirigido. Inicializa una matriz de adyacencia y asigna pesos aleatorios a los enlaces. Luego, Kruskal itera sobre los enlaces ordenados por peso creciente y agrega cada enlace al árbol si no crea un ciclo. Finalmente, imprime el árbol de expansión mínimo y su costo total.
This presentation covers tips and tricks for game developers and covers a little bit of everything for both work for hire developers, as well as those creating their own IP.
This session will arm you with all the key information you need to to figure out Open Source software licensing issues, understand legal situation, etc. The aim is to present your obligations in terms of OSS licences, patent and intellectual property, before launching a project.
Next, a method developed within QualiPSo project will be presented!
This presentation covers tips and tricks for game developers and covers a little bit of everything for both work for hire developers, as well as those creating their own IP.
This session will arm you with all the key information you need to to figure out Open Source software licensing issues, understand legal situation, etc. The aim is to present your obligations in terms of OSS licences, patent and intellectual property, before launching a project.
Next, a method developed within QualiPSo project will be presented!
1. #include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define INFINITO 999
#define MAX 100
void imprime(int a[][MAX],int n);
void matriz(int a[][MAX], int n){
int i,j;
srand(time(0));
for(i = 0;i < n; i++)
{
for(j = 0;j < i; j++)
{
a[i][j] = a[j][i]= rand()%50;
if( a[i][j]>40)a[i][j]=a[j][i]=INFINITO;
}
a[i][i] = INFINITO;
}
imprime(a,n);
}
void imprime(int a[][MAX],int n){
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%dt",a[i][j]);
}
printf("n");
}
return;
}
int comprobacion_nodos(int aux_j,int p[]){
while(p[aux_j] != aux_j){
aux_j = p[aux_j];
}
return aux_j;
}
void union_ij(int i,int j,int p[]){//le quitamos el valor que tenga el arreglo p[]
en ese momento y lo vamos sustituyendo por
if(j > i){
p[j] = i;
}else{
p[i] = j;
}
return;
}
void kruskal(int a[][MAX],int n){
int count, i, p[MAX], min, j, aux_i, aux_j, k, matriz_temporal[MAX][MAX],
2. sum;
count = k = sum = 0;
for(i = 0; i < n; i++)
{
p[i] = i;
}
while(count < n)
{
min = INFINITO; // hacemos min 999
for(i = 0; i < n; i++)
{
for(j = 0;j < n; j++)
{
if(a[i][j] < min)
{
min = a[i][j]; //asignamos el valor de min a el valor
minimo posible
aux_i = i; // aux_i variable que guarda el nodo que
participa en la distancia mas orta posible
aux_j = j; // aux_j variable auxiliar que guarda el
nodo que participa en la distancia mas orta posible
}
}
}
if(min != INFINITO)
{
i = comprobacion_nodos(aux_i, p);
j = comprobacion_nodos(aux_j, p);
if (i != j)
{
matriz_temporal[k][0] = aux_i;//es nuestra matriz auxiliar
para guardar los valores de la distancia mas corta
matriz_temporal[k][1] = aux_j;
k++;
sum += min;
union_ij(i,j,p);
}
a[aux_i][aux_j]
= a[aux_j][aux_i] = INFINITO;
}
count +=1;
}
if(count != n)
{
printf("no existe el arbol de expansionn");
}
if(count == n)
{
printf("El arbol de expansion minima es:n");
for(k = 0; k < n-1 ; k++)
{
printf(" %d -> %d ",matriz_temporal[k][0],matriz_temporal[k][1]);
}
printf("ncosto total = %d n",sum);
}
3. return;
}
int main(int pepe, char** juan)
{
int a[MAX][MAX],n;
printf("Dame el numero de nodosn");
scanf("%d",&n);
matriz(a,n);
kruskal(a,n);
return 45;
}