void testBetweennessCentrality(int bucketsNumber, int bucketSize) { //create small graph for testing betweenness Centrality Graph* gBetw = createGraph(bucketsNumber, bucketSize); Node* n1Betw = createNode(1, NULL); Node* n2Betw = createNode(2, NULL); Node* n3Betw = createNode(3, NULL); Node* n4Betw = createNode(4, NULL); Node* n5Betw = createNode(5, NULL); insertNode(n1Betw, gBetw); insertNode(n2Betw, gBetw); insertNode(n3Betw, gBetw); insertNode(n4Betw, gBetw); insertNode(n5Betw, gBetw); /* Create edges and set properties */ Edge* e1Betw = createEdge(1, 2, NULL); Edge* e2Betw = createEdge(2, 1, NULL); Edge* e3Betw = createEdge(2, 3, NULL); Edge* e4Betw = createEdge(2, 4, NULL); Edge* e5Betw = createEdge(3, 2, NULL); Edge* e6Betw = createEdge(3, 5, NULL); Edge* e7Betw = createEdge(4, 2, NULL); Edge* e8Betw = createEdge(4, 5, NULL); Edge* e9Betw = createEdge(5, 3, NULL); Edge* e10Betw = createEdge(5, 4, NULL); /* Insert edges in graph */ insertEdge(1, e1Betw, gBetw); insertEdge(2, e2Betw, gBetw); insertEdge(2, e3Betw, gBetw); insertEdge(2, e4Betw, gBetw); insertEdge(3, e5Betw, gBetw); insertEdge(3, e6Betw, gBetw); insertEdge(4, e7Betw, gBetw); insertEdge(4, e8Betw, gBetw); insertEdge(5, e9Betw, gBetw); insertEdge(5, e10Betw, gBetw); double betwCentrty1 = betweennessCentrality(n1Betw, gBetw); CHECKDOUBLE("Small Graph betweenness centrality node:1 ", betwCentrty1, 0.0 / 6.0); double betwCentrty2 = betweennessCentrality(n2Betw, gBetw); CHECKDOUBLE("Small Graph betweenness centrality node:2 ", betwCentrty2, 0.583); double betwCentrty3 = betweennessCentrality(n3Betw, gBetw); CHECKDOUBLE("Small Graph betweenness centrality node:3 ", betwCentrty3, 1.0 / 6.0); double betwCentrty4 = betweennessCentrality(n4Betw, gBetw); CHECKDOUBLE("Small Graph betweenness centrality node:4 ", betwCentrty4, 1.0 / 6.0); double betwCentrty5 = betweennessCentrality(n5Betw, gBetw); CHECKDOUBLE("Small Graph betweenness centrality node:5 ", betwCentrty5, 0.5 / 6.0); }
int main(int argc, char **argv) { t_vertex *v1, *v2, *v3, *v4, *v5, *v6, *v7, *v8, *v9, *v10; t_vertex *w; t_edge *e; t_graph *graph; t_stack stack; t_queue queue; int V = 0; int i = 0; int dot = 0; int bc = 0; int is_directed = FALSE; int index; int f, v, c, o; char *file = NULL; char *num_vertices = NULL; graph = createGraph(TRUE); while ((f = getopt (argc, argv, "odhbf:v:")) != -1) switch (f) { case 'd': dot = 1; break; case 'b': bc = 1; break; case 'o': is_directed = TRUE; break; case 'f': file = optarg; break; case 'h': help(); break; case 'v': num_vertices = optarg; break; case '?': if (isprint (optopt)) { fprintf (stderr, "Unknown option: `-%c'.\n", optopt); help(); } else { fprintf (stderr, "Invalid option `\\x%x'.\n", optopt); help(); } return 1; default: abort(); } for (index = optind; index < argc; index++) printf ("Non-option argument %s\n", argv[index]); graph = createGraph(is_directed); if (num_vertices != NULL) { V = atoi(num_vertices) + 1; for (i = 1; i < V; i++) addVertex(graph, i); } if (file != NULL) readFromCSV(graph, file); if (bc == 1) { betweennessCentrality(graph); printGraphCB(graph); } if (dot == 1) dumpDotGraph(graph); destroyCBData(graph); destroyGraph(graph); return 0; }
int main_part2(void) { //firstly execute a test for centrality metrics on small graphs int m = 2; int c = 3; testBetweennessCentrality(m, c); testClosenessCentrality(m, c); //small example for trust graph estimation testTidalTrust(m, c); /*create graph from file*/ int bucketsNumber = 5; int bucketSize = 10; Graph* g = loadGraph(bucketsNumber, bucketSize); //graph metrics calls //plot the graph degree distribution degreeDistribution(g, false); int diam = diameter(g); CHECKINT("Graph diameter", diam, 14); double avgPthLgth = averagePathLength(g); CHECKDOUBLE("Graph average path length", avgPthLgth, 5.0753); int ccNumber = numberOfCCs(g); CHECKINT("Graph number of components ", ccNumber, 1); int maximumCC = maxCC(g); CHECKINT("Graph maximum connected component ", maximumCC, 111); double dense = density(g); CHECKDOUBLE("Graph density ", dense, 0.073); /* * WRONG CALCULATIONS - only considering neighbors and not multi-step paths! int closenessIds[5] = { 1734, 38, 8899, 3501, 75}; float closenessIdsRes[5] = {0.109, 0.090, 0.072, 0.045, 0.009}; for (i = 0; i < 5; ++i) { int nodeID = closenessIds[i]; Node* node = lookupNode(nodeID, g); double closCentrty = closenessCentrality(node, g); CHECKDOUBLE("Graph closeness centrality ", closCentrty, closenessIdsRes[i]); } */ int i, k; int betweennessIds[5] = { 1734, 38, 8899, 9900, 75}; float betweennessIdsRes[5] = { 0.306, 0.053, 0.018, 0.005, 0.000}; for (i = 0; i < 5; ++i) { int nodeID = betweennessIds[i]; Node* node = lookupNode(nodeID, g); double betwCentrty = betweennessCentrality(node, g); CHECKDOUBLE("Graph betweenness centrality ", betwCentrty, betweennessIdsRes[i]); } //graph queries calls // Query 1 // Matches* match; Node *dateNode = lookupNode(3755, g); int commonInterests = 1, ageDiff = 30, acquaintanceHops = 3, matchesNum = 1; match = matchSuggestion(dateNode, commonInterests, acquaintanceHops, ageDiff, matchesNum, g); //match result : 7107 - work_at_organization: 1650 //get first pair's person ids int id1 = getMatch(0, match); CHECKINT("Query 1: Date result 1st id", id1, 7107); delete match; // Query 2 // //estimate stalkers graph with closeness centrality Graph* stalkersGraphCloseCentr; int stalkersNum = 7, likesNumber = 1, centralityMode = 1; Stalkers* stalkersCloseCentr = new Stalkers(stalkersNum); stalkersGraphCloseCentr = getTopStalkers(stalkersNum, likesNumber, centralityMode, g, stalkersCloseCentr); int stalkersResultsIds[] = {347, 495, 7768, 8354, 8403, 8899, 9633}; int stalkerResultSize = 7; int stalkerID; int counter = 0; for (int i = 0; i < stalkersNum; ++i) { stalkerID = getStalkerID(i, stalkersCloseCentr); if (stalkerID != -1) { ++counter; } for (k = 0; k < stalkerResultSize; ++k) { if (stalkersResultsIds[k] == stalkerID) { break; } } if (k == stalkerResultSize) { printf("You wrongly labeled person with id %d as Stalker\n", stalkerID); } } CHECKINT("Query 2: Stalker closeness results size", stalkerResultSize, counter); //run metrics on stalker-graph stalkersGraphRunMetrics(stalkersGraphCloseCentr); //estimate stalkers graph with betweenness centrality Graph* stalkersGraphBetwCentr; Stalkers* stalkersBetwCentr = new Stalkers(stalkersNum); centralityMode = 2; stalkersGraphBetwCentr = getTopStalkers(stalkersNum, likesNumber, centralityMode, g, stalkersBetwCentr); counter = 0; for (int i = 0; i < stalkersNum; ++i) { stalkerID = getStalkerID(i, stalkersBetwCentr); if (stalkerID != -1) { ++counter; } for (k = 0; k < stalkerResultSize; ++k) { if (stalkersResultsIds[k] == stalkerID) { break; } } if (k == stalkerResultSize) { printf("You wrongly labeled person with id %d as Stalker\n", stalkerID); } } CHECKINT("Query 2: Stalker betweenness results size", stalkerResultSize, counter); //run metrics on stalker-graph stalkersGraphRunMetrics(stalkersGraphBetwCentr); // Query 3 - Correct// int trendsNum = 4; //allocate result tables before calling query and pass them as parameters char** womenTrends; womenTrends = (char**) malloc(trendsNum * sizeof (char*)); char** menTrends; menTrends = (char**) malloc(trendsNum * sizeof (char*)); findTrends(trendsNum, g, &womenTrends, &menTrends); printf("Top Women - Men Trends\n"); int j; char* menTrendsResults[4] = {"Sun_Yat-sen", "Constantine_the_Great","Sigmund_Freud", "Hussein_of_Jordan"}; //IDS: {417,11622,468,1398} char* womenTrendsResults[4] = {"Adolf_Hitler", "Chiang_Kai-shek", NULL, NULL}; //IDS: {138,416,null,null} int counterW = 0, counterM = 0; for (j = 0; j < 4; ++j) { if (menTrends[j] != NULL) { ++counterM; for (k = 0; k < 4; ++k) { if (strcmp(menTrends[j], menTrendsResults[k]) == 0) { break; } } if (k == 4) { printf("You wrongly labeled tag with %s as top trend\n", menTrends[j]); } } } CHECKINT("Query 3: Mens Trends result size", 4, counterM); for (j = 0; j < 4; ++j) { if (womenTrends[j] != NULL) { ++counterW; for (k = 0; k < 2; ++k) { if (strcmp(womenTrends[j], womenTrendsResults[k]) == 0) { break; } } if (k == 2) { printf("You wrongly labeled tag with %s as top trend\n", menTrends[j]); } } } CHECKINT("Query 3: Women Trends result size", 2, counterW); cout<<"_____________________________________"<<endl; // Query 4 // // int forumID = 34680; char *forumName = "Wall of Xiomara Fernandez"; Graph *trustGraph = buildTrustGraph(forumName, g); int trustANodeId = 30; int trustBNodeId = 9805; int trustCNodeId = 9700; Node *ta = lookupNode(trustANodeId, trustGraph); assert(ta != NULL); Node *tb = lookupNode(trustBNodeId, trustGraph); assert(tb != NULL); Node *tc = lookupNode(trustCNodeId, trustGraph); assert(tc != NULL); double trustAB; trustAB = estimateTrust(ta, tb, trustGraph); printf("Trust between nodes (%d,%d) is %f\n", trustANodeId, trustBNodeId, trustAB); CHECKDOUBLE("Trust: (30,9805) ", trustAB, 0.134); cout<<"_____________________________________"<<endl; double trustAC; trustAC = estimateTrust(ta,tc, trustGraph); printf("Trust between nodes (%d,%d) is %f\n", trustANodeId, trustCNodeId, trustAC); CHECKDOUBLE("Trust: (30,9700) ", trustAC, 0.15); return EXIT_SUCCESS; }
int main(int argc, char** argv) { /* Data structure for storing generated tuples in the * Scalable Data Generation Stage -- see defs.h */ graphSDG* SDGdata; /* The graph data structure -- see defs.h */ graph* G; /* Kernel 2 output */ edge *maxIntWtList; INT_T maxIntWtListSize; /* Kernel 4 output */ DOUBLE_T *BC; DOUBLE_T elapsed_time; int filter, cutshort; #ifdef _OPENMP if (argc < 3) { fprintf(stderr, "Usage: ./SSCA2 <No. of threads> <SCALE>\n"); exit(-1); } NUM_THREADS = atoi(argv[1]); SCALE = atoi(argv[2]); omp_set_num_threads(NUM_THREADS); filter = atoi(argv[3]); cutshort = atoi(argv[4]); #else if (argc < 2) { fprintf(stderr, "Usage: ./SSCA2 <SCALE>\n"); exit(-1); } SCALE = atoi(argv[1]); filter = atoi(argv[2]); cutshort = atoi(argv[3]); #endif /* ------------------------------------ */ /* Initialization -- Untimed */ /* ------------------------------------ */ fprintf(stderr, "\nHPCS SSCA Graph Analysis Benchmark v2.2\n"); fprintf(stderr, "Running...\n\n"); init(SCALE, cutshort); #ifdef _OPENMP fprintf(stderr, "\nNo. of threads: %d\n", NUM_THREADS); #endif fprintf(stderr, "SCALE: %d\n\n", SCALE); /* -------------------------------------------- */ /* Scalable Data Generator -- Untimed */ /* -------------------------------------------- */ #ifndef VERIFYK4 fprintf(stderr, "Scalable Data Generator -- "); fprintf(stderr, "genScalData() beginning execution...\n"); SDGdata = (graphSDG *) malloc(sizeof(graphSDG)); elapsed_time = genScalData(SDGdata); fprintf(stderr, "\n\tgenScalData() completed execution\n"); fprintf(stderr, "\nTime taken for Scalable Data Generation is %9.6lf sec.\n\n", elapsed_time); #else fprintf(stderr, "Generating 2D torus for Kernel 4 validation -- "); fprintf(stderr, "gen2DTorus() beginning execution...\n"); SDGdata = (graphSDG *) malloc(sizeof(graphSDG)); elapsed_time = gen2DTorus(SDGdata); fprintf(stderr, "\n\tgen2DTorus() completed execution\n"); fprintf(stderr, "\nTime taken for 2D torus generation is %9.6lf sec.\n\n", elapsed_time); #endif /* ------------------------------------ */ /* Kernel 1 - Graph Construction */ /* ------------------------------------ */ /* From the SDG data, construct the graph 'G' */ fprintf(stderr, "\nKernel 1 -- computeGraph() beginning execution...\n"); G = (graph *) malloc(sizeof(graph)); /* Store the SDG edge lists in a compact representation * which isn't modified in subsequent Kernels */ elapsed_time = computeGraph(G, SDGdata); fprintf(stderr, "\n\tcomputeGraph() completed execution\n"); fprintf(stderr, "\nTime taken for Kernel 1 is %9.6lf sec.\n\n", elapsed_time); free(SDGdata); /* ---------------------------------------------------- */ /* Kernel 2 - Find max edge weight */ /* ---------------------------------------------------- */ fprintf(stderr, "\nKernel 2 -- getStartLists() beginning execution...\n"); /* Initialize vars and allocate temp. memory for the edge list */ maxIntWtListSize = 0; maxIntWtList = (edge *) malloc(sizeof(edge)); elapsed_time = getStartLists(G, &maxIntWtList, &maxIntWtListSize); fprintf(stderr, "\n\tgetStartLists() completed execution\n\n"); fprintf(stderr, "Max. int wt. list size is %d\n", maxIntWtListSize); fprintf(stderr, "\nTime taken for Kernel 2 is %9.6lf sec.\n\n", elapsed_time); /* ------------------------------------ */ /* Kernel 3 - Graph Extraction */ /* ------------------------------------ */ fprintf(stderr, "\nKernel 3 -- findSubGraphs() beginning execution...\n"); elapsed_time = findSubGraphs(G, maxIntWtList, maxIntWtListSize); fprintf(stderr, "\n\tfindSubGraphs() completed execution\n"); fprintf(stderr, "\nTime taken for Kernel 3 is %9.6lf sec.\n\n", elapsed_time); free(maxIntWtList); /* ------------------------------------------ */ /* Kernel 4 - Betweenness Centrality */ /* ------------------------------------------ */ fprintf(stderr, "\nKernel 4 -- betweennessCentrality() " "beginning execution...\n"); BC = (DOUBLE_T *) calloc(N, sizeof(DOUBLE_T)); elapsed_time = betweennessCentrality(G, BC, filter); fprintf(stderr, "\nTime taken for Kernel 4 is %9.6f sec.\n\n", elapsed_time); fprintf(stderr, "TEPS score for Kernel 4 is %lf\n\n", 7*N*(1<<K4approx)/elapsed_time); free(BC); /* -------------------------------------------------------------------- */ free(G->numEdges); free(G->endV); free(G->weight); free(G); return 0; }