Advertisement
jaskaran_1

Dijkstra Algorithm

Jun 12th, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.62 KB | None | 0 0
  1. //Dijkstra's shortest path algorithm
  2. #include<stdio.h>//edges are undirected and weighted fot the 200 vertex graph
  3. #include<conio.h>//
  4. #include<stdlib.h>
  5. #define size 200
  6. char c;
  7. inline int scan(FILE *f1)
  8. {int n=0;
  9.  c=getc(f1);
  10.  while(!(c>='0'&&c<='9'))
  11.  c=getc(f1);
  12.  while(c>='0'&&c<='9')
  13.  {n=10*n+c-'0';
  14.   c=getc(f1);}
  15. return n;}
  16. typedef struct node
  17. {int v;
  18.  int wt;
  19. struct node *next;}
  20. node;//We've to create an adjacency list
  21. int seen[size];
  22. int boolean[size];
  23. int top=-1;
  24. int pos;
  25. long int A[size];//This array stores the min path length
  26.                  //ie greedy dijkstra value corresponding to the vertex v in v-1th cell
  27.  
  28. //-------------------------------------------------------------
  29. int greedymin(int v,node **Graph)
  30. {//In this function one goes through all the vertices adjacent to v.For all the vertices unexplored in adj[v] we find the one which has the min(A[v]+edgewt[v,u])
  31. // where u is an unexplored vertex.Time complexity is deg(v).
  32. node *curr;
  33. int flag=0;
  34. int min;
  35. curr=Graph[v-1];
  36. while(curr!=NULL)
  37. {if(boolean[curr->v-1]=='n')
  38.  {flag=1;
  39.   min=A[v-1]+curr->wt;//initialising min
  40.   pos=curr->v;
  41.   curr=curr->next;
  42.   break;}
  43. curr=curr->next;}//this loop is there for initialising min
  44. if(flag==0)//all vertices connected to v have been explored
  45. return -1;
  46.  
  47. while(curr!=NULL)
  48. {if(boolean[curr->v-1]=='n'&&min>(A[v-1]+curr->wt))
  49.  {flag=1;
  50.   min=A[v-1]+curr->wt;
  51.   pos=curr->v;}
  52.  curr=curr->next;
  53. }
  54.  
  55. //printf("closest vertex to %d is %d with a distance of %d\n",v,pos,min-A[v-1]);
  56. return min;
  57. }
  58. //-------------------------------------------------------------
  59. void dijkstra(node **Graph)//we know that the start vertex is 1
  60. {int flag;
  61. int min,sum,pos1;
  62. seen[++top]=1;
  63. A[0]=0;
  64. boolean[0]='y';
  65. do
  66. {flag=0;
  67. for(int i=0;i<=top;i++)//This loop goes through every vertex in the explored region.Time complexity is O(E) since for every vertex in the explored region u call greedymin which has time complexity deg(v).
  68. //So time complexity calc is summation over v(deg(v)) where v is a vertex in explored region
  69. {
  70. sum=greedymin(seen[i],Graph);//returns the minimum sum A[seen[i]-1]+edgewt and also the position of the point to which this edge points is stored in pos
  71.  
  72. if(sum==-1)//that means the vertex seen isn't a frontier vertex
  73. continue;
  74. else
  75. flag++;//flag tells us about the frontier vertex.if flag==0 then no frontier vertex graph explored
  76. if(flag==1)
  77. {min=sum;
  78. pos1=pos;
  79. continue;
  80. }
  81. if(min>sum)
  82. {min=sum;
  83. pos1=pos;}
  84. }//end of for.pos1 contains the vertex to be added and min contains the min greedy sum at the frontier
  85. if(flag!=0)
  86. {seen[++top]=pos1;
  87.  A[pos1-1]=min;
  88.  boolean[pos1-1]='y';}
  89. }
  90. while(flag!=0);//when flag=0 this means the entire graph has been explored.Assumption that the graph given in question is connected.
  91.                //This while loop executes V-1 times since a single vertex is added to this loop after an iteration ends.
  92. }
  93.  
  94. int main(int argv,char* argc[])
  95. {FILE *fin;
  96. fin=fopen(argc[1],"r");
  97. if(fin==NULL)
  98. {fprintf(stderr,"File not opened");
  99. return 0;}
  100. rewind(fin);
  101. int i,v,wt;
  102. node **Graph;
  103. Graph=(node**)malloc(sizeof(node*)*200);
  104. if(Graph==NULL)
  105. {fprintf(stderr,"Graph not created");
  106.  return 0;
  107. }
  108. for(i=0;i<200;i++)
  109. {Graph[i]=NULL;
  110.  boolean[i]='n';
  111. A[i]=1000000;}//initialised the array
  112. int k=0;
  113. rewind(fin);
  114.  
  115. while(feof(fin)==0)
  116. {k=scan(fin);
  117.  while(c!='\n'&&c!=EOF)
  118. {v=scan(fin);
  119.  wt=scan(fin);
  120.  
  121.  if(Graph[k-1]==NULL)
  122.  {Graph[k-1]=(node*)malloc(sizeof(node));
  123.   Graph[k-1]->v=v;
  124.   Graph[k-1]->wt=wt;
  125.   Graph[k-1]->next=NULL;
  126.   }
  127.  else
  128.  {node *curr;
  129.   curr=(node*)malloc(sizeof(node));
  130.   curr->v=v;
  131.   curr->wt=wt;
  132.   curr->next=Graph[k-1];
  133.   Graph[k-1]=curr;}
  134.    
  135. }
  136.  
  137. }
  138. printf("Graph created\n");
  139. fclose(fin);
  140. //FILE *fout;
  141. //fout=fopen("dijkstragraph.txt","w");
  142. //Graph created.adjacency list created.
  143. dijkstra(Graph);//it calculates all the greedy values for those vertices which are reachable
  144.                 //from the start vertex 1
  145.                 //and stores it in the array corresponding to vertex
  146.  
  147. //find the shortest distances for vertices 7,37,59,82,99,115,133,165,188,197
  148. printf("The values of shortest path distance are\n");
  149. //for(i=0;i<200;i++)
  150. //printf("%dth vertex is %d distance from 1\n",i+1,A[i]);
  151. printf("%dth vertex is %d\n",7,A[6]);
  152. printf("%dth vertex is %d\n",37,A[36]);
  153. printf("%dth vertex is %d\n",59,A[58]);
  154. printf("%dth vertex is %d\n",82,A[81]);
  155. printf("%dth vertex is %d\n",99,A[98]);
  156. printf("%dth vertex is %d\n",115,A[114]);
  157. printf("%dth vertex is %d\n",133,A[132]);
  158. printf("%dth vertex is %d\n",165,A[164]);
  159. printf("%dth vertex is %d\n",188,A[187]);
  160. printf("%dth vertex is %d\n",197,A[196]);
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement