Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. void findTrends(int k, Graph *g, Graph *tagg, char **womenTrends, char **menTrends) {
  2. Graph *subGraph;
  3. tagHolder *holder=NULL, tempHolder;
  4. int errorFlag=0, num, i=0, op=0, *trends, size=0, commonInterests=0, gender=0, start=0, graphSize=0;
  5. int spot=0, j=0, *t=NULL, tagId=0, counter=0, lastPos=0, menFlag=0, womenFlag=0;
  6. char result[64];
  7.  
  8. // ----------- Create list of sub-coherent graphs ------------
  9. List listOfSubGraphs;
  10. createList(&listOfSubGraphs, "GraphPtr", &allocateGraphPtr, &destroyGraphPtr, &compareGraphPtr, &assignGraphPtr, &printGraphPtr);
  11. findAllCoherentGraphs(g, &listOfSubGraphs, &errorFlag);
  12. if(errorFlag == EXIT_VALUE){
  13. printf("findAllCoherentSubGraphs: failed!\n");
  14. }
  15. // --------------- Initialise tag data holder ----------------
  16. holder = malloc(listOfSubGraphs.items * sizeof(tagHolder));
  17. for (i=0 ; i<listOfSubGraphs.items ; i++) {
  18. holder[i].gender = -1;
  19. holder[i].tags = NULL;
  20. holder[i].size = -1;
  21. }
  22. // ------ for each subgraph check its common interests -------
  23. listOfSubGraphs.Current = listOfSubGraphs.Head;
  24. for (i=0 ; i<listOfSubGraphs.items ; i++) {
  25. // initialise trends data
  26. trends = malloc(5 * sizeof(int)); assert(trends != NULL);
  27. trends[0] = 5; // holds trends array size
  28. trends[1] = 2; // holds first available spot
  29. for (j=2 ; j<5 ; j++)
  30. trends[j] = -1;
  31. subGraph = (Graph*) listOfSubGraphs.Current->data;
  32. // receive tags of subgraph
  33. commonInterests = graphTrends(subGraph, trends, &start, &errorFlag);
  34. if (! commonInterests) { // array needs to be realloced
  35. size = trends[0];
  36. trends = (int*) realloc(trends, (size+1) * sizeof(int)); // increase the size of array by one
  37. graphTrends(subGraph, trends, &start, &errorFlag); // call again function to continue search
  38. }
  39. // receive gender of subgraph
  40. gender = checkGender(subGraph, &errorFlag); // receive gender of this coherent subgraph
  41. if (gender == 0) // report that the group is of one gender only
  42. if (commonInterests != -1) // but only if this group has a common interest
  43. printf("Mixed genders problem\n");
  44. // receive size of subgraph
  45. graphSize = getNumberOfGraphNodes(subGraph, &errorFlag);
  46. /*printf("gender: %d\n", gender);
  47. printf("Gender %d with size %d has these interests\n", gender, graphSize);
  48. for (op=0 ; op<trends[0] ; op++)
  49. printf("%d-", trends[op]); printf("\n");
  50. printf("Size: %d\n", graphSize);*/
  51. // insert tag data to appropriate place
  52. tempHolder.gender = gender;
  53. tempHolder.tags = trends;
  54. tempHolder.size = graphSize;
  55. insertHolder(holder, tempHolder, listOfSubGraphs.items);
  56. listOfSubGraphs.Current = listOfSubGraphs.Current->next;
  57. if (trends != NULL) free(trends);
  58. }
  59. counter = 0;
  60. lastPos = 0;
  61. // men first ( ! savoir vivre )
  62. for (j=0 ; j<listOfSubGraphs.items ; j++) {
  63. if (holder[j].gender != 1) continue;
  64. t = holder[j].tags;
  65. for (i=2 ; i<t[0] ; i++) {
  66. tagId = t[i];
  67. whatTagIsThis(tagId, tagg, result, &errorFlag);
  68. assert (result != NULL); // NULL means that no tag with such id exists
  69. if (tagId == -1)
  70. strcpy(menTrends[lastPos], "n/a");
  71. else
  72. strcpy(menTrends[lastPos], result);
  73. lastPos ++;
  74. counter ++;
  75. if (counter >= k) break;
  76. }
  77. if (counter >= k) break;
  78. }
  79. printf("\nΤOP MEN TRENDS\n---------------\n");
  80. for (i=0 ; i<k ; i++)
  81. printf("\t%s\n", menTrends[i]); printf("\n");
  82. // women now
  83. counter = 0;
  84. lastPos = 0;
  85. for (j=0 ; j<listOfSubGraphs.items ; j++) {
  86. if (holder[j].gender != 2) continue;
  87. t = holder[j].tags;
  88. for (i=2 ; i<t[0] ; i++) {
  89. tagId = t[i];
  90. whatTagIsThis(tagId, tagg, result, &errorFlag);
  91. assert (result != NULL); // NULL means that no tag with such id exists
  92. if (tagId == -1)
  93. strcpy(womenTrends[lastPos], "n/a");
  94. else
  95. strcpy(womenTrends[lastPos], result);
  96. lastPos ++;
  97. counter ++;
  98. if (counter >= k) break;
  99. }
  100. if (counter >= k) break;
  101. }
  102. printf("\nΤOP WOMEN TRENDS\n---------------\n");
  103. for (i=0 ; i<k ; i++)
  104. printf("\t%s\n", womenTrends[i]); printf("\n");
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement