Module fera_graph::builder
source · Expand description
Builder to create user defined, standard and random graphs.
This module offers an abstract way (independent of the graph type) to create graphs. To support
graph building a graph type must implement the WithBuilder
trait, which requires defining a
type that implements the Builder
trait. Each graph type has its own vertex and edge
representation, but the Builder
trait works with numeric vertices.
Examples
Creating a literal graph:
use fera_graph::prelude::*;
// Creates a graph builder for a graph with 4 vertices and initial capacity for 5 edges
let mut builder = StaticGraph::builder(4, 5);
builder.add_edge(0, 1);
builder.add_edge(1, 2);
builder.add_edge(1, 3);
builder.add_edge(2, 3);
builder.add_edge(3, 0);
// Note that we can add more than 5 edges
builder.add_edge(2, 0);
let g = builder.finalize();
assert_eq!(4, g.num_vertices());
assert_eq!(6, g.num_edges());
The graph!
macro can be used to simplify the creation of literal graphs.
Creating standard graphs (see also Complete
):
use fera_graph::prelude::*;
// Creates a complete graph with 4 vertices
let g = StaticGraph::new_complete(4);
assert_eq!(4, g.num_vertices());
assert_eq!(6, g.num_edges());
let edges = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)];
assert!(edges.iter().all(|&(u, v)| g.get_edge_by_ends(u, v).is_some()));
Creating random graphs:
extern crate rand;
extern crate fera_graph;
use fera_graph::prelude::*;
use fera_graph::algs::{Components, Trees};
let mut rng = rand::weak_rng();
// Creates a connected graph with 10 vertices
let g = StaticGraph::new_gn_connected(10, &mut rng);
assert!(g.is_connected());
// Creates a graph with 7 vertices that is a tree.
let g = StaticGraph::new_random_tree(7, &mut rng);
assert!(g.is_tree());
See WithBuilder
for other methods to create standard and random graphs.
Traits
A builder used to build graphs.
A graph that has a
Builder
.