Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <limits>
  4. #include <vector>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. class adjlist;
  10. class graph;
  11. struct node {
  12. string name;
  13. int num_children = 0;
  14. string colour = "white";
  15. adjlist* children = NULL;
  16. node* next = NULL;
  17. };
  18. struct child_node {
  19. node* point;
  20. child_node* next = NULL;
  21. double distance;
  22. node* parent;
  23. double shortest;
  24. child_node* path_parent = NULL;
  25. };
  26. class adjlist {
  27. private:
  28. public:
  29. child_node* head;
  30. int paths;
  31. adjlist() {
  32. head = NULL;
  33. paths = 0;
  34. }
  35.  
  36. void insert(node* city2, double distance1, node* parent_path) {
  37. child_node* child = new child_node;
  38. child->point = city2;
  39. child->distance = distance1;
  40. child->shortest = distance1;
  41. child->parent = parent_path;
  42. if (head == NULL) {
  43. head = child;
  44. paths = paths + 1;
  45. return;
  46. }
  47. child_node* temp = head;
  48. if (temp->point == city2) {
  49. temp->distance = distance1;
  50. temp->shortest = distance1;
  51. return;
  52. }
  53. while (temp->next != NULL) {
  54. temp = temp->next;
  55. if (temp->point == city2) {
  56. temp->distance = distance1;
  57. temp->shortest = distance1;
  58. return;
  59. }
  60. }
  61. temp->next = child;
  62. paths = paths + 1;
  63. }
  64.  
  65. void print() {
  66. if (head == NULL) {
  67. return;
  68. }
  69. child_node* temp = head;
  70. cout << temp->point->name;
  71. cout << temp->distance;
  72. while (temp->next != NULL) {
  73. temp = temp->next;
  74. cout << temp->point->name;
  75. cout << temp->distance;
  76. }
  77. }
  78. };
  79. class Graph {
  80. private:
  81. int size;
  82. public:
  83. node* head;
  84. Graph() {
  85. size = 0;
  86. head = NULL;
  87. }
  88. ~Graph() {
  89. node* temp = head;
  90. node* nextup;
  91. if (head != NULL) {
  92. if (head->next == NULL) {
  93. delete nextup;
  94. }
  95. else {
  96. while (temp->next != NULL) {
  97. nextup = temp;
  98. temp = temp->next;
  99. delete nextup;
  100. }
  101. }
  102. }
  103. head = NULL;
  104. size = 0;
  105. }
  106.  
  107. void insert(node* city) {
  108. if (head == NULL) {
  109. head = city;
  110. size = size + 1;
  111. return;
  112. }
  113. node* temp = head;
  114. while (temp->next != NULL) {
  115. temp = temp->next;
  116. }
  117. size = size + 1;
  118. temp->next = city;
  119. }
  120.  
  121. node* find_key(string city_name) {
  122. if (head == NULL) {
  123. return NULL;
  124. }
  125. node* temp = head;
  126. if (temp->name == city_name) {
  127. return temp;
  128. }
  129. while (temp->next != NULL) {
  130. temp = temp->next;
  131. if (temp->name == city_name) {
  132. return temp;
  133. }
  134. }
  135. return NULL;
  136. }
  137.  
  138. int return_size() {
  139. return size;
  140. }
  141.  
  142. };
  143. class Heap {
  144. private:
  145. public:
  146. void insert(child_node* heap_array[], int index) {
  147. int temp = index + 1;
  148. int half;
  149. child_node* swap;
  150. while (temp > 1) {
  151. half = temp / 2;
  152. if (heap_array[temp - 1]->shortest < heap_array[half - 1]->shortest) {
  153. child_node* swap = heap_array[half - 1];
  154. heap_array[half - 1] = heap_array[temp - 1];
  155. heap_array[temp - 1] = swap;
  156. }
  157. temp = temp / 2;
  158. }
  159.  
  160. }
  161. void relaxation(node* smallest, child_node* heap_array[], int& current_size, bool& already_found, int size, node*& found_distance, string city) {
  162. int index = size;
  163. node* temp = smallest;
  164. child_node* start = temp->children->head;
  165. child_node* current_min = heap_array[0];
  166. if (current_min->point->name == city) {
  167. found_distance = current_min->parent;
  168. already_found = true;
  169. }
  170. delete_min(heap_array, index, current_size, already_found);
  171. bool check = false;
  172. if (temp->children->head == NULL) {
  173. temp = smallest;
  174. }
  175. else {
  176. bool dupl = false;
  177. bool check = false;
  178. for (int i = 0; i < current_size; i++) {
  179. if (heap_array[i]->point == start->point) {
  180. if (current_min->shortest + start->shortest < heap_array[i]->shortest) {
  181. heap_array[i]->shortest = current_min->shortest + start->shortest;
  182. heap_array[i]->path_parent = current_min;
  183. insert(heap_array, i);
  184. }
  185. dupl = true;
  186. }
  187. }
  188. if (start->point->colour == "black") {
  189. check = true;
  190. }
  191. if (dupl == false && check == false) {
  192. heap_array[current_size] = start;
  193. heap_array[current_size]->path_parent = current_min;
  194. current_size = current_size + 1;
  195. start->shortest = current_min->shortest + start->shortest;
  196. insert(heap_array, current_size - 1);
  197. }
  198. dupl = false;
  199. check = false;
  200. while (start->next != NULL) {
  201. start = start->next;
  202. for (int i = 0; i < current_size; i++) {
  203. if (heap_array[i]->point == start->point) {
  204. if (current_min->shortest + start->shortest < heap_array[i]->shortest) {
  205. heap_array[i]->shortest = current_min->shortest + start->shortest;
  206. heap_array[i]->path_parent = current_min;
  207. insert(heap_array, i);
  208. }
  209. dupl = true;
  210. }
  211. }
  212. if (start->point->colour == "black") {
  213. check = true;
  214. }
  215. if (dupl == false && check == false) {
  216. heap_array[current_size] = start;
  217. heap_array[current_size]->path_parent = current_min;
  218. current_size = current_size + 1;
  219. start->shortest = current_min->shortest + start->shortest;
  220. insert(heap_array, current_size - 1);
  221. }
  222. dupl = false;
  223. check = false;
  224. }
  225. }
  226. if (current_min->point->colour == "white") {
  227. current_min->point->colour = "black";
  228. }
  229. }
  230. void delete_min(child_node* heap_array[], int& index, int& current_size, bool& already_found) {
  231. heap_array[0] = heap_array[current_size - 1];
  232. heap_array[current_size - 1] = NULL;
  233. current_size = current_size - 1;
  234. child_node* l;
  235. child_node* r;
  236. child_node* previous;
  237. int cycle = 1;
  238. while (cycle <= current_size) {
  239. previous = heap_array[cycle - 1];
  240. if (heap_array[(cycle * 2) - 1] == NULL || cycle * 2 > current_size) {
  241. break;
  242. }
  243. heapify(heap_array, index, current_size, already_found, cycle);
  244. l = heap_array[(cycle * 2) - 1];
  245. r = heap_array[cycle * 2];
  246. if (previous == l) {
  247. cycle = cycle * 2;
  248. }
  249. else if (previous == r) {
  250. cycle = cycle * 2 + 1;
  251. }
  252. else {
  253. break;
  254. }
  255. }
  256.  
  257. }
  258. void heapify(child_node* heap_array[], int& index, int& current_size, bool& already_found, int half) {
  259. int left = half * 2;
  260. int right = (half * 2) + 1;
  261. if (heap_array[left - 1] == NULL || left > current_size) {
  262. return;
  263. }
  264. else if (heap_array[right - 1] == NULL) {
  265. if (heap_array[half - 1]->shortest > heap_array[left - 1]->shortest) {
  266. child_node* swap = heap_array[left - 1];
  267. heap_array[left - 1] = heap_array[half - 1];
  268. heap_array[half - 1] = swap;
  269. }
  270. return;
  271. }
  272. else {
  273. //which one is smaller
  274. if (heap_array[left - 1]->shortest < heap_array[right - 1]->shortest) {
  275. if (heap_array[left - 1]->shortest < heap_array[half - 1]->shortest) {
  276. child_node* swap = heap_array[left - 1];
  277. heap_array[left - 1] = heap_array[half - 1];
  278. heap_array[half - 1] = swap;
  279. }
  280. }
  281. else if (heap_array[right - 1]->shortest < heap_array[left - 1]->shortest) {
  282. if (heap_array[right - 1]->shortest < heap_array[half - 1]->shortest) {
  283. child_node* swap = heap_array[right - 1];
  284. heap_array[right - 1] = heap_array[half - 1];
  285. heap_array[half - 1] = swap;
  286. }
  287. }
  288. else {
  289. // even
  290. if (heap_array[left - 1]->shortest < heap_array[half - 1]->shortest) {
  291. child_node* swap = heap_array[left - 1];
  292. heap_array[left - 1] = heap_array[half - 1];
  293. heap_array[half - 1] = swap;
  294. }
  295. }
  296. }
  297. }
  298.  
  299. void create(node* start, child_node* heap_array[], int& index, int& current_size, bool& already_found) {
  300. child_node* temp = start->children->head;
  301. heap_array[index] = temp;
  302. start->colour = "black";
  303. index = index + 1;
  304. current_size = current_size + 1;
  305. while (temp->next != NULL) {
  306. temp = temp->next;
  307. heap_array[index] = temp;
  308. current_size = current_size + 1;
  309. index = index + 1;
  310. }
  311. int half = current_size / 2;
  312. for (int i = half + 1; i >= 1; i = i - 1) {
  313. heapify(heap_array, index, current_size, already_found, i);
  314. }
  315. }
  316. void print(child_node* heap_array[], int size) {
  317. if (heap_array[0] == NULL) {
  318. return;
  319. }
  320. for (int i = 0; i < size; i++) {
  321. if (heap_array[i] != NULL) {
  322. cout << heap_array[i]->point->name << " " << heap_array[i]->shortest << endl;
  323. }
  324. }
  325. }
  326. };
  327. void search(node* start, int size, string name, node* BFsearch[], int& index, int& position, bool& already_found) {
  328. node* temp = start;
  329. if (temp->name == name) {
  330. cout << "found " << name << endl;
  331. already_found = true;
  332. return;
  333. }
  334. BFsearch[index] = temp;
  335. index = index + 1;
  336. if (temp->num_children == 0) {
  337. return;
  338. }
  339. bool dupl = false;
  340. child_node* cycle = temp->children->head;
  341. BFsearch[index] = cycle->point;
  342. index = index + 1;
  343. while (cycle->next != NULL) {
  344. cycle = cycle->next;
  345. for (int i = 0; i < index; i++) {
  346. if (BFsearch[i] == cycle->point) {
  347. bool dupl = true;
  348. }
  349. }
  350. if (dupl == false) {
  351. BFsearch[index] = cycle->point;
  352. index = index + 1;
  353. }
  354. dupl = false;
  355. }
  356. int new_pos = position + 1;
  357. if (new_pos > size) {
  358. return;
  359. }
  360. if (BFsearch[new_pos] != NULL) {
  361. search(BFsearch[new_pos], size, name, BFsearch, index, new_pos, already_found);
  362. }
  363. }
  364.  
  365. int main() {
  366. vector<string> information;
  367. vector<string> names;
  368. Graph my_graph;
  369. while (!cin.eof()) {
  370. string new_line = "";
  371. string input = "";
  372. getline(cin, input);
  373. int indexhold = 0;
  374. for (int i = 0; i < input.length(); i++) {
  375. indexhold = i;
  376. if (input[i] == ' ' || input[i] == '\0' || input[i] == '\n' || input[i] == '\t' || input[i] == '\r' || input[i] == '\v' || input[i] == '\f') {
  377. break;
  378. }
  379. new_line = new_line + input[i];
  380. if (i == input.length() - 1) {
  381. break;
  382. }
  383. }
  384. indexhold = indexhold + 1;
  385. if (new_line == "i") {
  386. string current = "";
  387. for (int i = indexhold; i < input.length(); i++) {
  388. if (input[i] == '\t' || input[i] == '\n' || input[i] == '\r' || input[i] == '\v' || input[i] == '\f') {
  389. break;
  390. }
  391. current = current + input[i];
  392. if (i == input.length() - 1) {
  393. break;
  394. }
  395. }
  396. bool duplicate = false;
  397. for (int i = 0; i < names.size(); i++) {
  398. if (names[i] == current) {
  399. cout << "failure" << endl;
  400. duplicate = true;
  401. }
  402. }
  403. if (duplicate == true) {
  404. continue;
  405. }
  406. node* city = new node;
  407. adjlist* children1 = new adjlist;
  408. city->children = children1;
  409. city->name = current;
  410. my_graph.insert(city);
  411. cout << "success" << endl;
  412. names.push_back(current);
  413. }
  414.  
  415. else if (new_line == "setd") {
  416. string current = "";
  417. for (int i = indexhold; i < input.length(); i++) {
  418. if (input[i] == '\t' || input[i] == '\n' || input[i] == '\r' || input[i] == '\v' || input[i] == '\f') {
  419. information.push_back(current);
  420. break;
  421. }
  422. if (input[i] == ';') {
  423. information.push_back(current);
  424. current = "";
  425. continue;
  426. }
  427. current = current + input[i];
  428. if (i == input.length() - 1) {
  429. information.push_back(current);
  430. break;
  431. }
  432. }
  433. int missing = 0;
  434. string city1 = information[0];
  435. string city2 = information[1];
  436. double distance = stod(information[2]);
  437. for (int i = 0; i < names.size(); i++) {
  438. if (names[i] == city1 || names[i] == city2) {
  439. missing = missing + 1;
  440. }
  441. }
  442. if (missing != 2) {
  443. cout << "failure" << endl;
  444. }
  445. else {
  446.  
  447. cout << "success" << endl;
  448. node* first = my_graph.find_key(city1);
  449. node* second = my_graph.find_key(city2);
  450. first->children->insert(second, distance, first);
  451. second->children->insert(first, distance, second);
  452. first->num_children = 1;
  453. second->num_children = 1;
  454. }
  455. information.clear();
  456. }
  457.  
  458. else if (new_line == "s") {
  459. int size = my_graph.return_size();
  460. string current = "";
  461. for (int i = indexhold; i < input.length(); i++) {
  462. if (input[i] == '\t' || input[i] == '\n' || input[i] == '\r' || input[i] == '\v' || input[i] == '\f') {
  463. break;
  464. }
  465. current = current + input[i];
  466. if (i == input.length()-1) {
  467. break;
  468. }
  469. }
  470. bool found = false;
  471. for (int i = 0; i < names.size(); i++) {
  472. if (names[i] == current) {
  473. found = true;
  474. }
  475. }
  476. if (found == true) {
  477. cout << "found " << current << endl;
  478. }
  479. else {
  480. cout << "not found" << endl;
  481. }
  482.  
  483. }
  484. else if (new_line == "graph_nodes") {
  485. int size = my_graph.return_size();
  486. cout << "number of nodes " << size << endl;
  487. }
  488. else if (new_line == "graph_edges") {
  489. int total = 0;
  490.  
  491. if (my_graph.head == NULL) {
  492. cout << "number of edges " << total << endl;
  493. }
  494. else {
  495. node* start = my_graph.head;
  496. if (start->children != NULL) {
  497. total = total + start->children->paths;
  498. }
  499. while (start->next != NULL) {
  500. start = start->next;
  501. if (start->children != NULL) {
  502. total = total + start->children->paths;
  503. }
  504. }
  505. total = total / 2;
  506. cout << "number of edges " << total << endl;
  507. }
  508. }
  509. else if (new_line == "degree") {
  510. string current = "";
  511. for (int i = indexhold; i < input.length(); i++) {
  512. if (input[i] == '\t' || input[i] == '\n' || input[i] == '\r' || input[i] == '\v' || input[i] == '\f') {
  513. break;
  514. }
  515. current = current + input[i];
  516. if (i == input.length() - 1) {
  517. break;
  518. }
  519. }
  520. bool found = false;
  521. for (int i = 0; i < names.size(); i++) {
  522. if (names[i] == current) {
  523. found = true;
  524. }
  525. }
  526. if (found == true) {
  527. node* start = my_graph.head;
  528. if (start->name == current) {
  529. cout << "degree of " << current << " " << start->children->paths << endl;
  530. }
  531. else {
  532. while (start->next != NULL) {
  533. start = start->next;
  534. if (start->name == current) {
  535. cout << "degree of " << current << " " << start->children->paths << endl;
  536. }
  537. }
  538. }
  539. }
  540. else {
  541. cout << "not found" << endl;
  542. }
  543. }
  544. else if (new_line == "d") {
  545. string current = "";
  546. string city1;
  547. string city2;
  548. for (int i = indexhold; i < input.length(); i++) {
  549. if (input[i] == '\t' || input[i] == '\n' || input[i] == '\r' || input[i] == '\v' || input[i] == '\f') {
  550. city2 = current;
  551. break;
  552. }
  553. if (input[i] == ';') {
  554. city1 = current;
  555. current = "";
  556. continue;
  557. }
  558. current = current + input[i];
  559. if (i == input.length()-1) {
  560. city2 = current;
  561. break;
  562. }
  563. }
  564. double distance = 0;
  565. bool city2_found = false;
  566. node* first = my_graph.find_key(city1);
  567. if (city2 == city1) {
  568. cout << "failure" << endl;
  569. city2_found = true;
  570. }
  571. else if (first == NULL) {
  572. cout << "failure" << endl;
  573. }
  574. else {
  575. child_node* start = first->children->head;
  576. if (start == NULL) {
  577. cout << "failure" << endl;
  578. }
  579. else {
  580. if (start->point->name == city2) {
  581. cout << "direct distance " << city1 << " to " << city2 << " " << start->distance << endl;
  582. city2_found = true;
  583. }
  584. while (start->next != NULL) {
  585. start = start->next;
  586. if (start->point->name == city2) {
  587. cout << "direct distance " << city1 << " to " << city2 << " " << start->distance << endl;
  588. city2_found = true;
  589. }
  590. }
  591. if (city2_found == false) {
  592.  
  593. cout << "failure" << endl;
  594. }
  595. }
  596. }
  597. }
  598. else if (new_line == "shortest_d") {
  599. string current = "";
  600. string city1;
  601. string city2;
  602. for (int i = indexhold; i < input.length(); i++) {
  603. if (input[i] == '\t' || input[i] == '\n' || input[i] == '\r' || input[i] == '\v' || input[i] == '\f') {
  604. city2 = current;
  605. break;
  606. }
  607. if (input[i] == ';') {
  608. city1 = current;
  609. current = "";
  610. continue;
  611. }
  612. current = current + input[i];
  613. if (i == input.length()-1) {
  614. city2 = current;
  615. break;
  616. }
  617. }
  618. //check existence
  619. double distance = 0;
  620. bool city2_found = false;
  621. int index = 0;
  622. int current_size = 0;
  623. int size = my_graph.return_size();
  624. node* first = my_graph.find_key(city1);
  625. if (city2 == city1) {
  626. cout << "shortest distance " << city1 << " to " << city1 << " " << distance << endl;
  627. city2_found = true;
  628. }
  629. else if (first == NULL) {
  630. cout << "failure" << endl;
  631. }
  632. else {
  633. child_node* start = first->children->head;
  634. if (start == NULL) {
  635. cout << "failure" << endl;
  636. }
  637. else {
  638. if (city2_found == false) {
  639. child_node* heap_array[size];
  640. for (int i = 0; i < size; i++) {
  641. heap_array[i] = NULL;
  642. }
  643. Heap my_heap;
  644. node* found_distance = NULL;
  645. my_heap.create(first, heap_array, index, current_size, city2_found);
  646.  
  647. while (current_size >= 1) {
  648. node* pass = heap_array[0]->point;
  649. my_heap.relaxation(pass, heap_array, current_size, city2_found, size, found_distance, city2);
  650. }
  651. if (found_distance != NULL) {
  652. child_node* start = found_distance->children->head;
  653. while (start != NULL) {
  654. if (start->point->name == city2) {
  655. cout << "shortest distance " << city1 << " to " << city2 << " " << start->shortest << endl;
  656. city2_found = true;
  657. }
  658. start = start->next;
  659. }
  660. }
  661. else {
  662. cout << "failure" << endl;
  663. }
  664. }
  665. }
  666. }
  667. //reset
  668. node* start = my_graph.head;
  669. while (start != NULL) {
  670. start->colour = "white";
  671. child_node* copyback = start->children->head;
  672. while (copyback != NULL) {
  673. copyback->path_parent = NULL;
  674. copyback->shortest = copyback->distance;
  675. copyback = copyback->next;
  676. }
  677. start = start->next;
  678. }
  679. }
  680. else if (new_line == "print_path") {
  681. string current = "";
  682. string city1;
  683. string city2;
  684. for (int i = indexhold; i < input.length(); i++) {
  685. if (input[i] == '\t' || input[i] == '\n' || input[i] == '\r' || input[i] == '\v' || input[i] == '\f') {
  686. city2 = current;
  687. break;
  688. }
  689. if (input[i] == ';') {
  690. city1 = current;
  691. current = "";
  692. continue;
  693. }
  694. current = current + input[i];
  695. if (i == input.length() - 1) {
  696. city2 = current;
  697. break;
  698. }
  699. }
  700.  
  701. double distance = 0;
  702. bool city2_found = false;
  703. int index = 0;
  704. int current_size = 0;
  705. int size = my_graph.return_size();
  706. node* first = my_graph.find_key(city1);
  707. if (city2 == city1) {
  708. cout << city1 << endl;
  709. city2_found = true;
  710. }
  711. else if (first == NULL) {
  712. cout << "failure" << endl;
  713. }
  714. else {
  715. child_node* start = first->children->head;
  716. if (start == NULL) {
  717. cout << "failure" << endl;
  718. }
  719. else {
  720. if (city2_found == false) {
  721. child_node* heap_array[size];
  722. for (int i = 0; i < size; i++) {
  723. heap_array[i] = NULL;
  724. }
  725. Heap my_heap;
  726. node* found_distance = NULL;
  727. my_heap.create(first, heap_array, index, current_size, city2_found);
  728.  
  729. while (current_size >= 1) {
  730. node* pass = heap_array[0]->point;
  731. my_heap.relaxation(pass, heap_array, current_size, city2_found, size, found_distance, city2);
  732. }
  733. if (found_distance != NULL) {
  734. node* citypath[size];
  735. for (int i = 0; i < size; i++) {
  736. citypath[i] = NULL;
  737. }
  738. int cycle = 0;
  739. child_node* start = found_distance->children->head;
  740. while (start != NULL) {
  741. if (start->point->name == city2) {
  742. city2_found = true;
  743. break;
  744. }
  745. start = start->next;
  746. }
  747. citypath[cycle] = start->point;
  748. cycle = cycle + 1;
  749. //path print
  750. while (start->path_parent != NULL) {
  751. citypath[cycle] = start->path_parent->point;
  752. cycle = cycle + 1;
  753. if (start->path_parent == NULL) {
  754. break;
  755. }
  756. start = start->path_parent;
  757. }
  758. citypath[cycle] = start->parent;
  759. string current = "";
  760. for (int i = cycle; i >= 0; i--) {
  761. if (citypath[i] != NULL) {
  762. current = current + citypath[i]->name + " ";
  763. }
  764. }
  765. cout << current << endl;
  766. }
  767. else {
  768. cout << "failure" << endl;
  769. }
  770. }
  771. }
  772. }
  773. //reset
  774. node* start = my_graph.head;
  775. while (start != NULL) {
  776. start->colour = "white";
  777. child_node* copyback = start->children->head;
  778. while (copyback != NULL) {
  779. copyback->path_parent = NULL;
  780. copyback->shortest = copyback->distance;
  781. copyback = copyback->next;
  782. }
  783. start = start->next;
  784. }
  785. }
  786. else if (new_line == "clear") {
  787. my_graph.~Graph();
  788. names.clear();
  789. cout << "success" << endl;
  790. }
  791.  
  792. if (cin.eof()) {
  793. break;
  794. }
  795. }
  796. return 0;
  797. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement