#include "defs.h" int main(int argc, char** argv) { graph* G; // The graph data structure -- see defs.h double *BC; // BC output int nrows, ncols, scale, nread, vtx, r, c, checkable; int *head, *tail; double elapsed_time; int torusgraph=0, gridgraph=0, readgraph=0; /* --------------------*/ /* Parse command line */ /* --------------------*/ if ((argc==3) || (argc==4)) { torusgraph = (strcmp(argv[1],"-t")==0); gridgraph = (strcmp(argv[1],"-g")==0); nrows = atoi(argv[2]); ncols = (argc==4)? atoi(argv[3]) : nrows; } readgraph = (argc == 1); if ( !(torusgraph||gridgraph||readgraph) ) { fprintf(stderr, "usage: ./BC -t (computes BC for a torus)\n"); fprintf(stderr, "usage: ./BC -g (computes BC for a grid)\n"); fprintf(stderr, "usage: ./BC < (computes BC for a graph with edges given in a file\n"); exit(-1); } /* ------------------------------------ */ /* Graph construction or input */ /* ------------------------------------ */ if (readgraph) { fprintf(stderr, "Reading graph edges from stdin.\n"); nread = read_edge_list (&tail, &head); fprintf(stderr, "Finished reading graph edges.\n"); G = graph_from_edge_list (tail, head, nread); free(tail); free(head); fprintf(stderr, "Finished constructing graph from edge list.\n\n"); } if (gridgraph) { fprintf(stderr, "Generating 2D grid with %d rows and %d columns, for %d vertices in all.\n", nrows, ncols, nrows*ncols); G = (graph *) malloc(sizeof(graph)); elapsed_time = generateGrid(G,nrows,ncols); fprintf(stderr, "Time to generate grid graph is %9.6lf sec.\n\n", elapsed_time); } if (torusgraph) { fprintf(stderr, "Generating 2D torus with %d rows and %d columns, for %d vertices in all.\n", nrows, ncols, nrows*ncols); G = (graph *) malloc(sizeof(graph)); elapsed_time = generateTorus(G,nrows,ncols); fprintf(stderr, "Time to generate torus graph is %9.6lf sec.\n\n", elapsed_time); } print_CSR_graph(G); /* ------------------------------------------ */ /* Betweenness centrality */ /* ------------------------------------------ */ BC = (double *) calloc(G->nv, sizeof(double)); fprintf(stderr, "\nbetweennessCentrality()\n"); elapsed_time = betweennessCentrality(G, BC); fprintf(stderr, "Time taken for serial BC is %9.6f sec.\n", elapsed_time); fprintf(stderr, "TEPS score for serial BC is %lf\n\n", (double)G->nv * (double)(G->ne) / elapsed_time ); /* ------------------------------------------------------------- */ /* Validation: Check the answer if graph is a power-of-two torus */ /* ------------------------------------------------------------- */ checkable = 0; if (torusgraph && (nrows==ncols)) { for (scale = 1; nrows*ncols > (1<nv; vtx++) { if (round(BC[vtx] - BCval) != 0) { failed = 1; break; } } if (failed) { fprintf(stderr, "sorry, answer is WRONG!\n\n"); } else { fprintf(stderr, "hurray, answer is CORRECT!\n\n"); } } else { fprintf(stderr, "\nAnswer checking is only available for a square, power-of-two torus.\n\n"); } /* ---------------------------------------------*/ /* Print the answer if the graph is tiny enough */ /* ---------------------------------------------*/ if ((torusgraph||gridgraph) && ncols<=10 && nrows <= 100) { fprintf(stderr, "\nBetweenness centrality values:\n\n"); for (r = 0; r < nrows; r++) { for (c = 0; c < ncols; c++) { vtx = r*ncols+c; fprintf(stderr, "%6.1f ", BC[vtx]); } fprintf(stderr, "\n"); } fprintf(stderr, "\n"); } else if (G->nv < 20) { fprintf(stderr, "\nBetweenness centrality values:\n"); for (vtx = 0; vtx < G->nv; vtx++) { fprintf(stderr, "%6.1f ", BC[vtx]); } fprintf(stderr, "\n"); } /* ---------*/ /* Clean up */ /* ---------*/ free(BC); free(G->nbr); free(G->firstnbr); free(G); return 0; }