graphqlite/algorithms/mod.rs
1//! Graph algorithm implementations and result types.
2
3mod centrality;
4mod community;
5mod components;
6pub(crate) mod parsing;
7mod paths;
8mod similarity;
9mod traversal;
10
11use serde::{Deserialize, Serialize};
12
13/// PageRank result for a single node.
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct PageRankResult {
16 /// Internal node identifier.
17 pub node_id: String,
18 /// User-defined node identifier (from the `id` property).
19 pub user_id: Option<String>,
20 /// PageRank score (higher = more important).
21 pub score: f64,
22}
23
24/// Community detection result for a single node.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct CommunityResult {
27 /// Internal node identifier.
28 pub node_id: String,
29 /// User-defined node identifier (from the `id` property).
30 pub user_id: Option<String>,
31 /// Community label (nodes with the same value belong to the same community).
32 pub community: i64,
33}
34
35/// Shortest path result from Dijkstra's algorithm.
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct ShortestPathResult {
38 /// List of node IDs along the path (source to target).
39 pub path: Vec<String>,
40 /// Total distance/cost of the path (`None` if no path found).
41 pub distance: Option<f64>,
42 /// Whether a path was found.
43 pub found: bool,
44}
45
46/// Degree centrality result for a single node.
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct DegreeCentralityResult {
49 /// Internal node identifier.
50 pub node_id: String,
51 /// User-defined node identifier (from the `id` property).
52 pub user_id: Option<String>,
53 /// Number of incoming edges.
54 pub in_degree: i64,
55 /// Number of outgoing edges.
56 pub out_degree: i64,
57 /// Total degree (in + out).
58 pub degree: i64,
59}
60
61/// Connected component result for a single node.
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct ComponentResult {
64 /// Internal node identifier.
65 pub node_id: String,
66 /// User-defined node identifier (from the `id` property).
67 pub user_id: Option<String>,
68 /// Component identifier (nodes in the same component share this value).
69 pub component: i64,
70}
71
72/// Betweenness centrality result for a single node.
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct BetweennessCentralityResult {
75 /// Internal node identifier.
76 pub node_id: String,
77 /// User-defined node identifier (from the `id` property).
78 pub user_id: Option<String>,
79 /// Betweenness centrality score (higher = more central).
80 pub score: f64,
81}
82
83/// Closeness centrality result for a single node.
84#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct ClosenessCentralityResult {
86 /// Internal node identifier.
87 pub node_id: String,
88 /// User-defined node identifier (from the `id` property).
89 pub user_id: Option<String>,
90 /// Closeness centrality score (0 to 1, higher = more central).
91 pub score: f64,
92}
93
94/// Triangle count result for a single node.
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct TriangleCountResult {
97 /// Internal node identifier.
98 pub node_id: String,
99 /// User-defined node identifier (from the `id` property).
100 pub user_id: Option<String>,
101 /// Number of triangles this node participates in.
102 pub triangles: i64,
103 /// Local clustering coefficient (0 to 1).
104 pub clustering_coefficient: f64,
105}
106
107/// A* shortest path result.
108#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct AStarResult {
110 /// List of node IDs along the path (source to target).
111 pub path: Vec<String>,
112 /// Total distance/cost of the path (`None` if no path found).
113 pub distance: Option<f64>,
114 /// Whether a path was found.
115 pub found: bool,
116 /// Number of nodes explored during search.
117 pub nodes_explored: i64,
118}
119
120/// Traversal result for BFS/DFS.
121#[derive(Debug, Clone, Serialize, Deserialize)]
122pub struct TraversalResult {
123 /// User-defined node identifier (from the `id` property).
124 pub user_id: String,
125 /// Depth/distance from the starting node.
126 pub depth: i64,
127 /// Order in which the node was visited.
128 pub order: i64,
129}
130
131/// Node similarity result using Jaccard coefficient.
132#[derive(Debug, Clone, Serialize, Deserialize)]
133pub struct NodeSimilarityResult {
134 /// First node's user-defined identifier.
135 pub node1: String,
136 /// Second node's user-defined identifier.
137 pub node2: String,
138 /// Jaccard similarity score (0.0 to 1.0).
139 pub similarity: f64,
140}
141
142/// K-nearest neighbor result.
143#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct KnnResult {
145 /// Neighbor node's user-defined identifier.
146 pub neighbor: String,
147 /// Jaccard similarity score (0.0 to 1.0).
148 pub similarity: f64,
149 /// Rank (1 = most similar).
150 pub rank: i64,
151}
152
153/// Eigenvector centrality result for a single node.
154#[derive(Debug, Clone, Serialize, Deserialize)]
155pub struct EigenvectorCentralityResult {
156 /// Internal node identifier.
157 pub node_id: String,
158 /// User-defined node identifier (from the `id` property).
159 pub user_id: Option<String>,
160 /// Eigenvector centrality score (higher = more central).
161 pub score: f64,
162}
163
164/// All pairs shortest path result for a single pair.
165#[derive(Debug, Clone, Serialize, Deserialize)]
166pub struct ApspResult {
167 /// Source node's user-defined identifier.
168 pub source: String,
169 /// Target node's user-defined identifier.
170 pub target: String,
171 /// Shortest path distance between source and target.
172 pub distance: f64,
173}