Question
Hey I don't know what I did wrong here I want it to only print 4 #include #include #define INFINITY 9999 #define MAX 10 void
Hey I don't know what I did wrong here I want it to only print 4
#include
#include
#define INFINITY 9999
#define MAX 10
void dijkstra(int graph[MAX][MAX],int length,int startnode)
{
int cost[MAX][MAX];
int distance[MAX];
int pred[MAX];
int path[MAX];
int visited[MAX];
int count;
int mindistance;
int nextnode;
int i;
int j;
//pred[] stores the predecessor of each node
//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i for(j=0;j if(graph[i][j]==0) cost[i][j]=INFINITY; else cost[i][j]=graph[i][j]; //initialize pred[],distance[] and visited[] for(i=0;i { distance[i]=cost[startnode][i]; pred[i]=startnode; visited[i]=0; } distance[startnode]=0; visited[startnode]=1; count=1; while(count { mindistance=INFINITY; /extnode gives the node at minimum distance for(i=0;i if(distance[i] { mindistance=distance[i]; nextnode=i; } //check if a better path exists through nextnode visited[nextnode]=1; for(i=0;i if(!visited[i]) if(mindistance+cost[nextnode][i] { distance[i]=mindistance+cost[nextnode][i]; pred[i]=nextnode; } count++; } //print the path and distance of each node for(i=0;i if(i != startnode){ scanf("%d", &distance[i]); scanf("%d", &visited[i]); scanf("%d", &i); j=i; do{ scanf("%d", &pred[j]); }while(j != startnode); } printf(" The shortest path is : "); for(i=0; i printf("%d ", pred[i]); } printf(" The visited path : "); for(i=length - 1; i >= 0; i--){ printf("%d ", visited[i]); } printf(" The distance of each node is : "); for(i=0; i printf("%d ", distance[i]); } } printf(" "); //print the path and distance of each node /*for(i=0;i if(i!=startnode) { printf(" Distance of node%d = %d",i,distance[i]); printf(" The visited node = %d", visited[i]); printf(" Path=%d",i); j=i; do{ j=pred[j]; printf(" }while(j != startnode); } }*/ } void dijkstra(int graph[MAX][MAX],int length,int startnode); int main() { int graph[MAX][MAX]; int i; int j; int length; int Snode; printf("Enter length of the vertices:"); scanf("%d",&length); printf(" Enter the adjacency matrix: "); for(i=0;i for(j=0;j scanf("%d",&graph[i][j]); printf(" Enter the starting node:"); scanf("%d",&Snode); dijkstra(graph,length,Snode); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started