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

Creates a new builder

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]);

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);

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);

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);

Build the graph from the given vec of edges.

Build the graph from the given vec of edges.

Set the location where the graph is stored.

Build the graph from the given input format and path.

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.