euler_path

Function euler_path 

Source
pub fn euler_path<G>(graph: &G) -> Result<Vec<NodeId>, EulerError>
where G: Graph<Weight = f64>,
Expand description

Finds an Euler path in an undirected graph using Hierholzer’s algorithm.

An Euler path visits every edge exactly once but need not return to the start. It exists iff the graph is connected (ignoring isolated nodes) and exactly two nodes have odd degree (these are the path endpoints).

§Returns

Ok(Vec<NodeId>) — the sequence of nodes visited, from the odd-degree start node to the other odd-degree end node.

§Errors

§Complexity

O(E).

§Examples

use graph_core::{AdjacencyList, Graph};
use graph_advanced::euler_path;

// Path graph 0-1-2-3: nodes 0 and 3 have odd degree (1 each).
let mut g: AdjacencyList<()> = AdjacencyList::undirected();
let n: Vec<_> = (0..4).map(|_| g.add_node(())).collect();
g.add_edge(n[0], n[1], 1.0).unwrap();
g.add_edge(n[1], n[2], 1.0).unwrap();
g.add_edge(n[2], n[3], 1.0).unwrap();

let path = euler_path(&g).unwrap();
assert_eq!(path.len(), 4); // 3 edges → 4 nodes