Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

#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);

}

a algorithms Upgrade 8+ Share + ojects Files FSymbols Workspace ADS Program 1 Sources 57 58 59 60 61 if(mindistance+cost[nextnode] [i]= 0; i--){ printf("%d", visited[i]); Enter the graph: 0 10 0 30 100 10 0 50 00 0 50 0 20 10 30 0 20 0 60 100 0 10 60 0 83 84 85 Enter the starting node:0 86 87 printf(" The distance of each node is : "); for(i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

7. Where Do We Begin?

Answered: 1 week ago