KShortestPaths.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * All found paths for every iteration of the current run.
  47. * Corresponds to the original graph.
  48. */
  49. std::vector<MultiPredecessorMap> i_shortest_paths_;
  50. /**
  51. * Creates a copy of the original graph and updates all corresponding
  52. * maps.
  53. */
  54. void CopyOriginalGraph();
  55. /**
  56. * Extends the current copied graph.
  57. * Splits the vertices along all paths at the given iteration into two
  58. * vertices, one for incoming edges and one for outgoing edges. Connects
  59. * the two vertices with an auxiliary edge.
  60. * @param i The targeted shortest paths iteration
  61. */
  62. void ExtendGraph(size_t i);
  63. /**
  64. * Calculates the path costs of every path at the iteration given.
  65. * @param i The targeted shortest paths iteration
  66. */
  67. double OverallCosts(size_t i);
  68. /**
  69. * Interlaces all paths in the given iterations.
  70. * Removes duplicate edges and produces node-disjoint paths.
  71. * @param i The targeted shortest paths iteration
  72. */
  73. void Interlace(size_t i);
  74. /**
  75. * Finds the shortest path in either the original or the copied graph.
  76. * Adds the path to the given iteration of the i_shortest_paths map.
  77. * @param i The targeted shortest paths iteration
  78. * @param original If true, the original graph will be used, else the
  79. * current copied graph will be used.
  80. */
  81. void FindAndAugmentPath(size_t i, bool original);
  82. /**
  83. * Utility variable. Simulates a queue for the edges to add in the next
  84. * edge update call. All edges and vertices correspond to the copied
  85. * graph.
  86. */
  87. std::vector<std::pair<std::pair<Vertex, Vertex>, double>> edges_to_add_;
  88. /**
  89. * Utility variable. Simulates a queue for the edges to remove in the
  90. * next edge update call. All edges and vertices correspond to the
  91. * copied graph.
  92. */
  93. std::vector<std::pair<Vertex, Vertex>> edges_to_remove_;
  94. /**
  95. * Queues the edge between the given two vertices.
  96. * In the next update edges call the edge will be removed.
  97. * @param source The source vertex
  98. * @param target The target vertex
  99. */
  100. void QueueRemoveEdge(Vertex source, Vertex target);
  101. /**
  102. * Queues the edge between the given two vertices.
  103. * In the next update edges call the edge will be added with the given
  104. * weight.
  105. * @param source The source vertex
  106. * @param target The target vertex
  107. * @param weight The weight of the edge
  108. */
  109. void QueueAddEdge(Vertex source, Vertex target, double weight);
  110. /**
  111. * Updates the copied graph with all queued edges.
  112. * First removes the queued edges then adds the queued edges.
  113. */
  114. void UpdateEdges();
  115. public:
  116. /**
  117. * Initializes the k-shortest-paths algorithm for the given graph.
  118. * Uses the source vertex as starting and the sink vertex as target
  119. * vertex.
  120. * @param graph The graph to work with
  121. * @param source The starting vertex
  122. * @param sink The target vertex
  123. */
  124. KShortestPaths(DirectedGraph graph, Vertex source, Vertex sink);
  125. /**
  126. * Runs the algorithm to find the given number of shortest paths.
  127. * The actual number of paths found is the number of entries in the
  128. * MultiPredecessorMap with the sink vertex as the key.
  129. * @param max_path_count The maximum number of paths to find
  130. */
  131. MultiPredecessorMap Run(size_t max_path_count);
  132. };
  133. }
  134. #endif //GBMOT_KSHORTESTPATHS_H