1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Path finding algorithm implementations.
use super::parsing::extract_float;
use super::{AStarResult, ApspResult, ShortestPathResult};
use crate::graph::Graph;
use crate::utils::escape_string;
use crate::{Result, Value};
impl Graph {
/// Find the shortest path between two nodes using Dijkstra's algorithm.
///
/// # Arguments
///
/// * `source_id` - ID of the source node
/// * `target_id` - ID of the target node
/// * `weight_property` - Optional edge property to use as weight
pub fn shortest_path(
&self,
source_id: &str,
target_id: &str,
weight_property: Option<&str>,
) -> Result<ShortestPathResult> {
let esc_source = escape_string(source_id);
let esc_target = escape_string(target_id);
let query = match weight_property {
Some(wp) => format!(
"RETURN dijkstra(\"{}\", \"{}\", \"{}\")",
esc_source,
esc_target,
escape_string(wp)
),
None => format!("RETURN dijkstra(\"{}\", \"{}\")", esc_source, esc_target),
};
let result = self.connection().cypher(&query)?;
if result.is_empty() {
return Ok(ShortestPathResult {
path: Vec::new(),
distance: None,
found: false,
});
}
let row = &result[0];
// Handle nested column_0 structure
if let Some(Value::Object(data)) = row.get_value("column_0") {
let path = data
.get("path")
.and_then(|v| match v {
Value::Array(arr) => Some(
arr.iter()
.filter_map(|v| match v {
Value::String(s) => Some(s.clone()),
_ => None,
})
.collect(),
),
_ => None,
})
.unwrap_or_default();
let distance = data.get("distance").and_then(|v| match v {
Value::Float(f) => Some(*f),
Value::Integer(i) => Some(*i as f64),
_ => None,
});
let found = data
.get("found")
.and_then(|v| match v {
Value::Bool(b) => Some(*b),
_ => None,
})
.unwrap_or(false);
return Ok(ShortestPathResult {
path,
distance,
found,
});
}
// Direct access
let path = row
.get_value("path")
.and_then(|v| match v {
Value::Array(arr) => Some(
arr.iter()
.filter_map(|v| match v {
Value::String(s) => Some(s.clone()),
_ => None,
})
.collect(),
),
_ => None,
})
.unwrap_or_default();
let distance = row.get_value("distance").and_then(|v| match v {
Value::Float(f) => Some(*f),
Value::Integer(i) => Some(*i as f64),
_ => None,
});
let found = row
.get_value("found")
.and_then(|v| match v {
Value::Bool(b) => Some(*b),
_ => None,
})
.unwrap_or(false);
Ok(ShortestPathResult {
path,
distance,
found,
})
}
/// Find shortest path using A* algorithm with heuristic guidance.
///
/// # Arguments
///
/// * `source_id` - Starting node's id
/// * `target_id` - Target node's id
/// * `lat_prop` - Optional property name for latitude
/// * `lon_prop` - Optional property name for longitude
pub fn astar(
&self,
source_id: &str,
target_id: &str,
lat_prop: Option<&str>,
lon_prop: Option<&str>,
) -> Result<AStarResult> {
let esc_source = escape_string(source_id);
let esc_target = escape_string(target_id);
let query = match (lat_prop, lon_prop) {
(Some(lat), Some(lon)) => format!(
"RETURN astar('{}', '{}', '{}', '{}')",
esc_source, esc_target, lat, lon
),
_ => format!("RETURN astar('{}', '{}')", esc_source, esc_target),
};
let result = self.connection().cypher(&query)?;
if result.is_empty() {
return Ok(AStarResult {
path: Vec::new(),
distance: None,
found: false,
nodes_explored: 0,
});
}
let row = &result[0];
let path = row
.get_value("path")
.and_then(|v| match v {
Value::Array(arr) => Some(
arr.iter()
.filter_map(|v| match v {
Value::String(s) => Some(s.clone()),
_ => None,
})
.collect(),
),
_ => None,
})
.unwrap_or_default();
let distance = row.get_value("distance").and_then(|v| match v {
Value::Float(f) => Some(*f),
Value::Integer(i) => Some(*i as f64),
_ => None,
});
let found = row
.get_value("found")
.and_then(|v| match v {
Value::Bool(b) => Some(*b),
_ => None,
})
.unwrap_or(false);
let nodes_explored = row
.get_value("nodes_explored")
.map(|v| match v {
Value::Integer(i) => *i,
_ => 0,
})
.unwrap_or(0);
Ok(AStarResult {
path,
distance,
found,
nodes_explored,
})
}
/// Compute shortest paths between all pairs of nodes.
///
/// Uses Floyd-Warshall algorithm with O(V³) time complexity.
pub fn apsp(&self) -> Result<Vec<ApspResult>> {
let result = self.connection().cypher("RETURN apsp()")?;
let mut paths = Vec::new();
for row in result.iter() {
let source = row.get_value("source").and_then(|v| match v {
Value::String(s) => Some(s.clone()),
_ => None,
});
let target = row.get_value("target").and_then(|v| match v {
Value::String(s) => Some(s.clone()),
_ => None,
});
if let (Some(source), Some(target)) = (source, target) {
paths.push(ApspResult {
source,
target,
distance: extract_float(row, "distance"),
});
}
}
Ok(paths)
}
}