You are on page 1of 3

#include <stdio.

h> typedef struct { int source,dest,weight; }Edge; Edge edges[1024]; long d[100],d1[100]; /* d[i] is the length of the shortest path between the source (s) and node i */ int nodes,graphtype,i,j,i1,j8,pathmatrix[100][100]; int x, y,status[100],chk[100],edgecount,goout=0; signed int source,dest,netweight,weight1; int i2,j2,k2,leastnode,path[100] [100],visited[100],leastpathvalue,prev,current,nodecount[100],check; int weight,check1,i3,k; void printoutputgraph(void); void getinputdirected(void); void shortpath(void); void computepath(void); //main int main() { for(x = 0; x < 100; x ++) { for(y = 0; y < 100; y ++) pathmatrix[x][y] = 999; } //slecting type of graph printf("welcome! enter directed graph"); getinputdirected(); printoutputgraph(); shortpath(); // exit (0); } //End of main //Directed graph void getinputdirected() { printf("\nenter number of nodes "); scanf("%d",&nodes); edgecount=0; for(i=1;i<=nodes;i++) { for(j=1;j<=nodes;j++) { if (i!=j) { printf("enter 999 for non existing edges\n"); printf("enter edge value from node %d to %d ",i,j); scanf("%d",&pathmatrix[i][j]); if(pathmatrix[i][j]!=999) { edgecount ++; edges[edgecount].source=i; edges[edgecount].dest=j; edges[edgecount].weight=pathmatrix[i][j]; } } } } printf("\n edgecount=%d",edgecount); return; } //print out solution

void printoutputgraph() { printf("\nThe path matrix of given network is: \n"); for(i1=1;i1<=nodes;i1++) { for(j8=1;j8<=nodes;j8++) printf(" %d \t",pathmatrix[i1][j8]); printf("\n"); } return; } //shortestpath void shortpath() { //getting type of output needed printf("\n \nshortest path using Bellman ford Algorithm"); printf("\nenter source node: "); scanf("%d",&source); if(source > nodes || source < 1) { printf("\nwrong entry ,no such nodes exists"); shortpath(); } computepath(); if(goout==1) return; printf("\n \nwant to check more paths ....? if yes press 1 else press other numerical key"); scanf("%d",&check); if(check == 1) shortpath(); return; } void computepath() { for (i = 1; i <=nodes; ++i) { nodecount[i]=0; d[i] = 999; } d[source]=0; //implementing bellman ford for(i=1;i<nodes;i++) {f or(j=1;j<=edgecount;j++) { if(d[edges[j].source]+edges[j].weight<d[edges[j].dest]) { d[edges[j].dest]=d[edges[j].source]+edges[j].weight; nodecount[edges[j].dest]=nodecount[edges[j].source]+1; for(k=1;k< nodecount[edges[j].dest];k++) { path[edges[j].dest][k]=path[edges[j].source][k]; } path[edges[j].dest][nodecount[edges[j].dest]]=edges[j].source; } } } //checking for negative cyclic paths for(i=1;i<=nodes;i++) d1[i]=d[i];

for(j=1;j<=edgecount;j++) { if(d1[edges[j].source]+edges[j].weight<d1[edges[j].dest]) { d1[edges[j].dest]=d1[edges[j].source]+edges[j].weight; } } for(i=1;i<=nodes;i++) { if(d1[i]!=d[i]) { printf("negative cyclic path exists"); goout=1; return; } } printf("\n"); //printing path for(i1=1;i1<=nodes;i1++) { if(i1!=source) printf("from %d to %d",source,i1); else continue; if(nodecount[i1]==0) { printf("\t this node is not reachable\n"); continue; } for(j8=1;j8<=nodecount[i1];j8++) printf(" %d-->",path[i1][j8]); if(i1!=source) { printf(" %d \t",i1); printf("\ttotal pathweight=%ld", d[i1]); printf("\t count=%d",nodecount[i1]); } printf("\n"); } return; }

You might also like