c - I am trying to implement dijkstra's algorithm for a class -
i supposed 0-3-6-5
output cost. -1-0-3-1
output of previous array. , 1-1-1-1
visit array.
i getting 0-3-7-5
output in cost , -1-0-1-1
previous. please if can.
i have tried see 7 comes when should 6 , not figuring out. first time have coded in c language might seem kind of sloppy.
#include <stdlib.h> #include <stdio.h> #include <math.h> #define infinity 999 int main (void){ int dij[4][4] = {{0,3,8,6}, {3,0,4,2}, {8,4,0,1}, {6,2,1,0}}; int visit[4]; int cost[4]; int previous[4]; //filling visit, cost, previous arrays for(int j=0; j<4; j++){ visit[j] = 0; cost[j] = infinity; previous[j] = -1; }//adding values arrays //node on cost[0] = 0; //first position in cost array set 0 int counter = 0; //counter while loop int currentrow = 0; //checks rows holding th smallest value in dij array while(counter < 4){ int min = infinity; //min value set infinity @ beginning of program for(int y=0; y<4; y++){ //if cost @ current position in th cost array < min , node not visited if(cost[y] < min && visit[y] == 0){ min = cost[y]; currentrow = y; }//if visit[currentrow] = 1; }//for loop col of dij array. //loop @ cost array find lowest cost unvisited node , set row index value for(int x=0; x<4; x++){ if(visit[x] != 1){ if(min + dij[currentrow][x] < cost[x]){ cost[x] = min + dij[currentrow][x]; previous[x] = currentrow; } } } counter++; }//while loop x column of dij array.
your visit flag must outside of statements. because visit flag must on iteration time.
for(int y=0; y<4; y++){ if(cost[y] <= min && visit[y] == 0){ min = cost[y]; currentrow = y; }//if //<-- not here }//for loop col of dij array. visit[currentrow] = 1;
here full source code print values.
#include <stdlib.h> #include <stdio.h> #include <math.h> #define infinity 999 int main (void) { int dij[4][4] = { {0,3,8,6}, {3,0,4,2}, {8,4,0,1}, {6,2,1,0} }; int visit[4]; int cost[4]; int previous[4]; //filling visit, cost, previous arrays for(int j=0; j<4; j++){ visit[j] = 0; cost[j] = infinity; previous[j] = -1; }//adding values arrays //node on cost[0] = 0; //first position in cost array set 0 int counter = 0; //counter while loop int currentrow = 0; //checks rows holding th smallest value in dij array while(counter < 4) { int min = infinity; //min value set infinity @ beginning of program for(int y=0; y<4; y++){ //if cost @ current position in th cost array < min , node not visited if(cost[y] <= min && visit[y] == 0){ min = cost[y]; currentrow = y; }//if }//for loop col of dij array. visit[currentrow] = 1; //loop @ cost array find lowest cost unvisited node , set row index value for(int x=0; x<4; x++){ if(visit[x] != 1 && cost[currentrow] + dij[currentrow][x] < cost[x] && cost[currentrow] != infinity ) { //if(min + dij[currentrow][x] < cost[x]) { cost[x] = cost[currentrow] + dij[currentrow][x]; previous[x] = currentrow; } } } counter++; }//while loop x column of dij array. printf("visit cost previous \n"); for(int j=0; j<4; j++){ printf("%d \t %d \t %d \n", visit[j], cost[j], previous[j]); }//adding values arrays return 0; }
the output should follows,
visit cost previous 1 0 -1 1 3 0 1 6 3 1 5 1
have nice day~~
Comments
Post a Comment