Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define size 10/*Defining maximum size of the weight-matrix*/
- #define max 9999
- int l;
- void floyd(int **a,int ** camino,int n);/*Function declaration*/
- void Warshall(int **a,int n);/*Function declaration*/
- void Imprimir(int **a, int n);
- void ciclofloyd(int **a,int **camino,int n, int ** aux,int *corto);
- int ImprimeCamino(int i, int j, int **camino,int n, int *corto);
- int main(int argc, char **argv){
- int **a,**m_floyd,**m_warshall,**camino,**aux,*corto;
- int i,j,n;
- printf("Enter no. of vertices : ");/*No. of vertex should be less equal to defined size*/
- scanf("%d",&n);
- printf("Give the initial weighted graph in weight matrix form(give 9999 in the place of infinity) : \n");
- corto=calloc(max,sizeof(int));
- a=calloc(n,sizeof(int *));
- aux=calloc(n,sizeof(int *));
- camino=calloc(n,sizeof(int*));
- m_floyd=calloc(n,sizeof(int *));
- m_warshall=calloc(n,sizeof(int *));
- for(i=0;i<n;i++){
- aux[i]=calloc(n,sizeof(int));
- a[i]=calloc(n,sizeof(int));
- camino[i]=calloc(n,sizeof(int));
- m_floyd[i]=calloc(n,sizeof(int ));
- m_warshall[i]=calloc(n,sizeof(int ));
- }
- for(i=0;i<n;i++){
- for(j=0;j<n;j++)
- camino[i][j]=i;
- }
- Imprimir(camino,n);
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- /*Se leen los valores fila a fila,si existe 9999 es infinito*/
- scanf("%d",&a[i][j]);
- m_floyd[i][j]=a[i][j];
- aux[i][j]=a[i][j];
- m_warshall[i][j]=a[i][j];
- }
- }
- for(j=0;j<n;j++){
- aux[j][j]=0;
- m_floyd[j][j]=0;
- }
- printf("\nLa matriz de entrada es:-\n");
- Imprimir(aux,n);
- //printf("Floyd\n");
- ciclofloyd(m_floyd,camino,n,aux,corto);/*Function call*/
- //floyd(m_floyd,camino,n);
- //printf("\nLa matriz con las distancias minimas es:\n\t");
- //Imprimir(m_floyd,n);
- //printf("\nAqui se puede buscar el camino:\n\t");
- //Imprimir(camino,n);
- //ImprimeCamino(4,0,camino,n);
- //printf("\nWarshall\n");
- //Warshall(m_warshall,n);
- //printf("\nLa matriz con las distancias minimas es:\n\t");
- //Imprimir(m_warshall,n);
- //printf("El camino de %d a %d es: \t",i,j);
- i=0;
- while(l>i){
- printf("(%d ) ", corto[i]);
- i++;
- }
- printf("\n");
- return 0;
- }
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- * Funciones
- *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- */
- int ImprimeCamino(int i, int j, int **camino,int n, int*corto){
- int *aux=calloc(max,sizeof(int)),l2=0,i1=0;
- if(i==j){
- printf("i e j son iguales por lo tanto el camino es el mismo: %d\n", camino[i][j]);
- }
- else{
- while(i!=j && l2 < n){
- //printf("pene %d j %d i %d", l2, j, i);
- l2=l2+1;
- j=camino[i][j];
- aux[l2]=j;
- }
- aux[0]=aux[l2];l2++;
- //printf(" l2 %d \n", l2);
- if(l2>3){
- //printf(" PANFLENl2 %d \n", l2);
- while(i1<l2){
- corto[i1]=aux[i1];
- i1++;
- }
- l=l2;
- //printf(" i1 %d \n", i1);
- i1=0;
- while(l>i1){
- printf("(%d ) ", corto[i1]);
- i1++;
- }
- printf("\n");
- return 1;
- }
- else return 0;
- }
- }
- void ciclofloyd(int **a,int **camino,int n, int ** aux, int *corto)/*Function definition*/
- {
- int k,i,j,l, menor=max,u,w;
- for(k=0;k<n;k++)/*n is the no.of vertices of the graph and k represents table no.*/
- {
- for(i=0;i<n;i++)/*i represents row no. within a table*/
- {
- for(j=0;j<n;j++)
- {
- for(l=0;l<n;l++)
- {
- a[j][l]=aux[j][l];
- camino[j][l]=j;
- }
- }/*
- printf("a\n");
- Imprimir(a,n);
- printf("aux\n");
- Imprimir(aux,n);
- printf("camino\n");
- Imprimir(camino,n);*/
- if(a[k][i]>0 && a[k][i]< max && k!=i){
- a[k][i]=0;a[i][k]=0; //borro arista
- //printf("ANTES A i %d j %d k %d menor %d\n", i, j ,k, menor);
- //Imprimir(a,n);
- floyd(a,camino,n); // aplico a floyd al resto
- //AQUI SE ANALIZA
- /*for( u =0; u<n ; u++)
- for(w=u;w<n;w++)
- camino[w][u]=camino[u][w];
- printf("DESPUES A i %d j %d k %d\n", i, j ,k);
- Imprimir(a,n);
- printf("camino\n");
- Imprimir(camino,n);*/
- for (j=0;j<n;j++){ // reviso los caminos que llegan al vertice donde borre la arista
- if(a[j][k]/*[k][i]*/>0 && (a[j][k]/*[k][i]*/+aux/*[i][k]/*/[k][i])<menor && k!=j){
- if(ImprimeCamino(k,j,camino,n,corto)){
- menor=a[j][k]/*[k][i]*/+aux/*[i][k]/*/[k][i];
- printf("c i %d j %d k %d a[j][k] %d aux[k][i] %d menor %d\n", i, j ,k,a[j][k],aux[k][i], menor);
- Imprimir(a,n);
- printf("aux");
- Imprimir(aux,n);
- printf("camino\n");
- Imprimir(camino,n);
- }
- }
- }
- }
- //printf(" k %d i %d j %d menor %d\n",k,i,j, menor);
- }
- }
- }
- void floyd(int **a,int **camino,int n)/*Function definition*/
- {
- int k,i,j;
- for(k=0;k<n;k++)/*n is the no.of vertices of the graph and k represents table no.*/
- {
- for(i=0;i<n;i++)/*i represents row no. within a table*/
- {
- for(j=0;j<n;j++)/*j represents column no. within a row*/
- {
- if(a[i][j]>(a[i][k]+a[k][j]))/*Minimum is to be selected*/
- /*a[i][j] denotes distance between ith vertex and jth vertex*/
- {
- a[i][j]=(a[i][k]+a[k][j]);
- camino[i][j]=camino[k][j];
- }
- }
- }
- }
- }
- void Warshall(int **a,int n)/*Function definition*/
- {
- int k,i,j;
- for(k=0;k<n;k++)/*n is the no.of vertices of the graph and k represents table no.*/
- {
- for(i=0;i<n;i++)/*i represents row no. within a table*/
- {
- for(j=0;j<n;j++)/*j represents column no. within a row*/
- {
- a[i][j]=a[i][j]||(a[i][k] && a[k][j]);
- }
- }
- }
- }
- void Imprimir(int **a, int n){
- int i,j;
- printf("\t");
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- if(a[i][j]>=max)
- printf("inf\t");/*Printing inf as infinity in the input matrix*/
- else
- printf("%d\t",a[i][j]);/*If data is not infinity then print the weight*/
- }
- printf("\n\t");
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement