Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <fstream>
  4. #include <string.h>
  5. #include <sstream>
  6. #include <cassert>
  7. #include "gph_io.h"
  8.  
  9. bool includes(int* list, int n, int v) {
  10. for (int i = 0; i < n; i++) {
  11. if (list[i] == v) {
  12. return (true);
  13. }
  14. }
  15. return (false);
  16. }
  17.  
  18. std::string* file_to_line_array(string filename, int length) {
  19. std::string line;
  20. std::string* lines = new std::string[length + 1];
  21. for (int i = 0; i < length; i++) {
  22. lines[i] = "";
  23. }
  24. ifstream myfile(filename);
  25. assert(myfile);
  26. int i = 0;
  27. while (getline(myfile, line)) {
  28. lines[i] = line;
  29. i++;
  30. };
  31. lines[i] = "_end";
  32. return (lines);
  33. }
  34.  
  35. int find(std::string* lines, std::string line) {
  36. for (int i = 0; lines[i] != "_end"; i++) {
  37. if (lines[i] == line) {
  38. return (i);
  39. }
  40. }
  41. return (-1);
  42. }
  43.  
  44.  
  45.  
  46. //main functions
  47.  
  48. int find_distance(struct graph* g, int v1, int v2) {
  49. int* visited = new int[g->node_count];
  50. int* previous = new int[g->node_count];
  51. int length = 0;
  52. int distance = 1;
  53. bool abort = false;
  54. bool success = false;
  55. std::queue<int> queue;
  56. queue.push(v1);
  57. queue.push(-1);
  58.  
  59. if (v1 == v2) {
  60. return (0);
  61. }
  62. if (g->nodes[index(queue.front())] == nullptr) {
  63. success = false;
  64. abort = true;
  65. }
  66. while (!queue.empty() && !abort) {
  67. if (queue.front() == -1) {
  68. queue.pop();
  69. if (queue.front() == -1 || queue.empty()) {
  70. distance--;
  71. abort = true;
  72. }
  73. distance++;
  74. queue.push(-1);
  75. }
  76. else {
  77. for (edge *t = g->nodes[index(queue.front())]; t != nullptr; t = t->next) {
  78. if (!includes(visited, length, t->target)) {
  79. queue.push(t->target);
  80. visited[length] = t->target;
  81. previous[index(t->target)] = queue.front();
  82. length++;
  83. }
  84. if (t->target == v2) {
  85. success = true;
  86. abort = true;
  87. cout << "found " << distance << endl;
  88. }
  89. }
  90. queue.pop();
  91. }
  92. }
  93. if (success == false) {
  94. cout << "there is no path" << endl;
  95. distance = -1;
  96. }
  97. else if (success == true) {
  98. cout << v2;
  99. for (int x = previous[index(v2)]; x != v1; x = previous[index(x)]) {
  100. cout << " < " << x;
  101. }
  102. cout << " < " << v1;
  103. cout << endl;
  104. }
  105. return (distance);
  106. }
  107.  
  108. int find_maximum_distance(struct graph* g, int v1) {
  109. int* visited = new int[g->node_count];
  110. int length = 0;
  111. int distance = 0;
  112. bool abort = false;
  113. std::queue<int> queue;
  114. queue.push(v1);
  115. queue.push(-1);
  116.  
  117. if (g->nodes[index(queue.front())] == nullptr) {
  118. abort = true;
  119. }
  120. while (!queue.empty() && !abort) {
  121. if (queue.front() == -1) {
  122. queue.pop();
  123. if (queue.front() == -1 || queue.empty()) {
  124. distance--;
  125. abort = true;
  126. }
  127. distance++;
  128. queue.push(-1);
  129. }
  130. else {
  131. for (edge *t = g->nodes[index(queue.front())]; t != nullptr; t = t->next) {
  132. if (!includes(visited, length, t->target)) {
  133. queue.push(t->target);
  134. visited[length] = t->target;
  135. length++;
  136. }
  137.  
  138. }
  139. queue.pop();
  140. }
  141. }
  142. return (distance);
  143. }
  144.  
  145. int get_connected_size(struct graph* g, int v1) {
  146. int* visited = new int[g->node_count];
  147. visited[0] = v1;
  148. int length = 1;
  149. std::queue<int> queue;
  150. queue.push(v1);
  151. while (!queue.empty()) {
  152. for (edge *t = g->nodes[index(queue.front())]; t != nullptr; t = t->next) {
  153. if (!includes(visited, length, t->target)) {
  154. queue.push(t->target);
  155. visited[length] = t->target;
  156. length++;
  157. }
  158. }
  159. queue.pop();
  160. }
  161. return (length);
  162. }
  163. int* get_connected_component(struct graph* g, int v1) {
  164. int* visited = new int[g->node_count];
  165. visited[0] = v1;
  166. int length = 1;
  167. std::queue<int> queue;
  168. queue.push(v1);
  169. while (!queue.empty()) {
  170. for (edge *t = g->nodes[index(queue.front())]; t != nullptr; t = t->next) {
  171. if (!includes(visited, length, t->target)) {
  172. queue.push(t->target);
  173. visited[length] = t->target;
  174. length++;
  175. }
  176. }
  177. queue.pop();
  178. }
  179. return (visited);
  180. }
  181.  
  182.  
  183. void task_find_distance(struct graph* g) {
  184. std::string name1, name2;
  185. int v1, v2;
  186. std::string* legende = file_to_line_array("legende", g->node_count);
  187. cout << "enter the names: " << endl;
  188. std::getline(std::cin, name1);
  189. std::getline(std::cin, name2);
  190. v1 = find(legende, name1) + 1;
  191. v2 = find(legende, name2) + 1;
  192. cout << v1 << endl;
  193. if (v1 != -1 && v2 != -1) {
  194. cout << "the distance is: " << find_distance(g, v1, v2) << endl;
  195. }
  196. else {
  197. cout << "name not found" << endl;
  198. }
  199. }
  200. void task_check_connected(struct graph *g) {
  201. if (g->node_count == get_connected_size(g, 1)) {
  202. cout << "this graph is connected" << endl;
  203. }
  204. else {
  205. cout << "this graph is not connected" << endl;
  206. }
  207. }
  208.  
  209. void task_find_diametre(struct graph* g) {
  210. int v;
  211. cout << "enter a vertex to specify the connected component: " << endl;
  212. cin >> v;
  213. int distance = 0;
  214. int maximum = 0;
  215. int size = get_connected_size(g, v);
  216. int* component = get_connected_component(g, v);
  217.  
  218. for (int i = 0; i < size; i++) {
  219. distance = find_maximum_distance(g, component[i]);
  220. if (distance > maximum) {
  221. maximum = distance;
  222. }
  223. }
  224. cout << "this connected component has diametre: " << distance << endl;
  225. }
  226. int main() {
  227. struct graph* erdos = read_edges_file("erdosgraph");
  228.  
  229. task_find_distance(erdos);
  230. task_check_connected(erdos);
  231. task_find_diametre(erdos);
  232. return (0);
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement