use scirs2_graph::algorithms::*;
use scirs2_graph::generators::*;
use scirs2_graph::Hypergraph;
#[allow(dead_code)]
fn main() {
println!("=== SciRS2 Graph Module Demo ===\n");
println!("1. Creating a Social Network Graph");
let mut graph = create_graph::<&str, f64>();
graph
.add_edge("Alice", "Bob", 0.8)
.expect("Operation failed");
graph
.add_edge("Bob", "Charlie", 0.6)
.expect("Operation failed");
graph
.add_edge("Charlie", "David", 0.7)
.expect("Operation failed");
graph
.add_edge("David", "Alice", 0.5)
.expect("Operation failed");
graph
.add_edge("Alice", "Charlie", 0.3)
.expect("Operation failed");
println!(
" Created graph with {} nodes and {} edges",
graph.node_count(),
graph.edge_count()
);
println!("\n2. Graph Traversal");
let bfs_result = breadth_first_search(&graph, &"Alice").expect("Operation failed");
println!(" BFS from Alice: {} nodes visited", bfs_result.len());
let dfs_result = depth_first_search(&graph, &"Alice").expect("Operation failed");
println!(" DFS from Alice: {} nodes visited", dfs_result.len());
println!("\n3. Shortest Path Algorithms");
match dijkstra_path(&graph, &"Alice", &"Charlie") {
Ok(Some(path)) => {
println!(" Shortest path from Alice to Charlie:");
println!(" Path: {:?}", path.nodes);
println!(" Total weight: {:.2}", path.total_weight);
}
Ok(None) => println!(" No path found"),
Err(e) => println!(" Error: {:?}", e),
}
println!("\n4. Connectivity Analysis");
let components = connected_components(&graph);
println!(" Number of connected components: {}", components.len());
let articulation_pts = articulation_points(&graph);
println!(" Articulation points: {:?}", articulation_pts);
println!("\n5. Community Detection");
let communities = louvain_communities_result(&graph);
println!(
" Quality Score: {:.4}",
communities.quality_score.unwrap_or(0.0)
);
println!(" Community assignments:");
for (node, community) in &communities.node_communities {
println!(" {} belongs to community {}", node, community);
}
println!("\n6. Graph Properties");
if let Some(d) = diameter(&graph) {
println!(" Graph diameter: {:.2}", d);
}
if let Some(r) = radius(&graph) {
println!(" Graph radius: {:.2}", r);
}
let centers = center_nodes(&graph);
println!(" Center nodes: {:?}", centers);
println!("\n7. Minimum Spanning Tree");
match minimum_spanning_tree(&graph) {
Ok(mst) => {
let total_weight: f64 = mst.iter().map(|e| e.weight).sum();
println!(" MST edges: {}", mst.len());
println!(" Total weight: {:.2}", total_weight);
}
Err(e) => println!(" Error: {:?}", e),
}
println!("\n8. Random Graph Generation");
let mut rng = scirs2_core::random::rng();
let random_graph = erdos_renyi_graph(10, 0.3, &mut rng).expect("Operation failed");
println!(
" Erdős-Rényi graph: {} nodes, {} edges",
random_graph.node_count(),
random_graph.edge_count()
);
let ba_graph = barabasi_albert_graph(20, 2, &mut rng).expect("Operation failed");
println!(
" Barabási-Albert graph: {} nodes, {} edges",
ba_graph.node_count(),
ba_graph.edge_count()
);
println!("\n9. Directed Graph Operations");
let mut digraph = create_digraph::<&str, f64>();
digraph.add_edge("A", "B", 1.0).expect("Operation failed");
digraph.add_edge("B", "C", 1.0).expect("Operation failed");
digraph.add_edge("C", "A", 1.0).expect("Operation failed");
digraph.add_edge("B", "D", 1.0).expect("Operation failed");
match topological_sort(&digraph) {
Ok(sorted) => println!(" Topological order: {:?}", sorted),
Err(_) => println!(" Graph has cycles, cannot perform topological sort"),
}
let pagerank = pagerank(&digraph, 0.85, 1e-6, 100);
println!(" PageRank scores:");
for (node, score) in pagerank {
println!(" {}: {:.4}", node, score);
}
println!("\n10. K-Core Decomposition");
let k_cores = k_core_decomposition(&graph);
println!(" K-core values:");
for (node, k) in k_cores {
println!(" {}: {}-core", node, k);
}
println!("\n11. Hypergraph Operations");
let mut hypergraph: Hypergraph<&str, f64> = Hypergraph::new();
let he1 = hypergraph
.add_hyperedge_from_vec(vec!["Alice", "Bob", "Charlie"], 1.0)
.expect("Operation failed");
let he2 = hypergraph
.add_hyperedge_from_vec(vec!["Bob", "Charlie", "David"], 1.2)
.expect("Operation failed");
let he3 = hypergraph
.add_hyperedge_from_vec(vec!["Alice", "David"], 0.8)
.expect("Operation failed");
println!(
" Created hypergraph with {} hyperedges",
hypergraph.hyperedge_count()
);
println!(" Hyperedge {} connects: Alice-Bob-Charlie", he1);
println!(" Hyperedge {} connects: Bob-Charlie-David", he2);
println!(" Hyperedge {} connects: Alice-David", he3);
let (min_size, max_size, avg_size) = hypergraph.hyperedge_size_stats();
println!(
" Hyperedge sizes - Min: {}, Max: {}, Avg: {:.2}",
min_size, max_size, avg_size
);
let converted_graph = hypergraph.to_graph();
println!(
" Converted to regular graph: {} nodes, {} edges",
converted_graph.node_count(),
converted_graph.edge_count()
);
println!("\n=== Demo Complete ===");
}