Created
April 11, 2015 02:24
-
-
Save kartikmaji/4b922c08f7c1ce769dd2 to your computer and use it in GitHub Desktop.
Dijkstra's Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| # define INF 999999 | |
| int vertices,edges; | |
| int check(int queue[vertices]) | |
| { | |
| int i; | |
| for(i=0;i<vertices;i++) | |
| { | |
| if(queue[i]==1) | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| int findshortest(int distance[vertices],int queue[vertices]) | |
| { | |
| int i,j; | |
| int min=INF; | |
| for(i=0;i<vertices;i++) | |
| { | |
| printf("queue=%d",queue[i]); | |
| } | |
| printf("\n"); | |
| /*for(i=0;i<vertices;i++) | |
| { | |
| for(j=0;j<vertices;j++) | |
| { | |
| if(queue[i]==1) | |
| if(graph[i][j]<min && i!=j) | |
| { | |
| printf("i=%d, j=%d\n",i,j); | |
| //if(queue[i]==1) | |
| { | |
| min=graph[i][j]; | |
| //printf(""); | |
| //printf("queue=%d",queue[i]); | |
| } | |
| } | |
| } | |
| }*/ | |
| for(i=0;i<vertices;i++) | |
| printf("%d ",distance[i]); | |
| printf("\n"); | |
| for(i=0;i<vertices;i++) | |
| { | |
| if(queue[i]==1 && distance[i]<min) | |
| min = i; | |
| } | |
| printf("min=%d\n",min); | |
| return min; | |
| } | |
| void dijkstra(int graph[vertices][vertices],int distance[vertices],int source) | |
| { | |
| //printf("hello\n"); | |
| int i,j,u; | |
| int perv[vertices]; | |
| for(i=0;i<vertices;i++) | |
| { | |
| perv[i]=-1; | |
| distance[i]=INF; | |
| } | |
| distance[source] = 0; | |
| int queue[vertices]; | |
| for(i=0;i<vertices;i++) | |
| { | |
| //printf("hello1\n"); | |
| if(i!=source) | |
| { | |
| distance[i]=INF; | |
| perv[i]=-1; | |
| } | |
| if(i==source) | |
| { | |
| distance[i]=0; | |
| perv[i]=-1; | |
| } | |
| queue[i]=1; | |
| printf("%d ",distance[i]); | |
| } | |
| int alt; | |
| j=0; | |
| while(j++<vertices) | |
| { | |
| //printf("hello2\n"); | |
| if(queue[source]==1) | |
| { | |
| printf("Source\n"); | |
| u=source; | |
| queue[source]=0; | |
| } | |
| else | |
| { | |
| printf("not source\n"); | |
| u=findshortest(distance,queue); | |
| queue[u]=0; | |
| //printf("u=%d\n",u); | |
| } | |
| for(i=0;i<vertices;i++) | |
| { | |
| if(graph[u][i]!=INF) | |
| { | |
| alt = distance[u] + graph[u][i]; | |
| if(alt<distance[i]) | |
| { | |
| distance[i]=alt; | |
| perv[i] = u; | |
| } | |
| } | |
| } | |
| } | |
| for(i=0;i<vertices;i++) | |
| { | |
| printf("%d ",distance[i]); | |
| } | |
| } | |
| int main() | |
| { | |
| scanf("%d %d",&vertices,&edges); | |
| int graph[vertices][vertices]; | |
| int distance[vertices]; | |
| int i,j; | |
| for(i=0;i<vertices;i++) | |
| for(j=0;j<vertices;j++) | |
| { | |
| if(i==j) | |
| graph[i][j] = 0; | |
| else | |
| graph[i][j] = INF; | |
| } | |
| int a,b,c; | |
| for(i=0;i<edges;i++) | |
| { | |
| scanf("%d %d %d",&a,&b,&c); | |
| graph[a][b] = c; | |
| } | |
| for(i=0;i<4;i++) | |
| { | |
| for(j=0;j<4;j++) | |
| { | |
| printf("%d ",graph[i][j]); | |
| } | |
| printf("\n"); | |
| } | |
| dijkstra(graph,distance,0); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment