Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <dag_shortest_path.h>
Public Types | |
using | NodeIndex = typename GraphType::NodeIndex |
using | ArcIndex = typename GraphType::ArcIndex |
Public Member Functions | |
KShortestPathsOnDagWrapper (const GraphType *graph, const std::vector< double > *arc_lengths, absl::Span< const NodeIndex > topological_order, int path_count) | |
void | RunKShortestPathOnDag (absl::Span< const NodeIndex > sources) |
bool | IsReachable (NodeIndex node) const |
const std::vector< NodeIndex > & | reached_nodes () const |
std::vector< double > | LengthsTo (NodeIndex node) const |
std::vector< std::vector< ArcIndex > > | ArcPathsTo (NodeIndex node) const |
std::vector< std::vector< NodeIndex > > | NodePathsTo (NodeIndex node) const |
const GraphType & | graph () const |
Accessors to the underlying graph and arc lengths. | |
const std::vector< double > & | arc_lengths () const |
int | path_count () const |
A wrapper that holds the memory needed to run many k-shortest paths computations efficiently on the given DAG. One call of RunKShortestPathOnDag()
has time complexity O(E
+ kV
log(d)) where d is the mean degree of the graph and space complexity O(kV
). GraphType
can use any of the interfaces defined in util/graph/graph.h
. IMPORTANT: Only use if path_count > 1
(k > 1) otherwise use ShortestPathsOnDagWrapper
.
Definition at line 196 of file dag_shortest_path.h.
using operations_research::KShortestPathsOnDagWrapper< GraphType >::ArcIndex = typename GraphType::ArcIndex |
Definition at line 199 of file dag_shortest_path.h.
using operations_research::KShortestPathsOnDagWrapper< GraphType >::NodeIndex = typename GraphType::NodeIndex |
Definition at line 198 of file dag_shortest_path.h.
operations_research::KShortestPathsOnDagWrapper< GraphType >::KShortestPathsOnDagWrapper | ( | const GraphType * | graph, |
const std::vector< double > * | arc_lengths, | ||
absl::Span< const NodeIndex > | topological_order, | ||
int | path_count ) |
IMPORTANT: All arguments must outlive the class.
The vector of arc_lengths
must be of size graph.num_arcs()
and indexed the same way as in graph
.
You must provide a topological order. You can use util::graph::FastTopologicalSort(graph)
to compute one if you don't already have one. An invalid topological order results in an upper bound for all shortest path computations. For maximum performance, you can further reindex the nodes under the topological order so that the memory access pattern is generally forward instead of random. For example, if the topological order for a graph with 4 nodes is [2,1,0,3], you can re-label the nodes 2, 1, and 0 to 0, 1, and 2 (and updates arcs accordingly).
Validity of arcs and topological order are CHECKed if compiled in DEBUG mode.
SUBTLE: You can modify the graph, the arc lengths or the topological order between calls to the RunKShortestPathOnDag()
function. That's fine. Doing so will obviously invalidate the result API of the last shortest path run, which could return an upper bound, junk, or crash.
KShortestPathsOnDagWrapper implementation.
GraphType
. Memory allocation is done here and only once in order to avoid reallocation at each call of RunKShortestPathOnDag()
for better performance.
Definition at line 492 of file dag_shortest_path.h.
|
inline |
Definition at line 251 of file dag_shortest_path.h.
std::vector< std::vector< typename GraphType::ArcIndex > > operations_research::KShortestPathsOnDagWrapper< GraphType >::ArcPathsTo | ( | NodeIndex | node | ) | const |
Returns the list of all the arcs of the k-shortest paths from node
's source to node
.
Definition at line 678 of file dag_shortest_path.h.
|
inline |
Accessors to the underlying graph and arc lengths.
Definition at line 250 of file dag_shortest_path.h.
bool operations_research::KShortestPathsOnDagWrapper< GraphType >::IsReachable | ( | NodeIndex | node | ) | const |
Returns true if node
is reachable from at least one source, i.e., the length of the shortest path from at least one source is finite.
Definition at line 650 of file dag_shortest_path.h.
std::vector< double > operations_research::KShortestPathsOnDagWrapper< GraphType >::LengthsTo | ( | NodeIndex | node | ) | const |
Returns the lengths of the k-shortest paths from node
's source to node
in increasing order. If there are less than k paths, return all path lengths.
Definition at line 659 of file dag_shortest_path.h.
std::vector< std::vector< typename GraphType::NodeIndex > > operations_research::KShortestPathsOnDagWrapper< GraphType >::NodePathsTo | ( | NodeIndex | node | ) | const |
Returns the list of all the nodes of the k-shortest paths from node
's source to node
(including the source). CHECKs if the node is reachable.
Definition at line 710 of file dag_shortest_path.h.
|
inline |
Definition at line 252 of file dag_shortest_path.h.
|
inline |
Definition at line 234 of file dag_shortest_path.h.
void operations_research::KShortestPathsOnDagWrapper< GraphType >::RunKShortestPathOnDag | ( | absl::Span< const NodeIndex > | sources | ) |
Computes the shortest path to all reachable nodes from the given sources. This must be called before any of the query functions below.
Caching the vector addresses allow to not fetch it on each access.
Avoid reassigning incoming_shortest_path_arc_
at every call for better performance, so it only makes sense for nodes that are reachable from at least one source, the other ones will contain junk.
Definition at line 548 of file dag_shortest_path.h.