pub struct GraphBuilder { /* private fields */ }Expand description
A fluent builder for constructing Graph instances.
GraphBuilder accumulates vertices and edges, then produces a
Graph in a single build() call. It validates structure only at
build time, so intermediate states need not be valid graphs.
§Examples
use rust_igraph::GraphBuilder;
// Build a directed triangle.
let g = GraphBuilder::directed()
.vertices(3)
.edge(0, 1)
.edge(1, 2)
.edge(2, 0)
.build()
.unwrap();
assert_eq!(g.vcount(), 3);
assert_eq!(g.ecount(), 3);
assert!(g.is_directed());use rust_igraph::GraphBuilder;
// Build from an edge list (vertex count inferred).
let g = GraphBuilder::undirected()
.edges(&[(0, 1), (1, 2), (2, 3), (3, 0)])
.build()
.unwrap();
assert_eq!(g.vcount(), 4);
assert_eq!(g.ecount(), 4);Implementations§
Source§impl GraphBuilder
impl GraphBuilder
Sourcepub fn directed() -> Self
pub fn directed() -> Self
Start building a directed graph.
§Examples
use rust_igraph::GraphBuilder;
let g = GraphBuilder::directed()
.vertices(2)
.edge(0, 1)
.build()
.unwrap();
assert!(g.is_directed());Sourcepub fn undirected() -> Self
pub fn undirected() -> Self
Start building an undirected graph.
§Examples
use rust_igraph::GraphBuilder;
let g = GraphBuilder::undirected()
.vertices(5)
.build()
.unwrap();
assert!(!g.is_directed());
assert_eq!(g.vcount(), 5);Sourcepub fn vertices(self, n: u32) -> Self
pub fn vertices(self, n: u32) -> Self
Set the vertex count explicitly.
If not called, vertex count is inferred from the maximum vertex id
in any added edge (plus one). Calling this with a value smaller
than the inferred count causes build() to return an error.
Sourcepub fn edge(self, from: VertexId, to: VertexId) -> Self
pub fn edge(self, from: VertexId, to: VertexId) -> Self
Add a single edge.
§Examples
use rust_igraph::GraphBuilder;
let g = GraphBuilder::undirected()
.edge(0, 1)
.edge(1, 2)
.build()
.unwrap();
assert_eq!(g.ecount(), 2);Sourcepub fn edges(self, pairs: &[(VertexId, VertexId)]) -> Self
pub fn edges(self, pairs: &[(VertexId, VertexId)]) -> Self
Add multiple edges from a slice of (from, to) pairs.
§Examples
use rust_igraph::GraphBuilder;
let g = GraphBuilder::undirected()
.edges(&[(0, 1), (1, 2), (2, 0)])
.build()
.unwrap();
assert_eq!(g.ecount(), 3);Sourcepub fn path(self, vertices: &[VertexId]) -> Self
pub fn path(self, vertices: &[VertexId]) -> Self
Add a chain of edges forming a path: v0 -> v1 -> v2 -> ... -> vk.
§Examples
use rust_igraph::GraphBuilder;
let g = GraphBuilder::undirected()
.path(&[0, 1, 2, 3, 4])
.build()
.unwrap();
assert_eq!(g.ecount(), 4);Sourcepub fn cycle(self, vertices: &[VertexId]) -> Self
pub fn cycle(self, vertices: &[VertexId]) -> Self
Add edges forming a cycle: v0 -> v1 -> ... -> vk -> v0.
§Examples
use rust_igraph::GraphBuilder;
let g = GraphBuilder::undirected()
.cycle(&[0, 1, 2, 3])
.build()
.unwrap();
assert_eq!(g.ecount(), 4);
assert!(g.has_edge(3, 0));Sourcepub fn clique(self, vertices: &[VertexId]) -> Self
pub fn clique(self, vertices: &[VertexId]) -> Self
Add edges forming a complete subgraph (clique) on the given vertices.
§Examples
use rust_igraph::GraphBuilder;
let g = GraphBuilder::undirected()
.clique(&[0, 1, 2, 3])
.build()
.unwrap();
// K_4 has 4*3/2 = 6 edges.
assert_eq!(g.ecount(), 6);Sourcepub fn star(self, center: VertexId, leaves: &[VertexId]) -> Self
pub fn star(self, center: VertexId, leaves: &[VertexId]) -> Self
Add edges connecting a center vertex to all given leaf vertices (star).
§Examples
use rust_igraph::GraphBuilder;
let g = GraphBuilder::undirected()
.star(0, &[1, 2, 3, 4])
.build()
.unwrap();
assert_eq!(g.ecount(), 4);Sourcepub fn build(self) -> IgraphResult<Graph>
pub fn build(self) -> IgraphResult<Graph>
Consume the builder and produce a Graph.
§Errors
Returns an error if the explicit vertex count is smaller than required by the edges.
§Examples
use rust_igraph::GraphBuilder;
// Error: vertex count too small for edge (0, 5).
let r = GraphBuilder::undirected()
.vertices(3)
.edge(0, 5)
.build();
assert!(r.is_err());Trait Implementations§
Source§impl Clone for GraphBuilder
impl Clone for GraphBuilder
Source§fn clone(&self) -> GraphBuilder
fn clone(&self) -> GraphBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more