use petgraph::{algo::page_rank, Graph};
#[cfg(feature = "rayon")]
use petgraph::algo::page_rank::parallel_page_rank;
fn graph_example() -> Graph<String, f32> {
let mut graph = Graph::<_, f32>::new();
graph.add_node("A".to_owned());
graph.add_node("B".to_owned());
graph.add_node("C".to_owned());
graph.add_node("D".to_owned());
graph.add_node("E".to_owned());
graph.add_node("F".to_owned());
graph.add_node("G".to_owned());
graph.add_node("H".to_owned());
graph.add_node("I".to_owned());
graph.add_node("J".to_owned());
graph.add_node("K".to_owned());
graph.add_node("L".to_owned());
graph.add_node("M".to_owned());
graph.extend_with_edges(&[
(1, 2), (2, 1), (4, 0), (4, 1), (5, 4), (5, 1), (5, 6), (6, 1), (6, 5), (7, 1), (7, 5), (8, 1), (8, 5), (9, 1), (9, 5), (10, 1), (10, 5), (11, 5), (12, 5), ]);
graph
}
fn expected_ranks() -> Vec<f32> {
vec![
0.029228685,
0.38176042,
0.3410649,
0.014170233,
0.035662483,
0.077429585,
0.035662483,
0.014170233,
0.014170233,
0.014170233,
0.014170233,
0.014170233,
0.014170233,
]
}
#[test]
fn test_page_rank() {
let graph = graph_example();
let output_ranks = page_rank(&graph, 0.85_f32, 100);
assert_eq!(expected_ranks(), output_ranks);
}
#[test]
#[cfg(feature = "rayon")]
fn test_par_page_rank() {
let graph = graph_example();
let output_ranks = parallel_page_rank(&graph, 0.85_f32, 100, Some(1e-12));
assert!(!expected_ranks()
.iter()
.zip(output_ranks)
.any(|(expected, computed)| ((expected - computed).abs() > 1e-6)
|| computed.is_nan()
|| expected.is_nan()));
}