Skip to main content

eulerian_path

Function eulerian_path 

Source
pub fn eulerian_path(graph: &Graph) -> IgraphResult<Option<Vec<u32>>>
Expand description

Build an Eulerian path or cycle for graph if one exists. Returns Some(edge_ids) (the walk visits each edge exactly once) or None if no Eulerian walk exists.

Counterpart of igraph_eulerian_path() (returns the path if any) from references/igraph/src/paths/eulerian.c:345. Undirected only in this slice; directed graphs return an error.

ยงExamples

use rust_igraph::{Graph, eulerian_path};

// Triangle 0-1-2-0: every vertex has even degree โ†’ Euler cycle exists.
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();   // edge 0
g.add_edge(1, 2).unwrap();   // edge 1
g.add_edge(2, 0).unwrap();   // edge 2
let walk = eulerian_path(&g).unwrap().unwrap();
assert_eq!(walk.len(), 3);

// Path 0-1-2: 2 odd-degree vertices โ†’ Euler path (no cycle).
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
let walk = eulerian_path(&g).unwrap().unwrap();
assert_eq!(walk.len(), 2);

// K4: 4 odd-degree vertices โ†’ no Euler path.
let mut g = Graph::with_vertices(4);
for u in 0..4u32 {
    for v in (u + 1)..4 {
        g.add_edge(u, v).unwrap();
    }
}
assert_eq!(eulerian_path(&g).unwrap(), None);