i am a beginner, guys plizzzzzzzz help me. i am given a assignment to find short paths either using dijkstra algorithm or using binary search algorithm in linux OS.
check out what is wrong the code i tried
/program to find shortest path
check out what is wrong the code i tried
/program to find shortest path
Code:
#include<stdio.h> #include<cstdlib> #include<cstring> #include<fstream> #include<iostream.h> #define member 1 // use to indicate current node #define nonmember 0 #define infinity 999 // intial distance struct arc { int wt; }; struct arc g[10][10]; int path[10]; //end of global defintn void jointwt(struct arc g[][10], int n1, int n2 ,int wt) // function to assign the weight on arcs { g[n1][n2].wt = wt; // assign the weight to arc n1 -n2 } // end of function joint cost(struct arc c[][10], int n) // function to get the weights { int i,j,wt; for(i=0;i<n;i++) for(j=0;j<n;j++) { printf("\n weight of arc %d %d", i,j); scanf("%d",&wt); jointwt(c,i,j,wt); } printf("\n"); } // end of function mat(struct arc a[][10], int n) // function to print the weight in matrx { int i,j; printf("\n\t"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d",a[i][j].wt); } printf("\n\t"); } } //end of mat shortpath(int wt, int s, int t, int proced, int n) struct arc wt[][10]; int s,t,proced[10]; int n; { int dist[10], perm[10]; int curr, i,k,dc,h=0; int smalldist, newdist; for(i=0;i<n;i++) { perm[i] = nonmember; dist[i] =infinity; } perm[s] = member; dist[s] = 0; curr = s; while(curr != t) { smalldist = infinity; dc = dist[curr]; for(i=0;i<n;i++) if(perm[i] == nonmember) { newdist = dc + wt[curr][i].wt; if(newdist<smalldist) { dist[i] = newdist; proced[i] = curr; } if(dist[i]<smalldist) { smalldist = dist[i]; k=i; } } curr = k; perm[curr] = member; path[h++] = curr; } printf("\n\t path is = %d", s+1); for(i=0;i<h;i++) printf("-> %d",path[i]+1); return(dist[t]); } // end of short path /*int cost(struct arc , int ); int mat(struct arc , int ); void jointwt(struct arc , int , int ,int ); int shortpath(int , int , int , int , int );*/ int main() { struct arc c[10][10]; int n,s,t,proced[10]; int x; printf("\n\t\t How many nodes :"); scanf("%d",&n); printf("\n\t\t Enter the adjacency matrix of weight"); printf("\n\t\t Enter 999 if no edge exits :"); cost(c,n); printf("\n\t\t Weight matrix :"); mat(c,n); printf("\n\t\t Enter source and distnation :"); scanf("%d %d",&s, &t); printf("\n\t\t shortest path from %d to %d is:\n",s,t); x = shortpath(c, s - 1, t - 1, proced, n); } //efmain
Comment