Pages

Sunday, June 12, 2011

FLOYD WARSHALL ALGORITHM

#include<stdio.h>
#include<stdlib.h>
#define MAX 10

void floyd(int);
int w[MAX][MAX], d[MAX][MAX][MAX];

void main()
{
int i, j,k,v;
clrscr();
printf("enter the no. of vertices\n");
scanf("%d",&v);
printf("enter the weights \n");

  for(i=1;i<=v;i++)
  {
   for(j=1;j<=v;j++)
    scanf("%d",&w[i][j]);
   }

 floyd(v);
 getch();
 }//end of main

 void floyd(int v)
 {
 int k, i,j;

  k=0;
for(i=1;i<=v;i++)
{
 for(j=1;j<=v;j++)
  d[k][i][j]=w[i][j];
 }

      for(k=1;k<=v;k++)
      {
       for(i=1;i<=v;i++)
    {
      for(j=1;j<=v;j++)
       d[k][i][j]=min(d[k-1][i][j], d[k-1][i][k]+ d[k-1][k][j]);
      }
    }

 //displayin matrix

 for(k=0;k<=v;k++)
 {
 printf(" k=%d \n",k);
   for(i=1;i<=v;i++)
   {
    printf("\n");
     for(j=1;j<=v;j++)
     printf("\t %d",d[k][i][j]);
    }
    printf("\n \n ");
  }
}


OUTPUT:
Enter the no. of vertices in the graph : 5
Enter the weight matrix :
0      3      8    1000    -4
1000   0    1000    1       7
1000   4      0    1000    1000
2     1000   -5     0      1000
1000  1000  1000    6       0

    k=0

     0       3       8       1000    -4
     1000    0       1000    1       7
     1000    4       0       1000    1000
     2       1000    -5      0       1000
     1000    1000    1000    6       0

     k=1

     0       3       8       1000    -4
     1000    0       1000    1       7
     1000    4       0       1000    996
     2       5       -5      0       -2
     1000    1000    1000    6       0

     k=2

     0       3       8       4       -4
     1000    0       1000    1       7
     1000    4       0       5       11
     2       5       -5      0       -2
     1000    1000    1000    6       0

     k=3

     0       3       8       4       -4
     1000    0       1000    1       7
     1000    4       0       5       11
     2       -1      -5      0       -2
     1000    1000    1000    6       0

     k=4

     0       3       -1      4       -4
     3       0       -4      1       -1
     7       4       0       5       3
     2       -1      -5      0       -2
     8       5       1       6       0

     k=5

     0       1       -3      2       -4
     3       0       -4      1       -1
     7       4       0       5       3
     2       -1      -5      0       -2
     8       5       1       6       0

12 comments:

  1. path reconstruction from wikipedia


    9 procedure GetPath (i,j)
    10 if path[i][j] equals infinity then
    11 return "no path";
    12 int intermediate := next[i][j];
    13 if intermediate equals 'null' then
    14 return " "; /* there is an edge from i to j, with no vertices between */
    15 else
    16 return GetPath(i,intermediate) + intermediate + GetPath(intermediate,j);

    ReplyDelete
  2. i tried to run the loops from i=0 to i<v
    and j=0 to j<v
    but it gave zeroes all along...
    however in principle since each matrix start from (0,0) it should work f9
    can u resolve this..??
    plz reply @ falaksinghal@gmail.com

    ReplyDelete
  3. Replies
    1. Exactly!
      We haven't defined or declared it anywhere...

      Delete
  4. thank u..helped me alot!

    ReplyDelete
  5. min( int a, int b )
    {
    if ( a <= b )
    return a;
    else
    return b;
    } /*End of minimum()*/

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. Thanks buddy... i have to submit my file tomorrow. so because of you i am able to show.thank you very much for all sortings and algorithms.....

    ReplyDelete
  8. Great web site you have got here.. It's difficult to find good
    quality writing like yours nowadays. I really appreciate people like you!
    Take care!!

    forex rebates forum ()

    ReplyDelete