Advertisement
Guest User

Untitled

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