Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.52 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 = current_min;
  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] == NULL || 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. }
  269. return;
  270. }
  271. else {
  272. //which one is smaller
  273. if (heap_array[left - 1]->shortest < heap_array[right - 1]->shortest) {
  274. if (heap_array[left - 1]->shortest < heap_array[half - 1]->shortest) {
  275. child_node* swap = heap_array[left - 1];
  276. heap_array[left - 1] = heap_array[half - 1];
  277. heap_array[half - 1] = swap;
  278. }
  279. }
  280. else if (heap_array[right - 1]->shortest < heap_array[left - 1]->shortest) {
  281. if (heap_array[right - 1]->shortest < heap_array[half - 1]->shortest) {
  282. child_node* swap = heap_array[right - 1];
  283. heap_array[right - 1] = heap_array[half - 1];
  284. heap_array[half - 1] = swap;
  285. }
  286. }
  287. else {
  288. // even
  289. if (heap_array[left - 1]->shortest < heap_array[half - 1]->shortest) {
  290. child_node* swap = heap_array[left - 1];
  291. heap_array[left - 1] = heap_array[half - 1];
  292. heap_array[half - 1] = swap;
  293. }
  294. }
  295. }
  296. }
  297.  
  298. void create(node* start, child_node* heap_array[], int& index, int& current_size, bool& already_found) {
  299. child_node* temp = start->children->head;
  300. heap_array[index] = temp;
  301. start->colour = "black";
  302. index = index + 1;
  303. current_size = current_size + 1;
  304. while (temp->next != NULL) {
  305. temp = temp->next;
  306. heap_array[index] = temp;
  307. current_size = current_size + 1;
  308. index = index + 1;
  309. }
  310. int half = current_size / 2;
  311. for (int i = half + 1; i >= 1; i = i -1) {
  312. heapify(heap_array, index, current_size, already_found, i);
  313. }
  314. }
  315. void print(child_node* heap_array[], int size) {
  316. if (heap_array[0] == NULL) {
  317. return;
  318. }
  319. for (int i = 0; i < size; i++) {
  320. if (heap_array[i] != NULL) {
  321. cout << heap_array[i]->point->name << " " << heap_array[i]->shortest<< endl;
  322. }
  323. }
  324. }
  325. };
  326. void search(node* start, int size, string name, node* BFsearch[], int& index, int& position, bool& already_found) {
  327. node* temp = start;
  328. if (temp->name == name) {
  329. cout << "found " << name << endl;
  330. already_found = true;
  331. return;
  332. }
  333. BFsearch[index] = temp;
  334. index = index + 1;
  335. if (temp->num_children == 0) {
  336. return;
  337. }
  338. bool dupl = false;
  339. child_node* cycle = temp->children->head;
  340. BFsearch[index] = cycle->point;
  341. index = index + 1;
  342. while (cycle->next != NULL){
  343. cycle = cycle->next;
  344. for (int i = 0; i < index; i++) {
  345. if (BFsearch[i] == cycle->point) {
  346. bool dupl = true;
  347. }
  348. }
  349. if (dupl == false) {
  350. BFsearch[index] = cycle->point;
  351. index = index + 1;
  352. }
  353. dupl = false;
  354. }
  355. int new_pos = position + 1;
  356. if (new_pos > size) {
  357. return;
  358. }
  359. if (BFsearch[new_pos] != NULL) {
  360. search(BFsearch[new_pos], size, name, BFsearch, index, new_pos, already_found);
  361. }
  362. }
  363.  
  364.  
  365. int main() {
  366. string input = "";
  367. vector<string> information;
  368. vector<string> names;
  369. Graph my_graph;
  370. while (getline(cin,input)) {
  371. string bucket = "";
  372. int bucket_index;
  373. for (int i = 0; i < input.length(); i++) {
  374. if (input[i] == ' ') {
  375. bucket_index = i;
  376. break;
  377. }
  378. bucket = bucket + input[i];
  379. }
  380.  
  381. if (bucket == "i") {
  382. string current = "";
  383. for (int i = bucket_index; i < input.length(); i++) {
  384. if (input[i] == ' ') {
  385. continue;
  386. }
  387. current = current + input[i];
  388. if (i == input.length() - 1) {
  389. break;
  390. }
  391. }
  392. bool duplicate = false;
  393. for (int i = 0; i < names.size(); i++) {
  394. if (names[i] == current) {
  395. cout << "failure" << endl;
  396. duplicate = true;
  397. }
  398. }
  399. if (duplicate == true) {
  400. continue;
  401. }
  402. node* city = new node;
  403. adjlist* children1 = new adjlist;
  404. city->children = children1;
  405. city->name = current;
  406. my_graph.insert(city);
  407. cout << "success" << endl;
  408. names.push_back(current);
  409. }
  410.  
  411. else if (bucket == "setd") {
  412. string current = "";
  413. for (int i = bucket_index; i < input.length(); i++) {
  414. if (input[i] == ' ') {
  415. continue;
  416. }
  417. if (input[i] == ';') {
  418. information.push_back(current);
  419. current = "";
  420. continue;
  421. }
  422. current = current + input[i];
  423. if (i == input.length() - 1) {
  424. information.push_back(current);
  425. }
  426. }
  427. int missing = 0;
  428. string city1 = information[0];
  429. string city2 = information[1];
  430. double distance = stod(information[2]);
  431. cout << city1 << city2 << endl;
  432. for (int i = 0; i < names.size(); i++) {
  433. cout << names[i] << endl;
  434. if (names[i] == city1 || names[i] == city2) {
  435. missing = missing + 1;
  436. }
  437. }
  438. if (missing != 2) {
  439. cout << "failure" << endl;
  440. }
  441. else {
  442.  
  443. cout << "success" << endl;
  444. node* first = my_graph.find_key(city1);
  445. node* second = my_graph.find_key(city2);
  446. first->children->insert(second, distance, first);
  447. second->children->insert(first, distance, second);
  448. first->num_children = 1;
  449. second->num_children = 1;
  450. }
  451. information.clear();
  452. }
  453.  
  454. else if (bucket == "s") {
  455. cin >> input;
  456. int size = my_graph.return_size();
  457. string current = "";
  458. for (int i = 0; i < input.length(); i++) {
  459. current = current + input[i];
  460. if (i == input.length() - 1) {
  461. break;
  462. }
  463. }
  464. node* BFsearch[size];
  465. for (int i = 0; i < size; i++) {
  466. BFsearch[i] = NULL;
  467. }
  468. int index = 0;
  469. int position = 0;
  470. if (my_graph.head == NULL) {
  471. cout << "not found" << endl;
  472. }
  473. else {
  474. node* start = my_graph.head;
  475. bool already_found = false;
  476. while (start != NULL) {
  477. search(start, size, current, BFsearch, index, position, already_found);
  478. if (already_found == true) {
  479. break;
  480. }
  481. index = 0;
  482. position = 0;
  483. start = start->next;
  484. node* BFsearch[size];
  485. for (int i = 0; i < size; i++) {
  486. BFsearch[i] = NULL;
  487. }
  488. }
  489. if (already_found == false) {
  490. cout << "not found" << endl;
  491. }
  492. }
  493. }
  494. else if (bucket == "graph_nodes") {
  495. int size = my_graph.return_size();
  496. cout << "number of nodes " << size << endl;
  497. }
  498. else if (bucket == "graph_edges") {
  499. int total = 0;
  500.  
  501. if (my_graph.head == NULL) {
  502. cout << "number of edges " << total << endl;
  503. }
  504. else {
  505. node* start = my_graph.head;
  506. if (start->children != NULL) {
  507. total = total + start->children->paths;
  508. }
  509. while (start->next != NULL) {
  510. start = start->next;
  511. if (start->children != NULL) {
  512. total = total + start->children->paths;
  513. }
  514. }
  515. total = total / 2;
  516. cout << "number of edges " << total << endl;
  517. }
  518. }
  519. else if (bucket == "degree") {
  520. cin >> input;
  521. string current = "";
  522. for (int i = 0; i < input.length(); i++) {
  523. current = current + input[i];
  524. if (i == input.length() - 1) {
  525. break;
  526. }
  527. }
  528. node* start = my_graph.head;
  529. if (start->name == current) {
  530. cout << "degree of " << current << " " << start->children->paths << endl;
  531. }
  532. else {
  533. while (start->next != NULL) {
  534. start = start->next;
  535. if (start->name == current) {
  536. cout << "degree of " << current << " " << start->children->paths << endl;
  537. }
  538. }
  539. }
  540. }
  541. else if (bucket == "d") {
  542. cin >> input;
  543. string current = "";
  544. string city1;
  545. string city2;
  546. for (int i = 0; i < input.length(); i++) {
  547. if (input[i] == ';') {
  548. city1 = current;
  549. current = "";
  550. continue;
  551. }
  552. current = current + input[i];
  553. if (i == input.length() - 1) {
  554. city2 = current;
  555. }
  556. }
  557. double distance = 0;
  558. bool city2_found = false;
  559. node* first = my_graph.find_key(city1);
  560. if (city2 == city1) {
  561. cout << "direct distance " << city1 << " to " << city1 << " " << distance << endl;
  562. city2_found = true;
  563. }
  564. else if (first == NULL) {
  565. cout << "failure" << endl;
  566. }
  567. else{
  568. child_node* start = first->children->head;
  569. if (start == NULL) {
  570. cout << "failure" << endl;
  571. }
  572. else {
  573. if (start->point->name == city2) {
  574. cout << "direct distance " << city1 << " to " << city2 << " " << start->distance << endl;
  575. city2_found = true;
  576. }
  577. while (start->next != NULL) {
  578. start = start->next;
  579. if (start->point->name == city2) {
  580. cout << "direct distance " << city1 << " to " << city2 << " " << start->distance << endl;
  581. city2_found = true;
  582. }
  583. }
  584. if (city2_found == false) {
  585.  
  586. cout << "failure" << endl;
  587. }
  588. }
  589. }
  590. }
  591. else if (bucket == "shortest_d") {
  592. cin >> input;
  593. string current = "";
  594. string city1;
  595. string city2;
  596. for (int i = 0; i < input.length(); i++) {
  597. if (input[i] == ';') {
  598. city1 = current;
  599. current = "";
  600. continue;
  601. }
  602. current = current + input[i];
  603. if (i == input.length() - 1) {
  604. city2 = current;
  605. }
  606. }
  607. //check existence
  608. double distance = 0;
  609. bool city2_found = false;
  610. int index = 0;
  611. int current_size = 0;
  612. int size = my_graph.return_size();
  613. node* first = my_graph.find_key(city1);
  614. if (city2 == city1) {
  615. cout << "shortest distance " << city1 << " to " << city1 << " " << distance << endl;
  616. city2_found = true;
  617. }
  618. else if (first == NULL) {
  619. cout << "failure" << endl;
  620. }
  621. else {
  622. child_node* start = first->children->head;
  623. if (start == NULL) {
  624. cout << "failure" << endl;
  625. }
  626. else {
  627. if (city2_found == false) {
  628. child_node* heap_array[size];
  629. for (int i = 0; i < size; i++) {
  630. heap_array[i] = NULL;
  631. }
  632. Heap my_heap;
  633. node* found_distance = NULL;
  634. my_heap.create(first, heap_array, index, current_size, city2_found);
  635.  
  636. while(current_size >= 1){
  637. node* pass = heap_array[0]->point;
  638. my_heap.relaxation(pass, heap_array, current_size, city2_found, size, found_distance, city2);
  639. }
  640. if (found_distance != NULL) {
  641. child_node* start = found_distance->children->head;
  642. while (start != NULL) {
  643. if (start->point->name == city2) {
  644. cout << "shortest distance " << city1 << " to " << city2 << " " << start->shortest << endl;
  645. city2_found = true;
  646. }
  647. start = start->next;
  648. }
  649. }
  650. else {
  651. cout << "failure" << endl;
  652. }
  653. }
  654. }
  655. }
  656. //reset
  657. node* start = my_graph.head;
  658. while (start != NULL) {
  659. start->colour = "white";
  660. child_node* copyback = start->children->head;
  661. while (copyback != NULL) {
  662. copyback->path_parent = NULL;
  663. copyback->shortest = copyback->distance;
  664. copyback = copyback->next;
  665. }
  666. start = start->next;
  667. }
  668. }
  669. else if (bucket == "print_path") {
  670. cin >> input;
  671. string current = "";
  672. string city1;
  673. string city2;
  674. for (int i = 0; i < input.length(); i++) {
  675. if (input[i] == ';') {
  676. city1 = current;
  677. current = "";
  678. continue;
  679. }
  680. current = current + input[i];
  681. if (i == input.length() - 1) {
  682. city2 = current;
  683. }
  684. }
  685.  
  686. double distance = 0;
  687. bool city2_found = false;
  688. int index = 0;
  689. int current_size = 0;
  690. int size = my_graph.return_size();
  691. node* first = my_graph.find_key(city1);
  692. if (city2 == city1) {
  693. cout << city1 << endl;
  694. city2_found = true;
  695. }
  696. else if (first == NULL) {
  697. cout << "failure" << endl;
  698. }
  699. else {
  700. child_node* start = first->children->head;
  701. if (start == NULL) {
  702. cout << "failure" << endl;
  703. }
  704. else {
  705. if (city2_found == false) {
  706. child_node* heap_array[size];
  707. for (int i = 0; i < size; i++) {
  708. heap_array[i] = NULL;
  709. }
  710. Heap my_heap;
  711. node* found_distance = NULL;
  712. my_heap.create(first, heap_array, index, current_size, city2_found);
  713.  
  714. while (current_size >= 1) {
  715. node* pass = heap_array[0]->point;
  716. my_heap.relaxation(pass, heap_array, current_size, city2_found, size, found_distance, city2);
  717. }
  718. if (found_distance != NULL) {
  719. node* citypath[size];
  720. for (int i = 0; i < size; i++) {
  721. citypath[i] = NULL;
  722. }
  723. int cycle = 0;
  724. child_node* start = found_distance->children->head;
  725. while (start != NULL) {
  726. if (start->point->name == city2) {
  727. city2_found = true;
  728. break;
  729. }
  730. start = start->next;
  731. }
  732. citypath[cycle] = start->point;
  733. cycle = cycle + 1;
  734. //path print
  735. while (start->path_parent != NULL) {
  736. citypath[cycle] = start->path_parent->point;
  737. cycle = cycle + 1;
  738. if (start->path_parent == NULL) {
  739. break;
  740. }
  741. start = start->path_parent;
  742. }
  743. citypath[cycle] = start->parent;
  744. string current = "";
  745. for (int i = cycle; i >= 0; i--) {
  746. if (citypath[i] != NULL) {
  747. current = current + citypath[i]->name + " ";
  748. }
  749. }
  750. cout << current << endl;
  751. }
  752. else {
  753. cout << "failure" << endl;
  754. }
  755. }
  756. }
  757. }
  758. //reset
  759. node* start = my_graph.head;
  760. while (start != NULL) {
  761. start->colour = "white";
  762. child_node* copyback = start->children->head;
  763. while (copyback != NULL) {
  764. copyback->path_parent = NULL;
  765. copyback->shortest = copyback->distance;
  766. copyback = copyback->next;
  767. }
  768. start = start->next;
  769. }
  770. }
  771. else if (bucket == "clear") {
  772. my_graph.~Graph();
  773. names.clear();
  774. cout << "success" << endl;
  775. }
  776.  
  777. if (cin.eof()) {
  778. break;
  779. }
  780. }
  781. return 0;
  782. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement