-
-
Save yuvipanda/609263 to your computer and use it in GitHub Desktop.
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 infi 999 | |
int mat[20][20], dist[20], path[20]; | |
int n,i,j; | |
int q[100]; | |
int front=-1, rear=-1; | |
int isempty() | |
{ | |
if ((front == -1 && rear == -1) || (front > rear)) | |
{ | |
front = rear = -1; | |
return 1; | |
} | |
return 0; | |
} | |
void push(int a) | |
{ | |
if (isempty()) | |
front++; | |
q[++rear] = a; | |
}; | |
int del() | |
{ | |
return q[front++]; | |
} | |
void input() | |
{ | |
printf("Enter number of nodes:\n"); | |
scanf("%d", &n); | |
printf( "\n\nEnter adjacency matrix\n"); | |
for (i = 1;i <= n;i++) | |
{ | |
printf( " enter the weights of row %d : ",i); | |
for ( j = 1;j <= n;j++) | |
scanf("%d", &mat[i][j]); | |
} | |
} | |
void init(int m) | |
{ | |
push(m); | |
dist[m] = 0; | |
for (i = 1;i <= n;i++) | |
{ | |
if (i != m) | |
{ | |
push(i); | |
dist[i] = infi; | |
} | |
path[i] = 0; | |
} | |
} | |
void min_dist(int m) | |
{ | |
int v, w; | |
init(m); | |
while (!(isempty())) | |
{ | |
v = del(); | |
printf( "this is v del %d", v); | |
for (i = 1;i <= n;i++) | |
{ | |
if (mat[v][i] != 0) | |
{ | |
w = i; | |
if ((dist[v] + mat[v][w]) < (dist[w])) | |
{ | |
dist[w] = dist[v] + mat[v][w]; | |
path[w] = v; | |
} | |
} | |
} | |
} | |
} | |
void disp_path(int m) | |
{ | |
int p = path[m]; | |
if (p == 0) | |
return; | |
disp_path(p); | |
printf(" %d",m); | |
} | |
void disp_dist(int m) | |
{ | |
printf( "Cost: %d", dist[m]); | |
} | |
int main() | |
{ | |
int startnode,endnode; | |
char answer; | |
input(); | |
do | |
{ | |
printf( "start node: "); | |
scanf("%d", &startnode); | |
printf( "end node: "); | |
scanf("%d", &endnode); | |
min_dist(startnode); | |
printf("\nFrom %d to %d\n", startnode, endnode); | |
printf("------------\n"); | |
printf("Minimum distance route: (%d", startnode); | |
disp_path(endnode); | |
printf(")\n"); | |
disp_dist(endnode); | |
printf( "Do you want to calculate another path (Y/N)? "); | |
scanf("%c", &answer); | |
} while (answer != 'n'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment