Skip to main content

roots_for_tree_layout

Function roots_for_tree_layout 

Source
pub fn roots_for_tree_layout(
    graph: &Graph,
    mode: RtMode,
    heuristic: RootChoice,
) -> IgraphResult<Vec<VertexId>>
Expand description

Choose “nice” roots for a tree layout.

Returns one root per reachable component so that every vertex is reachable from at least one root.

  • Undirected (or mode == RtMode::All): one root per weak connected component — the vertex with the highest degree (or lowest eccentricity).
  • Directed: strongly connected components with no incoming (when mode == RtMode::Out) or no outgoing (when mode == RtMode::In) edges get one root each. Components that do have such edges are omitted — they are reachable from some root component.

Counterpart of igraph_roots_for_tree_layout() from references/igraph/src/layout/reingold_tilford.c:536–660.

§Examples

use rust_igraph::{Graph, roots_for_tree_layout, RtMode, RootChoice};

// Simple undirected tree: 0-1-2-3-4
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 4).unwrap();

let roots = roots_for_tree_layout(&g, RtMode::All, RootChoice::Degree).unwrap();
// One root, should be vertex 1 or 2 or 3 (degree 2 — the hubs of a path)
assert_eq!(roots.len(), 1);
assert!([1, 2, 3].contains(&roots[0]));

// Disconnected: two components
let mut g2 = Graph::with_vertices(4);
g2.add_edge(0, 1).unwrap();
g2.add_edge(2, 3).unwrap();
let roots2 = roots_for_tree_layout(&g2, RtMode::All, RootChoice::Degree).unwrap();
assert_eq!(roots2.len(), 2);