Struct graph_builder::builder::GraphBuilder
source · pub struct GraphBuilder<State> { /* private fields */ }
Expand description
A builder to create graphs in a type-safe way.
The builder implementation uses different states to allow staged building of graphs. Each individual state enables stage-specific methods on the builder.
Examples
Create a directed graph from a vec of edges:
use graph_builder::prelude::*;
let graph: DirectedCsrGraph<usize> = GraphBuilder::new()
.edges(vec![(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)])
.build();
assert_eq!(graph.node_count(), 4);
Create an undirected graph from a vec of edges:
use graph_builder::prelude::*;
let graph: UndirectedCsrGraph<usize> = GraphBuilder::new()
.edges(vec![(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)])
.build();
assert_eq!(graph.node_count(), 4);
Implementations§
source§impl GraphBuilder<Uninitialized>
impl GraphBuilder<Uninitialized>
sourcepub fn csr_layout(self, csr_layout: CsrLayout) -> Self
pub fn csr_layout(self, csr_layout: CsrLayout) -> Self
Sets the CsrLayout
to use during CSR construction.
Examples
Store the neighbors sorted:
use graph_builder::prelude::*;
let graph: UndirectedCsrGraph<usize> = GraphBuilder::new()
.csr_layout(CsrLayout::Sorted)
.edges(vec![(0, 7), (0, 3), (0, 3), (0, 1)])
.build();
assert_eq!(graph.neighbors(0).copied().collect::<Vec<_>>(), &[1, 3, 3, 7]);
Store the neighbors sorted and deduplicated:
use graph_builder::prelude::*;
let graph: UndirectedCsrGraph<usize> = GraphBuilder::new()
.csr_layout(CsrLayout::Deduplicated)
.edges(vec![(0, 7), (0, 3), (0, 3), (0, 1)])
.build();
assert_eq!(graph.neighbors(0).copied().collect::<Vec<_>>(), &[1, 3, 7]);
sourcepub fn edges<NI, Edges>(self, edges: Edges) -> GraphBuilder<FromEdges<NI, Edges>>where
NI: Idx,
Edges: IntoIterator<Item = (NI, NI)>,
pub fn edges<NI, Edges>(self, edges: Edges) -> GraphBuilder<FromEdges<NI, Edges>>where NI: Idx, Edges: IntoIterator<Item = (NI, NI)>,
Create a graph from the given edge tuples.
Example
use graph_builder::prelude::*;
let graph: DirectedCsrGraph<usize> = GraphBuilder::new()
.edges(vec![(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)])
.build();
assert_eq!(graph.node_count(), 4);
assert_eq!(graph.edge_count(), 5);
sourcepub fn edges_with_values<NI, Edges, EV>(
self,
edges: Edges
) -> GraphBuilder<FromEdgesWithValues<NI, Edges, EV>>where
NI: Idx,
Edges: IntoIterator<Item = (NI, NI, EV)>,
pub fn edges_with_values<NI, Edges, EV>( self, edges: Edges ) -> GraphBuilder<FromEdgesWithValues<NI, Edges, EV>>where NI: Idx, Edges: IntoIterator<Item = (NI, NI, EV)>,
Create a graph from the given edge triplets.
Example
use graph_builder::prelude::*;
let graph: DirectedCsrGraph<usize, (), f32> = GraphBuilder::new()
.edges_with_values(vec![(0, 1, 0.1), (0, 2, 0.2), (1, 2, 0.3), (1, 3, 0.4), (2, 3, 0.5)])
.build();
assert_eq!(graph.node_count(), 4);
assert_eq!(graph.edge_count(), 5);
sourcepub fn gdl_str<NI, S>(self, gdl: S) -> GraphBuilder<FromGdlString<NI>>where
NI: Idx,
S: Into<String>,
Available on crate feature gdl
only.
pub fn gdl_str<NI, S>(self, gdl: S) -> GraphBuilder<FromGdlString<NI>>where NI: Idx, S: Into<String>,
gdl
only.Creates a graph using Graph Definition Language (GDL).
Creating graphs from GDL is recommended for small graphs only, e.g., during testing. The graph construction is not parallelized.
See GDL on crates.io for more information on how to define graphs using GDL.
Example
use graph_builder::prelude::*;
let g: UndirectedCsrGraph<usize> = GraphBuilder::new()
.gdl_str::<usize, _>("(a)-->(),(a)-->()")
.build()
.unwrap();
assert_eq!(g.node_count(), 3);
assert_eq!(g.edge_count(), 2);
One can also create weighted graphs using GDL. There needs to be exactly one edge property with the same type as specified for the edge value. The property key is not relevant.
use graph_builder::prelude::*;
let g: UndirectedCsrGraph<usize, (), f32> = GraphBuilder::new()
.csr_layout(CsrLayout::Sorted)
.gdl_str::<usize, _>("(a)-[{f: 0.42}]->(),(a)-[{f: 13.37}]->()")
.build()
.unwrap();
assert_eq!(g.node_count(), 3);
assert_eq!(g.edge_count(), 2);
let mut neighbors = g.neighbors_with_values(0);
assert_eq!(neighbors.next(), Some(&Target::new(1, 0.42)));
assert_eq!(neighbors.next(), Some(&Target::new(2, 13.37)));
assert_eq!(neighbors.next(), None);
sourcepub fn gdl_graph<NI>(
self,
gdl_graph: &Graph
) -> GraphBuilder<FromGdlGraph<'_, NI>>where
NI: Idx,
Available on crate feature gdl
only.
pub fn gdl_graph<NI>( self, gdl_graph: &Graph ) -> GraphBuilder<FromGdlGraph<'_, NI>>where NI: Idx,
gdl
only.Creates a graph from an already constructed GDL graph.
Creating graphs from GDL is recommended for small graphs only, e.g., during testing. The graph construction is not parallelized.
See GDL on crates.io for more information on how to define graphs using GDL.
Example
use graph_builder::prelude::*;
let gdl_graph = "(a)-->(),(a)-->()".parse::<::gdl::Graph>().unwrap();
let g: DirectedCsrGraph<usize> = GraphBuilder::new()
.gdl_graph::<usize>(&gdl_graph)
.build()
.unwrap();
assert_eq!(g.node_count(), 3);
assert_eq!(g.edge_count(), 2);
let id_a = gdl_graph.get_node("a").unwrap().id();
assert_eq!(g.out_neighbors(id_a).len(), 2);
sourcepub fn file_format<Format, Path, NI>(
self,
_format: Format
) -> GraphBuilder<FromInput<NI, Path, Format>>where
Path: AsRef<StdPath>,
NI: Idx,
Format: InputCapabilities<NI>,
Format::GraphInput: TryFrom<InputPath<Path>>,
pub fn file_format<Format, Path, NI>( self, _format: Format ) -> GraphBuilder<FromInput<NI, Path, Format>>where Path: AsRef<StdPath>, NI: Idx, Format: InputCapabilities<NI>, Format::GraphInput: TryFrom<InputPath<Path>>,
Creates a graph by reading it from the given file format.
Examples
Read a directed graph from an edge list file:
use std::path::PathBuf;
use graph_builder::prelude::*;
let path = [env!("CARGO_MANIFEST_DIR"), "resources", "example.el"]
.iter()
.collect::<PathBuf>();
let graph: DirectedCsrGraph<usize> = GraphBuilder::new()
.file_format(EdgeListInput::default())
.path(path)
.build()
.expect("loading failed");
assert_eq!(graph.node_count(), 4);
assert_eq!(graph.edge_count(), 5);
source§impl<NI, Edges> GraphBuilder<FromEdges<NI, Edges>>where
NI: Idx,
Edges: IntoIterator<Item = (NI, NI)>,
impl<NI, Edges> GraphBuilder<FromEdges<NI, Edges>>where NI: Idx, Edges: IntoIterator<Item = (NI, NI)>,
pub fn node_values<NV, I>( self, node_values: I ) -> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, ()>>where I: IntoIterator<Item = NV>,
source§impl<NI, Edges, EV> GraphBuilder<FromEdgesWithValues<NI, Edges, EV>>where
NI: Idx,
EV: Sync,
Edges: IntoIterator<Item = (NI, NI, EV)>,
impl<NI, Edges, EV> GraphBuilder<FromEdgesWithValues<NI, Edges, EV>>where NI: Idx, EV: Sync, Edges: IntoIterator<Item = (NI, NI, EV)>,
pub fn node_values<NV, I>( self, node_values: I ) -> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, EV>>where I: IntoIterator<Item = NV>,
source§impl<NI: Idx, NV, EV> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, EV>>
impl<NI: Idx, NV, EV> GraphBuilder<FromEdgeListAndNodeValues<NI, NV, EV>>
source§impl<NI> GraphBuilder<FromGdlString<NI>>where
NI: Idx,
impl<NI> GraphBuilder<FromGdlString<NI>>where NI: Idx,
source§impl<'a, NI> GraphBuilder<FromGdlGraph<'a, NI>>where
NI: Idx,
impl<'a, NI> GraphBuilder<FromGdlGraph<'a, NI>>where NI: Idx,
source§impl<NI, Path, Format> GraphBuilder<FromInput<NI, Path, Format>>where
Path: AsRef<StdPath>,
NI: Idx,
Format: InputCapabilities<NI>,
Format::GraphInput: TryFrom<InputPath<Path>>,
impl<NI, Path, Format> GraphBuilder<FromInput<NI, Path, Format>>where Path: AsRef<StdPath>, NI: Idx, Format: InputCapabilities<NI>, Format::GraphInput: TryFrom<InputPath<Path>>,
sourcepub fn path(self, path: Path) -> GraphBuilder<FromPath<NI, Path, Format>>
pub fn path(self, path: Path) -> GraphBuilder<FromPath<NI, Path, Format>>
Set the location where the graph is stored.