/** writes problem to file */ SCIP_RETCODE SCIPwriteCcg( SCIP* scip, /**< SCIP data structure */ FILE* file, /**< output file, or NULL if standard output should be used */ const char* name, /**< problem name */ SCIP_Bool transformed, /**< TRUE iff problem is the transformed problem */ SCIP_VAR** vars, /**< array with active variables ordered binary, integer, implicit, continuous */ int nvars, /**< number of mutable variables in the problem */ SCIP_CONS** conss, /**< array with constraints of the problem */ int nconss, /**< number of constraints in the problem */ SCIP_RESULT* result /**< pointer to store the result of the file writing call */ ) { /*lint --e{715}*/ int c; int v; int i; SCIP_CONSHDLR* conshdlr; const char* conshdlrname; SCIP_CONS* cons; SCIP_VAR** consvars; SCIP_Real* consvals; int nconsvars; SparseGraph G; assert( scip != NULL ); assert( nvars >= 0 ); /* initialize graph */ SCIP_CALL( initGraph(scip, &G, (unsigned int) nvars, 10) ); /* check all constraints */ for( c = 0; c < nconss; ++c) { cons = conss[c]; assert( cons != NULL); /* in case the transformed is written only constraint are posted which are enabled in the current node */ assert(!transformed || SCIPconsIsEnabled(cons)); conshdlr = SCIPconsGetHdlr(cons); assert( conshdlr != NULL ); conshdlrname = SCIPconshdlrGetName(conshdlr); assert( transformed == SCIPconsIsTransformed(cons) ); if( strcmp(conshdlrname, "linear") == 0 ) { consvars = SCIPgetVarsLinear(scip, cons); nconsvars = SCIPgetNVarsLinear(scip, cons); assert( consvars != NULL || nconsvars == 0 ); if( nconsvars > 0 ) { SCIP_CALL( handleLinearCons(scip, SCIPgetVarsLinear(scip, cons), SCIPgetValsLinear(scip, cons), SCIPgetNVarsLinear(scip, cons), transformed, &G) ); } } else if( strcmp(conshdlrname, "setppc") == 0 ) { consvars = SCIPgetVarsSetppc(scip, cons); nconsvars = SCIPgetNVarsSetppc(scip, cons); assert( consvars != NULL || nconsvars == 0 ); if( nconsvars > 0 ) { SCIP_CALL( handleLinearCons(scip, consvars, NULL, nconsvars, transformed, &G) ); } } else if( strcmp(conshdlrname, "logicor") == 0 ) { consvars = SCIPgetVarsLogicor(scip, cons); nconsvars = SCIPgetNVarsLogicor(scip, cons); assert( consvars != NULL || nconsvars == 0 ); if( nconsvars > 0 ) { SCIP_CALL( handleLinearCons(scip, SCIPgetVarsLogicor(scip, cons), NULL, SCIPgetNVarsLogicor(scip, cons), transformed, &G) ); } } else if( strcmp(conshdlrname, "knapsack") == 0 ) { SCIP_Longint* w; consvars = SCIPgetVarsKnapsack(scip, cons); nconsvars = SCIPgetNVarsKnapsack(scip, cons); assert( consvars != NULL || nconsvars == 0 ); /* copy Longint array to SCIP_Real array */ w = SCIPgetWeightsKnapsack(scip, cons); SCIP_CALL( SCIPallocBufferArray(scip, &consvals, nconsvars) ); for( v = 0; v < nconsvars; ++v ) consvals[v] = (SCIP_Real)w[v]; if( nconsvars > 0 ) { SCIP_CALL( handleLinearCons(scip, consvars, consvals, nconsvars, transformed, &G) ); } SCIPfreeBufferArray(scip, &consvals); } else if( strcmp(conshdlrname, "varbound") == 0 ) { SCIP_CALL( SCIPallocBufferArray(scip, &consvars, 2) ); SCIP_CALL( SCIPallocBufferArray(scip, &consvals, 2) ); consvars[0] = SCIPgetVarVarbound(scip, cons); consvars[1] = SCIPgetVbdvarVarbound(scip, cons); consvals[0] = 1.0; consvals[1] = SCIPgetVbdcoefVarbound(scip, cons); SCIP_CALL( handleLinearCons(scip, consvars, consvals, 2, transformed, &G) ); SCIPfreeBufferArray(scip, &consvars); SCIPfreeBufferArray(scip, &consvals); } else { SCIPwarningMessage(scip, "constraint handler <%s> cannot print requested format\n", conshdlrname ); SCIPinfoMessage(scip, file, "\\ "); SCIP_CALL( SCIPprintCons(scip, cons, file) ); SCIPinfoMessage(scip, file, ";\n"); } } /* output graph */ SCIPinfoMessage(scip, file, "c graph generated from %s\n", name); SCIPinfoMessage(scip, file, "p edge %d %d\n", nvars, G.m); for( i = 0; i < nvars; ++i ) { unsigned int k; int a; k = 0; a = G.A[i][k]; while( a >= 0 ) { /* only output edges from lower to higher number */ if( i < a ) { /* note: node numbers start with 1 in the DIMACS format */ SCIPinfoMessage(scip, file, "e %d %d %f\n", i+1, a+1, G.W[i][k]); } a = G.A[i][++k]; assert( k <= G.size[i] ); } assert( k == G.deg[i] ); } freeGraph(scip, &G); *result = SCIP_SUCCESS; return SCIP_OKAY; }
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; }
int main(int argc, const char * argv[]) { char buffer[MAX_LEN]; //checks for correct usage if( argc != 3 ){ printf("Usage: Lex <input file> <output file>\n"); exit(1); } // Check to see if input is open // opens the file FILE* input = fopen(argv[1], "r"); FILE* output = fopen(argv[2], "w"); // checks if files have been open and or created if(input == NULL){ printf("Unable to open file %s\n", argv[1]); return 1; } else if (output == NULL){ printf("Unable to open file %s\n", argv[2]); return 1; } char numOfVerticesChar[MAX_LEN]; int numOfVertices; fgets (numOfVerticesChar, MAX_LEN, input); numOfVertices = atoi(numOfVerticesChar); Graph graph = newGraph(numOfVertices); while( fgets(buffer, MAX_LEN, input) != NULL) { int numOne = 0; int numTwo = 0; sscanf(buffer, "%d %d", &numOne, &numTwo); if(numOne == 0 && numTwo == 0) break; addArc(graph, numOne, numTwo); } fprintf(output, "Adjacency list representation of G:\n"); printGraph(output, graph); fprintf(output,"\n"); List S = newList(); int i; for(i = 1; i <=numOfVertices; i++) { append(S, i); } DFS(graph, S); Graph graphTrans = transpose(graph); DFS(graphTrans, S); List connectedComp = newList(); int counter = 0; for(moveTo(S,length(S)-1); getIndex(S) != -1; movePrev(S)) { prepend(connectedComp, getElement(S)); if(getParent(graphTrans,getElement(S)) == 0) { counter ++; clear(connectedComp); } } fprintf(output, "G contains %i strongly connected components: \n", counter); counter = 0; for(moveTo(S,length(S)-1); getIndex(S) != -1; movePrev(S)) { prepend(connectedComp, getElement(S)); if(getParent(graphTrans,getElement(S)) == 0) { counter ++; fprintf(output, "Component %i: ", counter); for(moveTo(connectedComp,0); getIndex(connectedComp) != -1; moveNext(connectedComp)) { fprintf(output, "%d ",getElement(connectedComp)); } clear(connectedComp); fprintf(output, "\n"); } } fclose(input); fclose(output); freeGraph(&graphTrans); freeGraph(&graph); freeList(&S); freeList(&connectedComp); return 0; }
int spadAction(void) { int code,viewCommand; float f1,f2; int i1,i2,i3,viewGoAhead; static int ack = 1; if (viewAloned==yes) { close(0); return(-1); } readViewman(&viewCommand,intSize); switch (viewCommand) { case hideControl2D: readViewman(&i1,intSize); if (i1) { /* show control panel */ if (viewport->haveControl) XUnmapWindow(dsply,control->controlWindow); putControlPanelSomewhere(someInt); } else { /* turn off control panel */ if (viewport->haveControl) { viewport->haveControl = no; XUnmapWindow(dsply,control->controlWindow); } } break; case changeTitle: readViewman(&i1,intSize); readViewman(viewport->title,i1); viewport->title[i1] = '\0'; writeTitle(); writeControlTitle(); XFlush(dsply); spadDraw=no; break; case writeView: readViewman(&i1,intSize); readViewman(filename,i1); filename[i1] = '\0'; sprintf(errorStr,"writing of viewport data"); i3 = 0; readViewman(&i2,intSize); while (i2) { i3 = i3 | (1<<i2); readViewman(&i2,intSize); } if (writeViewport(i3) < 0) fprintf(stderr," Nothing was written\n"); break; case closeAll2D: code = check(write(Socket,&ack,intSize)); goodbye(-1); case ps2D: readViewman(&i1,intSize); buttonAction(viewCommand); break; case axesOnOff2D: readViewman(&i1,intSize); i1--; readViewman(&i2,intSize); graphStateArray[i1].axesOn = i2; if (graphStateArray[i1].showing) spadDraw=yes; break; case axesColor2D: readViewman(&i1,intSize); i1--; readViewman(&i2,intSize); graphStateArray[i1].axesColor = i2; if (graphStateArray[i1].showing) spadDraw=yes; break; case unitsOnOff2D: readViewman(&i1,intSize); i1--; readViewman(&i2,intSize); graphStateArray[i1].unitsOn = i2; if (graphStateArray[i1].showing) spadDraw=yes; break; case unitsColor2D: readViewman(&i1,intSize); i1--; readViewman(&i2,intSize); graphStateArray[i1].unitsColor = i2; if (graphStateArray[i1].showing) spadDraw=yes; break; case connectOnOff: readViewman(&i1,intSize); i1--; readViewman(&i2,intSize); graphStateArray[i1].connectOn = i2; if (graphStateArray[i1].showing) spadDraw=yes; break; case pointsOnOff: readViewman(&i1,intSize); i1--; readViewman(&i2,intSize); graphStateArray[i1].pointsOn = i2; if (graphStateArray[i1].showing) spadDraw=yes; break; case spline2D: readViewman(&i1,intSize); i1--; readViewman(&i2,intSize); graphStateArray[i1].splineOn = i2; if (graphStateArray[i1].showing) spadDraw=yes; break; case showing2D: readViewman(&i1,intSize); i1--; readViewman(&i2,intSize); /* simulate a button press to turn display number on/off */ graphStateArray[i1].showing = !i2; clickedOnGraph(i1,i1+graphStart); break; case scale2D: readViewman(&i1,intSize); i1--; /* passed index is [1..9] but internal representation is [0..8] */ readViewman(&f1,floatSize); readViewman(&f2,floatSize); graphStateArray[i1].scaleX = f1; graphStateArray[i1].scaleY = f2; if (graphStateArray[i1].scaleX > maxScale) graphStateArray[i1].scaleX = maxScale; else if (graphStateArray[i1].scaleX < minScale) graphStateArray[i1].scaleX = minScale; if (graphStateArray[i1].scaleY > maxScale) graphStateArray[i1].scaleY = maxScale; else if (graphStateArray[i1].scaleY < minScale) graphStateArray[i1].scaleY = minScale; if (graphStateArray[i1].showing) spadDraw=yes; break; /* scale2D */ case translate2D: readViewman(&i1,intSize); i1--; /* passed index is [1..9] but internal representation is [0..8] */ readViewman(&f1,floatSize); readViewman(&f2,floatSize); graphStateArray[i1].centerX = f1; graphStateArray[i1].centerY = f2; if (graphStateArray[i1].centerX > maxDelta) graphStateArray[i1].centerX = maxDelta; else if (graphStateArray[i1].centerX < -maxDelta) graphStateArray[i1].centerX = maxDelta; if (graphStateArray[i1].centerY > maxDelta) graphStateArray[i1].centerY = maxDelta; else if (graphStateArray[i1].centerY < -maxDelta) graphStateArray[i1].centerY = maxDelta; if (graphStateArray[i1].showing) spadDraw=yes; break; /* translate2D */ case moveViewport: readViewman(&i1,intSize); readViewman(&i2,intSize); XMoveWindow(dsply,viewport->titleWindow,i1,i2); XSync(dsply,False); break; case resizeViewport: readViewman(&i1,intSize); readViewman(&i2,intSize); XResizeWindow(dsply,viewport->titleWindow,i1,i2+titleHeight); XResizeWindow(dsply,viewport->viewWindow,i1,i2); spadDraw=yes; break; case putGraph: readViewman(&i1,intSize); /* key of graph to get */ readViewman(&i2,intSize); /* slot to drop graph onto 0..8*/ readViewman(&viewGoAhead,intSize); if (viewGoAhead < 0) { sprintf(control->message,"%s%d","Couldn't put into graph ",i2+1); writeControlMessage(); } else { sprintf(control->message,"%s%d","Dropped onto graph ",i2+1); writeControlMessage(); freeGraph(i2); graphArray[i2].key = i1; getGraphFromViewman(i2); /* simulate a button press to turn display number on and select on */ /* need !yes since it will be inverted */ graphStateArray[i2].selected = no; graphStateArray[i2].connectOn = yes; graphStateArray[i2].showing = !(graphStateArray[i2].showing); clickedOnGraph(i2,i2+graphStart); clickedOnGraphSelect(i2,i2+graphSelectStart); } break; case spadPressedAButton: readViewman(&i1,intSize); buttonAction(i1); break; default: return(-1); } /* switch */ ack++; code = check(write(Socket,&ack,intSize)); return(0); }
int main(int argc, char * argv[]){ int count = 0; FILE *in, *out; char line[MAX_LEN]; char *token; int graphOrder = 0; int edge[2]; char blank[] = " "; Graph G = NULL; // check command line for correct number of arguments if( argc != 3 ){ printf("Usage: %s <input file> <output file>\n", argv[0]); exit(1); } // open files for reading and writing in = fopen(argv[1], "r"); out = fopen(argv[2], "w"); if( in==NULL ){ printf("Unable to open file %s for reading\n", argv[1]); exit(1); } if( out==NULL ){ printf("Unable to open file %s for writing\n", argv[2]); exit(1); } /* read each line of input file, then count and print tokens */ if(fgets(line, MAX_LEN, in) == NULL){ printf("Unable to read first line of file"); exit(1); } graphOrder = atoi(line); G = newGraph(graphOrder); while( fgets(line, MAX_LEN, in) != NULL) { count++; token = strtok(line, blank); edge[0] = atoi(token); token = strtok(NULL, blank); edge[1] = atoi(token); if(edge[0] == 0 && edge[1] == 0){ break; } addArc(G,edge[0],edge[1]); } printGraph(out,G); fprintf(out, "\n"); List S = newList(); for(int i = 1; i <= getOrder(G); i++){ append(S, i); } DFS(G,S); Graph T = transpose(G); DFS(T,S); revList(S); printComponents(out,T,S); freeGraph(&G); freeGraph(&T); freeList(&S); fclose(in); fclose(out); return(0); }
int main(int argc, char* argv[]){ int i, n=8; List S = newList(); Graph G = newGraph(n); Graph T=NULL, C=NULL; Graph G_undir = newGraph(35); for(i=1; i<35; i++) { if( i%7!=0 ) addEdge(G_undir, i, i+1); if( i<=28 ) addEdge(G_undir, i, i+7); } printf("The Order of this undirected graph is: %d\n", getOrder(G_undir)); printf("The Size of this undirected graph is: %d\n", getSize(G_undir)); printf("Adjacency list representation of G_undir: \n"); printGraph(stdout, G_undir); printf("\n"); for(i=1; i<=n; i++) append(S, i); addArc(G, 1,2); addArc(G, 1,5); addArc(G, 2,5); addArc(G, 2,6); addArc(G, 3,2); addArc(G, 3,4); addArc(G, 3,6); addArc(G, 3,7); addArc(G, 3,8); addArc(G, 6,5); addArc(G, 6,7); addArc(G, 8,4); addArc(G, 8,7); printf("The Order of this graph is: %d\n", getOrder(G)); printf("The Size of this graph is: %d\n", getSize(G)); printf("Adjacency list representation of G: \n"); printGraph(stdout, G); DFS(G, S); fprintf(stdout, "\n"); fprintf(stdout, "x: d f p\n"); for(i=1; i<=n; i++){ fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(G, i), getFinish(G, i), getParent(G, i)); } fprintf(stdout, "\n"); printList(stdout, S); fprintf(stdout, "\n"); T = transpose(G); C = copyGraph(G); fprintf(stdout, "\n"); printGraph(stdout, C); fprintf(stdout, "\n"); printGraph(stdout, T); fprintf(stdout, "\n"); DFS(T, S); fprintf(stdout, "\n"); fprintf(stdout, "x: d f p\n"); for(i=1; i<=n; i++){ fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(T, i), getFinish(T, i), getParent(T, i)); } fprintf(stdout, "\n"); printList(stdout, S); fprintf(stdout, "\n"); freeList(&S); freeGraph(&G); freeGraph(&T); freeGraph(&C); return(0); }
int main (int argc, char * argv[]) { /* check command line for correct number of arguments */ if( argc != 3 ){ printf("Usage: %s infile outfile\n", argv[0]); exit(1); } /* open files for reading and writing */ FILE *in, *out; in = fopen(argv[1], "r"); out = fopen(argv[2], "w"); if( in==NULL ) { printf("Unable to open file %s for reading\n", argv[1]); exit(1); } if( out==NULL ) { printf("Unable to open file %s for writing\n", argv[2]); exit(1); } /* declare and initialize variables */ char line[MAX_LEN]; char* token; int linenum = 0; bool flag = FALSE; int n, x, y, source, dest; GraphRef G; ListRef path = newList(); /* read and store the graph and print out its adjacency list */ while( !flag && fgets(line, MAX_LEN, in) != NULL ) { ++linenum; token = strtok(line, " \n"); if( linenum == 1 ) { n = atoi(token); G = newGraph(n); }else { x = atoi(token); token = strtok(NULL, " \n"); y = atoi(token); if( x != 0 ) { addEdge(G, x, y); } else { flag = TRUE; } } } printGraph(out, G); fprintf(out, "\n"); /* read in pair of vertices, run BFS on source vertex, print the distance * to the destination vertex, then find and print the resulting shortest * path, it if exists, or print a message if no path from source to * destination exists */ while( fgets(line, MAX_LEN, in) != NULL ) { token = strtok(line, " \n"); source = atoi(token); token = strtok(NULL, " \n"); dest = atoi(token); if( source != NIL ){ BFS(G, source); makeEmpty(path); getPath(path, G, dest); printDestandPath(out, G, path, dest); } } /* free memory */ freeList(&path); freeGraph(&G); /* close files */ fclose(in); fclose(out); return(0); }
void splitNDnode(nestdiss_t *nd, options_t *options, timings_t *cpus) { nestdiss_t *b_nd, *w_nd; graph_t *Gsub; gbisect_t *Gbisect; int *map, *intvertex, *intcolor, *b_intvertex, *w_intvertex; int nvint, b_nvint, w_nvint, u, i; map = nd->map; nvint = nd->nvint; intvertex = nd->intvertex; intcolor = nd->intcolor; /* ------------------------------------------------------------- extract the subgraph for which a bisection has to be computed ------------------------------------------------------------- */ if (nd->G->nvtx == nd->nvint) { Gsub = nd->G; /* a hack to save time and space */ for (u = 0; u < nd->nvint; u++) /* but do not forget the map vector */ map[u] = u; } else Gsub = setupSubgraph(nd->G, intvertex, nvint, map); Gbisect = newGbisect(Gsub); /* --------------------------------- compute the bisection for Gbisect --------------------------------- */ starttimer(cpus[TIME_MULTILEVEL]); constructSeparator(Gbisect, options, cpus); stoptimer(cpus[TIME_MULTILEVEL]); starttimer(cpus[TIME_SMOOTH]); if (Gbisect->cwght[GRAY] > 0) smoothSeparator(Gbisect, options); stoptimer(cpus[TIME_SMOOTH]); /* ---------------------------------------- copy the bisection back to the nd object ---------------------------------------- */ b_nvint = w_nvint = 0; nd->cwght[GRAY] = Gbisect->cwght[GRAY]; nd->cwght[BLACK] = Gbisect->cwght[BLACK]; nd->cwght[WHITE] = Gbisect->cwght[WHITE]; for (i = 0; i < nvint; i++) { u = intvertex[i]; intcolor[i] = Gbisect->color[map[u]]; switch(intcolor[i]) { case GRAY: break; case BLACK: b_nvint++; break; case WHITE: w_nvint++; break; default: fprintf(stderr, "\nError in function splitNDnode\n" " node %d has unrecognized color %d\n", u, intcolor[i]); quit(); } } /* ------------------------------------------------------ and now split the nd object according to the bisection ------------------------------------------------------ */ b_nd = newNDnode(nd->G, map, b_nvint); b_intvertex = b_nd->intvertex; w_nd = newNDnode(nd->G, map, w_nvint); w_intvertex = w_nd->intvertex; b_nvint = w_nvint = 0; for (i = 0; i < nvint; i++) { u = intvertex[i]; if (intcolor[i] == BLACK) b_intvertex[b_nvint++] = u; if (intcolor[i] == WHITE) w_intvertex[w_nvint++] = u; } nd->childB = b_nd; b_nd->parent = nd; nd->childW = w_nd; w_nd->parent = nd; b_nd->depth = nd->depth + 1; w_nd->depth = nd->depth + 1; /* ----------------- free the subgraph ----------------- */ if (Gsub != nd->G) freeGraph(Gsub); freeGbisect(Gbisect); }
int bigTest(int N, int d, char *file_name) { /* unmatched, quickmatch_steps, quickmatch_time, bfs_steps/unmatchedFloat, bfs2bfs_steps/unmatchedFloat, dfs2bfs_steps/unmatchedFloat, dfs2dfs_steps/unmatchedFloat, dfs_steps/unmatchedFloat, bfs_time, bfs_time, dfs_time, bfs2bfs_time, dfs2bfs_time, dfs2dfs_time */ long last_flush = 0; long cur_time = 0; FILE *file; file = fopen(file_name,"a+"); // append file (add text to a file or // create a file if it does not exist. assert(file != NULL); //srandom(time(NULL)); srandom(SEED); int quickmatch_steps; int bfs_steps; int dfs_steps; int bfs2bfs_steps; int dfs2bfs_steps; int dfs2dfs_steps; int hopcroft_steps; float quickmatch_time; float bfs_time; float dfs_time; float bfs2bfs_time; float dfs2bfs_time; float dfs2dfs_time; float hopcroft_time; int bfs_path_length; int i; while (true) { int *right_sides; int *matching; int unmatched = 0; struct Graph *graph; right_sides = createRightSides(N,d); graph = createRandomRegBipartite(N,d,0,right_sides); START_TIMER quickmatch_steps = quickmatch(graph,&matching,&unmatched); STOP_TIMER quickmatch_time = seconds; // Copy a matching for each process int* matching1 = copyMatching(matching, N); int* matching2 = copyMatching(matching, N); int* matching3 = copyMatching(matching, N); int* matching4 = copyMatching(matching, N); int* matching5 = copyMatching(matching, N); int* matching6 = copyMatching(matching, N); // Perform resetGraph(graph); bfs_steps=0; dfs_steps=0; bfs2bfs_steps=0; dfs2bfs_steps=0; dfs2dfs_steps=0; hopcroft_steps=0; bfs_time=0; dfs_time=0; bfs2bfs_time=0; dfs2bfs_time=0; dfs2dfs_time=0; hopcroft_steps=0; for (i=0; i<unmatched; i++) { START_TIMER bfs_steps += bfs(graph,matching1,&bfs_path_length); STOP_TIMER bfs_time += seconds; START_TIMER dfs_steps += dfs(graph,matching5); STOP_TIMER dfs_time += seconds; START_TIMER bfs2bfs_steps += bfs2bfs(graph,matching2); STOP_TIMER bfs2bfs_time += seconds; START_TIMER dfs2bfs_steps += dfs2bfs(graph,matching3); STOP_TIMER dfs2bfs_time += seconds; START_TIMER dfs2dfs_steps += dfs2dfs(graph,matching4); STOP_TIMER dfs2dfs_time += seconds; START_TIMER hopcroft_steps += hopcroftPartial(graph,matching6); STOP_TIMER hopcroft_time += seconds; } // Validate validateMatching(matching1, graph); validateMatching(matching2, graph); validateMatching(matching3, graph); validateMatching(matching4, graph); validateMatching(matching5, graph); validateMatching(matching6, graph); assert(isMatchingComplete(graph, matching1)); assert(isMatchingComplete(graph, matching2)); assert(isMatchingComplete(graph, matching3)); assert(isMatchingComplete(graph, matching4)); assert(isMatchingComplete(graph, matching5)); assert(isMatchingComplete(graph, matching6)); float unmatchedFloat = (float) unmatched; fprintf(file,"%i,%i,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", unmatched, quickmatch_steps, quickmatch_time, bfs_steps/unmatchedFloat, bfs2bfs_steps/unmatchedFloat, dfs2bfs_steps/unmatchedFloat, dfs2dfs_steps/unmatchedFloat, dfs_steps/unmatchedFloat, hopcroft_steps/unmatchedFloat, bfs_time, dfs_time, bfs2bfs_time, dfs2bfs_time, dfs2dfs_time, hopcroft_time); cur_time = (long) time(NULL); if (time(NULL) >= last_flush + FLUSH_TIME) { fflush(file); last_flush = cur_time; } // Free freeGraph(graph); free(matching); free(matching1); free(matching2); free(matching3); free(matching4); free(matching5); free(matching6); free(right_sides); //free(visited); //free(targets); } fclose(file); }
void printAsGraph(int i, int j, Intstack gr) { Graph g; g = newGraph(i, j, gr); writeGraph(g); freeGraph(g); }
int altTest(int N, int d, char *file_name) { /* N, d, unmatched, bfs_steps, dfs_steps, bfs2bfs_steps, dfs2bfs_steps, dfs2dfs_steps, hopcroft_steps, bfs_time, dfs_time, bfs2bfs_time, dfs2bfs_time, dfs2dfs_time, hopcroft_time */ long last_flush = 0; long cur_time = 0; FILE *file; file = fopen(file_name,"a+"); // append file (add text to a file or // create a file if it does not exist. assert(file != NULL); //srandom(time(NULL)); srandom(SEED); int quickmatch_steps; int bfs_steps; int dfs_steps; int bfs2bfs_steps; int dfs2bfs_steps; int dfs2dfs_steps; int hopcroft_steps; float quickmatch_time; float bfs_time; float dfs_time; float bfs2bfs_time; float dfs2bfs_time; float dfs2dfs_time; float hopcroft_time; int bfs_path_length; int hopcroft_phases; int orig_unmatched; int i; while (true) { int *right_sides; int *matching; int unmatched = 0; struct Graph *graph; right_sides = createRightSides(N,d); graph = createRandomRegBipartite(N,d,0,right_sides); START_TIMER quickmatch_steps = quickmatch(graph,&matching,&unmatched); STOP_TIMER quickmatch_time = seconds; orig_unmatched = unmatched; // Copy a matching for each process int* matching1 = copyMatching(matching, N); int* matching2 = copyMatching(matching, N); int* matching3 = copyMatching(matching, N); int* matching4 = copyMatching(matching, N); int* matching5 = copyMatching(matching, N); int* matching6 = copyMatching(matching, N); // Perform resetGraph(graph); bfs_steps=0; dfs_steps=0; bfs2bfs_steps=0; dfs2bfs_steps=0; dfs2dfs_steps=0; hopcroft_steps=0; bfs_time=0; dfs_time=0; bfs2bfs_time=0; dfs2bfs_time=0; dfs2dfs_time=0; hopcroft_steps=0; hopcroft_phases=0; int hopcroft_unmatched = unmatched; int temp, num_inserted; for (unmatched=unmatched; unmatched>0; unmatched--) { START_TIMER bfs_steps = bfs(graph,matching1,&bfs_path_length); STOP_TIMER bfs_time = seconds; START_TIMER dfs_steps = dfs(graph,matching5); STOP_TIMER dfs_time = seconds; START_TIMER bfs2bfs_steps = bfs2bfs(graph,matching2); STOP_TIMER bfs2bfs_time = seconds; START_TIMER dfs2bfs_steps = dfs2bfs(graph,matching3); STOP_TIMER dfs2bfs_time = seconds; START_TIMER dfs2dfs_steps = dfs2dfs(graph,matching4); STOP_TIMER dfs2dfs_time = seconds; // this logic accounts for the fact that sometimes hopcroftPhase inserts more than one edge into matching if (hopcroft_unmatched == unmatched) { hopcroft_phases++; hopcroft_steps = 0; temp = hopcroft_unmatched; START_TIMER hopcroftPhase(graph, matching6, &hopcroft_unmatched, &hopcroft_steps); STOP_TIMER num_inserted = temp - hopcroft_unmatched; //assert(hopcroft_unmatched == validateMatching(matching6, graph)); //assert(temp>hopcroft_unmatched); hopcroft_steps = hopcroft_steps/num_inserted; hopcroft_time = seconds/num_inserted; } fprintf(file,"%i,%i,%i,%i,%i,%i,%i,%i,%i,%f,%f,%f,%f,%f,%f,%i\n", N, d, unmatched, bfs_steps, dfs_steps, bfs2bfs_steps, dfs2bfs_steps, dfs2dfs_steps, hopcroft_steps, bfs_time, dfs_time, bfs2bfs_time, dfs2bfs_time, dfs2dfs_time, hopcroft_time, bfs_path_length); } printf("%f,", orig_unmatched / (float) hopcroft_phases); // Validate validateMatching(matching1, graph); validateMatching(matching2, graph); validateMatching(matching3, graph); validateMatching(matching4, graph); validateMatching(matching5, graph); validateMatching(matching6, graph); assert(isMatchingComplete(graph, matching1)); assert(isMatchingComplete(graph, matching2)); assert(isMatchingComplete(graph, matching3)); assert(isMatchingComplete(graph, matching4)); assert(isMatchingComplete(graph, matching5)); assert(isMatchingComplete(graph, matching6)); float unmatchedFloat = (float) unmatched; cur_time = (long) time(NULL); if (time(NULL) >= last_flush + FLUSH_TIME) { fflush(file); fflush(stdout); last_flush = cur_time; } // Free freeGraph(graph); free(matching); free(matching1); free(matching2); free(matching3); free(matching4); free(matching5); free(matching6); free(right_sides); //free(visited); //free(targets); } fclose(file); }
int main (int argc, char *argv[]) { graph *g, *backup; node *v; float t; double epsilon=1.; int i, numPhases = 0; while ((i=getopt(argc, argv, "e:")) != -1) switch (i) { case '?': fprintf(stderr, "usage: %s [-e epsilon]\n", argv[0]); exit(1); case 'e': if (*optarg != '=') epsilon = atof(optarg); else epsilon = atof(optarg+1); break; } /* read input */ g = dimacsParse(stdin); printf("c nodes: %14d arcs: %15d\n", g->currentN, g->m); t = timer (); /* initialization */ makeHeap ( h, g->currentN); compact(g); /* clean up multiple edges and/or self-loops in input */ #ifndef NO_PR PRpreprocess(g, 0.75, 0.5, 0.5); /* preprocess with PR heuristics */ #endif backup = makeBackupGraph(g); if (g->currentN > 2) { numPhases++; matulaApprox(g, epsilon); v = sparseCertContract(g, g->minCap); if (g->currentN > 1) { compact(g); sourcePR(g, v, 0.9); PRpass(g, 0.75, 0.5, 0.5, 0); } } restoreBackupGraph(g, backup); sparseCertContract(g, g->minCap); freeBackupGraph(backup); freeHeap(h); g->dtime = g->dtime - t; t = timer() - t; printf("c sparse_nodes: %d sparse_arcs: %d\n", g->currentN, g->currentM/2); /* print out stats */ #ifndef NO_PR printf("c internal PR: %-6d PR 1: %-6d PR 2: %-6d PR 3: %-6d PR 4: %-6d\n", g->PR1Cnt+g->PR2Cnt+g->PR3Cnt+g->PR4Cnt, g->PR1Cnt, g->PR2Cnt, g->PR3Cnt, g->PR4Cnt); printf("c PR internal edge scans 1+2: %-8ld 3+4: %-8ld\n", g->edgeScanCnt[PR12], g->edgeScanCnt[PR34]); #endif printf("c phases: %14d scans: %14d\n", numPhases, g->numScans); printf("c edge scans: %11ld\n", totalEdgeScans(g)); printf("c MinCuts discovered:%4d\n", g->cutCount); printf("c ttime: %16.2f capacity: ", t); fprintf_wt(stdout, "%11", g->minCap); printf("\nc dtime: %16.2f\n", g->dtime); printf("\n"); freeGraph(g); return 0; }
int main(int argc, char **argv) { char **inputNameTable, **name, inputFileName[STRING_SIZE], outputFileName[STRING_SIZE], effectifFileName[STRING_SIZE], outputPrefix[STRING_SIZE], outputFileNameRand[STRING_SIZE], buffer[STRING_SIZE], inputFileNameModel[STRING_SIZE], *tmp, option[256]; FILE *fi, *fo, *fs; int i, j, t, sizeTable, singleF = 0; TypeMultiGraph *graph, *gtmp; TypeGraph **tableGraph, *g; TypePartition part, *tablePart; double alpha = 1.; for(i=0; i<256; i++) option[i] = 0; sprintf(outputFileName, "%s.%s", NAME_OUTPUT, EXT_OUTPUT); tableGraph = (TypeGraph**) malloc((argc+3)*sizeof(TypeGraph*)); inputNameTable = (char**) malloc((argc+3)*sizeof(char*)); sizeTable = 0; for(i=1; i<argc && *(argv[i]) == '-'; i++) { for(j=1; argv[i][j] != '\0'; j++) option[argv[i][j]] = 1; if(option['o']) { option['o'] = 0; if((i+1)<argc && sscanf(argv[i+1], "%s", outputFileName) == 1) i++; else exitProg(ErrorArgument, "a file name is required after option -f"); } if(option['s']) { option['s'] = 0; singleF = 1; } if(option['p']) { option['p'] = 0; if((i+1)<argc && sscanf(argv[i+1], "%lf", &alpha) == 1) i++; else exitProg(ErrorArgument, "a real number is required after option -p"); } if(option['m']) { option['m'] = 0; if(!(sscanf(argv[i+1], "%s", inputFileName) == 1)) exitProg(ErrorArgument, "wrong file name"); i++; inputNameTable[sizeTable] = (char*) malloc((strlen(inputFileName)+1)*sizeof(char)); strcpy(inputNameTable[sizeTable], inputFileName); printf("Reading file %s\n", inputNameTable[sizeTable]); if(fi = fopen(inputNameTable[sizeTable], "r")) { tableGraph[sizeTable++] = readMatrixGraph(fi); fclose(fi); } else exitProg(ErrorReading, inputNameTable[sizeTable]); } if(option['h']) { option['h'] = 0; printf("%s\n", HELPMESSAGE); exit(0); } } for(j=i; j<argc; j++) { if(!(sscanf(argv[j], "%s", inputFileName) == 1)) exitProg(ErrorArgument, "wrong file name"); inputNameTable[sizeTable] = (char*) malloc((strlen(inputFileName)+1)*sizeof(char)); strcpy(inputNameTable[sizeTable], inputFileName); printf("Reading file %s\n", inputNameTable[sizeTable]); if(fi = fopen(inputNameTable[sizeTable], "r")) { tableGraph[sizeTable] = readGraph(fi); fclose(fi); } else exitProg(ErrorReading, inputNameTable[sizeTable]); fixEdgeGraph(tableGraph[sizeTable]); printf("%d nodes\n", tableGraph[sizeTable]->sizeGraph); sizeTable++; } if(sizeTable <= 0) exitProg(ErrorArgument, "at least one graph is required."); strcpy(outputPrefix, outputFileName); if((tmp = strrchr(outputPrefix, '.')) != NULL) tmp[0] = '\0'; graph = toMultiGraph(tableGraph, sizeTable); part = getPartition(graph, LouvainType, &alpha); tableGraph[sizeTable] = sumMultiGraph(graph); inputNameTable[sizeTable] = (char*) malloc((strlen("Sum")+1)*sizeof(char)); strcpy(inputNameTable[sizeTable], "Sum"); sizeTable++; tableGraph[sizeTable] = unionMultiGraph(graph); inputNameTable[sizeTable] = (char*) malloc((strlen("Union")+1)*sizeof(char)); strcpy(inputNameTable[sizeTable], "Union"); sizeTable++; tableGraph[sizeTable] = interMultiGraph(graph); inputNameTable[sizeTable] = (char*) malloc((strlen("Intersection")+1)*sizeof(char)); strcpy(inputNameTable[sizeTable], "Intersection"); sizeTable++; name = (char**) malloc(sizeTable*sizeof(char*)); for(t=0; t<sizeTable; t++) { char *tmp; if((tmp = strrchr(inputNameTable[t], '.')) != NULL) tmp[0] = '\0'; if((tmp=strrchr(inputNameTable[t], '/')) == NULL) tmp = inputNameTable[t]; else tmp++; name[t] = (char*) malloc((strlen(tmp)+1)*sizeof(char)); strcpy(name[t], tmp); // printf("name[%d]\t%s\n", t, name[t]); } tablePart = (TypePartition*) malloc(sizeTable*sizeof(TypePartition)); if(singleF) { TypeMultiGraph *gtmp; gtmp = (TypeMultiGraph*) malloc(sizeof(TypeMultiGraph)); gtmp->sizeTable = 1; gtmp->edge = (TypeEdgeG***) malloc(sizeof(TypeEdgeG**)); gtmp->present = (int**) malloc(sizeof(int*)); gtmp->present[0] = (int*) malloc(graph->sizeGraph*sizeof(int)); for(i=0; i<graph->sizeGraph; i++) gtmp->present[0][i] = 1; for(t=0; t<sizeTable; t++) { char output[STRING_SIZE], *tmp; fillMultiOne(tableGraph[t], gtmp); sprintf(output, "%s_%s.%s", outputPrefix, name[t], EXT_OUTPUT); printf("Computing %s\n", output); tablePart[t] = getPartition(gtmp, LouvainType, &alpha); if(fo = fopen(output, "w")) { fprintPartitionClustNSee(fo, &(tablePart[t]), gtmp->name); fclose(fo); } else exitProg(ErrorWriting, output); } free((void*)gtmp->present[0]); free((void*)gtmp->present); free((void*)gtmp->edge); free((void*)gtmp); } if(fo = fopen(outputFileName, "w")) { fprintPartitionClustNSee(fo, &part, graph->name); fclose(fo); } else exitProg(ErrorWriting, outputFileName); sprintf(effectifFileName, "%s_effectif.csv", outputPrefix); if(fo = fopen(effectifFileName, "w")) { int **eff, c, t, *cs; eff = getEdgesNumbers(&part, graph); cs = getClassSize(&part); fprintf(fo, "\tSize"); for(t=0; t<graph->sizeTable; t++) fprintf(fo, "\t%s", name[t]); fprintf(fo, "\n"); for(c=0; c<part.sizeAtom; c++) { fprintf(fo, "ClusterID:%d", c+1); fprintf(fo, "\t%d", cs[c]); for(t=0; t<graph->sizeTable; t++) { fprintf(fo, "\t%d", eff[c][t]); } fprintf(fo, "\n"); } fclose(fo); for(c=0; c<part.sizeAtom; c++) free((void*)eff[c]); free((void*)eff); free((void*)cs); } else exitProg(ErrorWriting, effectifFileName); if(singleF) { for(t=sizeTable-3; t<sizeTable; t++) { sprintf(effectifFileName, "%s_%s_effectif.csv", outputPrefix, name[t]); if((fo = fopen(effectifFileName, "w"))) { int **eff, c, t, *cs; eff = getEdgesNumbers(&(tablePart[sizeTable-1]), graph); cs = getClassSize(&(tablePart[sizeTable-1])); fprintf(fo, "\tSize"); for(t=0; t<graph->sizeTable; t++) fprintf(fo, "\t%s", name[t]); fprintf(fo, "\n"); for(c=0; c<tablePart[sizeTable-1].sizeAtom; c++) { fprintf(fo, "ClusterID:%d", c+1); fprintf(fo, "\t%d", cs[c]); for(t=0; t<graph->sizeTable; t++) { fprintf(fo, "\t%d", eff[c][t]); } fprintf(fo, "\n"); } fclose(fo); for(c=0; c<tablePart[sizeTable-1].sizeAtom; c++) free((void*)eff[c]); free((void*)eff); free((void*)cs); } else exitProg(ErrorWriting, effectifFileName); sprintf(effectifFileName, "%s_%s_graph.csv", outputPrefix, name[t]); if((fo = fopen(effectifFileName, "w"))) { fprintGraph(fo, tableGraph[t]); } else exitProg(ErrorWriting, effectifFileName); } } if(singleF) { int tl, tc; char *tmp; sprintf(outputFileNameRand, "%s_Rand.csv", outputPrefix); if(fo = fopen(outputFileNameRand, "w")) { char *tmp; fprintf(fo,"All\t%lf\n", comparePartDiff(correctedRandIndex, &(part), graph->name, &(part), graph->name)); for(tl=0; tl<sizeTable; tl++) { fprintf(fo, "%s\t%lf", name[tl], comparePartDiff(correctedRandIndex, &(part), graph->name, &(tablePart[tl]), tableGraph[tl]->name)); for(tc=0; tc<=tl; tc++) fprintf(fo, "\t%lf", comparePartDiff(correctedRandIndex, &(tablePart[tc]), tableGraph[tc]->name, &(tablePart[tl]), tableGraph[tl]->name)); fprintf(fo, "\n"); } fprintf(fo, "\tAll"); for(tl=0; tl<sizeTable; tl++) { fprintf(fo, "\t%s", name[tl]); } fprintf(fo, "\n"); fclose(fo); } else exitProg(ErrorWriting, outputFileNameRand); } for(t=0; t<sizeTable; t++) { freeGraph(tableGraph[t]); free((void*) name[t]); free((void*) inputNameTable[t]); } free((void*) tableGraph); free((void*) name); free((void*) inputNameTable); exit(0); return 0; }
int main(int argc, char* argv[]){ // VARIABLE DECLARATION /////////////////////////////////////////// FILE *in, *out; char* token; char line[MAX_LEN]; int x, y; Graph G, Gt; List stack = newList(); // ERROR CHECKING AND I/O PREP //////////////////////////////////// //verifies the correct number of arguments if(argc != 3){ printf("Usage: %s <input file> <output file>\n", argv[0]); exit(1); } //opens the in file and out file in = fopen(argv[1], "r"); out = fopen(argv[2], "w"); //checks that they exist if(in == NULL) { printf("Unable to open file %s for reading\n", argv[1]); exit(1); } if(out == NULL) { printf("Unable to open file %s for writing\n", argv[2]); exit(1); } // INPUT AND GRAPH CREATION /////////////////////////////////////// // get first line and store in x fgets(line, MAX_LEN, in); token = strtok(line, "\n"); x = y = atoi(token); // create new graph of size x G = newGraph(x); // verify the 0 0 line hasn't been reached then get next line while(x != 0 && y != 0 && fgets(line, MAX_LEN, in) != NULL){ // store first and second // numbers in x and y // respectively token = strtok(line, " "); x = atoi(token); token = strtok(NULL, " "); y = atoi(token); // add a new arc with // origin x and terminus y if( x != 0 && y != 0){ addArc(G, x, y); } } // print out G's adjacency list representation fprintf(out, "Adjacency list representation of G:\n"); printGraph(out, G); // STRONGLY-CONNECTED-COMPONENTS ALGORITHM IMPLEMENTATION /////////// // Initialize the stack to contain the vertices // of G in ascending order for(int i = 1; i < getOrder(G); i++){ append(stack, i); } // Run a depth first search on G using stack // as the processing order of the vertices, // after stack will hold the vertices sorted // in order of decreasing finish time DFS(G, stack); // Create a transpose graph of G Gt = transpose(G); // Run the depth first search again this time // on the transpose of G using the output // stack from the first call DFS(Gt, stack); // the resulting stack contains the strongly // connected components of G fprintf(out, "G contains %d strongly connected components:", getSCC(Gt)); // for however many SCC's there are for(int i = 1; i <= getSCC(Gt); i++){ // start at the bottom of the stack moveTo(stack, length(stack)-1); // move up until you reach a vertex with a // NIL parent, that is the root of a SCC while(Gt->parent[getElement(stack)] != NIL){ movePrev(stack); } // print it as the first in its component list fprintf(out, "\nComponent %d: ", i); fprintf(out, "%d ", getElement(stack)); moveNext(stack); // and move down the stack printing out vertices // until you reach the bottom while(getIndex(stack) >= 0){ fprintf(out, "%d ", getElement(stack)); moveNext(stack); } // now move back up the stack deleting as you // go until you reach the root of this SCC while(Gt->parent[back(stack)] != NIL){ deleteBack(stack); } // and delete it deleteBack(stack); } // CLEAN UP ///////////////////////////////////////////////////////// fclose(out); fclose(in); freeList(&stack); stack = NULL; freeGraph(&G); freeGraph(&Gt); G = NULL; Gt = NULL; return (0); }
// {{{ elapsedTime stat_t elapsedTime (int n, int m, int c, int nb_iter) { int i=0; stat_t result; GTimer *timer = g_timer_new(); memset(&result, 0, sizeof(stat_t)); for (i=0; i< nb_iter; i++) { // Graph Initialization Graph * lcLab = allocListGraph(n); Graph * lcFIFO = allocListGraph(n); randFill(lcLab,m,c,1,lcFIFO); Graph *lcDinic = copyGraph(lcLab); int f,g,h; // FIFO Algorithm Graph * lfFIFO = allocGraph(lcFIFO); Graph * ldFIFO = copyGraph(lcFIFO); // printf ("Entrée FIFO\n"); g_timer_start(timer); h=algoFIFO (lcFIFO,ldFIFO,lfFIFO,0,1); g_timer_stop(timer); // printf ("Sortie FIFO\n"); freeGraph(ldFIFO); freeGraph(lfFIFO); if (h) { result._st1 += (double) g_timer_elapsed(timer, NULL); g_timer_reset(timer); // High Label Algorithm Graph * lfLab = allocGraph(lcLab); Graph * ldLab = copyGraph(lcLab); // printf ("Entrée High Label\n"); g_timer_start(timer); g=algoLabel (lcLab,ldLab,lfLab,0,1); g_timer_stop(timer); result._st2 += (double) g_timer_elapsed(timer, NULL); g_timer_reset(timer); // printf ("Sortie High Label\n"); freeGraph(lfLab); freeGraph(ldLab); // Dinic Algorithm Graph * lfDinic = allocGraph(lcLab); // printf("Entrée Dinic\n"); g_timer_start(timer); f=algoDinic (lcDinic,lfDinic,0,1); g_timer_stop(timer); result._st3 += (double) g_timer_elapsed(timer, NULL); g_timer_reset(timer); // printf("Sortie Dinic\n"); freeGraph (lfDinic); } else { printf ("Flot nul.\n"); g_timer_reset(timer); i --; } freeGraph (lcFIFO); freeGraph (lcLab); freeGraph (lcDinic); } result._st1 /= nb_iter; result._st2 /= nb_iter; result._st3 /= nb_iter; result._st4 /= nb_iter; g_timer_destroy(timer); return result; }
int main(int argc, char* argv[]){ int i, n=8; List S = newList(); Graph G = newGraph(n); Graph T=NULL, C=NULL; for(i=1; i<=n; i++) append(S, i); addArc(G, 1,2); addArc(G, 1,5); addArc(G, 2,5); addArc(G, 2,6); addArc(G, 3,2); addArc(G, 3,4); addArc(G, 3,6); addArc(G, 3,7); addArc(G, 3,8); addArc(G, 6,5); addArc(G, 6,7); addArc(G, 8,4); addArc(G, 8,7); printGraph(stdout, G); DFS(G, S); fprintf(stdout, "\n"); fprintf(stdout, "x: d f p\n"); for(i=1; i<=n; i++){ fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(G, i), getFinish(G, i), getParent(G, i)); } fprintf(stdout, "\n"); printList(stdout, S); fprintf(stdout, "\n"); T = transpose(G); C = copyGraph(G); fprintf(stdout, "\n"); printGraph(stdout, C); fprintf(stdout, "\n"); printGraph(stdout, T); fprintf(stdout, "\n"); DFS(T, S); fprintf(stdout, "\n"); fprintf(stdout, "x: d f p\n"); for(i=1; i<=n; i++){ fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(T, i), getFinish(T, i), getParent(T, i)); } fprintf(stdout, "\n"); printList(stdout, S); fprintf(stdout, "\n"); //********************* List apple = newList(); for(int m = 1; i <= 20; ++i) append(apple, m); Graph grape = newGraph(20); addEdge(grape, 1, 2); addEdge(grape, 2, 3); addEdge(grape, 3, 4); addEdge(grape, 4, 1); DFS(grape, apple); fprintf(stdout, "\n"); fprintf(stdout, "x: d f p\n"); for(i=1; i<=n; i++){ fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(T, i), getFinish(T, i), getParent(T, i)); } fprintf(stdout, "\n"); Graph orange = transpose(grape); DFS(orange, apple); fprintf(stdout, "\n"); fprintf(stdout, "x: d f p\n"); for(i=1; i<=n; i++){ fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(T, i), getFinish(T, i), getParent(T, i)); } fprintf(stdout, "\n"); //******************** freeList(&S); freeGraph(&G); freeGraph(&T); freeGraph(&C); return(0); }
int main (void) { printf ("This program tests the graph client\n"); Graph test_one = newGraph (6); Graph test_two = newGraph (10); /************************************************ * Graph test_one has a representation as follows * 1--------------2 * | /|\ * | / | \ * | / | \ * 3----------4---5---6 * In this case, all the edges are undirected * and we will initialize our adjacency lists to * represent this graph * We will perform BFS on vertex 3 and test other * appropriate functions in the ADT as well ***********************************************/ addArc (test_one, 1, 2); addArc (test_one, 1, 3); addArc (test_one, 2, 1); addArc (test_one, 2, 4); addArc (test_one, 2, 5); addArc (test_one, 2, 6); addArc (test_one, 3, 1); addArc (test_one, 3, 4); addArc (test_one, 4, 2); addArc (test_one, 4, 3); addArc (test_one, 4, 5); addArc (test_one, 5, 2); addArc (test_one, 5, 4); addArc (test_one, 5, 6); addArc (test_one, 6, 2); addArc (test_one, 6, 5); BFS (test_one, 3); List threesix = newList(); List threefiv = newList(); getPath (threesix, test_one, 6); getPath (threefiv, test_one, 5); printf ("Path from three to five, Expected output: 3 1 2 6, Test output: "); printList (stdout, threesix); printf ("\n"); printf ("Path from three to six: Expected output: 3 4 5, Test output: "); printList (stdout, threefiv); printf ("\n"); freeList (&threesix); freeList (&threefiv); printf ("Order: Expected = 6, Output = %d\n", getOrder (test_one)); printf ("Parent (6) = Expected = 2, Output = %d\n", getParent (test_one, 6)); printf ("Distance (6) = Expected = 3, Output = %d\n", getDist (test_one, 6)); printf ("___Adjacency List of test_one, Expected___\n"); printf ("1: 2 3\n2: 1 4 5 6\n3: 1 4\n4: 2 3 5\n5: 2 4 6\n6: 2 5\n"); printf ("___Adjacency List of test_two, Output___\n"); printGraph (stdout, test_one); printf ("\n"); printf ("The source of this graph is: Expected = 3, Output = %d\n", getSource (test_one)); //Finish test of graph one, tests graph two now addEdge (test_two, 1, 2); addEdge (test_two, 1, 4); addEdge (test_two, 2, 3); addEdge (test_two, 2, 5); addEdge (test_two, 3, 6); addEdge (test_two, 4, 7); addEdge (test_two, 5, 6); addEdge (test_two, 5, 8); addEdge (test_two, 6, 8); addEdge (test_two, 7, 8); addEdge (test_two, 8, 9); addEdge (test_two, 9, 10); /************************************************** * visual representation of this graph * 1----------2----3 * | | | * | | | * 4 5----6 * | | / * | | / * | | / * | | / * 7-----------8---9----10 ***************************************************/ BFS (test_two, 3); printf ("Parent list of each vertex after BFS (test_two, 3) (Expected)\n"); printf ("parent 1 = 2\n"); printf ("parent 2 = 3\n"); printf ("parent 3 = NIL\n"); printf ("parent 4 = 1\n"); printf ("parent 5 = 2\n"); printf ("parent 6 = 3\n"); printf ("parent 7 = 8\n"); printf ("parent 8 = 6\n"); printf ("parent 9 = 8\n"); printf ("parent 10 = 9\n"); printf ("The parent vertex of each vertex after BFS (test_two, 3)\n"); for (int i = 1; i <= getOrder (test_two); ++i) { printf ("parent %d = ", i); getParent (test_two, i) == -1 ? printf ("NIL\n") : printf ("%d\n", getParent (test_two, i)); } printf ("The size of test_two is: Expected = 24, Output = %d\n", getSize (test_two)); makeNull (test_two); printf ("The size of test_two after makeNull (test_two) is "); printf ("Expected = 0, Output =%d\n", getSize (test_two)); freeGraph (&test_one); freeGraph (&test_two); }