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 <functional>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "ortools/base/integral_types.h"
27 #include "ortools/base/macros.h"
28 
29 namespace operations_research {
30 
31 // Dijsktra Shortest path with callback based description of the
32 // graph. The callback returns the distance between two nodes, a
33 // distance of 'disconnected_distance' indicates no arcs between these
34 // two nodes. Ownership of the callback is taken by the function that
35 // will delete it in the end. This function returns true if
36 // 'start_node' and 'end_node' are connected, false otherwise.
37 bool DijkstraShortestPath(int node_count, int start_node, int end_node,
38  std::function<int64_t(int, int)> graph,
39  int64_t disconnected_distance,
40  std::vector<int>* nodes);
41 
42 // Stable version of the Dijsktra Shortest path with callback based description
43 // of the graph. The callback returns the distance between two nodes, a
44 // distance of 'disconnected_distance' indicates no arcs between these
45 // two nodes. Ownership of the callback is taken by the function that
46 // will delete it in the end. This function returns true if
47 // 'start_node' and 'end_node' are connected, false otherwise.
48 bool StableDijkstraShortestPath(int node_count, int start_node, int end_node,
49  std::function<int64_t(int, int)> graph,
50  int64_t disconnected_distance,
51  std::vector<int>* nodes);
52 
53 // Bellman-Ford Shortest path with callback-based description of the
54 // graph. The callback returns the distance between two nodes, a
55 // distance of 'disconnected_distance' indicates no arcs between these
56 // two nodes. Ownership of the callback is taken by the function that
57 // will delete it in the end. This function returns true if
58 // 'start_node' and 'end_node' are connected, false otherwise. If
59 // true, it will fill the 'nodes' vector with the sequence of nodes on
60 // the shortest path between 'start_node' and 'end_node'.
61 bool BellmanFordShortestPath(int node_count, int start_node, int end_node,
62  std::function<int64_t(int, int)> graph,
63  int64_t disconnected_distance,
64  std::vector<int>* nodes);
65 
66 // A* Shortest path with function based description of the
67 // graph. The graph function returns the distance between two nodes, a
68 // distance of 'disconnected_distance' indicates no arcs between these
69 // two nodes. Additionally, the heuristic callback returns a
70 // an approximate distance between the node and the target, which guides
71 // the search. If the heuristic is admissible (ie. never overestimates cost),
72 // the A* algorithm returns an optimal solution.
73 // This function returns true if 'start_node' and 'end_node' are
74 // connected, false otherwise.
75 bool AStarShortestPath(int node_count, int start_node, int end_node,
76  std::function<int64_t(int, int)> graph,
77  std::function<int64_t(int)> heuristic,
78  int64_t disconnected_distance, std::vector<int>* nodes);
79 
80 } // namespace operations_research
81 
82 #endif // OR_TOOLS_GRAPH_SHORTESTPATHS_H_
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 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 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)
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)