Advertisement
rootUser

Dijkstra 1

Jul 9th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<limits.h>
  3. #include<stdbool.h>
  4. int Graph[100][100];
  5. int d[100];
  6. int w[100][100];
  7. int path[100];
  8. bool visited[100];
  9. //int queue[100];
  10. int i,j,k,vertex,edge,start,end,Source;
  11. int stack[100], top=-1;
  12.  
  13. void push(int node)
  14. {
  15.     stack[++top] = node;
  16. }
  17.  
  18. int pop()
  19. {
  20.     return (stack[top--]);
  21. }
  22.  
  23. int isEmptyStack()
  24. {
  25.     if(top == -1)
  26.     {
  27.         return 1;
  28.     }
  29.     else
  30.     {
  31.         return 0;
  32.     }
  33. }
  34. void inputGraph()
  35. {
  36.     printf("Enter number of vertex : ");
  37.     scanf("%d",&vertex);
  38.     printf("Enter number of Edge:");
  39.     scanf("%d", &edge);
  40.     for(i=1; i<=edge; i++)
  41.     {
  42.         printf("Enter edge %d: ",i);
  43.         scanf("%d %d",&start,&end);
  44.         Graph[start][end] = 1;
  45.         //Graph[end][start] = 1;
  46.         printf("Weight: ");
  47.         scanf("%d",&w[start][end]);
  48.     }
  49.  
  50.     printf("MATRIX REPRESENTATION OF THE GRAPH:\n");
  51.     for(i=1; i<=vertex; i++)
  52.     {
  53.         for(j=1; j<=vertex; j++)
  54.             printf("%d ",Graph[i][j]);
  55.         printf("\n");
  56.     }
  57. }
  58.  
  59. void inputSource()
  60. {
  61.     printf("\nEnter Source:");
  62.     scanf("%d", &Source);
  63. }
  64.  
  65. void Initilize_Single_Source(int Source)
  66. {
  67.     int j;
  68.     for(j=1; j<=vertex; j++)
  69.     {
  70.         path[j]=0;
  71.         d[j]=9999;
  72.     }
  73.     d[Source]=0;
  74.  
  75. }
  76.  
  77. void Relax(int u,int v)
  78. {
  79.     if(d[v] > d[u]+ w[u][v])
  80.     {
  81.         d[v] = d[u] + w[u][v];
  82.         path[v] = u;
  83.     }
  84. }
  85.  
  86. int Extract_Min()
  87. {
  88.     int min_dis = 9999,unvisitedVertex;
  89.     for(i=1; i<=vertex; i++)
  90.     {
  91.  
  92.         if(!visited[i] && min_dis >= d[i])
  93.         {
  94.  
  95.             min_dis = d[i];
  96.  
  97.             unvisitedVertex = i;
  98.  
  99.         }
  100.     }
  101.     return unvisitedVertex;
  102. }
  103.  
  104. void Dijkstra(int Source)
  105. {
  106.     Initilize_Single_Source(Source);
  107.  
  108.     int i,j,each_vertex=0,u,v;
  109.  
  110.     while(each_vertex<vertex)
  111.     {
  112.         u = Extract_Min();
  113.         visited[u]=true;
  114.         for(i=1; i<=vertex; i++)
  115.         {
  116.             for(j=1; j<=vertex; j++)
  117.             {
  118.                 if(Graph[i][j])
  119.                 {
  120.                     Relax(i,j);
  121.                 }
  122.             }
  123.         }
  124.         each_vertex++;
  125.     }
  126.  
  127.  
  128. }
  129.  
  130. void print()
  131. {
  132.     printf("Cost       : Path\n");
  133.     for(i=1; i<=vertex; i++)
  134.     {
  135.  
  136.         if(d[i]<9999)
  137.             printf("%d          : ",d[i]);
  138.         else
  139.             printf("\nInfinity\n: ");
  140.         push(i);
  141.         while(stack[top]!=0)
  142.         {
  143.             push(path[stack[top]]);
  144.         }
  145.         while(!isEmptyStack())
  146.         {
  147.             if(stack[top]!=0)
  148.                 printf("%d -> ",stack[top]);
  149.             pop();
  150.         }
  151.         printf("End!\n");
  152.     }
  153. }
  154.  
  155. int main()
  156. {
  157.     inputGraph();
  158.     inputSource();
  159.     Dijkstra(Source);
  160.     print();
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement