KShortestPaths.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // Created by wrede on 25.05.16.
  3. //
  4. #ifndef GBMOT_KSHORTESTPATHS_H
  5. #define GBMOT_KSHORTESTPATHS_H
  6. #include "../graph/Definitions.h"
  7. #include "../core/ObjectData.h"
  8. #include "../core/Tracklet.h"
  9. namespace algo
  10. {
  11. /**
  12. * Class providing a k-shortest-paths algorithm implementation.
  13. */
  14. class KShortestPaths
  15. {
  16. private:
  17. /**
  18. * The original graph, will not be altered.
  19. */
  20. DirectedGraph original_graph_;
  21. /**
  22. * The current copy of the original graph, will be transformed.
  23. */
  24. DirectedGraph copied_graph_;
  25. /**
  26. * The starting vertex for the next algorithm run.
  27. * Corresponds to the original graph.
  28. */
  29. const Vertex source_;
  30. /**
  31. * The target vertex for the next algorithm run.
  32. * Corresponds to the original graph.
  33. */
  34. const Vertex sink_;
  35. /**
  36. * Maps the vertices in the original graph to the vertices in the copied
  37. * graph.
  38. */
  39. std::unordered_map<Vertex, Vertex> original_to_copy_;
  40. /**
  41. * Maps the vertices in the copied graph to the vertices in the original
  42. * graph.
  43. */
  44. std::unordered_map<Vertex, Vertex> copy_to_original_;
  45. /**
  46. * Maps the vertices in the original graph to the distances of the
  47. * shortest path found in the i-th iteration.
  48. */
  49. std::vector<std::unordered_map<Vertex, double>> i_distances_;
  50. /**
  51. * All found paths for every iteration of the current run.
  52. * Corresponds to the original graph.
  53. */
  54. std::vector<MultiPredecessorMap> i_shortest_paths_;
  55. /**
  56. * Creates a copy of the original graph and updates all corresponding
  57. * maps.
  58. */
  59. void CopyOriginalGraph();
  60. /**
  61. * Extends the current copied graph.
  62. * Splits the vertices along all paths at the given iteration into two
  63. * vertices, one for incoming edges and one for outgoing edges. Connects
  64. * the two vertices with an auxiliary edge.
  65. * @param i The targeted shortest paths iteration
  66. */
  67. void ExtendGraph(size_t i);
  68. //TODO comment
  69. void TransformEdgeCosts(size_t i);
  70. /**
  71. * Calculates the path costs of every path at the iteration given.
  72. * @param i The targeted shortest paths iteration
  73. */
  74. double OverallCosts(size_t i);
  75. /**
  76. * Interlaces all paths in the given iterations.
  77. * Removes duplicate edges and produces node-disjoint paths.
  78. * @param i The targeted shortest paths iteration
  79. */
  80. void Interlace(size_t i);
  81. /**
  82. * Finds the shortest path in either the original or the copied graph.
  83. * Adds the path to the given iteration of the i_shortest_paths map.
  84. * @param i The targeted shortest paths iteration
  85. * @param original If true, the original graph will be used, else the
  86. * current copied graph will be used.
  87. */
  88. void FindAndAugmentPath(size_t i, bool original);
  89. /**
  90. * Utility variable. Simulates a queue for the edges to add in the next
  91. * edge update call. All edges and vertices correspond to the copied
  92. * graph.
  93. */
  94. std::vector<std::pair<std::pair<Vertex, Vertex>, double>> edges_to_add_;
  95. /**
  96. * Utility variable. Simulates a queue for the edges to remove in the
  97. * next edge update call. All edges and vertices correspond to the
  98. * copied graph.
  99. */
  100. std::vector<std::pair<Vertex, Vertex>> edges_to_remove_;
  101. /**
  102. * Queues the edge between the given two vertices.
  103. * In the next update edges call the edge will be removed.
  104. * @param source The source vertex
  105. * @param target The target vertex
  106. */
  107. void QueueRemoveEdge(Vertex source, Vertex target);
  108. /**
  109. * Queues the edge between the given two vertices.
  110. * In the next update edges call the edge will be added with the given
  111. * weight.
  112. * @param source The source vertex
  113. * @param target The target vertex
  114. * @param weight The weight of the edge
  115. */
  116. void QueueAddEdge(Vertex source, Vertex target, double weight);
  117. /**
  118. * Updates the copied graph with all queued edges.
  119. * First removes the queued edges then adds the queued edges.
  120. */
  121. void UpdateEdges();
  122. public:
  123. /**
  124. * Initializes the k-shortest-paths algorithm for the given graph.
  125. * Uses the source vertex as starting and the sink vertex as target
  126. * vertex.
  127. * @param graph The graph to work with
  128. * @param source The starting vertex
  129. * @param sink The target vertex
  130. */
  131. KShortestPaths(DirectedGraph graph, Vertex source, Vertex sink);
  132. /**
  133. * Runs the algorithm to find the given number of shortest paths.
  134. * The actual number of paths found is the number of entries in the
  135. * MultiPredecessorMap with the sink vertex as the key.
  136. * @param max_path_count The maximum number of paths to find
  137. */
  138. MultiPredecessorMap Run(size_t max_path_count);
  139. };
  140. }
  141. #endif //GBMOT_KSHORTESTPATHS_H