Professional Documents
Culture Documents
(PCCCS 494)
Assignment – 7
PROBLEM STATEMENT:
Write a program to implement DIJKSTRA’s ALGORITHM .
ALGORITHM:
1 Declaring: function 'Dijkstra' of type void with 'int Graph[MAX][MAX], int n, int start' params
1.3.1.1.1 cost[i][j]=9999
[End of if]
1.3.1.2 ELSE
1.3.1.2.1 cost[i][j]=Graph[i][j]
[End of else]
[End of for]
[End of for]
1.4.1 dist[i]=cost[start][i]
1.4.2 pred[i]=start
1.4.3 visited[i]=0
[End of for]
1.5 dist[start]=0
1.6 visited[start]=1
1.7 count=1
1.8.1 mindist=9999
1.8.2.1.1 mindist=dist[i]
1.8.2.1.2 nextnode=i
[End of if]
[End of for]
1.8.3 visited[nextnode]=0
1.8.4.1.1.2 pred[i]=nextnode
[End of if]
[End of if]
[End of for]
[End of while]
1.9.1.2 j=i
1.9.1.3.1 j=pred[j]
[End of if]
[End of while]
[End of for]
[End of Dijkstra]
2.3 INPUT n,
[End of for]
[End of for]
[End of main]
SOURCE CODE:
#include <stdio.h>
#define MAX 10
if (Graph[i][j] == 0)
cost[i][j] = 9999;
else
cost[i][j] = Graph[i][j];
}
}
dist[i] = cost[start][i];
pred[i] = start;
visited[i] = 0;
dist[start] = 0;
visited[start] = 1;
count = 1;
mindist = 9999;
mindist = dist[i];
nextnode = i;
visited[nextnode] = 0;
if (!visited[i])
pred[i] = nextnode;
count++;
if (i != start)
printf ("\nDist from src to %d: %d\nPath from src : %d", i+1, dist[i], i+1);
j = i;
do
j = pred[j];
while (j != start);
int main ()
int i, j, n, u = 0;
int Graph[MAX][MAX];
printf ("Enter elements of adjacency matrix:\n");
printf ("\n");
OUTPUT:
Enter no.of vertices:
6 2 0 1 4 2 9999
9999 4 1 0 2 9999 4
9999 9999 4 2 0 2 1
[Program finished]