pub struct GraphBuilder<N, W = f64> { /* private fields */ }Expand description
A fluent builder for constructing graphs from either representation.
Call GraphBuilder::new, chain configuration and node/edge additions,
then finalise with build_adjacency_list
or build_adjacency_matrix.
§Examples
use graph_core::{GraphBuilder, Graph};
let g = GraphBuilder::<&str, f64>::new()
.directed()
.node("A")
.node("B")
.node("C")
.edge(0, 1, 1.5)
.edge(1, 2, 2.0)
.build_adjacency_list();
assert_eq!(g.node_count(), 3);
assert_eq!(g.edge_count(), 2);Implementations§
Source§impl<N, W> GraphBuilder<N, W>
impl<N, W> GraphBuilder<N, W>
Sourcepub fn new() -> GraphBuilder<N, W>
pub fn new() -> GraphBuilder<N, W>
Creates a new builder with no nodes or edges (directed by default).
§Examples
use graph_core::GraphBuilder;
let b: GraphBuilder<(), f64> = GraphBuilder::new();Source§impl<N, W> GraphBuilder<N, W>
impl<N, W> GraphBuilder<N, W>
Sourcepub fn directed(self) -> GraphBuilder<N, W>
pub fn directed(self) -> GraphBuilder<N, W>
Configures the graph to be directed (default).
§Examples
use graph_core::GraphBuilder;
let b: GraphBuilder<(), f64> = GraphBuilder::new().directed();Sourcepub fn undirected(self) -> GraphBuilder<N, W>
pub fn undirected(self) -> GraphBuilder<N, W>
Configures the graph to be undirected.
Each call to edge will insert edges in both directions.
§Examples
use graph_core::{GraphBuilder, Graph};
let g = GraphBuilder::<(), f64>::new()
.undirected()
.node(())
.node(())
.edge(0, 1, 1.0)
.build_adjacency_list();
assert!(g.contains_edge(0usize.into(), 1usize.into()));
assert!(g.contains_edge(1usize.into(), 0usize.into()));Source§impl<N, W> GraphBuilder<N, W>
impl<N, W> GraphBuilder<N, W>
Sourcepub fn node(self, data: N) -> GraphBuilder<N, W>
pub fn node(self, data: N) -> GraphBuilder<N, W>
Adds a node with the given data and returns self for chaining.
Nodes are assigned indices in insertion order (0, 1, 2 …).
§Examples
use graph_core::GraphBuilder;
let b = GraphBuilder::<&str, f64>::new()
.node("X")
.node("Y");Sourcepub fn edge(self, from: usize, to: usize, weight: W) -> GraphBuilder<N, W>
pub fn edge(self, from: usize, to: usize, weight: W) -> GraphBuilder<N, W>
Source§impl<N, W> GraphBuilder<N, W>where
W: Clone,
impl<N, W> GraphBuilder<N, W>where
W: Clone,
Sourcepub fn build_adjacency_list(self) -> AdjacencyList<N, W>
pub fn build_adjacency_list(self) -> AdjacencyList<N, W>
Consumes the builder and produces an AdjacencyList.
§Panics
Panics if any edge references a node index that is out of range.
§Examples
use graph_core::{GraphBuilder, Graph};
let g = GraphBuilder::<&str, f64>::new()
.node("A")
.node("B")
.edge(0, 1, 5.0)
.build_adjacency_list();
assert_eq!(g.edge_count(), 1);Sourcepub fn build_adjacency_matrix(self) -> AdjacencyMatrix<N, W>
pub fn build_adjacency_matrix(self) -> AdjacencyMatrix<N, W>
Consumes the builder and produces an AdjacencyMatrix.
§Panics
Panics if any edge references a node index that is out of range.
§Examples
use graph_core::{GraphBuilder, Graph};
let g = GraphBuilder::<&str, f64>::new()
.node("A")
.node("B")
.edge(0, 1, 2.0)
.build_adjacency_matrix();
assert!(g.contains_edge(0usize.into(), 1usize.into()));