Advertisement
Guest User

sdfgasgasdgag

a guest
Dec 7th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #ifndef DIJKSTRA_HPP_
  2. #define DIJKSTRA_HPP_
  3.  
  4. #include <string>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <limits> //Biggest int: std::numeric_limits<int>::max()
  9. #include "array_queue.hpp"
  10. #include "array_stack.hpp"
  11. #include "heap_adjustable_hash_priority_queue.hpp"
  12. #include "hash_graph.hpp"
  13.  
  14.  
  15. namespace ics {
  16.  
  17.  
  18. class Info {
  19. public:
  20. Info() { }
  21.  
  22. Info(std::string a_node) : node(a_node) { }
  23.  
  24. bool operator==(const Info &rhs) const { return node == rhs.node && cost == rhs.cost && from == rhs.from; }
  25.  
  26. bool operator!=(const Info &rhs) const { return !(*this == rhs); }
  27.  
  28. friend std::ostream &operator<<(std::ostream &outs, const Info &i) {
  29. outs << "Info[" << i.node << "," << i.cost << "," << i.from << "]";
  30. return outs;
  31. }
  32.  
  33. //Public instance variable definitions
  34. std::string node = "?";
  35. int cost = std::numeric_limits<int>::max();
  36. std::string from = "?";
  37. };
  38.  
  39.  
  40. bool gt_info(const Info &a, const Info &b) { return a.cost < b.cost; }
  41. int hash_info(const Info &a){return (a.cost == std::numeric_limits<int>::max()) ? a.cost : 0;}
  42.  
  43. //Put needed hashing functions here
  44.  
  45. typedef ics::HashGraph<int> DistGraph;
  46. typedef ics::HeapAdjustableHashPriorityQueue<Info, gt_info,hash_info> CostPQ;
  47. typedef ics::HashOpenMap<std::string, Info, DistGraph::hash_str> CostMap;
  48. typedef ics::pair<std::string, Info> CostMapEntry;
  49.  
  50. //Return the final_map as specified in the lecture-note description of
  51. // extended Dijkstra algorithm
  52. CostMap extended_dijkstra(const DistGraph &g, std::string start_node) {
  53. CostMap answer_map;
  54. CostMap info_map;
  55. for(auto i: g.all_nodes()) {
  56. info_map[i.first] = Info();
  57. }
  58. info_map[start_node].cost = 0;
  59.  
  60. CostPQ info_pq;
  61. for(auto i:info_map){
  62. info_pq.enqueue(i.second);
  63. }
  64.  
  65. while(!info_map.empty()){
  66. Info current = info_pq.dequeue();
  67. if(current.cost == std::numeric_limits<int>::max()){
  68. break;
  69. }
  70. std::string min_node = current.node;
  71. int min_cost = current.cost;
  72. info_map.erase(min_node);
  73. answer_map.put(min_node,current);
  74.  
  75. for(const auto& d: g.out_nodes(min_node)){
  76. if(!answer_map.has_key(d)){
  77. Info old = info_map[d];
  78. if( info_map[d].cost == std::numeric_limits<int>::max() || info_map[d].cost < (min_cost + g.edge_value(min_node,d))){
  79. info_map[min_node].cost = info_map[d].cost;
  80. info_map[d].from = min_node;
  81. info_pq.update(old,info_map[d]);
  82. }
  83. }
  84. }
  85.  
  86. }
  87. return answer_map;
  88.  
  89.  
  90. }
  91.  
  92.  
  93. //Return a queue whose front is the start node (implicit in answer_map) and whose
  94. // rear is the end node
  95. ArrayQueue <std::string> recover_path(const CostMap &answer_map, std::string end_node) {
  96. CostMap temp = answer_map;
  97. ArrayStack <std::string>tempStack;
  98. std::string counter = end_node;
  99. while(temp[counter].from != "?"){
  100. tempStack.push(counter);
  101. counter = temp[counter].from;
  102. }
  103. tempStack.push(counter);
  104.  
  105. ArrayQueue <std::string> minPath;
  106. while(!tempStack.empty()){
  107. minPath.enqueue(tempStack.pop());
  108. }
  109. return minPath;
  110.  
  111. }
  112.  
  113.  
  114. }
  115.  
  116. #endif /* DIJKSTRA_HPP_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement