10#include <unordered_map>
15#include "absl/strings/str_cat.h"
22using Parents = std::unordered_map<NodeId, Hop>;
27 Path
path = {{node}, {}};
29 parent =
parents.at(parent.node)) {
49std::optional<NodeId> Expand(
const Graph& graph,
50 const std::vector<std::string>& types, Side& side,
52 std::vector<NodeId> next;
53 for (
NodeId node : side.frontier) {
54 for (Hop hop : graph.Hops(node, types, side.direction)) {
55 Hop parent = {hop.relationship, node};
56 if (!side.parents.try_emplace(hop.node, parent).second)
continue;
57 if (other.parents.contains(hop.node))
return hop.node;
58 next.push_back(hop.node);
61 side.frontier = std::move(next);
67std::optional<int64_t> Weight(
const Graph& graph,
RelationshipId relationship,
68 std::string_view key) {
69 std::optional<Value> value =
70 graph.FindRelationshipProperty(relationship, key);
71 const int64_t* weight = value ? std::get_if<int64_t>(&*value) : nullptr;
72 if (weight ==
nullptr || *weight < 0)
return std::nullopt;
79using Entry = std::pair<int64_t, NodeId>;
80using Queue = std::priority_queue<Entry, std::vector<Entry>, std::greater<>>;
88 std::unordered_map<NodeId, int64_t>
costs;
96std::optional<int64_t> NextCost(WeightedSide& side) {
97 while (!side.queue.empty() &&
98 side.queue.top().first > side.costs.at(side.queue.top().second)) {
101 if (side.queue.empty())
return std::nullopt;
102 return side.queue.top().first;
109void Settle(
const Graph& graph,
const std::vector<std::string>& types,
110 std::string_view key, WeightedSide& side,
const WeightedSide& other,
111 int64_t& best,
NodeId& meet) {
112 auto [cost, node] = side.queue.top();
114 for (Hop hop : graph.Hops(node, types, side.
direction)) {
115 std::optional<int64_t> weight = Weight(graph, hop.relationship, key);
116 if (!weight)
continue;
117 int64_t through = cost + *weight;
118 auto [known, added] = side.costs.try_emplace(hop.node, through);
119 if (!added && through >= known->second)
continue;
120 known->second = through;
121 side.parents[hop.node] = {hop.relationship, node};
122 side.queue.push({through, hop.node});
123 auto there = other.costs.find(hop.node);
124 if (there != other.costs.end() && through + there->second < best) {
125 best = through + there->second;
133std::string Literal(
const Value& value) {
134 if (
const std::string*
text = std::get_if<std::string>(&value)) {
135 return absl::StrCat(
"'", *
text,
"'");
137 return absl::StrCat(std::get<int64_t>(value));
143 std::string_view type,
int hops) {
144 std::vector<std::vector<NodeId>> reached =
WalkHops(graph, start, type, hops);
145 return std::move(reached.back());
149 std::string_view type,
int hops) {
150 std::vector<std::vector<NodeId>> reached = {{start}};
151 for (
int hop = 0; hop < hops; ++hop) {
152 std::vector<NodeId> next;
153 for (
NodeId node : reached.back()) {
156 if (std::find(next.begin(), next.end(), neighbour) == next.end()) {
157 next.push_back(neighbour);
161 reached.push_back(std::move(next));
167 const std::vector<std::string>& types,
169 if (from == to)
return {
Path{{from}, {}}, 1};
175 while (!start.frontier.empty() && !end.frontier.empty()) {
178 bool forwards = start.frontier.size() <= end.frontier.size();
179 std::optional<NodeId> met = forwards ? Expand(graph, types, start, end)
180 : Expand(graph, types, end, start);
184 Path path = PathTo(start.parents, *met);
185 Path back = PathTo(end.parents, *met);
191 return {
path, start.parents.size() + end.parents.size() - 1};
193 return {std::nullopt, start.parents.size() + end.parents.size()};
197 const std::vector<std::string>& types,
199 if (from == to)
return {
Path{{from}, {}}, 1};
200 WeightedSide start = {.direction =
direction,
201 .costs = {{from, 0}},
208 start.queue.push({0, from});
209 end.queue.push({0, to});
210 int64_t best = std::numeric_limits<int64_t>::max();
216 std::optional<int64_t> forwards = NextCost(start);
217 std::optional<int64_t> backwards = NextCost(end);
218 if (!forwards || !backwards || *forwards + *backwards >= best)
break;
219 if (*forwards <= *backwards) {
220 Settle(graph, types, key, start, end, best, meet);
222 Settle(graph, types, key, end, start, best, meet);
225 size_t reached = start.costs.size() + end.costs.size();
226 if (best == std::numeric_limits<int64_t>::max())
227 return {std::nullopt, reached};
228 Path path = PathTo(start.parents, meet);
229 Path back = PathTo(end.parents, meet);
236 return {
path, reached};
247 property.key,
": ", Literal(property.value));
A graph kept in three files of records, one for nodes, one for relationships and one for their proper...
RelationshipRecord relationship(RelationshipId id) const
std::string type(RelationshipId relationship) const
std::vector< NodeId > Neighbours(NodeId node, std::string_view type, Direction direction=Direction::kOut) const
Returns the nodes the hops along relationships of type lead to.
std::string name(NodeId node) const
std::vector< Property > RelationshipProperties(RelationshipId relationship) const
std::vector< Property > properties
std::string PathText(const Graph &graph, const Path &path)
A path as Cypher writes it, with the nodes' names, and each relationship's arrow pointing the way it ...
std::vector< NodeId > Walk(const Graph &graph, NodeId start, std::string_view type, int hops)
Returns the nodes exactly hops relationships of type away from start, each once, in the order a bread...
std::variant< int64_t, std::string > Value
A property's value, as the graph hands it out: a whole number or a string.
constexpr RelationshipId kNoRelationship
The ids at the end of a chain, where there's no next relationship, or no next property.
PathSearch CheapestPath(const Graph &graph, NodeId from, NodeId to, const std::vector< std::string > &types, Direction direction, std::string_view key)
Returns a cheapest path from from to to, along relationships of any of types followed in direction: o...
Direction
A node and a property take 64 bytes, and a relationship 24, so a page holds 170 relationships,...
Direction Reversed(Direction direction)
The direction to follow a relationship in from its other end: out becomes in, in becomes out,...
PathSearch ShortestPath(const Graph &graph, NodeId from, NodeId to, const std::vector< std::string > &types, Direction direction)
Returns a shortest path from from to to, along relationships of any of types followed in direction: o...
std::vector< std::vector< NodeId > > WalkHops(const Graph &graph, NodeId start, std::string_view type, int hops)
Returns the nodes each hop of that walk reaches: the start on its own, then the nodes one hop away,...
std::unordered_map< NodeId, int64_t > costs
std::vector< NodeId > frontier
What a search for a path found: the path, if there is one, and how many nodes the search reached,...
A path through the graph: its nodes, from its start to its end, and the relationships between them,...
std::vector< NodeId > nodes
std::vector< RelationshipId > relationships