/* Sample solution for CPSC 441 Assignment */ /* Code source credit: thecrazyprogrammer.com */ #include #define MAX 24 #define INFINITY 9999 #define NAME_LENGTH 3 /* #define HOPS_ONLY 1 */ /* #define DEBUG 1 */ void dijkstra(int G[MAX][MAX],int n,int startnode); /* Global variable */ char nodelist[MAX][NAME_LENGTH+1]; int main() { int G[MAX][MAX],i,j,n,u; /* Number of vertices */ scanf("%d",&n); if( n > MAX ) printf("Too many nodes!!!\n"); /* Scan in the city names for each vertex */ for(i=0;i 0 ) printf("Distance from %s to %s is %d\n", nodelist[i], nodelist[j], G[i][j]); #endif #ifdef HOPS_ONLY if( G[i][j] > 0 ) G[i][j] = 1; #endif if( (i > j) && (G[i][j] != G[j][i]) ) printf("Sanity check: G[%d][%d] = %d != G[%d][%d] = %d\n", i, j, G[i][j], j, i, G[j][i]); } } printf("Adjacency matrix sanity check complete...\n"); /* Specify start node */ scanf("%d",&u); /* Determine the route */ dijkstra(G,n,u); return 0; } void dijkstra(int G[MAX][MAX],int n,int startnode) { int cost[MAX][MAX],distance[MAX],pred[MAX]; int visited[MAX],count,mindistance,nextnode,i,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