pub struct Graph { /* private fields */ }Expand description
A weighted graph on vertices 0..n stored as adjacency lists. Undirected graphs store each
edge in both lists; Graph::edges reports it once.
Implementations§
Source§impl Graph
impl Graph
Sourcepub fn from_edges(
n: usize,
edges: &[(usize, usize, f64)],
directed: bool,
) -> Result<Self, ErrorsJSL>
pub fn from_edges( n: usize, edges: &[(usize, usize, f64)], directed: bool, ) -> Result<Self, ErrorsJSL>
A graph from (from, to, weight) triples.
Sourcepub fn from_unweighted_edges(
n: usize,
edges: &[(usize, usize)],
directed: bool,
) -> Result<Self, ErrorsJSL>
pub fn from_unweighted_edges( n: usize, edges: &[(usize, usize)], directed: bool, ) -> Result<Self, ErrorsJSL>
A graph from unweighted (from, to) pairs (weight 1).
Sourcepub fn from_adjacency_matrix(
matrix: &[Vec<Option<f64>>],
directed: bool,
) -> Result<Self, ErrorsJSL>
pub fn from_adjacency_matrix( matrix: &[Vec<Option<f64>>], directed: bool, ) -> Result<Self, ErrorsJSL>
A graph from a square adjacency matrix: entry (i, j) is the weight of i → j, and None
or a NaN entry means no edge. Undirected graphs read the upper triangle.
pub fn add_edge( &mut self, from: usize, to: usize, weight: f64, ) -> Result<(), ErrorsJSL>
pub fn vertex_count(&self) -> usize
pub fn is_directed(&self) -> bool
Sourcepub fn neighbors(&self, v: usize) -> &[(usize, f64)]
pub fn neighbors(&self, v: usize) -> &[(usize, f64)]
The out-neighbours of v with edge weights.
Sourcepub fn degree(&self, v: usize) -> usize
pub fn degree(&self, v: usize) -> usize
The out-degree of v (the degree for undirected graphs).
pub fn edge_count(&self) -> usize
Sourcepub fn adjacency_matrix(&self) -> Vec<Vec<Option<f64>>>
pub fn adjacency_matrix(&self) -> Vec<Vec<Option<f64>>>
The adjacency matrix (None where there is no edge; the smallest weight for parallel
edges).
Sourcepub fn reversed(&self) -> Graph
pub fn reversed(&self) -> Graph
The graph with every edge reversed (itself for undirected graphs).
Sourcepub fn breadth_first(&self, source: usize) -> Vec<usize>
pub fn breadth_first(&self, source: usize) -> Vec<usize>
Vertices reachable from source in breadth-first order.
Sourcepub fn depth_first(&self, source: usize) -> Vec<usize>
pub fn depth_first(&self, source: usize) -> Vec<usize>
Vertices reachable from source in depth-first (preorder), visiting neighbours in
insertion order; iterative, so deep graphs do not overflow the stack.
Sourcepub fn connected_components(&self) -> Vec<usize>
pub fn connected_components(&self) -> Vec<usize>
The component label of every vertex (weakly connected components for directed graphs), labels numbered from 0 in order of first appearance.