Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Ayuda codigo Bellman-Ford

Estas en el tema de Ayuda codigo Bellman-Ford en el foro de C/C++ en Foros del Web. Hola espero puedan ayudarme, tengo el siguiente codigo para el algoritmo de Bellman-Ford, el problema esque no imprime el camino al ultimo nodo, siempre aparece ...
  #1 (permalink)  
Antiguo 15/03/2009, 18:24
 
Fecha de Ingreso: noviembre-2007
Mensajes: 5
Antigüedad: 16 años, 5 meses
Puntos: 0
Ayuda codigo Bellman-Ford

Hola espero puedan ayudarme, tengo el siguiente codigo para el algoritmo de Bellman-Ford, el problema esque no imprime el camino al ultimo nodo, siempre aparece que no existe espero puedan ayudarme:


Código HTML:
#include<stdio.h>
#include<stdlib.h> 
    
    #define INFINITY 10000
    int graph[50][50],s[50],pathestimate[50],mark[50];
     int num_of_vertices,source,i,j,u,predecessor[50];
     int count=0;
     int minimum(int a[],int m[],int k);
     void printpath(int,int,int[]);
    void main()
     {
     
     printf("\nIntroduce el numero de vertices:\n");
     scanf("%d",&num_of_vertices);
            if(num_of_vertices<=0)
            {
              printf("\nLa matriz esta vacia\n");
              exit(1);
            }
     printf("\nIntroduce la matriz:\n");
        
        for(i=1;i<=num_of_vertices;i++)
        {
          printf("\nIntroduce los elementos del renglon %d\n",i);
           for(j=1;j<=num_of_vertices;j++)
           {
            scanf("%d",&graph[i][j]);
            }
         }
    
     printf("\nIntroduce el vertice origen\n");
     scanf("%d",&source);
    
        for(j=1;j<=num_of_vertices;j++)
        {
         mark[j]=0;
         pathestimate[j]=999;
         predecessor[j]=0;
        }
     pathestimate[source]=0;
     
     while(count<num_of_vertices)
     {
      u=minimum(pathestimate,mark,num_of_vertices);
      s[++count]=u;
      mark[u]=0;//le puse cero y tenia 1
      
      for (i = 0; i < num_of_vertices; ++i)
		s[i] = INFINITY;
       for (i = 0; i < num_of_vertices; i++){
        {
           if(pathestimate[i]>pathestimate[u]+graph[u][i])
            {
              pathestimate[i]=pathestimate[u]+graph[u][i];
              predecessor[i]=u;
        }
      }
     }
     for(i=1;i<=num_of_vertices;i++)
     {
       printpath(source,i,predecessor);
        if(pathestimate[i]!=999)
           printf("->(%d)\n",pathestimate[i]);
     }
     scanf("%d");
    }
    
}    
    int minimum(int a[],int m[],int k)
    {
     int mi=999;
     int i,t;
      for(i=1;i<=k;i++)
      {
        if(m[i]!=1)
        {
         if(mi>=a[i])
         {
          mi=a[i];
          t=i;
         }
        }
     }
     return t;
    }
    
    
    void printpath(int x,int i,int p[])
    {
     printf("\n");
     if(i==x)
      {
       printf("%d",x);
      }
       else if(p[i]==0)
       printf("no path from %d to %d",x,i);
      
       else
        {
         printpath(x,p[i],p);
         printf("..%d",i);
        }
    }
  #2 (permalink)  
Antiguo 16/03/2009, 03:15
Avatar de Eternal Idol  
Fecha de Ingreso: mayo-2004
Ubicación: Lucentum
Mensajes: 6.192
Antigüedad: 20 años
Puntos: 74
Respuesta: Ayuda codigo Bellman-Ford

Depuralo ...
__________________
¡Peron cumple, Evita dignifica! VIVA PERON CARAJO
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:20.