Struct graph::builder::GraphBuilder [−][src]
pub struct GraphBuilder<State> { /* fields omitted */ }
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::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::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
Sets the CsrLayout
to use during CSR construction.
Examples
Store the neighbors sorted:
use graph::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), &[1, 3, 3, 7]);
Store the neighbors sorted and deduplicated:
use graph::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), &[1, 3, 7]);
pub 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::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);
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)>,
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::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);
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>>,
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::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);
Read a node labeled graph from a “dot graph” file:
use std::path::PathBuf;
use graph::prelude::*;
let path = [env!("CARGO_MANIFEST_DIR"), "resources", "example.graph"]
.iter()
.collect::<PathBuf>();
let graph: DirectedCsrGraph<usize, usize> = GraphBuilder::new()
.file_format(DotGraphInput::default())
.path(path)
.build()
.expect("loading failed");
assert_eq!(graph.node_count(), 4);
assert_eq!(graph.edge_count(), 5);
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>,
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>,
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>>,
Set the location where the graph is stored.