Advertisement
Guest User

asdf

a guest
Jun 30th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define size 10/*Defining maximum size of the weight-matrix*/
  4. #define max 9999
  5. int l;
  6. void floyd(int **a,int ** camino,int n);/*Function declaration*/
  7. void Warshall(int **a,int n);/*Function declaration*/
  8. void Imprimir(int **a, int n);
  9. void ciclofloyd(int **a,int **camino,int n, int ** aux,int *corto);
  10. int ImprimeCamino(int i, int j, int **camino,int n, int *corto);
  11.  
  12.  
  13. int main(int argc, char **argv){
  14.     int **a,**m_floyd,**m_warshall,**camino,**aux,*corto;
  15.     int i,j,n;
  16.    
  17.     printf("Enter no. of vertices : ");/*No. of vertex should be less equal to defined size*/
  18.     scanf("%d",&n);
  19.     printf("Give the initial weighted graph in weight matrix form(give 9999 in the place of infinity) : \n");
  20.     corto=calloc(max,sizeof(int));
  21.     a=calloc(n,sizeof(int *));
  22.     aux=calloc(n,sizeof(int *));
  23.     camino=calloc(n,sizeof(int*));
  24.     m_floyd=calloc(n,sizeof(int *));
  25.     m_warshall=calloc(n,sizeof(int *));
  26.     for(i=0;i<n;i++){
  27.         aux[i]=calloc(n,sizeof(int));
  28.         a[i]=calloc(n,sizeof(int));
  29.         camino[i]=calloc(n,sizeof(int));
  30.         m_floyd[i]=calloc(n,sizeof(int ));
  31.         m_warshall[i]=calloc(n,sizeof(int ));      
  32.     }
  33.    
  34.     for(i=0;i<n;i++){
  35.         for(j=0;j<n;j++)
  36.             camino[i][j]=i;
  37.         }
  38.     Imprimir(camino,n);
  39.     for(i=0;i<n;i++)
  40.     {
  41.         for(j=0;j<n;j++)
  42.         {
  43.             /*Se leen los valores fila a fila,si existe 9999 es infinito*/
  44.             scanf("%d",&a[i][j]);
  45.             m_floyd[i][j]=a[i][j];
  46.             aux[i][j]=a[i][j];
  47.             m_warshall[i][j]=a[i][j];
  48.         }
  49.     }
  50.     for(j=0;j<n;j++){
  51.         aux[j][j]=0;
  52.         m_floyd[j][j]=0;
  53.     }
  54.     printf("\nLa matriz de entrada es:-\n");
  55.     Imprimir(aux,n);
  56.     //printf("Floyd\n");
  57.     ciclofloyd(m_floyd,camino,n,aux,corto);/*Function call*/
  58.     //floyd(m_floyd,camino,n);
  59.     //printf("\nLa matriz con las distancias minimas es:\n\t");
  60.     //Imprimir(m_floyd,n);
  61.     //printf("\nAqui se puede buscar el camino:\n\t");
  62.     //Imprimir(camino,n);
  63.     //ImprimeCamino(4,0,camino,n);
  64.     //printf("\nWarshall\n");
  65.     //Warshall(m_warshall,n);
  66.     //printf("\nLa matriz con las distancias minimas es:\n\t");
  67.     //Imprimir(m_warshall,n);
  68.     //printf("El camino de %d a %d es:  \t",i,j);
  69.     i=0;
  70.     while(l>i){
  71.             printf("(%d ) ", corto[i]);
  72.             i++;
  73.         }
  74.         printf("\n");
  75.     return 0;
  76. }
  77.  
  78. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  79.  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  80.  *                  Funciones
  81.  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  82.  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83.  */
  84.  
  85. int ImprimeCamino(int i, int j, int **camino,int n, int*corto){
  86.     int *aux=calloc(max,sizeof(int)),l2=0,i1=0;
  87.     if(i==j){
  88.         printf("i e j son iguales por lo tanto el camino es el mismo: %d\n", camino[i][j]);
  89.     }
  90.     else{
  91.         while(i!=j && l2 < n){
  92.             //printf("pene %d  j %d i %d", l2, j, i);
  93.             l2=l2+1;
  94.             j=camino[i][j];
  95.            
  96.             aux[l2]=j;
  97.            
  98.         }
  99.         aux[0]=aux[l2];l2++;
  100.        
  101.         //printf(" l2 %d  \n", l2);
  102.         if(l2>3){
  103.             //printf(" PANFLENl2 %d  \n", l2);
  104.             while(i1<l2){
  105.                 corto[i1]=aux[i1];
  106.                 i1++;
  107.             }
  108.             l=l2;
  109.             //printf(" i1 %d  \n", i1);
  110.             i1=0;
  111.             while(l>i1){
  112.                 printf("(%d ) ", corto[i1]);
  113.                 i1++;
  114.             }
  115.             printf("\n");
  116.             return 1;
  117.            
  118.         }
  119.         else return 0;
  120.     }
  121. }
  122.  
  123. void ciclofloyd(int **a,int **camino,int n, int ** aux, int *corto)/*Function definition*/
  124. {
  125.     int k,i,j,l, menor=max,u,w;
  126.     for(k=0;k<n;k++)/*n is the no.of vertices of the graph and k represents table no.*/
  127.     {
  128.         for(i=0;i<n;i++)/*i represents row no. within a table*/
  129.         {
  130.            
  131.             for(j=0;j<n;j++)
  132.             {
  133.                 for(l=0;l<n;l++)
  134.                 {
  135.                     a[j][l]=aux[j][l];
  136.                     camino[j][l]=j;
  137.                 }
  138.             }/*
  139.             printf("a\n");
  140.             Imprimir(a,n);
  141.             printf("aux\n");
  142.             Imprimir(aux,n);
  143.             printf("camino\n");
  144.             Imprimir(camino,n);*/
  145.             if(a[k][i]>0 && a[k][i]< max && k!=i){
  146.                 a[k][i]=0;a[i][k]=0;   //borro arista
  147.                 //printf("ANTES A     i %d j %d k %d menor %d\n", i, j ,k, menor);
  148.                 //Imprimir(a,n);
  149.                 floyd(a,camino,n); // aplico a floyd al resto
  150.                             //AQUI SE ANALIZA
  151.                 /*for( u =0; u<n ; u++)
  152.                     for(w=u;w<n;w++)
  153.                         camino[w][u]=camino[u][w];
  154.                 printf("DESPUES A    i %d j %d k %d\n", i, j ,k);
  155.                 Imprimir(a,n);
  156.                 printf("camino\n");
  157.                 Imprimir(camino,n);*/
  158.                 for (j=0;j<n;j++){          //  reviso los caminos que llegan al vertice donde borre la arista 
  159.                     if(a[j][k]/*[k][i]*/>0 && (a[j][k]/*[k][i]*/+aux/*[i][k]/*/[k][i])<menor && k!=j){
  160.                        
  161.                         if(ImprimeCamino(k,j,camino,n,corto)){
  162.                             menor=a[j][k]/*[k][i]*/+aux/*[i][k]/*/[k][i];
  163.                             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);
  164.                             Imprimir(a,n);
  165.                             printf("aux");
  166.                             Imprimir(aux,n);
  167.                             printf("camino\n");
  168.                             Imprimir(camino,n);
  169.                            
  170.                         }
  171.                     }
  172.                    
  173.                 }      
  174.             }
  175.             //printf(" k %d i %d j %d menor %d\n",k,i,j, menor);
  176.         }
  177.     }
  178. }
  179.  
  180. void floyd(int **a,int **camino,int n)/*Function definition*/
  181. {
  182.     int k,i,j;
  183.     for(k=0;k<n;k++)/*n is the no.of vertices of the graph and k represents table no.*/
  184.     {
  185.         for(i=0;i<n;i++)/*i represents row no. within a table*/
  186.         {
  187.             for(j=0;j<n;j++)/*j represents column no. within a row*/
  188.             {
  189.                 if(a[i][j]>(a[i][k]+a[k][j]))/*Minimum is to be selected*/
  190.                 /*a[i][j] denotes distance between ith vertex and jth vertex*/
  191.                 {  
  192.                     a[i][j]=(a[i][k]+a[k][j]);
  193.                     camino[i][j]=camino[k][j];
  194.                 }
  195.             }
  196.         }
  197.     }
  198. }
  199. void Warshall(int **a,int n)/*Function definition*/
  200. {
  201.     int k,i,j;
  202.     for(k=0;k<n;k++)/*n is the no.of vertices of the graph and k represents table no.*/
  203.     {
  204.         for(i=0;i<n;i++)/*i represents row no. within a table*/
  205.         {
  206.             for(j=0;j<n;j++)/*j represents column no. within a row*/
  207.             {
  208.                     a[i][j]=a[i][j]||(a[i][k] && a[k][j]);
  209.             }
  210.         }
  211.     }
  212. }
  213. void Imprimir(int **a, int n){
  214.     int i,j;   
  215.     printf("\t");
  216.     for(i=0;i<n;i++)
  217.     {
  218.         for(j=0;j<n;j++)
  219.         {  
  220.             if(a[i][j]>=max)
  221.                 printf("inf\t");/*Printing inf as infinity in the input matrix*/
  222.             else
  223.                 printf("%d\t",a[i][j]);/*If data is not infinity then print the weight*/
  224.         }
  225.         printf("\n\t");
  226.     }
  227.     printf("\n");  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement