Skip to main content

GraphBuilder

Struct GraphBuilder 

Source
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

Source

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

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

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.

Source

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

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

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

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

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

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

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

Source§

fn clone(&self) -> GraphBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GraphBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.