Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion examples/centrality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,52 @@ fn katz_centrality_impl_example() {
println!("{:.5?}", centrality); // [0.23167, 0.71650, 0.65800]
}

fn closeness_centrality_example() {
use graphina::core::types::Graph;

use graphina::centrality::algorithms::closeness_centrality;

let mut graph = Graph::new();
let ids = (0..5).map(|i| graph.add_node(i)).collect::<Vec<_>>();
let edges = [(0, 1, 1.0), (0, 2, 1.0), (1, 3, 1.0)];
for (s, d, w) in edges {
graph.add_edge(ids[s], ids[d], w);
}

let centrality = closeness_centrality(&graph, false).unwrap();
println!("{:.5?}", centrality); // [0.75000, 0.75000, 0.50000, 0.50000, 0.00000]
}

fn closeness_centrality_impl_example() {
use graphina::core::types::Graph;

use graphina::centrality::algorithms::closeness_centrality_impl;

let mut graph: Graph<i32, (String, f64)> = Graph::new();

let ids = (0..5).map(|i| graph.add_node(i)).collect::<Vec<_>>();

let edges = [
(0, 1, ("friend".to_string(), 0.9)),
(0, 2, ("family".to_string(), 0.8)),
(1, 3, ("friend".to_string(), 0.7)),
(2, 4, ("enemy".to_string(), 0.1)),
];
for (s, d, w) in edges {
graph.add_edge(ids[s], ids[d], w);
}

let eval_cost = |(s, f): &(String, f64)| match s.as_str() {
"friend" => Some(1.0 / *f / 2.0),
"family" => Some(1.0 / *f / 4.0),
"enemy" => None,
_ => Some(1.0 / *f),
};

let centrality = closeness_centrality_impl(&graph, eval_cost, true).unwrap();
println!("{:.5?}", centrality); // [1.05244, 1.05244, 0.81436, 0.63088, 0.00000]
}

macro_rules! run_examples {
($($func:ident),* $(,)?) => {
$(
Expand All @@ -189,6 +235,9 @@ fn main() {
// katz centrality
katz_centrality_example,
katz_centrality_numpy_example,
katz_centrality_impl_example
katz_centrality_impl_example,
// closeness centrality
closeness_centrality_example,
closeness_centrality_impl_example,
);
}
91 changes: 91 additions & 0 deletions examples/path_dijkstra.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
fn line() {
use graphina::core::types::Graph;

use graphina::core::paths::dijkstra_path_f64;

let mut graph = Graph::new();
let ids = (0..5).map(|i| graph.add_node(i)).collect::<Vec<_>>();
let edges = [(0, 1, 1.0), (1, 2, 1.0), (2, 3, 2.0), (3, 4, 1.0)];
for (s, d, w) in edges {
graph.add_edge(ids[s], ids[d], w);
}

let (cost, trace) = dijkstra_path_f64(&graph, ids[0], None).unwrap();

println!("cost : {:?}", cost);
println!("trace: {:?}", trace);
// cost : [Some(0.0), Some(1.0), Some(2.0), Some(4.0), Some(5.0)]
// trace: [None, Some(NodeId(NodeIndex(0))), Some(NodeId(NodeIndex(1))), Some(NodeId(NodeIndex(2))), Some(NodeId(NodeIndex(3)))]
}

fn flight() {
use graphina::core::types::Digraph;

use graphina::core::paths::dijkstra_path_impl;

let mut graph: Digraph<String, (f64, String)> = Digraph::new();
// ^^^^^^^^^^^^^
// L arbitrary type as edge

let cities = ["ATL", "PEK", "LHR", "HND", "CDG", "FRA", "HKG"];

let ids = cities
.iter()
.map(|s| graph.add_node(s.to_string()))
.collect::<Vec<_>>();

let edges = [
//
("ATL", "PEK", (900.0, "boeing")),
("ATL", "LHR", (500.0, "airbus")),
("ATL", "HND", (700.0, "airbus")),
//
("PEK", "LHR", (800.0, "boeing")),
("PEK", "HND", (100.0, "airbus")),
("PEK", "HKG", (100.0, "airbus")),
//
("LHR", "CDG", (100.0, "airbus")),
("LHR", "FRA", (200.0, "boeing")),
("LHR", "HND", (600.0, "airbus")),
//
("HND", "ATL", (700.0, "airbus")),
("HND", "FRA", (600.0, "airbus")),
("HND", "HKG", (100.0, "airbus")),
//
];

for (s, d, w) in edges {
let depart = cities.iter().position(|city| s == *city).unwrap();
let destin = cities.iter().position(|city| d == *city).unwrap();
graph.add_edge(ids[depart], ids[destin], (w.0, w.1.to_string()));
}

// function for evaluating possible cost for the edge
// Some(f64) for cost
// None for impassable
let eval_cost = |(price, manufactuer): &(f64, String)| match manufactuer.as_str() {
"boeing" => None, // avoid boeing plane
_ => Some(*price), // return price as the cost
};

let (cost, trace) = dijkstra_path_impl(&graph, ids[0], Some(1000.0), eval_cost).unwrap();

println!("cost : {:?}", cost);
println!("trace: {:?}", trace);
// cost : [Some(0.0), None, Some(500.0), Some(700.0), Some(600.0), None, Some(800.0)]
// trace: [None, None, Some(NodeId(NodeIndex(0))), Some(NodeId(NodeIndex(0))), Some(NodeId(NodeIndex(2))), None, Some(NodeId(NodeIndex(3)))]
}

macro_rules! run_examples {
($($func:ident),* $(,)?) => {
$(
println!("<{}>", stringify!($func));
$func();
println!();
)*
};
}

fn main() {
run_examples!(line, flight);
}
Loading