Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Dijkstra's shortest path algorithm
- #include<stdio.h>//edges are undirected and weighted fot the 200 vertex graph
- #include<conio.h>//
- #include<stdlib.h>
- #define size 200
- char c;
- inline int scan(FILE *f1)
- {int n=0;
- c=getc(f1);
- while(!(c>='0'&&c<='9'))
- c=getc(f1);
- while(c>='0'&&c<='9')
- {n=10*n+c-'0';
- c=getc(f1);}
- return n;}
- typedef struct node
- {int v;
- int wt;
- struct node *next;}
- node;//We've to create an adjacency list
- int seen[size];
- int boolean[size];
- int top=-1;
- int pos;
- long int A[size];//This array stores the min path length
- //ie greedy dijkstra value corresponding to the vertex v in v-1th cell
- //-------------------------------------------------------------
- int greedymin(int v,node **Graph)
- {//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])
- // where u is an unexplored vertex.Time complexity is deg(v).
- node *curr;
- int flag=0;
- int min;
- curr=Graph[v-1];
- while(curr!=NULL)
- {if(boolean[curr->v-1]=='n')
- {flag=1;
- min=A[v-1]+curr->wt;//initialising min
- pos=curr->v;
- curr=curr->next;
- break;}
- curr=curr->next;}//this loop is there for initialising min
- if(flag==0)//all vertices connected to v have been explored
- return -1;
- while(curr!=NULL)
- {if(boolean[curr->v-1]=='n'&&min>(A[v-1]+curr->wt))
- {flag=1;
- min=A[v-1]+curr->wt;
- pos=curr->v;}
- curr=curr->next;
- }
- //printf("closest vertex to %d is %d with a distance of %d\n",v,pos,min-A[v-1]);
- return min;
- }
- //-------------------------------------------------------------
- void dijkstra(node **Graph)//we know that the start vertex is 1
- {int flag;
- int min,sum,pos1;
- seen[++top]=1;
- A[0]=0;
- boolean[0]='y';
- do
- {flag=0;
- 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).
- //So time complexity calc is summation over v(deg(v)) where v is a vertex in explored region
- {
- 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
- if(sum==-1)//that means the vertex seen isn't a frontier vertex
- continue;
- else
- flag++;//flag tells us about the frontier vertex.if flag==0 then no frontier vertex graph explored
- if(flag==1)
- {min=sum;
- pos1=pos;
- continue;
- }
- if(min>sum)
- {min=sum;
- pos1=pos;}
- }//end of for.pos1 contains the vertex to be added and min contains the min greedy sum at the frontier
- if(flag!=0)
- {seen[++top]=pos1;
- A[pos1-1]=min;
- boolean[pos1-1]='y';}
- }
- while(flag!=0);//when flag=0 this means the entire graph has been explored.Assumption that the graph given in question is connected.
- //This while loop executes V-1 times since a single vertex is added to this loop after an iteration ends.
- }
- int main(int argv,char* argc[])
- {FILE *fin;
- fin=fopen(argc[1],"r");
- if(fin==NULL)
- {fprintf(stderr,"File not opened");
- return 0;}
- rewind(fin);
- int i,v,wt;
- node **Graph;
- Graph=(node**)malloc(sizeof(node*)*200);
- if(Graph==NULL)
- {fprintf(stderr,"Graph not created");
- return 0;
- }
- for(i=0;i<200;i++)
- {Graph[i]=NULL;
- boolean[i]='n';
- A[i]=1000000;}//initialised the array
- int k=0;
- rewind(fin);
- while(feof(fin)==0)
- {k=scan(fin);
- while(c!='\n'&&c!=EOF)
- {v=scan(fin);
- wt=scan(fin);
- if(Graph[k-1]==NULL)
- {Graph[k-1]=(node*)malloc(sizeof(node));
- Graph[k-1]->v=v;
- Graph[k-1]->wt=wt;
- Graph[k-1]->next=NULL;
- }
- else
- {node *curr;
- curr=(node*)malloc(sizeof(node));
- curr->v=v;
- curr->wt=wt;
- curr->next=Graph[k-1];
- Graph[k-1]=curr;}
- }
- }
- printf("Graph created\n");
- fclose(fin);
- //FILE *fout;
- //fout=fopen("dijkstragraph.txt","w");
- //Graph created.adjacency list created.
- dijkstra(Graph);//it calculates all the greedy values for those vertices which are reachable
- //from the start vertex 1
- //and stores it in the array corresponding to vertex
- //find the shortest distances for vertices 7,37,59,82,99,115,133,165,188,197
- printf("The values of shortest path distance are\n");
- //for(i=0;i<200;i++)
- //printf("%dth vertex is %d distance from 1\n",i+1,A[i]);
- printf("%dth vertex is %d\n",7,A[6]);
- printf("%dth vertex is %d\n",37,A[36]);
- printf("%dth vertex is %d\n",59,A[58]);
- printf("%dth vertex is %d\n",82,A[81]);
- printf("%dth vertex is %d\n",99,A[98]);
- printf("%dth vertex is %d\n",115,A[114]);
- printf("%dth vertex is %d\n",133,A[132]);
- printf("%dth vertex is %d\n",165,A[164]);
- printf("%dth vertex is %d\n",188,A[187]);
- printf("%dth vertex is %d\n",197,A[196]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement