int main(){ Graph G; G = NULL; G = newGraph (6); if(G){ printf("Graph Created\n"); }else{ printf("Graph Not Created\n"); } addEdge(G,0,1); addEdge(G,1,2); addEdge(G,1,3); addEdge(G,3,4); addEdge(G,4,2); doBFS(G,0); printf("%d\n", getDistance(G,0)); printf("%d\n", getDistance(G,1)); printf("%d\n", getDistance(G,2)); printf("%d\n", getDistance(G,3)); printf("%d\n", getDistance(G,4)); printf("%d\n", getDistance(G,5)); printList(stdout,getPathTo(G,4)); /* freeGraph(&G); */ return(0); }
int main(int argc, char** argv) { int numVerts; int numQueries; int from; char edge[1024]; int to; int start; int end; char *filename = argv[1]; FILE *input = fopen(filename, "r"); int i = 0; char *c; if(input != NULL) { fscanf(input, "%d", &numVerts); fscanf(input, "%d", &numQueries); /*printf("%d %d\n", numVerts, numQueries);*/ Graph G = newGraph(numVerts); for(i = 0; i < numVerts; i++) { fscanf(input, "%d", &from); /*printf("%d ", from);*/ fgets(edge, 1024, input); c = strtok(edge, " \n"); while(c != NULL) { addEdge(G, from, atoi(c)); c = strtok(NULL, " \n"); } } for(i = 0; i < numQueries; i++) { fscanf(input, "%d", &start); fscanf(input, "%d", &end); doBFS(G, start); if(getDistance(G, end) > 0) { printf("The shortest path from %d to %d requires %d edges:\n", start, end, getDistance(G, end)); ListHndl L = getPathTo(G, end); } else { printf("No path from %d to %d exists.\n", start, end); } } } fclose(input); return(0); }
/* main program*/ int main(int argc, char *argv[]){ for(int i = 1; i < argc; i++){ // open file and read in numVerts and numQueries FILE* in = fopen(argv[i], "r"); int numVerts, numQueries; char tempLine[1024]; fgets(tempLine, 1024, in); sscanf(tempLine, "%d %d", &numVerts, &numQueries); // read in and make graph Graph Map = newGraph(numVerts); for(int i = 0; i < numVerts; i++){ fgets(tempLine, 1024, in); int vert = atoi(strtok(tempLine, " ")); char *tempTo = strtok(NULL, " "); while(tempTo != NULL){ addEdge(Map, vert, atoi(tempTo)); tempTo = strtok(NULL, " "); } } // process queries for(int i = 0; i < numQueries; i++){ int from, to; fscanf(in, "%d %d", &from, &to); doBFS(Map, from); if(getDistance(Map, to) == -1) printf("No path from %d to %d exists.\n\n", from, to); else{ printf("The shortest path from %d to %d requires %d edges: \n", from, to, getDistance(Map, to)); ListHndl L = getPathTo(Map, to); moveFirst(L); while(!atLast(L)){ printf("%d -> ", getCurrent(L)); moveNext(L); } printf("%d \n\n", getCurrent(L)); freeList(&L); } } /*free everythingggggggggggg*/ freeGraph(&Map); fclose(in); } /* end program */ return 0; }
int main(int argc, char** argv) { Graph G = newGraph(5); addEdge(G, 0, 1); addEdge(G, 0, 2); addEdge(G, 2, 3); addEdge(G, 2, 4); addEdge(G, 3, 4); addEdge(G, 4, 3); addEdge(G, 4, 2); doBFS(G, 0); printf("To get from 0 to 4 requires %d edges.\n",getDistance(G, 0)); ListHndl L = getPathTo(G, 4); printf("Follow the path "); printList(L); return(0); }
int main(int argc, char **argv) { FILE *inFile; char str[20000]; char *token; int nVerts; int nReqs; Graph pathGraph; /* * Validate input file * */ if (argc < 2) { perror("Please specify an input file\n"); return (-1); } inFile = fopen(argv[1], "r"); if (inFile == NULL) { perror("Could not open input file"); return (-1); } /* * Read Input file * */ fgets(str, 60, inFile); strtok(str, "\n"); token = strtok(str, " "); nVerts = atoi( token ); token = strtok(NULL, " "); nReqs = atoi( token ); /* * Build Graph & Insert Edges * */ pathGraph = newGraph( nVerts ); for (int from = 0; from < nVerts; ++from ) { fgets (str, 20000, inFile); strtok(str, "\n"); token = strtok(str, " "); token = strtok (NULL, " "); while( token != NULL) { int to = atoi( token ); insertEdge( pathGraph, from, to); token = strtok (NULL, " "); } } /* * Process requests and find paths * */ for (int from = 0; from < nReqs; ++from ) { int source; int dest; fgets (str, 20000, inFile); strtok(str, "\n"); token = strtok(str, " "); source = atoi( token ); token = strtok(NULL, " "); dest = atoi( token ); doBFS(pathGraph, source); int dist = getDistance( pathGraph, dest); if ( dist > -1) { printf("The shortest path from %d to %d requires %d edge(s):\n", source, dest, dist); ListHndl path = getPathTo( pathGraph, dest); moveFirst( path ); while( !offEnd( path )) { printf("%d ", getCurrent( path)); if ( !atLast( path )) { printf( "-> "); } else { printf("\n\n"); } moveNext( path ); } freeList( path ); } else { printf("No path from %d to %d exists\n\n", source, dest); } } fclose (inFile); freeGraph(pathGraph); return 0; }