Skip to content

Commit 2953da2

Browse files
authored
Dijkstra Path generic, Closeness Centrality, Iterate Edge with directionality (#20)
1 parent 385c85b commit 2953da2

File tree

6 files changed

+512
-40
lines changed

6 files changed

+512
-40
lines changed

examples/centrality.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,52 @@ fn katz_centrality_impl_example() {
166166
println!("{:.5?}", centrality); // [0.23167, 0.71650, 0.65800]
167167
}
168168

169+
fn closeness_centrality_example() {
170+
use graphina::core::types::Graph;
171+
172+
use graphina::centrality::algorithms::closeness_centrality;
173+
174+
let mut graph = Graph::new();
175+
let ids = (0..5).map(|i| graph.add_node(i)).collect::<Vec<_>>();
176+
let edges = [(0, 1, 1.0), (0, 2, 1.0), (1, 3, 1.0)];
177+
for (s, d, w) in edges {
178+
graph.add_edge(ids[s], ids[d], w);
179+
}
180+
181+
let centrality = closeness_centrality(&graph, false).unwrap();
182+
println!("{:.5?}", centrality); // [0.75000, 0.75000, 0.50000, 0.50000, 0.00000]
183+
}
184+
185+
fn closeness_centrality_impl_example() {
186+
use graphina::core::types::Graph;
187+
188+
use graphina::centrality::algorithms::closeness_centrality_impl;
189+
190+
let mut graph: Graph<i32, (String, f64)> = Graph::new();
191+
192+
let ids = (0..5).map(|i| graph.add_node(i)).collect::<Vec<_>>();
193+
194+
let edges = [
195+
(0, 1, ("friend".to_string(), 0.9)),
196+
(0, 2, ("family".to_string(), 0.8)),
197+
(1, 3, ("friend".to_string(), 0.7)),
198+
(2, 4, ("enemy".to_string(), 0.1)),
199+
];
200+
for (s, d, w) in edges {
201+
graph.add_edge(ids[s], ids[d], w);
202+
}
203+
204+
let eval_cost = |(s, f): &(String, f64)| match s.as_str() {
205+
"friend" => Some(1.0 / *f / 2.0),
206+
"family" => Some(1.0 / *f / 4.0),
207+
"enemy" => None,
208+
_ => Some(1.0 / *f),
209+
};
210+
211+
let centrality = closeness_centrality_impl(&graph, eval_cost, true).unwrap();
212+
println!("{:.5?}", centrality); // [1.05244, 1.05244, 0.81436, 0.63088, 0.00000]
213+
}
214+
169215
macro_rules! run_examples {
170216
($($func:ident),* $(,)?) => {
171217
$(
@@ -189,6 +235,9 @@ fn main() {
189235
// katz centrality
190236
katz_centrality_example,
191237
katz_centrality_numpy_example,
192-
katz_centrality_impl_example
238+
katz_centrality_impl_example,
239+
// closeness centrality
240+
closeness_centrality_example,
241+
closeness_centrality_impl_example,
193242
);
194243
}

examples/path_dijkstra.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
fn line() {
2+
use graphina::core::types::Graph;
3+
4+
use graphina::core::paths::dijkstra_path_f64;
5+
6+
let mut graph = Graph::new();
7+
let ids = (0..5).map(|i| graph.add_node(i)).collect::<Vec<_>>();
8+
let edges = [(0, 1, 1.0), (1, 2, 1.0), (2, 3, 2.0), (3, 4, 1.0)];
9+
for (s, d, w) in edges {
10+
graph.add_edge(ids[s], ids[d], w);
11+
}
12+
13+
let (cost, trace) = dijkstra_path_f64(&graph, ids[0], None).unwrap();
14+
15+
println!("cost : {:?}", cost);
16+
println!("trace: {:?}", trace);
17+
// cost : [Some(0.0), Some(1.0), Some(2.0), Some(4.0), Some(5.0)]
18+
// trace: [None, Some(NodeId(NodeIndex(0))), Some(NodeId(NodeIndex(1))), Some(NodeId(NodeIndex(2))), Some(NodeId(NodeIndex(3)))]
19+
}
20+
21+
fn flight() {
22+
use graphina::core::types::Digraph;
23+
24+
use graphina::core::paths::dijkstra_path_impl;
25+
26+
let mut graph: Digraph<String, (f64, String)> = Digraph::new();
27+
// ^^^^^^^^^^^^^
28+
// L arbitrary type as edge
29+
30+
let cities = ["ATL", "PEK", "LHR", "HND", "CDG", "FRA", "HKG"];
31+
32+
let ids = cities
33+
.iter()
34+
.map(|s| graph.add_node(s.to_string()))
35+
.collect::<Vec<_>>();
36+
37+
let edges = [
38+
//
39+
("ATL", "PEK", (900.0, "boeing")),
40+
("ATL", "LHR", (500.0, "airbus")),
41+
("ATL", "HND", (700.0, "airbus")),
42+
//
43+
("PEK", "LHR", (800.0, "boeing")),
44+
("PEK", "HND", (100.0, "airbus")),
45+
("PEK", "HKG", (100.0, "airbus")),
46+
//
47+
("LHR", "CDG", (100.0, "airbus")),
48+
("LHR", "FRA", (200.0, "boeing")),
49+
("LHR", "HND", (600.0, "airbus")),
50+
//
51+
("HND", "ATL", (700.0, "airbus")),
52+
("HND", "FRA", (600.0, "airbus")),
53+
("HND", "HKG", (100.0, "airbus")),
54+
//
55+
];
56+
57+
for (s, d, w) in edges {
58+
let depart = cities.iter().position(|city| s == *city).unwrap();
59+
let destin = cities.iter().position(|city| d == *city).unwrap();
60+
graph.add_edge(ids[depart], ids[destin], (w.0, w.1.to_string()));
61+
}
62+
63+
// function for evaluating possible cost for the edge
64+
// Some(f64) for cost
65+
// None for impassable
66+
let eval_cost = |(price, manufactuer): &(f64, String)| match manufactuer.as_str() {
67+
"boeing" => None, // avoid boeing plane
68+
_ => Some(*price), // return price as the cost
69+
};
70+
71+
let (cost, trace) = dijkstra_path_impl(&graph, ids[0], Some(1000.0), eval_cost).unwrap();
72+
73+
println!("cost : {:?}", cost);
74+
println!("trace: {:?}", trace);
75+
// cost : [Some(0.0), None, Some(500.0), Some(700.0), Some(600.0), None, Some(800.0)]
76+
// trace: [None, None, Some(NodeId(NodeIndex(0))), Some(NodeId(NodeIndex(0))), Some(NodeId(NodeIndex(2))), None, Some(NodeId(NodeIndex(3)))]
77+
}
78+
79+
macro_rules! run_examples {
80+
($($func:ident),* $(,)?) => {
81+
$(
82+
println!("<{}>", stringify!($func));
83+
$func();
84+
println!();
85+
)*
86+
};
87+
}
88+
89+
fn main() {
90+
run_examples!(line, flight);
91+
}

0 commit comments

Comments
 (0)