Guest User

Untitled

a guest
Nov 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. parent_ID Gender Name Surname parent1_ID parent2_ID
  2. 0 (indicates end of file)
  3.  
  4. Count TribeFather(or TribeMother depending on the gender) Name Surname
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. using namespace std;
  9.  
  10. class Node
  11. {
  12. public:
  13. int PK;
  14. char Gender;
  15. char Name[31];
  16. char Surname[31];
  17. int P1PK;
  18. int P2PK;
  19. int kids; //for storing members of tribe
  20. int index;
  21. int lastVisited;
  22.  
  23. //nodes to parents
  24. Node * P1;
  25. Node * P2;
  26.  
  27.  
  28. Node(fstream & file)
  29. {
  30. file >> PK;
  31. file >> Gender;
  32. file >> Name;
  33. file >> Surname;
  34. file >> P1PK;
  35. file >> P2PK;
  36. P1 = NULL;
  37. P2 = NULL;
  38. kids = 1;
  39. lastVisited = -1;
  40. };
  41. };
  42.  
  43. void countVisits(Node * vertex, Node * ancestor, int vertCount, Node ** vertices, int ** adjMatrix)
  44. {
  45. if (vertex != NULL)
  46. {
  47. if (vertex->lastVisited!=ancestor->index)
  48. {
  49.  
  50. // Depth first search, for each original parent increment size of the tribe
  51. for (int i = 0; i < vertCount; i++)
  52. {
  53. //if edge exists from parent to children
  54. if (adjMatrix[vertex->index][i] == 1)
  55. {
  56. countVisits(vertices[i], ancestor, vertCount, vertices, adjMatrix);
  57. ancestor->kids++;
  58. }
  59. }
  60. vertex->lastVisited = ancestor->index;
  61. }
  62. }
  63. }
  64.  
  65. int main()
  66. {
  67. //read all the info into array
  68. fstream file;
  69. file.open("tribe.in", ios::in);
  70. char temp[100];
  71. int vertCount = 0;
  72.  
  73. file.getline(temp, 100);
  74. //count how many objects in the file
  75.  
  76. do{
  77. file.getline(temp, 100);
  78. vertCount++;
  79. }while (temp[0] != '0');
  80.  
  81. //reset to beginning
  82. file.clear();
  83. file.seekg(0, ios::beg);
  84.  
  85. //pointer array, which points to graph peaks
  86. Node ** vertices = new Node*[vertCount];
  87. //fill in the array
  88. for (int i = 0; i < vertCount; i++)
  89. {
  90. vertices[i] = new Node(file);
  91. vertices[i]->index = i; //
  92. }
  93.  
  94. //arrange parent pointers
  95. for (int i = 0; i < vertCount; i++)
  96. {
  97. for (int j = 0; j < vertCount; j++)
  98. {
  99. if (vertices[i]->P1PK == vertices[j]->PK)
  100. {
  101. vertices[i]->P1 = vertices[j];
  102. }
  103.  
  104. if (vertices[i]->P2PK == vertices[j]->PK)
  105. {
  106. vertices[i]->P2 = vertices[j];
  107. }
  108. }
  109. }
  110.  
  111.  
  112.  
  113. //create matrix
  114. int **adjMatrix = new int *[vertCount];
  115. for (int i = 0; i < vertCount; i++)
  116. {
  117. adjMatrix[i] = new int[vertCount];
  118. for (int j = 0; j < vertCount; j++) adjMatrix[i][j] = 0;
  119. }
  120.  
  121. //fill in the matrix
  122. for (int i = 0; i < vertCount; i++)
  123. {
  124. if (vertices[i]->P1 != NULL) adjMatrix[vertices[i]->P1->index][i] = 1;
  125. if (vertices[i]->P2 != NULL) adjMatrix[vertices[i]->P2->index][i] = 1;
  126. }
  127.  
  128. //Depth first counting children
  129. for (int i = 0; i<vertCount; i++)
  130. {
  131. if (vertices[i]->P1 == NULL && vertices[i]->P2 == NULL)
  132. {
  133. countVisits(vertices[i], vertices[i], vertCount, vertices, adjMatrix);
  134. }
  135. }
  136.  
  137. //Find parent with largest count of children
  138. int kidsCount = 0;
  139. for (int i = 0; i < vertCount; i++)
  140. {
  141. if (vertices[i]->kids > vertices[kidsCount]->kids)
  142. {
  143. kidsCount = i;
  144. }
  145. else if ((vertices[i]->kids == vertices[kidsCount]->kids) && (vertices[i]->PK < vertices[kidsCount]->PK))
  146. {
  147. kidsCount = i;
  148. }
  149. }
  150.  
  151. //writing result to the output file
  152. fstream file2;
  153. file2.open("tribe.out", fstream::out);
  154. if (vertCount>0)
  155. {
  156.  
  157. file2 << vertices[kidsCount]->kids << ' ';
  158. if (vertices[kidsCount]->Gender == 'W') file2 << "TribeMother" << ' ';
  159. else file2 << "TribeFather" << ' ';
  160. file2 << vertices[kidsCount]->Name << ' ' << vertices[kidsCount]->Surname;
  161. }
  162. file2.close();
  163. file.close();
  164.  
  165. //memory clean-up
  166. for (int i = 0; i < vertCount; i++)
  167. {
  168. delete vertices[i];
  169. }
  170. delete vertices;
  171.  
  172. for (int i = 0; i < vertCount; i++)
  173. {
  174. delete adjMatrix[i];
  175. }
  176.  
  177. delete adjMatrix;
  178. return 0;
  179. }
  180.  
  181. 1 M Adam Father 0 0
  182. 2 W Liza Daughter 1 0
  183. 3 M John Son 0 1
  184. 4 M Oho Child 2 3
  185. 0
  186.  
  187. 4 TribeFather Adam Father
  188.  
  189. 5 TribeFather Adam Father
Add Comment
Please, Sign In to add comment