void numberOfCCsMaxCC(Graph* g, int* numberOfCCs, int* maxCC) { List* intList = g->getAllIDs(); int numberOfCC = 0; int numberOfNodes = 0; int maxNumCC = 0; LinearHashTable *hashTable = g->getHashTable(); BucketList **bucketListTable = hashTable->getTable(); for (int i = 0; i < hashTable->getCurrentBucketNo(); i++) { BucketList *bucketList = bucketListTable[i]; BucketListItem *cur = (BucketListItem*) bucketList->getHead(); while (cur != NULL) { Bucket *bucket = cur->getElement(); for (int j = 0; j < bucket->getSize(); j++) { BucketItem *bi = bucket->getBucketItemByIndex(j); int startNodeId = bi->getNodeID(); numberOfNodes = 0; bool skip = true; IntegerListItem* listNode = (IntegerListItem*) intList->getHead(); while (listNode != NULL) { if (listNode->getInt() == startNodeId) { intList->deleteItem(listNode); numberOfCC++; numberOfNodes++; skip = false; break; } listNode = (IntegerListItem*) listNode->getNext(); } if (!skip) { //reachNodeN from each node of the graph ResultSet *res = reachNodeN(startNodeId, g); Pair* pair; while (pair = res->next()) { IntegerListItem* searchListNode = (IntegerListItem*) intList->getHead(); while (searchListNode != NULL) { if (searchListNode->getInt() == pair->getNodeId()) { intList->deleteItem(searchListNode); numberOfNodes++; break; } searchListNode = (IntegerListItem*) searchListNode->getNext(); } } //delete res; //free(): invalid next size (fast): } if (numberOfNodes > maxNumCC) maxNumCC = numberOfNodes; } cur = (BucketListItem*) cur->getNext(); } } delete intList; *numberOfCCs = numberOfCC; *maxCC = maxNumCC; }
/** * Calculates the degree distribution of a graph and visualizes it using GNUPLOT. * Only considers out_degree. */ void degreeDistribution(Graph *g, bool checkFlag) { int nodeNo = g->getNodeNo(); // Initialize array of N-1 elements to zeros ( holding the P(k)s, for k in (0,n-1) ). double totalCounters[nodeNo]; for (int i = 0; i < nodeNo; i++) totalCounters[i] = 0; // Traverse HashTable and update all counters. LinearHashTable *hashTable = g->getHashTable(); BucketList **bucketListTable = hashTable->getTable(); for (int i = 0; i < hashTable->getCurrentBucketNo(); i++) { BucketList *bucketList = bucketListTable[i]; BucketListItem *cur = (BucketListItem*) bucketList->getHead(); while (cur) { Bucket *bucket = cur->getElement(); // Count neighbors and update appropriate counter. for (int j = 0; j < bucket->getSize(); j++) { BucketItem *bi = bucket->getBucketItemByIndex(j); int count = bi->getEdgeList()->getSize(); totalCounters[count]++; } cur = (BucketListItem*) cur->getNext(); } } // Divide each counter by N. for (int i = 0; i < nodeNo; i++) totalCounters[i] = totalCounters[i] / nodeNo; if (checkFlag == true) { assert(int(totalCounters[0] * 10000) == 9898); assert(int(totalCounters[98] * 10000) == 101); } // Make data file. ofstream myFile; const char *filename = "graph_metrics/gnuplot/bin/data.txt"; myFile.open(filename); if (myFile.fail()) { cout << "ERROR: Unable to open " << filename << endl; return; } myFile << "# K P(K)" << endl; for (int i = 0; i < nodeNo; i++) myFile << i << " " << 100 * totalCounters[i] << endl; myFile.close(); // Run gnuplot to display graph. #ifdef __CYGWIN__ system("cd graph_metrics/gnuplot/bin; ./gnuplot.exe degree.gnu"); #else system("cd graph_metrics/gnuplot/bin; wine gnuplot.exe degree.gnu"); #endif }