Guest User

Untitled

a guest
Aug 3rd, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int front = 0;
  5. int rear = -1;
  6.  
  7. int max_cost = 9999;
  8. int max_vartex = 0;
  9. int max_edge = 0;
  10.  
  11. int start = 0;
  12. int destination = 0;
  13.  
  14. int **graph;
  15. int *cost;
  16. int *stack;
  17. int *visit_list;
  18.  
  19. void show_graph()
  20. {
  21.     for(int i=0; i<max_vartex; i++)
  22.     {
  23.         for(int j=0; j<max_vartex; j++)
  24.             printf("%d ",graph[i][j]);
  25.         printf("\n");
  26.     }
  27. }
  28.  
  29. void push(int v)
  30. {
  31.     stack[++rear] = v;
  32. }
  33.  
  34. int pop()
  35. {
  36.     return stack[rear--];
  37. }
  38.  
  39. bool goal(int v)
  40. {
  41.     if(v == destination) return true;
  42.     else return false;
  43. }
  44.  
  45. void sort_stack()
  46. {
  47.     int i, j;
  48.     for(i=0; i<max_vartex-1; i++)
  49.         for(j=i+1; j<max_vartex; j++)
  50.             if(stack[i] > stack[j])
  51.             {
  52.                 int temp = stack[i];
  53.                 stack[i] = stack[j];
  54.                 stack[j] = temp;
  55.             }
  56. }
  57.  
  58. bool dijkstra()
  59. {
  60.     if(rear < 0) return false;
  61.     else
  62.     {
  63.         int temp_v = pop();
  64.         visit_list[temp_v] = 1;
  65.  
  66.         if(goal(temp_v)) return true;
  67.         else
  68.         {
  69.             for(int i=0; i<max_vartex; i++)
  70.             {
  71.                 if(i != temp_v)
  72.                 {
  73.                     if(graph[temp_v][i] != 0 && cost[i] > (cost[temp_v]+graph[temp_v][i]))
  74.                     {
  75.                         cost[i] = (cost[temp_v]+graph[temp_v][i]);
  76.                         push(i);
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.         sort_stack();
  82.     }
  83.     //dijkstra();
  84.     //return
  85. }
  86.  
  87. int main()
  88. {
  89.     printf("Enter Vertex : ");
  90.     scanf("%d",&max_vartex);
  91.  
  92.     graph = (int **) malloc(max_vartex * sizeof (int *));
  93.     for(int i=0; i<max_vartex; i++)
  94.         graph[i] = (int *) malloc(max_vartex * sizeof (int));
  95.  
  96.     stack = (int *) malloc(max_vartex * sizeof (int));
  97.  
  98.     for(int i=0; i<max_vartex; i++)
  99.     {
  100.         stack[i] = 0;
  101.         visit_list[i] = 0;
  102.         cost[i] = max_cost;
  103.         for(int j=0; j<max_vartex; j++)
  104.             graph[i][j] = 0;
  105.     }
  106.  
  107.     printf("Enter Edge : ");
  108.     scanf("%d",&max_edge);
  109.  
  110.     printf("Enter Value as : Source Destination Cost\n");
  111.     for(int i=0; i<max_edge; i++)
  112.     {
  113.         int s = 0;
  114.         int d = 0;
  115.         int c = 0;
  116.  
  117.         scanf("%d %d %d", &s, &d, &c);
  118.         graph[s][d] = c;
  119.         graph[d][s] = c;
  120.     }
  121.  
  122.     printf("Enter Starting Vartex : ");
  123.     scanf("%d",&start);
  124.     cost[start] = 0;
  125.  
  126.     printf("Enter Destination Vartex : ");
  127.     scanf("%d",&destination);
  128.  
  129.     push(start);
  130.     dijkstra();
  131.  
  132.     show_graph();
  133.  
  134.     printf("Minimum Cost: %d",cost[destination]);
  135.  
  136.     return 0;
  137. }
Add Comment
Please, Sign In to add comment