C++ Reference

C++ Reference: Graph

shortestpaths.h
Go to the documentation of this file.
1 // Copyright 2010-2021 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // This file contains various shortest paths utilities.
15 //
16 // Keywords: directed graph, cheapest path, shortest path, Dijkstra, spp.
17 
18 #ifndef OR_TOOLS_GRAPH_SHORTESTPATHS_H_
19 #define OR_TOOLS_GRAPH_SHORTESTPATHS_H_
20 
21 #include <cstdint>
22 #include <functional>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include "ortools/base/integral_types.h"
28 #include "ortools/base/macros.h"
29 
30 namespace operations_research {
31 
32 // Dijsktra Shortest path with callback based description of the
33 // graph. The callback returns the distance between two nodes, a
34 // distance of 'disconnected_distance' indicates no arcs between these
35 // two nodes. Ownership of the callback is taken by the function that
36 // will delete it in the end. This function returns true if
37 // 'start_node' and 'end_node' are connected, false otherwise.
38 bool DijkstraShortestPath(int node_count, int start_node, int end_node,
39  std::function<int64_t(int, int)> graph,
40  int64_t disconnected_distance,
41  std::vector<int>* nodes);
42 
43 // Stable version of the Dijsktra Shortest path with callback based description
44 // of the graph. The callback returns the distance between two nodes, a
45 // distance of 'disconnected_distance' indicates no arcs between these
46 // two nodes. Ownership of the callback is taken by the function that
47 // will delete it in the end. This function returns true if
48 // 'start_node' and 'end_node' are connected, false otherwise.
49 bool StableDijkstraShortestPath(int node_count, int start_node, int end_node,
50  std::function<int64_t(int, int)> graph,
51  int64_t disconnected_distance,
52  std::vector<int>* nodes);
53 
54 // Bellman-Ford Shortest path with callback-based description of the
55 // graph. The callback returns the distance between two nodes, a
56 // distance of 'disconnected_distance' indicates no arcs between these
57 // two nodes. Ownership of the callback is taken by the function that
58 // will delete it in the end. This function returns true if
59 // 'start_node' and 'end_node' are connected, false otherwise. If
60 // true, it will fill the 'nodes' vector with the sequence of nodes on
61 // the shortest path between 'start_node' and 'end_node'.
62 bool BellmanFordShortestPath(int node_count, int start_node, int end_node,
63  std::function<int64_t(int, int)> graph,
64  int64_t disconnected_distance,
65  std::vector<int>* nodes);
66 
67 // A* Shortest path with function based description of the
68 // graph. The graph function returns the distance between two nodes, a
69 // distance of 'disconnected_distance' indicates no arcs between these
70 // two nodes. Additionally, the heuristic callback returns a
71 // an approximate distance between the node and the target, which guides
72 // the search. If the heuristic is admissible (ie. never overestimates cost),
73 // the A* algorithm returns an optimal solution.
74 // This function returns true if 'start_node' and 'end_node' are
75 // connected, false otherwise.
76 bool AStarShortestPath(int node_count, int start_node, int end_node,
77  std::function<int64_t(int, int)> graph,
78  std::function<int64_t(int)> heuristic,
79  int64_t disconnected_distance, std::vector<int>* nodes);
80 
81 } // namespace operations_research
82 
83 #endif // OR_TOOLS_GRAPH_SHORTESTPATHS_H_
bool StableDijkstraShortestPath(int node_count, int start_node, int end_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance, std::vector< int > *nodes)
bool DijkstraShortestPath(int node_count, int start_node, int end_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance, std::vector< int > *nodes)
bool AStarShortestPath(int node_count, int start_node, int end_node, std::function< int64_t(int, int)> graph, std::function< int64_t(int)> heuristic, int64_t disconnected_distance, std::vector< int > *nodes)
bool BellmanFordShortestPath(int node_count, int start_node, int end_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance, std::vector< int > *nodes)