pub trait Graph:
Clone
+ Send
+ Sync {
// Required methods
fn num_vertices(&self) -> usize;
fn num_edges(&self) -> usize;
fn edges(&self) -> Vec<(usize, usize)>;
fn has_edge(&self, u: usize, v: usize) -> bool;
fn neighbors(&self, v: usize) -> Vec<usize>;
// Provided methods
fn degree(&self, v: usize) -> usize { ... }
fn is_empty(&self) -> bool { ... }
fn for_each_edge<F>(&self, f: F)
where F: FnMut(usize, usize) { ... }
}Expand description
Trait for graph types, following Julia’s Graphs.jl AbstractGraph pattern.
This trait abstracts over different graph representations, allowing problems to be generic over the underlying graph type.
§Example
ⓘ
use problemreductions::topology::{Graph, SimpleGraph};
fn count_triangles<G: Graph>(graph: &G) -> usize {
let mut count = 0;
for u in 0..graph.num_vertices() {
for v in graph.neighbors(u) {
if v > u {
for w in graph.neighbors(v) {
if w > v && graph.has_edge(u, w) {
count += 1;
}
}
}
}
}
count
}Required Methods§
Sourcefn num_vertices(&self) -> usize
fn num_vertices(&self) -> usize
Returns the number of vertices in the graph.
Sourcefn edges(&self) -> Vec<(usize, usize)>
fn edges(&self) -> Vec<(usize, usize)>
Returns all edges as a list of (u, v) pairs.
For undirected graphs, each edge appears once with u < v.
Provided Methods§
Sourcefn for_each_edge<F>(&self, f: F)
fn for_each_edge<F>(&self, f: F)
Iterates over all edges, calling a closure for each.
This can be more efficient than edges() when you don’t need to collect.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.