SimpleGraph

Struct SimpleGraph 

Source
pub struct SimpleGraph<W> { /* private fields */ }
Expand description

A simple and undirected graph.

A simple graph assumes that the node indexing starts from 0 and is not equipped with a hash map for a mapping from external complex objects to internal graph indices. As a result, SimpleGraph doesn’t have no runtime overhead for such object storage and mapping.

§Examples

The following example shows how to construct a graph and find the shortest path between node 1 and 5. The data is taken from the illustration in Wikipedia’s page for Dijkstra’s algorithm.

Here, the numbering is adjusted so that the node indexing starts from 0.

use pheap::graph::SimpleGraph;

let mut g = SimpleGraph::<u32>::with_capacity(6);

g.add_weighted_edges(0, 1, 7);
g.add_weighted_edges(0, 2, 9);
g.add_weighted_edges(0, 5, 14);
g.add_weighted_edges(1, 2, 10);
g.add_weighted_edges(1, 3, 15);
g.add_weighted_edges(2, 5, 2);
g.add_weighted_edges(2, 3, 11);
g.add_weighted_edges(3, 4, 6);
g.add_weighted_edges(4, 5, 9);

// Finds an SSSP from 0 to 4.
let mut sp = g.sssp_dijkstra(0, &[4]);
assert_eq!(1, sp.len());

let sp = sp.pop().unwrap();
assert_eq!(20, sp.dist());
assert_eq!(&[0, 2, 5, 4], sp.path().as_slice());

// Adds a disconnected component to the graph.
g.add_weighted_edges(6, 7, 2);
g.add_weighted_edges(6, 8, 3);

// Finds an SSSP starting from 0. The result can be used for later query.
let lsp = g.sssp_dijkstra_lazy(0);
let lsp = g.sssp_dijkstra_lazy(0);
let sp = lsp.get(7);
assert_eq!(false, sp.is_feasible());

let sp = lsp.get(4);
assert_eq!(true, sp.is_feasible());
assert_eq!(20, sp.dist());
assert_eq!(&[0, 2, 5, 4], sp.path().as_slice());

Implementations§

Source§

impl<W> SimpleGraph<W>

Source

pub fn new() -> Self

Creates an empty graph.

Source

pub fn with_capacity(n_nodes: usize) -> Self

Creates an empty graph with the given capacitiy of nodes.

Source

pub fn n_nodes(&self) -> usize

Returns the number of nodes in the graph.

Source

pub fn n_edges(&self) -> usize

Returns the number of edges in the graph.

Source

pub fn add_weighted_edges(&mut self, node1: usize, node2: usize, weight: W)
where W: Clone + Copy,

Adds a weighted edge to the graph.

If the edge already exists in the graph, the weight will be updated.

Source

pub fn sssp_dijkstra(&self, src: usize, dest: &[usize]) -> Vec<ShortestPath<W>>
where W: Bounded + Num + Zero + PartialOrd + Copy,

Finds the shortest paths from a source node to destination nodes.

If you want to keep the result for later usage and/or want to save memory, consider using the lazy version SimpleGraph::sssp_dijkstra_lazy, which returns the intermediate result from Dijkstra’s algorithm.

Source

pub fn sssp_dijkstra_lazy(&self, src: usize) -> LazyShortestPaths<W>
where W: Bounded + Num + Zero + PartialOrd + Copy,

Finds the shortest paths from a source node to all nodes and returns the intermediate result for later usage.

Source

pub fn write_edgelist<P>(&self, filepath: P) -> Result<()>
where P: AsRef<Path>, W: Display,

Write graph as a list of edges.

Each line contains one edge, following networkx’s format: index 1 index 2 {'weight': {}}.

Trait Implementations§

Source§

impl<W: Debug> Debug for SimpleGraph<W>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<W: Default> Default for SimpleGraph<W>

Source§

fn default() -> SimpleGraph<W>

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

Auto Trait Implementations§

§

impl<W> Freeze for SimpleGraph<W>

§

impl<W> RefUnwindSafe for SimpleGraph<W>
where W: RefUnwindSafe,

§

impl<W> Send for SimpleGraph<W>
where W: Send,

§

impl<W> Sync for SimpleGraph<W>
where W: Sync,

§

impl<W> Unpin for SimpleGraph<W>
where W: Unpin,

§

impl<W> UnwindSafe for SimpleGraph<W>
where W: UnwindSafe,

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> 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, 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.