Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.26 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. string input = "";
  367. vector<string> information;
  368. vector<string> names;
  369. Graph my_graph;
  370. while (!cin.eof()) {
  371. cin >> input;
  372. if (input == "i") {
  373. getline(cin, input);
  374. string current = "";
  375. int end_value = 1;
  376. for (int i = 1; i < input.length(); i++) {
  377. current = current + input[i];
  378. if (i >= input.length() - end_value) {
  379. break;
  380. }
  381. }
  382. bool duplicate = false;
  383. for (int i = 0; i < names.size(); i++) {
  384. if (names[i] == current) {
  385. cout << "failure" << endl;
  386. duplicate = true;
  387. }
  388. }
  389. if (duplicate == true) {
  390. continue;
  391. }
  392. node* city = new node;
  393. adjlist* children1 = new adjlist;
  394. city->children = children1;
  395. city->name = current;
  396. my_graph.insert(city);
  397. cout << "success" << endl;
  398. names.push_back(current);
  399. }
  400.  
  401. else if (input == "setd") {
  402. getline(cin, input);
  403. string current = "";
  404. int end_value = 1;
  405. for (int i = 1; i < input.length(); i++) {
  406. if (input[i] == ';') {
  407. information.push_back(current);
  408. current = "";
  409. continue;
  410. }
  411. current = current + input[i];
  412. if (i == input.length() - end_value) {
  413. information.push_back(current);
  414. }
  415. }
  416. int missing = 0;
  417. string city1 = information[0];
  418. string city2 = information[1];
  419. double distance = stod(information[2]);
  420. for (int i = 0; i < names.size(); i++) {
  421. if (names[i] == city1 || names[i] == city2) {
  422. missing = missing + 1;
  423. }
  424. }
  425. if (missing != 2) {
  426. cout << "failure" << endl;
  427. }
  428. else {
  429.  
  430. cout << "success" << endl;
  431. node* first = my_graph.find_key(city1);
  432. node* second = my_graph.find_key(city2);
  433. first->children->insert(second, distance, first);
  434. second->children->insert(first, distance, second);
  435. first->num_children = 1;
  436. second->num_children = 1;
  437. }
  438. information.clear();
  439. }
  440.  
  441. else if (input == "s") {
  442. getline(cin, input);
  443. int size = my_graph.return_size();
  444. string current = "";
  445. int end_value = 1;
  446. for (int i = 1; i < input.length(); i++) {
  447. current = current + input[i];
  448. if (i >= input.length() - end_value) {
  449. break;
  450. }
  451. }
  452. bool found = false;
  453. for (int i = 0; i < names.size(); i++) {
  454. if (names[i] == current) {
  455. found = true;
  456. }
  457. }
  458. if (found == true) {
  459. cout << "found " << current << endl;
  460. }
  461. else {
  462. cout << "not found" << endl;
  463. }
  464.  
  465. }
  466. else if (input == "graph_nodes") {
  467. int size = my_graph.return_size();
  468. cout << "number of nodes " << size << endl;
  469. getline(cin, input);
  470. }
  471. else if (input == "graph_edges") {
  472. int total = 0;
  473.  
  474. if (my_graph.head == NULL) {
  475. cout << "number of edges " << total << endl;
  476. }
  477. else {
  478. node* start = my_graph.head;
  479. if (start->children != NULL) {
  480. total = total + start->children->paths;
  481. }
  482. while (start->next != NULL) {
  483. start = start->next;
  484. if (start->children != NULL) {
  485. total = total + start->children->paths;
  486. }
  487. }
  488. total = total / 2;
  489. cout << "number of edges " << total << endl;
  490. }
  491. }
  492. else if (input == "degree") {
  493. getline(cin, input);
  494. string current = "";
  495. int end_value = 1;
  496. for (int i = 1; i < input.length(); i++) {
  497. current = current + input[i];
  498. if (i >= input.length() - end_value) {
  499. break;
  500. }
  501. }
  502. bool found = false;
  503. for (int i = 0; i < names.size(); i++) {
  504. if (names[i] == current) {
  505. found = true;
  506. }
  507. }
  508. if (found == true) {
  509. node* start = my_graph.head;
  510. if (start->name == current) {
  511. cout << "degree of " << current << " " << start->children->paths << endl;
  512. }
  513. else {
  514. while (start->next != NULL) {
  515. start = start->next;
  516. if (start->name == current) {
  517. cout << "degree of " << current << " " << start->children->paths << endl;
  518. }
  519. }
  520. }
  521. }
  522. else {
  523. cout << "not found" << endl;
  524. }
  525. }
  526. else if (input == "d") {
  527. getline(cin, input);
  528. string current = "";
  529. string city1;
  530. string city2;
  531. int end_value = 1;
  532. for (int i = 1; i < input.length(); i++) {
  533. if (input[i] == ';') {
  534. city1 = current;
  535. current = "";
  536. continue;
  537. }
  538. current = current + input[i];
  539. if (i == input.length() - end_value) {
  540. city2 = current;
  541. }
  542. }
  543. double distance = 0;
  544. bool city2_found = false;
  545. node* first = my_graph.find_key(city1);
  546. if (city2 == city1) {
  547. cout << "direct distance " << city1 << " to " << city1 << " " << distance << endl;
  548. city2_found = true;
  549. }
  550. else if (first == NULL) {
  551. cout << "failure" << endl;
  552. }
  553. else {
  554. child_node* start = first->children->head;
  555. if (start == NULL) {
  556. cout << "failure" << endl;
  557. }
  558. else {
  559. if (start->point->name == city2) {
  560. cout << "direct distance " << city1 << " to " << city2 << " " << start->distance << endl;
  561. city2_found = true;
  562. }
  563. while (start->next != NULL) {
  564. start = start->next;
  565. if (start->point->name == city2) {
  566. cout << "direct distance " << city1 << " to " << city2 << " " << start->distance << endl;
  567. city2_found = true;
  568. }
  569. }
  570. if (city2_found == false) {
  571.  
  572. cout << "failure" << endl;
  573. }
  574. }
  575. }
  576. }
  577. else if (input == "shortest_d") {
  578. getline(cin, input);
  579. int end_value = 1;
  580. string current = "";
  581. string city1;
  582. string city2;
  583. for (int i = 1; i < input.length(); i++) {
  584. if (input[i] == ';') {
  585. city1 = current;
  586. current = "";
  587. continue;
  588. }
  589. current = current + input[i];
  590. if (i == input.length() - end_value) {
  591. city2 = current;
  592. }
  593. }
  594. //check existence
  595. double distance = 0;
  596. bool city2_found = false;
  597. int index = 0;
  598. int current_size = 0;
  599. int size = my_graph.return_size();
  600. node* first = my_graph.find_key(city1);
  601. if (city2 == city1) {
  602. cout << "shortest distance " << city1 << " to " << city1 << " " << distance << endl;
  603. city2_found = true;
  604. }
  605. else if (first == NULL) {
  606. cout << "failure" << endl;
  607. }
  608. else {
  609. child_node* start = first->children->head;
  610. if (start == NULL) {
  611. cout << "failure" << endl;
  612. }
  613. else {
  614. if (city2_found == false) {
  615. child_node* heap_array[size];
  616. for (int i = 0; i < size; i++) {
  617. heap_array[i] = NULL;
  618. }
  619. Heap my_heap;
  620. node* found_distance = NULL;
  621. my_heap.create(first, heap_array, index, current_size, city2_found);
  622.  
  623. while (current_size >= 1) {
  624. node* pass = heap_array[0]->point;
  625. my_heap.relaxation(pass, heap_array, current_size, city2_found, size, found_distance, city2);
  626. }
  627. if (found_distance != NULL) {
  628. child_node* start = found_distance->children->head;
  629. while (start != NULL) {
  630. if (start->point->name == city2) {
  631. cout << "shortest distance " << city1 << " to " << city2 << " " << start->shortest << endl;
  632. city2_found = true;
  633. }
  634. start = start->next;
  635. }
  636. }
  637. else {
  638. cout << "failure" << endl;
  639. }
  640. }
  641. }
  642. }
  643. //reset
  644. node* start = my_graph.head;
  645. while (start != NULL) {
  646. start->colour = "white";
  647. child_node* copyback = start->children->head;
  648. while (copyback != NULL) {
  649. copyback->path_parent = NULL;
  650. copyback->shortest = copyback->distance;
  651. copyback = copyback->next;
  652. }
  653. start = start->next;
  654. }
  655. }
  656. else if (input == "print_path") {
  657. getline(cin, input);
  658. int end_value = 1;
  659. string current = "";
  660. string city1;
  661. string city2;
  662. for (int i = 1; i < input.length(); i++) {
  663. if (input[i] == ';') {
  664. city1 = current;
  665. current = "";
  666. continue;
  667. }
  668. current = current + input[i];
  669. if (i == input.length() - end_value) {
  670. city2 = current;
  671. }
  672. }
  673.  
  674. double distance = 0;
  675. bool city2_found = false;
  676. int index = 0;
  677. int current_size = 0;
  678. int size = my_graph.return_size();
  679. node* first = my_graph.find_key(city1);
  680. if (city2 == city1) {
  681. cout << city1 << endl;
  682. city2_found = true;
  683. }
  684. else if (first == NULL) {
  685. cout << "failure" << endl;
  686. }
  687. else {
  688. child_node* start = first->children->head;
  689. if (start == NULL) {
  690. cout << "failure" << endl;
  691. }
  692. else {
  693. if (city2_found == false) {
  694. child_node* heap_array[size];
  695. for (int i = 0; i < size; i++) {
  696. heap_array[i] = NULL;
  697. }
  698. Heap my_heap;
  699. node* found_distance = NULL;
  700. my_heap.create(first, heap_array, index, current_size, city2_found);
  701.  
  702. while (current_size >= 1) {
  703. node* pass = heap_array[0]->point;
  704. my_heap.relaxation(pass, heap_array, current_size, city2_found, size, found_distance, city2);
  705. }
  706. if (found_distance != NULL) {
  707. node* citypath[size];
  708. for (int i = 0; i < size; i++) {
  709. citypath[i] = NULL;
  710. }
  711. int cycle = 0;
  712. child_node* start = found_distance->children->head;
  713. while (start != NULL) {
  714. if (start->point->name == city2) {
  715. city2_found = true;
  716. break;
  717. }
  718. start = start->next;
  719. }
  720. citypath[cycle] = start->point;
  721. cycle = cycle + 1;
  722. //path print
  723. while (start->path_parent != NULL) {
  724. citypath[cycle] = start->path_parent->point;
  725. cycle = cycle + 1;
  726. if (start->path_parent == NULL) {
  727. break;
  728. }
  729. start = start->path_parent;
  730. }
  731. citypath[cycle] = start->parent;
  732. string current = "";
  733. for (int i = cycle; i >= 0; i--) {
  734. if (citypath[i] != NULL) {
  735. current = current + citypath[i]->name + " ";
  736. }
  737. }
  738. cout << current << endl;
  739. }
  740. else {
  741. cout << "failure" << endl;
  742. }
  743. }
  744. }
  745. }
  746. //reset
  747. node* start = my_graph.head;
  748. while (start != NULL) {
  749. start->colour = "white";
  750. child_node* copyback = start->children->head;
  751. while (copyback != NULL) {
  752. copyback->path_parent = NULL;
  753. copyback->shortest = copyback->distance;
  754. copyback = copyback->next;
  755. }
  756. start = start->next;
  757. }
  758. }
  759. else if (input == "clear") {
  760. my_graph.~Graph();
  761. names.clear();
  762. cout << "success" << endl;
  763. }
  764.  
  765. if (cin.eof()) {
  766. break;
  767. }
  768. }
  769. return 0;
  770. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement