/** * @fn APair* (*AHashtable::set)(AHashtable* self, void* key, void* value) * @param self The hash table * @param key The key * @param value The value * @return A key-value pair or NULL on error * * Maps the value to the key. If the same key was inserted before, * it and the previous value will be removed using @link AHashtable::freeKey self->freeKey@endlink * and @link AHashtable::freeValue self->freeValue@endlink (if they're not NULL). */ static APair* AHashtableSet(AHashtable* self, void* key, void* value) { size_t bucket; AHashtableNode* node; AHashtableMaybeExpand(self); bucket = self->hash(key) & self->capacity; node = lookupNode(self->table[bucket], key, self->comp); /* New key */ if (node == NULL) { if ((node = makeNode(key, value, self->table[bucket])) == NULL) { return NULL; } self->lists += self->table[bucket] == NULL; self->table[bucket] = node; self->size++; } else /* Replace the old one */ { clearNode(node, self->freeKey, self->freeValue); node->key = key; node->value = value; self->size++; } return (APair *)node; }
void printInterestGraph(Graph * interGraph) { int i,j; Node * n; InterObj * obj; InterList * team; Queue * q; for(i=0;i<interGraph->size;i++) { for(j=0;j<interGraph->position[i];j++) { n = lookupNode(interGraph->table[i][j]->ID,interGraph); obj = n->obj; printf("\nInterest %d\n",interGraph->table[i][j]->ID); team = obj->team; while(team != NULL) // printing teams { printf("Team %d: ",team->iNode->tag); q = team->iNode->q; while(q != NULL) //printing members of every team { printf("%d ",q->ID); q = q->next; } printf("\n"); team = team->next; } } } printf("\n"); }
void* createForum(void *arg){ Graph* currentG; Edge* current; Node* n; create_args* args = (create_args*)arg; Forum* forum = args->forum; Graph* gforum = args->gforum; Graph* graph = args->g; Node* node = lookupNode(forum->id,gforum); //printf("%s\n",forum->name); currentG = createGraph(5, 10 , forum->name); currentG->next = NULL; insertEdgeProperty(currentG,"knows"); if ( node->edges==NULL || node->edges->size < 0) current = NULL; else current = node->edges->edge[0]; while (current!=NULL){ n = setNodeProperties(current->endID,NULL,NULL,0); insertNode(n,currentG); current = current->next; } find_copy_edges(graph,currentG,"knows","knows"); pthread_exit(currentG); }
/** * @fn void* (*AHashtable::get)(AHashtable* self, void* key) * @param self The hash table * @param key The key * @return The value or NULL on error * * Get the value a key previously inserted by AHashtable::set(). */ static void* AHashtableGet(AHashtable* self, void* key) { AHashtableNode* node; size_t bucket = self->hash(key) & self->capacity; if ((node = lookupNode(self->table[bucket], key, self->comp)) == NULL) { return NULL; } return node->value; }
Graph * CreateComments(char * file, char * file2, Graph * PostGraph) //finding comments and creators of comments { FILE* fp = fopen(file,"r"); //file = comment_replyOf_post Node * n; int *creator; Graph * CommentsGraph = createGraph(M,C); if (fp == NULL) { perror ("Error opening file"); exit(-1); } char buffer[200],*s; fgets(buffer,200,fp); while(fgets(buffer,200,fp)!= NULL) { s = strtok (buffer,"|"); if((n = lookupNode(atoi(strtok(NULL,"\n")), PostGraph)) != NULL) { n = malloc(sizeof(Node)); n->ID = atoi(s); n->obj = NULL; insertNode(n,CommentsGraph); } } fclose(fp); fp = fopen(file2, "r"); //file2 = comment_hasCreator_person fgets(buffer,200,fp); while(fgets(buffer,200,fp)!= NULL) { if((n = lookupNode(atoi(strtok (buffer,"|")), CommentsGraph)) != NULL) { creator = malloc(4); *creator = atoi(strtok (NULL, "\n")); n->obj = creator; } } fclose(fp); return CommentsGraph; }
// TRUE ==> key is present in the HashMap and it's value will be set in *val. // FALSE ==> key is not present in the HashMap BOOL HashMap_get(const HashMap *hm, // IN const void *key, // IN void **val) // OUT { node *n = lookupNode(hm, key); if (n != NULL) { *val = n->val; return TRUE; } else { *val = NULL; return FALSE; } }
void updateQueue(int interID, int team, int new_member, Graph * interGraph) { Node * n = lookupNode(interID,interGraph); InterObj * obj = n->obj; InterList * list = obj->team; Queue * tmp; while(list != NULL) { if(list->iNode->tag == team) //finding proper team { list->iNode->noe++; tmp = malloc(sizeof(Queue)); tmp->ID = new_member; tmp->next = list->iNode->q; list->iNode->q = tmp; } list = list->next; } }
// 0 ==> key not present previously and new val successfully added // 1 ==> key present previously and new val successfully updated // -1 ==> error in key insertion and new val NOT added int HashMap_put(HashMap *hm, // IN void *key, // IN void *val, // IN void **prevVal) // OUT { node *n = lookupNode(hm, key); if (n != NULL) { // Key already present in the HashMap, // so simply replace the old val. *prevVal = n->val; n->val = val; return 1; } // Add the new key at the head of the bucket. { const int bucketIndex = getHashIndex(hm, key); node *newNode = malloc(sizeof *newNode); node *prevBucketHead; if (newNode == NULL) { return -1; } // Shallow copy of key. newNode->key = key; newNode->val = val; newNode->prev = NULL; prevBucketHead = hm->buckets[bucketIndex]; if (prevBucketHead != NULL) { prevBucketHead->prev = newNode; } newNode->next = prevBucketHead; hm->buckets[bucketIndex] = newNode; } hm->totalElems++; *prevVal = NULL; return 0; }
Graph * createPostForum(char * file,char * file2, int forumID) { FILE * fp = fopen(file,"r"); if (fp == NULL) perror ("Error opening file"); char buffer[200]; int i = 0, *creator,ID; Node * postNode; fgets(buffer,200,fp); Graph * postGraph = createGraph(M,C); while(fgets(buffer,200,fp)!= NULL) { ID = atoi(strtok (buffer,"|")); if(ID != forumID) continue; // add to post forum postNode = malloc(sizeof(Node)); postNode ->ID = atoi(strtok (NULL,"\n")); postNode->obj = NULL; insertNode(postNode,postGraph); i++; } fclose(fp); //finding posts creators fp = fopen(file2, "r"); //file2 = post_hasCreator_person if (fp == NULL) perror ("Error opening file"); fgets(buffer,200,fp); while(fgets(buffer,200,fp)!= NULL) { if((postNode = lookupNode(atoi(strtok (buffer,"|")), postGraph)) != NULL) { creator = malloc(sizeof(int)); *creator = atoi(strtok (NULL, "\n")); postNode->obj = creator; } } fclose(fp); return postGraph; }
// TRUE ==> key found and removed successfully // FALSE ==> key not found BOOL HashMap_removeEntry(HashMap *hm, // IN const void *key) // IN { node *n = lookupNode(hm, key); if (!n) { return FALSE; } node *prevNode = n->prev; node *nextNode = n->next; if (prevNode != NULL) { prevNode->next = nextNode; } if (nextNode != NULL) { nextNode->prev= prevNode; } // Check whether head node of bucket is being removed. { const int bucketHead = getHashIndex(hm, key); if (hm->buckets[bucketHead] == n) { assert(prevNode == NULL); hm->buckets[bucketHead] = nextNode; } } // Only a reference to key was stored(shallow copy), so need to free the key. free(n); hm->totalElems--; return TRUE; }
void groupInterests(Graph * g,Graph * interGraph,Node * n,int mode) { int i,j, c1, c2, sum1,sum2, index; Person *p1, *p = n->obj, * ptmp = NULL; Node *n1, * inter = NULL; p->distN = 1; List * l; Queue *q = NULL, * end = NULL, * start = NULL, * tmp = NULL; //teams list InterObj * obj; InterList * team,*prev1,*prev2; Interests * inter1, * inter2, *swap; for(i = 0; i < p->inter_num; i++) //inserting interests { if ((n1 = lookupNode(p->interests[0][i],interGraph)) == NULL) { p->interests[1][i] = 1; obj = malloc(sizeof(InterObj)); obj->mmax = 0; obj->wmax = 0; obj->next_num = 2; //next team number obj->team = malloc(sizeof(InterList)); //interest team list obj->team->next = NULL; obj->team->iNode = malloc(sizeof(Interests)); //team node obj->team->iNode->tag = 1; //team number obj->team->iNode->noe = 1; //team number of members obj->team->iNode->q = malloc(sizeof(Queue)); obj->team->iNode->q->ID = n->ID; //member ID obj->team->iNode->q->next = NULL; inter = malloc(sizeof(Node)); inter->ID = p->interests[0][i]; inter->obj = obj; insertNode(inter,interGraph); } else { obj = n1->obj; p->interests[1][i] = 1; team = malloc(sizeof(InterList)); team->next = NULL; team->iNode = malloc(sizeof(Interests)); team->iNode->tag = 1; team->iNode->noe = 1; team->iNode->q = malloc(sizeof(Queue)); team->iNode->q->ID = n->ID; team->iNode->q->next = NULL; obj->next_num = 2; obj->team = team; } } end = malloc(sizeof(Queue)); end->ID = n->ID; end->next = NULL; q = start = end; while(q != NULL) { n = lookupNode(q->ID,g); p = n->obj; l = p->list; while(l != NULL) { n1 = lookupNode(l->neighbor->ID,g); p1 = n1->obj; if(!strcmp(p->nProp->prop[2],p1->nProp->prop[2])) //same gender { c1 = c2 = 0; if((sum1 = p->inter_num) == 0) break; if((sum2 = p1->inter_num) == 0) { l = l->next; continue; } while(c2<sum2) { n = lookupNode(q->ID,g); if (p->interests[0][c1] == p1->interests[0][c2]) //common interest { if(p1->interests[1][c2] == 0) //node not found { p1->interests[1][c2] = p->interests[1][c1]; updateQueue(p1->interests[0][c2],p->interests[1][c1],n1->ID,interGraph); } else if(p->interests[1][c1] == p1->interests[1][c2]) //same team with parent node { if(c1 < sum1-1) c1++; c2++; continue; } else //node - parent node have different interest team { inter = lookupNode(p1->interests[0][c2],interGraph); //common interest in interestGraph obj = inter->obj; team = obj->team; swap = inter1 = inter2 = NULL; prev1 = prev2 = NULL; while(inter1 == NULL || inter2 == NULL) { if (inter1 == NULL) { if(team->iNode->tag == p->interests[1][c1]) inter1 = team->iNode; else prev1 = team; } if (inter2 == NULL){ if(team->iNode->tag == p1->interests[1][c2]) inter2 = team->iNode; else prev2 = team; } team = team->next; } if(inter1->noe < inter2->noe) { swap = inter1; inter1 = inter2; inter2 = swap; prev2 = prev1; } inter1->noe += inter2->noe; //updating number of members after the 2 team union tmp = inter2->q; //absorbed list while(tmp != NULL) { n = lookupNode(tmp->ID,g); ptmp = n->obj; index = binary_search(ptmp->interests[0],ptmp->inter_num,p1->interests[0][c2]); ptmp->interests[1][index] = inter1->tag; //absorbing list if (tmp->next == NULL) // unifying teams { tmp->next = inter1->q; inter1->q = inter2->q; break; } else tmp = tmp->next; } if (prev2 != NULL) { prev1 = prev2->next; prev2->next = prev2->next->next; } else { prev1 = obj->team; obj->team = obj->team->next; } free(prev1->iNode); //freeing the absorbed list free(prev1); } if(c1 < sum1-1) //checking for end of interest list c1++; c2++; } else if(p->interests[0][c1] < p1->interests[0][c2] && c1 < sum1-1) //checking for common interests c1++; else { if (p1->interests[1][c2] != 0) { c2++; continue; } inter = NULL; obj = NULL; if ((n = lookupNode(p1->interests[0][c2],interGraph)) == NULL) { p1->interests[1][c2] = 1; //marking inter = malloc(sizeof(Node)); inter->ID = p1->interests[0][c2]; inter->obj = malloc(sizeof(InterObj)); obj = inter->obj; obj->mmax = 0; obj->wmax = 0; obj->next_num = 2; obj->team = malloc(sizeof(InterList)); obj->team->next = NULL; obj->team->iNode = malloc(sizeof(Interests)); obj->team->iNode->tag = 1; obj->team->iNode->noe = 1; obj->team->iNode->q = malloc(sizeof(Queue)); obj->team->iNode->q->ID = n1->ID; obj->team->iNode->q->next = NULL; insertNode(inter,interGraph); } else { obj = n->obj; p1->interests[1][c2] = obj->next_num; team = malloc(sizeof(InterList)); team->next = obj->team; team->iNode = malloc(sizeof(Interests)); team->iNode->tag = obj->next_num; team->iNode->noe = 1; team->iNode->q = malloc(sizeof(Queue)); team->iNode->q->ID = n1->ID; team->iNode->q->next = NULL; obj->next_num++; obj->team = team; } c2++; } } if(p1->distN == -1) //checking if we visited the current node in the past { p1->distN = 1; end->next = malloc(sizeof(Queue)); end->next->ID = n1->ID; end->next->next = NULL; end = end->next; } } l = l->next; } tmp = q; q = q->next; free(tmp); } //find max and deleting lists for(i=0;i<interGraph->size;i++) { for(j=0;j<interGraph->position[i];j++) { obj = interGraph->table[i][j]->obj; if (obj->next_num == 1) continue; while(obj->team != NULL) { if (mode == 0) //men's max { if(obj->mmax < obj->team->iNode->noe) obj->mmax = obj->team->iNode->noe; } else //women's max { if(obj->wmax < obj->team->iNode->noe) obj->wmax = obj->team->iNode->noe; } while(obj->team->iNode->q != NULL) { tmp = obj->team->iNode->q; obj->team->iNode->q = obj->team->iNode->q->next; free(tmp); } team = obj->team; free(obj->team->iNode); obj->team = obj->team->next; free(team); } obj->next_num = 1; obj->team = NULL; } } }
void insertLikes(char * file, Graph * postGraph, Graph * trustGraph, Graph * g) { FILE * fp = fopen(file,"r"); //person_likes_post if (fp == NULL) { perror ("Error opening file"); exit(-1); } int ID; char buffer[200]; Node * n, * n1; Person * p, * p1; List * l; LikeCom * likes, * tmp, * previous; int creatorID; fgets(buffer,200,fp); while(fgets(buffer,200,fp)!= NULL) { ID = atoi(strtok (buffer,"|")); n = lookupNode(atoi(strtok(NULL,"|")),postGraph); if (n == NULL) continue; creatorID = *(int *)n->obj; n = lookupNode(ID,g); p = n->obj; l = p->list; while(l != NULL) //checking neighbors { if(l->neighbor->ID != creatorID || lookupNode(l->neighbor->ID,trustGraph) == NULL) //if the post creator is not a neighbor of the "liker", continue { l = l->next; continue; } n1 = lookupNode(ID,trustGraph); p1 = n1->obj; if(p1->nProp == NULL) { p1->nProp = (void*)createProperties(3); p1->nProp->prop[0] = (void *)1; //sum of likes p1->nProp->prop[1] = 0; //sum of replies p1->nProp->prop[2] = NULL; // list likes = malloc(sizeof(LikeCom)); likes->creatorID = creatorID; likes->likes_count = 1; likes->replies_count = 0; likes->next = NULL; p1->nProp->prop[2] = likes; l = l->next; continue; } p1->nProp->prop[0]++; likes = p1->nProp->prop[2]; previous = NULL; while(likes != NULL) //while the likes list is not empty, search for a previous like in the "creator" { if(likes->creatorID == creatorID) { likes->likes_count++; break; } if(likes->creatorID > creatorID) { tmp = malloc(sizeof(LikeCom)); tmp->creatorID = creatorID; tmp->likes_count = 1; tmp->replies_count = 0; tmp->next = likes; if (previous != NULL) previous->next = tmp; else p1->nProp->prop[2] = tmp; break; } else if (likes->next == NULL) { likes->next = malloc(sizeof(LikeCom)); likes->next->creatorID = creatorID; likes->next->likes_count = 1; likes->next->replies_count = 0; likes->next->next = NULL; break; } else { previous = likes; likes = likes->next; } } l = l->next; } } fclose(fp); }
double estimateTrust(Node * a, Node * b, Graph * trustGraph) //calculating trust { Person * p = a->obj,*p1; List * l = p->list; Queue * q = NULL, * tmp = NULL,*end = NULL; Node * n = NULL,*n1 = NULL; if(a == b) return 0.0; if(l == NULL) return 0.0; p->distN = 0; q = malloc(sizeof(Queue)); q->ID = l->neighbor->ID; q->next = NULL; n = lookupNode(q->ID,trustGraph); p = n->obj; p->distN = 1; *(double*)p->nProp->prop[0] = *(double *)l->neighbor->eProp->prop[1]; l = l->next; end = q; while(l != NULL) { end->next = malloc(sizeof(Queue)); end->next->ID = l->neighbor->ID; end->next->next = NULL; end = end->next; n = lookupNode(l->neighbor->ID,trustGraph); p = n->obj; p->distN = 1; //init trust *(double*)p->nProp->prop[0] = *(double *)l->neighbor->eProp->prop[1]; //adding trust l = l->next; } while(q->ID != b->ID) { n = lookupNode(q->ID,trustGraph); p = n->obj; l = p->list; while(l != NULL) { n1 = lookupNode(l->neighbor->ID,trustGraph); p1 = n1->obj; if(p1->distN == -1) { end->next = malloc(sizeof(Queue)); end->next->ID = l->neighbor->ID; end->next->next = NULL; end = end->next; p1->distN = p->distN+1; //adding trust *(double*)p1->nProp->prop[0] = (*(double *)l->neighbor->eProp->prop[1])*(*(double *)p->nProp->prop[0]); //edge trust * trust till now } else if(p1->distN == p->distN+1 && (*(double *)l->neighbor->eProp->prop[1])*(*(double *)p->nProp->prop[0] > *(double *)p1->nProp->prop[0])) //if current trust and the product > trust in p1 { *(double *)p1->nProp->prop[0] = (*(double *)l->neighbor->eProp->prop[1])*(*(double *)p->nProp->prop[0]); } else if(p1->distN > p->distN+1) { *(double *)p1->nProp->prop[0] = (*(double *)l->neighbor->eProp->prop[1])*(*(double *)p->nProp->prop[0]); p1->distN = p->distN+1; } l = l->next; } tmp = q; q = q->next; free(tmp); } n = lookupNode(q->ID,trustGraph); p = n->obj; double res = *(double *)p->nProp->prop[0]; while(q != NULL) { tmp = q; q = q->next; free(tmp); } int i,j; for(i=0;i<trustGraph->size;i++) { for(j=0;j<trustGraph->position[i];j++) { p = trustGraph->table[i][j]->obj; p->distN = -1; } } return res; }
void testTidalTrust(int bucketsNumber, int bucketSize) { //create small graph for testing tidal's trust algorithm result Graph* gtt = createGraph(bucketsNumber, bucketSize); TrustNode* n1tt = new TrustNode(1, NULL, NULL); Properties* tidalTrustProperties1 = new Properties(2); tidalTrustProperties1->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties1->setIntegerProperty(INT_MAX, 1); //last updated depth n1tt->setTidalTrustProperties(tidalTrustProperties1); TrustNode* n2tt = new TrustNode(2, NULL, NULL); Properties* tidalTrustProperties2 = new Properties(2); tidalTrustProperties2->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties2->setIntegerProperty(INT_MAX, 1); //last updated depth n2tt->setTidalTrustProperties(tidalTrustProperties2); TrustNode* n3tt = new TrustNode(3, NULL, NULL); Properties* tidalTrustProperties3 = new Properties(2); tidalTrustProperties3->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties3->setIntegerProperty(INT_MAX, 1); //last updated depth n3tt->setTidalTrustProperties(tidalTrustProperties3); TrustNode* n4tt = new TrustNode(4, NULL, NULL); Properties* tidalTrustProperties4 = new Properties(2); tidalTrustProperties4->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties4->setIntegerProperty(INT_MAX, 1); //last updated depth n4tt->setTidalTrustProperties(tidalTrustProperties4); TrustNode* n5tt = new TrustNode(5, NULL, NULL); Properties* tidalTrustProperties5 = new Properties(2); tidalTrustProperties5->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties5->setIntegerProperty(INT_MAX, 1); //last updated depth n5tt->setTidalTrustProperties(tidalTrustProperties5); TrustNode* n6tt = new TrustNode(6, NULL, NULL); Properties* tidalTrustProperties6 = new Properties(2); tidalTrustProperties6->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties6->setIntegerProperty(INT_MAX, 1); //last updated depth n6tt->setTidalTrustProperties(tidalTrustProperties6); TrustNode* n7tt = new TrustNode(7, NULL, NULL); Properties* tidalTrustProperties7 = new Properties(2); tidalTrustProperties7->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties7->setIntegerProperty(INT_MAX, 1); //last updated depth n7tt->setTidalTrustProperties(tidalTrustProperties7); TrustNode* n8tt = new TrustNode(8, NULL, NULL); Properties* tidalTrustProperties8 = new Properties(2); tidalTrustProperties8->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties8->setIntegerProperty(INT_MAX, 1); //last updated depth n8tt->setTidalTrustProperties(tidalTrustProperties8); TrustNode* n9tt = new TrustNode(9, NULL, NULL); Properties* tidalTrustProperties9 = new Properties(2); tidalTrustProperties9->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties9->setIntegerProperty(INT_MAX, 1); //last updated depth n9tt->setTidalTrustProperties(tidalTrustProperties9); TrustNode* n10tt = new TrustNode(10, NULL, NULL); Properties* tidalTrustProperties10 = new Properties(2); tidalTrustProperties10->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties10->setIntegerProperty(INT_MAX, 1); //last updated depth n10tt->setTidalTrustProperties(tidalTrustProperties10); TrustNode* n11tt = new TrustNode(11, NULL, NULL); Properties* tidalTrustProperties11 = new Properties(2); tidalTrustProperties11->setDoubleProperty(-1.0, 0); //trustValue tidalTrustProperties11->setIntegerProperty(INT_MAX, 1); //last updated depth n11tt->setTidalTrustProperties(tidalTrustProperties11); insertNode(n1tt, gtt); insertNode(n2tt, gtt); insertNode(n3tt, gtt); insertNode(n4tt, gtt); insertNode(n5tt, gtt); insertNode(n6tt, gtt); insertNode(n7tt, gtt); insertNode(n8tt, gtt); insertNode(n9tt, gtt); insertNode(n10tt, gtt); insertNode(n11tt, gtt); Edge* e1tt = setEdgeTrustProperties(1, 2, 1.0); Edge* e2tt = setEdgeTrustProperties(1, 5, 1.0); Edge* e3tt = setEdgeTrustProperties(2, 3, 0.9); Edge* e4tt = setEdgeTrustProperties(2, 4, 0.9); Edge* e5tt = setEdgeTrustProperties(3, 6, 0.8); Edge* e6tt = setEdgeTrustProperties(4, 6, 0.3); Edge* e7tt = setEdgeTrustProperties(4, 7, 0.9); Edge* e8tt = setEdgeTrustProperties(5, 10, 0.9); Edge* e9tt = setEdgeTrustProperties(6, 9, 1.0); Edge* e10tt = setEdgeTrustProperties(7, 8, 1.0); Edge* e11tt = setEdgeTrustProperties(8, 9, 1.0); Edge* e12tt = setEdgeTrustProperties(9, 11, 1.0); Edge* e13tt = setEdgeTrustProperties(10, 11, 0.4); /* Insert edges in graph */ insertEdge(1, e1tt, gtt); insertEdge(1, e2tt, gtt); insertEdge(2, e3tt, gtt); insertEdge(2, e4tt, gtt); insertEdge(3, e5tt, gtt); insertEdge(4, e6tt, gtt); insertEdge(4, e7tt, gtt); insertEdge(5, e8tt, gtt); insertEdge(6, e9tt, gtt); insertEdge(7, e10tt, gtt); insertEdge(8, e11tt, gtt); insertEdge(9, e12tt, gtt); insertEdge(10, e13tt, gtt); Node *att = lookupNode(1, gtt); Node *btt = lookupNode(11, gtt); //Estimate trust(1,11) double trust1_11 = estimateTrust(att, btt, gtt); CHECKDOUBLE("Graph estimate trust (1,11)", trust1_11, 0.36); //Estimate trust(1,9) Node *ctt = lookupNode(9, gtt); double trust1_9 = estimateTrust(att, ctt, gtt); CHECKDOUBLE("Graph estimate trust (1,9)", trust1_9, 0.72); }
void addTrust(Graph * trustGraph, Graph * g) //insert trust edges in trustGraph { int i,j; Node * n; Person * p; List * l; Properties * props; Edge * e; LikeCom * tmp,*lc; for(i=0; i < trustGraph->size; i++) //for every node in trustGraph, adding edges { for(j=0; j < trustGraph->position[i]; j++) { n = lookupNode(trustGraph->table[i][j]->ID,g); p = n->obj; l = p->list; while(l != NULL) { if((n = lookupNode(l->neighbor->ID,trustGraph)) == NULL) { l = l->next; continue; } props = createProperties(2); setStringProperty("trust",0,props); props->prop[1] = malloc(sizeof(double)); e = createEdge(0,l->neighbor->ID,props); insertEdge(trustGraph->table[i][j]->ID,e,trustGraph); l = l->next; } } } LikeCom * pivot; for(i=0; i < trustGraph->size; i++) //calculating weight { for(j=0; j < trustGraph->position[i]; j++) { p = trustGraph->table[i][j]->obj; if (p->nProp == NULL) { p->prop_num = 2; p->nProp = (void*)createProperties(1); p->nProp->prop[0] = malloc(sizeof(double)); continue; } pivot = p->nProp->prop[2]; l = p->list; while(l != NULL) { while(pivot != NULL) { if (l->neighbor->ID == pivot->creatorID) { *(double*)l->neighbor->eProp->prop[1] = 0; if((int)p->nProp->prop[0] != 0) *(double *) l->neighbor->eProp->prop[1] = 0.3*pivot->likes_count/(int)p->nProp->prop[0]; //calculating likes weight if((int)p->nProp->prop[1] != 0) *(double *) l->neighbor->eProp->prop[1] += 0.7*pivot->replies_count/(int)p->nProp->prop[1]; //calculating replies weight pivot = pivot->next; } l = l->next; } if (l != NULL) l = l->next; } lc = p->nProp->prop[2]; while(lc!=NULL) //free { tmp = lc; lc = lc->next; free(tmp); } p->prop_num = 2; p->nProp->prop[0] = malloc(sizeof(double)); //allocating trust } } }
void insertComments(char * file, Graph * CommentsGraph, Graph * postGraph, Graph * trustGraph, Graph * g) { FILE * fp = fopen(file,"r"); //person_likes_post if (fp == NULL) { perror ("Error opening file"); exit(-1); } int commentID, com_creatorID, post_creatorID; char buffer[200]; Node * n, * n1; Person * p, * p1; List * l; LikeCom * replies, * tmp, * previous; fgets(buffer,200,fp); while(fgets(buffer,200,fp)!= NULL) { commentID = atoi(strtok (buffer,"|")); n1 = lookupNode(commentID,CommentsGraph); if (n1 == NULL) //comment does not exist in CommentsGraph continue; n = lookupNode(atoi(strtok(NULL,"|")),postGraph); if (n == NULL) //post does not exist in postGraph continue; if(n->obj == NULL || n1->obj == NULL) continue; post_creatorID = *(int *)n->obj; com_creatorID = *(int *)n1->obj; if((n = lookupNode(com_creatorID,g))==NULL) // comment creator in graph g continue; p = n->obj; l = p->list; while(l != NULL) { if(l->neighbor->ID != post_creatorID || lookupNode(l->neighbor->ID,trustGraph) == NULL) { l = l->next; continue; } n = lookupNode(com_creatorID,trustGraph); p1 = n->obj; if(p1->nProp == NULL) { p1->nProp = (void*)createProperties(3); p1->nProp->prop[0] = 0; p1->nProp->prop[1] = (void *) 1; p1->nProp->prop[2] = NULL; replies = malloc(sizeof(LikeCom)); replies->creatorID = post_creatorID; replies->likes_count = 0; replies->replies_count = 1; replies->next = NULL; p1->nProp->prop[2] = replies; l = l->next; continue; } p1->nProp->prop[1]++; replies = p1->nProp->prop[2]; previous = NULL; while(replies != NULL) //while the replies list is not empty, search for a previous like in the "creator" { if(replies->creatorID == post_creatorID) { replies->replies_count++; break; } if(replies->creatorID > post_creatorID) { tmp = malloc(sizeof(LikeCom)); tmp->creatorID = post_creatorID; tmp->likes_count = 0; tmp->replies_count = 1; tmp->next = replies; if (previous != NULL) previous->next = tmp; else p1->nProp->prop[2] = tmp; break; } else if (replies->next == NULL) { replies->next = malloc(sizeof(LikeCom)); replies->next->creatorID = post_creatorID; replies->next->likes_count = 0; replies->next->replies_count = 1; replies->next->next = NULL; break; } else { previous = replies; replies = replies->next; } } l = l->next; } } int i,j; for(i=0;i<trustGraph->size;i++){ for(j=0;j<trustGraph->position[i];j++) { p1 = trustGraph->table[i][j]->obj; if (p1->nProp == NULL) continue; replies = p1->nProp->prop[2]; while(replies != NULL) replies = replies->next; } } fclose(fp); }
//Eisagwgh susxetisewn void loadRelationship(char* filename,Graph* A, Graph *B,char* relationship,int number_of_properties,const int* type,int option){ FILE* from = fopen(filename,"r"); char* line=NULL; int line_size=50; int startid,endid; int i; int line_len; Edge* edge; char* stringID; Node* node; char** property; property = (char**)malloc(sizeof(char*)*number_of_properties); if (A!=NULL) insertEdgeProperty(A,relationship); if (B!=A && B!=NULL) insertEdgeProperty(B,relationship); getline(&line,&line_size,from); free(line); line = NULL; while (!feof(from)){ line_len = getline(&line,&line_size,from); if (line_len == -1){ free(line); line=NULL; continue; } stringID = strtok(line,"|"); if (stringID==NULL){ free(line); line = NULL; continue; } startid = atoi(stringID); stringID = strtok(NULL,"|\n"); if (stringID==NULL){ free(line); line = NULL; continue; } endid = atoi(stringID); //An dn uparxei to startID h to endID pame sthn epomenh grammh for (i=0;i<number_of_properties;i++){ property[i] = strtok(NULL,"|\n"); } //An to option=1 tote ama oi susxetiseis einai metaxu 2 komvwn (an dn uparxoun tous prosthetw) //Gia paradeigma sta posts an dn uparxoun ta posts pou kapios exei kanei like/replie ta prosthetw if (option==1){ if (A!=NULL && lookupNode(startid,A)==NULL){ node = setNodeProperties(startid,NULL,NULL,0); insertNode(node,A); } if (A!=B && B!=NULL && lookupNode(endid,B)==NULL) { node = setNodeProperties(endid,NULL,NULL,0); insertNode(node,B); } } if (A!=NULL && lookupNode(startid,A)!=NULL){ edge = setEdgeProperties(startid,endid,property,type,number_of_properties); find_insertEdge(startid,edge,A,relationship); } if (A!=B && B!=NULL && lookupNode(endid,B)!=NULL){ edge = setEdgeProperties(endid,startid,property,type,number_of_properties); find_insertEdge(endid,edge,B,relationship); } free(line); line=NULL; } free(property); fclose(from); }
int main(void) { int m = 2; int c = 3; /*create empty graph*/ Graph* g = createGraph(m, c); /*create node and set node properties*/ Node* n1 = setPersonProperties(5, "lonely", "loner", 29); Node* n2 = setPersonProperties(1, "herald", "kllapi", 22); Node* n3 = setPersonProperties(2, "marialena", "kiriakidi", 25); Node* n4 = setPersonProperties(10, "antonia", "saravanou", 18); Node* n5 = setPersonProperties(6, "manos", "karvounis", 19); Node* n6 = setPersonProperties(3, "giannis", "chronis", 20); Node* n7 = setPersonProperties(4, "christoforos", "sfiggos", 16); Node* n8 = setPersonProperties(7, "stamatis", "xristoforidis", 24); Node* n9 = setPersonProperties(8, "xristos", "mallios", 29); Node* n10 = setPersonProperties(14, "johnny", "depp", 35); Node* n11 = setPersonProperties(12, "fox", "mulder", 29); Node* n12 = setPersonProperties(16, "dana", "scully", 25); /*print person*/ // printPersonProperties(n1); /*insert nodes in graph*/ insertNode(n1, g); insertNode(n2, g); insertNode(n3, g); insertNode(n4, g); insertNode(n5, g); insertNode(n6, g); insertNode(n7, g); insertNode(n8, g); insertNode(n10, g); insertNode(n9, g); insertNode(n11, g); insertNode(n12, g); /* Create edges and set properties */ Edge* e1 = setEdgeProperties(1, 6, "knows", 30, g); Edge* e2 = setEdgeProperties(6, 1, "knows", 30, g); Edge* e3 = setEdgeProperties(1, 2, "knows", 20, g); Edge* e4 = setEdgeProperties(2, 1, "knows", 20, g); Edge* e5 = setEdgeProperties(1, 4, "knows", 30, g); Edge* e6 = setEdgeProperties(4, 1, "knows", 30, g); Edge* e7 = setEdgeProperties(2, 6, "knows", 10, g); Edge* e8 = setEdgeProperties(6, 2, "knows", 10, g); Edge* e9 = setEdgeProperties(4, 3, "knows", 30, g); Edge* e10 = setEdgeProperties(3, 4, "knows", 30, g); Edge* e11 = setEdgeProperties(4, 7, "knows", 30, g); Edge* e12 = setEdgeProperties(7, 4, "knows", 30, g); Edge* e13 = setEdgeProperties(4, 8, "knows", 10, g); Edge* e14 = setEdgeProperties(8, 4, "knows", 10, g); Edge* e15 = setEdgeProperties(3, 10, "knows", 30, g); Edge* e16 = setEdgeProperties(10, 3, "knows", 30, g); Edge* e17 = setEdgeProperties(10, 7, "knows", 30, g); Edge* e18 = setEdgeProperties(7, 10, "knows", 30, g); Edge* e19 = setEdgeProperties(10, 14, "knows", 50, g); Edge* e20 = setEdgeProperties(14, 10, "knows", 50, g); Edge* e21 = setEdgeProperties(14, 12, "knows", 30, g); Edge* e22 = setEdgeProperties(12, 14, "knows", 30, g); Edge* e23 = setEdgeProperties(12, 16, "knows", 30, g); Edge* e24 = setEdgeProperties(16, 12, "knows", 30, g); Edge* e25 = setEdgeProperties(16, 14, "knows", 30, g); Edge* e26 = setEdgeProperties(14, 16, "knows", 30, g); /* Insert edges in graph */ insertEdge(1, e1, g); insertEdge(6, e2, g); insertEdge(1, e3, g); insertEdge(2, e4, g); insertEdge(1, e5, g); insertEdge(4, e6, g); insertEdge(2, e7, g); insertEdge(6, e8, g); insertEdge(4, e9, g); insertEdge(3, e10, g); insertEdge(4, e11, g); insertEdge(7, e12, g); insertEdge(4, e13, g); insertEdge(8, e14, g); insertEdge(3, e15, g); insertEdge(10, e16, g); insertEdge(10, e17, g); insertEdge(7, e18, g); insertEdge(10, e19, g); insertEdge(14, e20, g); insertEdge(14, e21, g); insertEdge(12, e22, g); insertEdge(12, e23, g); insertEdge(16, e24, g); insertEdge(16, e25, g); insertEdge(14, e26, g); /* Perform lookups in the graph */ Node* nl1 = lookupNode(12, g); printPersonProperties(nl1); Node* nl2 = lookupNode(16, g); printPersonProperties(nl2); /* Find shortest path 1-1 */ int spt1 = reachNode1(1, 12, g); CHECK("Shortest path between nodes (1,12)", spt1, 5); int spt2 = reachNode1(14, 14, g); CHECK("Shortest path between nodes (14,14)", spt2, 0); int spt3 = reachNode1(3, 16, g); CHECK("Shortest path between nodes (3,16)", spt3, 3); int spt4 = reachNode1(5, 3, g); CHECK("Shortest path between nodes (5,3)", spt4, INFINITY_REACH_NODE); //reachNode1 must return INFINITY_REACH_NODE defined in defines.h when two nodes are not connected /* Find shortest paths 1-N */ ResultSet* res = reachNodeN(1, g); int i = 0; Pair results[10] = { {2, 1}, {6, 1}, {4, 1}, {3, 2}, {7, 2}, {8, 2}, {10, 3}, {14, 4}, {16, 5}, {12, 5} }; Pair pair; int k; int counter = 0; while (next(res, &pair)) { ++counter; for (k = 0; k < 10; ++k) { if (results[k].ID == pair.ID) { if (results[k].distance == pair.distance) { printf("Shortest path between nodes (%d,%d): Success\n", 1, results[k].ID); } else { printf("Shortest path between nodes (%d,%d): Failed | actual = %3d, expected = %3d\n", 1, results[k].ID, pair.distance, results[k].distance); } break; } } if (k == 10) { printf("ReachNodeN : Failed | Your returned an extra Pair(%d,%d) ", pair.ID, pair.distance); } } delete res; CHECK("Number of pairs in the ResultSet", counter, 10); delete g; return EXIT_SUCCESS; }
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; }