Skip to main content

layout_bipartite

Function layout_bipartite 

Source
pub fn layout_bipartite(
    graph: &Graph,
    types: &[bool],
    hgap: f64,
    vgap: f64,
    maxiter: u32,
) -> IgraphResult<Vec<[f64; 2]>>
Expand description

Compute a bipartite layout.

Vertices are placed in two rows based on their boolean type. Horizontal positions are optimized to minimize edge crossings using the Sugiyama algorithm.

§Arguments

  • graph — the input graph (need not actually be bipartite; any graph with a two-coloring works).
  • types — boolean slice of length vcount: true places the vertex in row 0 (top), false in row 1 (bottom).
  • hgap — preferred minimum horizontal gap between vertices in the same row.
  • vgap — vertical distance between the two rows.
  • maxiter — maximum crossing-minimization iterations (100 is typical).

Returns [x, y] positions for each vertex.

§Errors

Returns InvalidArgument if types.len() != graph.vcount() or if hgap is negative.

§Examples

use rust_igraph::{Graph, layout_bipartite};

// K_{2,3}: vertices 0,1 in one part, 2,3,4 in the other
let mut g = Graph::with_vertices(5);
for &u in &[0u32, 1] {
    for &v in &[2u32, 3, 4] {
        g.add_edge(u, v).unwrap();
    }
}

let types = vec![true, true, false, false, false];
let pos = layout_bipartite(&g, &types, 1.0, 1.0, 100).unwrap();
assert_eq!(pos.len(), 5);
// Top-row vertices share the same y
assert!((pos[0][1] - pos[1][1]).abs() < 1e-10);
// Bottom-row vertices share a different y
assert!((pos[2][1] - pos[3][1]).abs() < 1e-10);
assert!((pos[0][1] - pos[2][1]).abs() > 0.5);