Advertisement
Guest User

dafasfsdf

a guest
Dec 7th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 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. info_map[i.first].node = i.first;
  58. }
  59. info_map[start_node].cost = 0;
  60.  
  61. CostPQ info_pq;
  62. for(auto i:info_map){
  63. info_pq.enqueue(i.second);
  64. }
  65.  
  66. while(!info_map.empty()){
  67. Info current = info_pq.dequeue();
  68. if(current.cost == std::numeric_limits<int>::max()){
  69. break;
  70. }
  71. std::string min_node = current.node;
  72. int min_cost = current.cost;
  73. info_map.erase(min_node);
  74. answer_map.put(min_node,current);
  75.  
  76. for(const auto& d: g.out_nodes(min_node)){
  77. if(!answer_map.has_key(d)){
  78. Info old = info_map[d];
  79. if( info_map[d].cost == std::numeric_limits<int>::max() || info_map[d].cost > (min_cost + g.edge_value(min_node,d))){
  80. info_map[d].cost = (min_cost + g.edge_value(min_node,d));
  81. info_map[d].from = min_node;
  82. info_pq.update(old,info_map[d]);
  83. }
  84. }
  85. }
  86.  
  87. }
  88. return answer_map;
  89.  
  90.  
  91. }
  92.  
  93.  
  94. //Return a queue whose front is the start node (implicit in answer_map) and whose
  95. // rear is the end node
  96. ArrayQueue <std::string> recover_path(const CostMap &answer_map, std::string end_node) {
  97. CostMap temp = answer_map;
  98. ArrayStack <std::string>tempStack;
  99. std::string counter = end_node;
  100. while(temp[counter].from != "?"){
  101. tempStack.push(counter);
  102. counter = temp[counter].from;
  103. }
  104. tempStack.push(counter);
  105.  
  106. ArrayQueue <std::string> minPath;
  107. while(!tempStack.empty()){
  108. minPath.enqueue(tempStack.pop());
  109. }
  110. return minPath;
  111.  
  112. }
  113.  
  114.  
  115. }
  116.  
  117. #endif /* DIJKSTRA_HPP_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement