KShortestPaths.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Created by wrede on 28.06.16.
  3. //
  4. #ifndef GBMOT_KSHORTESTPATHS_H
  5. #define GBMOT_KSHORTESTPATHS_H
  6. #include "../graph/Definitions.h"
  7. namespace algo
  8. {
  9. /**
  10. * Finds the k-shortest paths in a specified graph, from the source vertex to the sink vertex.
  11. * The algorithm provides a global optimal path, thus reducing the overall sum of all path costs
  12. * in trade off for higher cost of one single path.
  13. */
  14. class KShortestPaths
  15. {
  16. private:
  17. /**
  18. * The original/input graph (will not be altered, but can't be const because of boost)
  19. */
  20. DirectedGraph orig_graph_;
  21. /**
  22. * The source vertex. This is where all paths start from
  23. */
  24. Vertex source_;
  25. /**
  26. * The sink vertex. This is where all paths end
  27. */
  28. Vertex sink_;
  29. /**
  30. * A map containing all predecessors for every vertex in every path found so far
  31. */
  32. VertexPredecessorMap paths_;
  33. /**
  34. * The predecessor of the sink vertex on evey path. Useful to iterate all paths
  35. */
  36. std::vector<Vertex> sink_neighbors_;
  37. /**
  38. * Finds the shortest path in the specified graph from source to sink.
  39. * Stores the predecessor vertices and the distances of the vertices.
  40. *
  41. * @param graph The graph to work in
  42. * @param source The vertex to start the path search from
  43. * @param sink The vertex to end the path at
  44. * @param predecessors A map containing the predecessors for every vertex
  45. * @param distances A map containing the distances for every vertex
  46. */
  47. bool FindPath(DirectedGraph& graph, Vertex& source, Vertex& sink,
  48. VertexPredecessorMap& predecessors, VertexDistanceMap& distances);
  49. bool FindPath(DirectedGraph& graph, Vertex& source, Vertex& sink,
  50. VertexPredecessorMap& predecessors);
  51. /**
  52. * Finds a global optimal pair of shortest paths from source to sink in the original graph.
  53. */
  54. void FindPathPair();
  55. /**
  56. * Finds 'count' global optimal shortest paths from source to sink in the original graph.
  57. *
  58. * @param count The number of paths to finds
  59. */
  60. void FindPaths(size_t count);
  61. /**
  62. * Stores the path to the paths member map. Also updates the sink_neighbors.
  63. *
  64. * @param path The path to store
  65. */
  66. void AddPath(VertexPredecessorMap& path);
  67. /**
  68. * Stores the path to the specified multi predecessor map.
  69. *
  70. * @param in The path to store
  71. * @param out The map to store the path in
  72. */
  73. void AddPath(VertexPredecessorMap& in, MultiPredecessorMap& out);
  74. /**
  75. * Stores all paths in the given multi predecessor map into the paths member map.
  76. * Also updates the sink_neighbors.
  77. *
  78. * @param paths A map containing all paths to store
  79. */
  80. void AddPaths(MultiPredecessorMap& paths);
  81. public:
  82. /**
  83. * Initializes the algorithm for the specified graph with the specified source and sink
  84. * vertex to find a path between.
  85. *
  86. * @param input_graph The graph to work with (will not be altered)
  87. * @param source The vertex to start all path searches from
  88. * @param sink The vertex to end all path searches at
  89. */
  90. KShortestPaths(DirectedGraph input_graph, Vertex source, Vertex sink);
  91. /**
  92. * Runs the algorithm to store the specified number of paths.
  93. * These paths can later be retrieved by the GetPaths method.
  94. *
  95. * @param max_path_count The number of paths to find
  96. */
  97. void Run(size_t max_path_count);
  98. /**
  99. * Gets the last found paths.
  100. *
  101. * @param paths The vector to store all found paths in
  102. */
  103. void GetPaths(std::vector<std::vector<Vertex>>& paths);
  104. };
  105. }
  106. #endif //GBMOT_KSHORTESTPATHS_H