An implementation of the union-find disjoint set graph algorithm
For relatively small networks you can simply interact with nodes by name.
``` rust use cozadunionfind::client as ufclient;
let mut client = ufclient::Client::new();
client.addnode("A"); client.addnode("B"); client.addnode("C"); client.addnode("D"); client.addnode("E"); client.addnode("F"); client.addnode("G"); client.addnode("H"); client.addnode("I"); client.addnode("J");
client.connectnodes("E", "D"); client.connectnodes("D", "I"); client.connectnodes("G", "F"); client.connectnodes("J", "E"); client.connectnodes("C", "B"); client.connectnodes("I", "J"); client.connectnodes("F", "A"); client.connectnodes("H", "B"); client.connectnodes("G", "B"); client.connectnodes("B", "A"); client.connect_nodes("G", "H");
println!("\nDisjoint sets found: {}", client.disjointsetcount()) ```
Output
Disjoint sets found: 2
When you have a large volume of connections to process you can skip the lookups that occur with named nodes and use the bulk interfaces. The process involves giving a vector of node names and then specifying connections between nodes by index.
``` rust use cozadunionfind::client as ufclient; use cozadunionfind::client::BulkConnection as ufconnection;
let mut client = ufclient::Client::new(); let nodes = vec![ String::from("A"), String::from("B"), String::from("C"), String::from("D"), String::from("E"), String::from("F"), String::from("G"), String::from("H"), String::from("I"), String::from("J") ]; client.addnodesbulk(nodes);
let connections = vec![ ufconnection { a: 4, b: 3 }, ufconnection { a: 3, b: 8 }, ufconnection { a: 6, b: 5 }, ufconnection { a: 9, b: 4 }, ufconnection { a: 2, b: 1 }, ufconnection { a: 8, b: 9 }, ufconnection { a: 5, b: 0 }, ufconnection { a: 7, b: 2 }, ufconnection { a: 6, b: 1 }, ufconnection { a: 1, b: 0 }, ufconnection{ a: 6, b: 7 } ]; client.connectnodesbulk(connections);
println!("\nDisjoint sets found: {}", client.disjointsetcount()) ```
Output
Disjoint sets found: 2