Skip to main content

Graph

Trait Graph 

Source
pub trait Graph {
    // Required methods
    fn node_count(&self) -> usize;
    fn neighbors(&self, node: usize) -> Vec<usize>;

    // Provided method
    fn out_degree(&self, node: usize) -> usize { ... }
}
Expand description

Owned-neighbor graph adapter. The simplest adapter to implement — each neighbors(node) call returns a fresh Vec.

Node ids are dense, 0..node_count(). An edge u -> v is expressed by v appearing in neighbors(u). Duplicates are allowed but operators assume unique neighbors; deduplicate upstream if needed.

Prefer GraphRef when the operator reads neighbors repeatedly in a hot loop — allocation cost of owned Vec compounds.

Required Methods§

Source

fn node_count(&self) -> usize

Number of nodes in the graph. Must be stable across calls.

Source

fn neighbors(&self, node: usize) -> Vec<usize>

Out-neighbors of node as an owned Vec of node ids in 0..node_count().

Behavior is unspecified if node >= node_count(); operators either panic or return empty. Use *_checked wrappers for explicit validation.

Provided Methods§

Source

fn out_degree(&self, node: usize) -> usize

Number of out-neighbors of node. Default calls neighbors(node).len(); override if out-degree is cheaper to compute without materializing the list.

Implementations on Foreign Types§

Source§

impl<N, E, Ty, Ix> Graph for Graph<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

Available on crate feature petgraph only.
Source§

fn node_count(&self) -> usize

Source§

fn neighbors(&self, node: usize) -> Vec<usize>

Implementors§