Repository navigation
Expand file tree
/
Copy pathgraph_query.rs
More file actions
142 lines (115 loc) · 4.78 KB
/
Copy pathgraph_query.rs
File metadata and controls
142 lines (115 loc) · 4.78 KB
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
use crate::types::Memory;
use std::collections::{HashMap, HashSet, VecDeque};
pub struct MemoryGraph {
adj: HashMap<String, Vec<String>>,
}
impl MemoryGraph {
pub fn from_memories(memories: &[Memory]) -> Self {
let mut adj: HashMap<String, Vec<String>> = HashMap::new();
for m in memories {
adj.entry(m.id.clone()).or_default();
for rid in &m.related_ids {
adj.entry(m.id.clone()).or_default().push(rid.clone());
adj.entry(rid.clone()).or_default().push(m.id.clone());
}
}
Self { adj }
}
pub fn shortest_path(&self, from: &str, to: &str) -> Option<Vec<String>> {
if from == to {
return Some(vec![from.to_string()]);
}
let mut visited: HashSet<String> = HashSet::new();
let mut queue: VecDeque<(String, Vec<String>)> = VecDeque::new();
visited.insert(from.to_string());
queue.push_back((from.to_string(), vec![from.to_string()]));
while let Some((current, path)) = queue.pop_front() {
if let Some(neighbors) = self.adj.get(¤t) {
for neighbor in neighbors {
if neighbor == to {
let mut result = path;
result.push(neighbor.clone());
return Some(result);
}
if !visited.contains(neighbor) {
visited.insert(neighbor.clone());
let mut new_path = path.clone();
new_path.push(neighbor.clone());
queue.push_back((neighbor.clone(), new_path));
}
}
}
}
None
}
pub fn connected_components(&self) -> Vec<Vec<String>> {
let mut visited: HashSet<String> = HashSet::new();
let mut components = Vec::new();
for node in self.adj.keys() {
if !visited.contains(node) {
let mut component = Vec::new();
let mut queue = VecDeque::new();
visited.insert(node.clone());
queue.push_back(node.clone());
component.push(node.clone());
while let Some(current) = queue.pop_front() {
if let Some(neighbors) = self.adj.get(¤t) {
for neighbor in neighbors {
if !visited.contains(neighbor) {
visited.insert(neighbor.clone());
queue.push_back(neighbor.clone());
component.push(neighbor.clone());
}
}
}
}
components.push(component);
}
}
components.sort_by(|a, b| b.len().cmp(&a.len()));
components
}
pub fn neighbors(&self, node: &str) -> Vec<String> {
self.adj.get(node).cloned().unwrap_or_default()
}
pub fn degree(&self, node: &str) -> usize {
self.adj.get(node).map_or(0, |n| n.len())
}
pub fn nodes(&self) -> Vec<String> {
self.adj.keys().cloned().collect()
}
pub fn edges(&self) -> usize {
self.adj.values().map(|n| n.len()).sum::<usize>() / 2
}
pub fn isolated_nodes(&self) -> Vec<String> {
self.adj.iter()
.filter(|(_, neighbors)| neighbors.is_empty())
.map(|(id, _)| id.clone())
.collect()
}
pub fn hub_nodes(&self, min_degree: usize) -> Vec<(String, usize)> {
let mut hubs: Vec<(String, usize)> = self.adj.iter()
.filter(|(_, neighbors)| neighbors.len() >= min_degree)
.map(|(id, neighbors)| (id.clone(), neighbors.len()))
.collect();
hubs.sort_by(|a, b| b.1.cmp(&a.1));
hubs
}
}
pub fn format_graph_stats(graph: &MemoryGraph) -> String {
let mut output = String::new();
output.push_str("Graph Statistics\n");
output.push_str("================\n\n");
output.push_str(&format!("Nodes: {}\n", graph.nodes().len()));
output.push_str(&format!("Edges: {}\n", graph.edges()));
output.push_str(&format!("Components: {}\n", graph.connected_components().len()));
output.push_str(&format!("Isolated nodes: {}\n", graph.isolated_nodes().len()));
let hubs = graph.hub_nodes(2);
if !hubs.is_empty() {
output.push_str("\nHub nodes (degree >= 2):\n");
for (id, degree) in hubs.iter().take(5) {
output.push_str(&format!(" {}: {} connections\n", &id[..8.min(id.len())], degree));
}
}
output
}