Advertisement
rootUser

Bellman-Ford 1

Jul 9th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdbool.h>
  3. #include<stdlib.h>
  4. #include<limits.h>
  5.  
  6. int edge,vertex,source,top=-1;
  7. int adjacency_matrix[100][100],weight_matrix[100][100];
  8. int parent[100],cost[100],stack[100];
  9.  
  10. void push(int);
  11. int pop();
  12. int isEmptyStack();
  13. void inputGraph();
  14. void inputSource();
  15. void initilizeSingleSource(int);
  16. void relax(int,int);
  17. bool bellmanFord(int);
  18. void printCostAndPath(bool);
  19.  
  20. int main(void)
  21. {
  22.     inputGraph();
  23.     inputSource();
  24.     bool isPossible = bellmanFord(source);
  25.     printCostAndPath(isPossible);
  26.     return 0;
  27. }
  28.  
  29. void push(int node)
  30. {
  31.     stack[++top]=node;
  32. }
  33. int pop()
  34. {
  35.     return stack[top--];
  36. }
  37. int isEmptyStack()
  38. {
  39.     if(top==-1)
  40.     {
  41.         return 1;
  42.     }
  43.     return 0;
  44. }
  45. void inputGraph()
  46. {
  47.     printf("Enter total vertex : ");
  48.     scanf("%d",&vertex);
  49.     printf("Enter total edge   : ");
  50.     scanf("%d",&edge);
  51.     int i,j,start,end;
  52.     for(i=1; i<=edge; i++)
  53.     {
  54.         printf("Enter edges %d 's start and end vertex : ",i);
  55.         scanf("%d %d",&start,&end);
  56.         // optional edit needed : this is for non - directed graph
  57.         // so both [start][end] = [end][start] = 1 double assign
  58.         // scan for each vertex separately
  59.         adjacency_matrix[start][end]=1;
  60.         //adjacency_matrix[end][start]=1;
  61.         printf("Enter weight for edge %d : ",i);
  62.         scanf("%d",&weight_matrix[start][end]);
  63.         //edit needed : add this line
  64.         //weight_matrix[end][start] = weight_matrix[start][end]
  65.     }
  66.     printf("Adjacency matrix : \n");
  67.     for(i=1; i<=vertex; i++)
  68.     {
  69.         for(j=1; j<=vertex; j++)
  70.         {
  71.             printf("%d ",adjacency_matrix[i][j]);
  72.         }
  73.         printf("\n");
  74.     }
  75.     printf("Weight matrix : \n");
  76.     for(i=1; i<=vertex; i++)
  77.     {
  78.         for(j=1; j<=vertex; j++)
  79.         {
  80.             printf("%d ",weight_matrix[i][j]);
  81.         }
  82.         printf("\n");
  83.     }
  84. }
  85. void inputSource()
  86. {
  87.     //edit needed : this code does not work if source is not 1
  88.     printf("\nEnter source node (1 to %d) : ",vertex);
  89.     scanf("%d", &source);
  90. }
  91. void initilizeSingleSource(int Source)
  92. {
  93.     int j;
  94.     for(j=1; j<=vertex; j++)
  95.     {
  96.         parent[j]=0;
  97.         cost[j]=999999;
  98.     }
  99.     cost[Source]=0;
  100. }
  101. void relax(int u,int v)
  102. {
  103.     if(cost[u]+ weight_matrix[u][v]<cost[v])
  104.     {
  105.         cost[v] = cost[u] + weight_matrix[u][v];
  106.         parent[v] = u;
  107.     }
  108. }
  109. bool bellmanFord(int sourceNode)
  110. {
  111.     int i,j,k;
  112.     initilizeSingleSource(sourceNode);
  113.     for(i=1; i<vertex; i++)
  114.     {
  115.         for(j=1; j<=vertex; j++)
  116.         {
  117.             for(k=1; k<=vertex; k++)
  118.             {
  119.                 // !=0
  120.                 if(adjacency_matrix[j][k])
  121.                 {
  122.                     relax(j,k);
  123.                 }
  124.             }
  125.         }
  126.     }
  127.  
  128.     for(j=1; j<=vertex; j++)
  129.     {
  130.         for(k=1; k<=vertex; k++)
  131.         {
  132.             // !=0
  133.             if(adjacency_matrix[j][k])
  134.             {
  135.                 if(cost[j]+weight_matrix[j][k]<cost[k])
  136.                 {
  137.                     return false;
  138.                 }
  139.             }
  140.         }
  141.     }
  142.     return true;
  143. }
  144.  
  145. void printCostAndPath(bool possibleOrNot)
  146. {
  147.     if(possibleOrNot==true)
  148.     {
  149.         int i;
  150.         printf("\nBellman Ford function returns 'True'\n");
  151.         printf("Cost       : Path\n");
  152.         for(i=1; i<=vertex; i++)
  153.         {
  154.  
  155.             if(cost[i]<999999)
  156.                 printf("%d          : ",cost[i]);
  157.             else
  158.                 printf("\nInfinity\n:");
  159.             push(i);
  160.             while(stack[top]!=0)
  161.             {
  162.                 push(parent[stack[top]]);
  163.             }
  164.             while(!isEmptyStack())
  165.             {
  166.                 if(stack[top]!=0)
  167.                     printf("%d -> ",stack[top]);
  168.                 pop();
  169.             }
  170.             printf("End!\n");
  171.         }
  172.     }
  173.     else
  174.     {
  175.         printf("\nBellman Ford function returns 'False'\n");
  176.         printf("This Graph has no shortest path\n");
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement