Dijkstra prg
Dijkstra prg
Program:-
#include <stdio.h>
#define MAX 15
#define INFINITY 20
void create_graph();
void display();
void path();
int main() {
int i;
create_graph();
scanf("%d", &s);
dij(s);
path();
return 0;
void create_graph() {
int i, j;
scanf("%d", &n);
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = INFINITY;
void dij(int v) {
permanent[i] = 0;
dist[i] = cost[v][i];
if (dist[i] != INFINITY)
prefix[i] = v;
permanent[v] = 1;
dist[v] = 0;
min = INFINITY;
min = dist[w];
u = w;
permanent[u] = 1;
prefix[w] = u;
}
}
void display() {
int i;
void path() {
int i, j;
j = prefix[i];
while (j != 0) {
j = prefix[j];
printf("\n");
Output:-
Enter the no.of nodes: 3
123
215
123
Shortest paths:
2->1 via 2 2
2->2 via 2 0
2->3 via 2 5
1 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <--
2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <--
2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <--
2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <--
2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <--
2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <--
2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <-- 2 <