Graph

Trait Graph 

Source
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§

Source

fn num_vertices(&self) -> usize

Returns the number of vertices in the graph.

Source

fn num_edges(&self) -> usize

Returns the number of edges in the graph.

Source

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.

Source

fn has_edge(&self, u: usize, v: usize) -> bool

Checks if an edge exists between vertices u and v.

Source

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

Returns all neighbors of vertex v.

Provided Methods§

Source

fn degree(&self, v: usize) -> usize

Returns the degree of vertex v (number of neighbors).

Source

fn is_empty(&self) -> bool

Returns true if the graph has no vertices.

Source

fn for_each_edge<F>(&self, f: F)
where F: FnMut(usize, usize),

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.

Implementors§