Thread: need some help with ford bellman algorithm

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    1

    need some help with ford bellman algorithm

    Hi guys, friend of mine gived me code that works correctly but i had to do for my homevork an application that shows full fastest road and distance. Code he gived to me shows only distance and I can't figure out how to edit it so it will show path also.
    Thnx for your suggestions

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #define nieskonczonosc 100000   //infinity
     
    #define MIN(a,b) (a<b)?a:b
     
    void main(void)
    {
    FILE *plik;
    int     n; //number of verticles
    int     A[100][100];
    int     D[100];
    char    s[5];
    int     i,j,k;
    int N;
     
     
    if ((plik=fopen("graf.txt","r"))==NULL)
    printf("missing file graf.txt!\n"); else
    {
    fscanf(plik,"%i",&n);
    printf("number of verticles: %i\n",n);
    for (j=0;j<n;j++)
    for (i=0;i<n;i++)
    {
    fscanf(plik,"%s",s);
    if (strcmp(s,"*")!=0)
    A[j][i]=atoi(s); else A[j][i]=nieskonczonosc;
    }
    fclose(plik);
     
    for (i=0;i<n;i++) D[i]=A[0][i];
     
    //start
    for (k=1;k<=n-2;k++)
    {
    for (i=1;i<n;i++)
    for (j=0;j<n;j++)
    {
    if (D[j]+A[j][i]>nieskonczonosc)
    N=nieskonczonosc; 
    else{
    
    
    N=D[j]+A[j][i];
    D[i]=MIN(D[i],N);
    }
    }
    }
    
    
    for (i=0;i<n;i++)
    if (D[i]<nieskonczonosc)
    printf("distance to (%i) = %i\n",i+1,D[i]);
    else
    printf("distance to (%i) = %s\n",i+1,"*");
    }
    }


    .txt file named graf.txt

    9
    0 5 * 23 * 7 * * *
    5 0 8 * * * 23 * *
    * 8 0 4 7 9 * * *
    23 * 4 0 23 * * * *
    * * 7 23 0 2 * * 4
    7 * 9 * 2 0 2 * *
    * 23 * * * 2 0 1 *
    * * * * * * 1 0 2
    * * * * 4 * * 2 0


    and another sample of graf.txt

    6
    0 * 3 * * *
    * 0 2 1 3 *
    3 2 0 * 1 *
    * 1 * 0 * *
    * 3 1 * 0 6
    * * * * 6 0

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your friend is doing you no favors giving you answers to homework. This prevents you from learning the material yourself, meaning you will not be able to handle future assignments either.

    Study and practice the concepts yourself, so you are properly equipped to do your own work.

    I will not comment on the code you posted, except for two general points:


  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Radek View Post
    Hi guys, friend of mine gived me code that works correctly but i had to do for my homevork an application that shows full fastest road and distance. Code he gived to me shows only distance and I can't figure out how to edit it so it will show path also.
    That's a very uninspiring speech.
    I'd be surprised if this wasn't cross-posted.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yeah, that special friend known as google.

    The code looks identical apart from stripping away the header comment, and translating a few bits into English.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seeking Former Ford Employees
    By FordInfo in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 07-22-2015, 10:41 AM
  2. Bellman Ford algorithm - unknown weight
    By lios1984 in forum Tech Board
    Replies: 4
    Last Post: 05-21-2012, 07:07 AM

Tags for this Thread