Skip to main content

IndexedGraph

Struct IndexedGraph 

Source
pub struct IndexedGraph<C> { /* private fields */ }
Expand description

A directed graph stored as dense usize indices with weighted adjacency lists.

Implementations§

Source§

impl<C> IndexedGraph<C>

Source

pub const fn from_adjacency(adjacency: Vec<Vec<(usize, C)>>) -> Self

Build a directed indexed graph from an adjacency list.

Source

pub const fn node_count(&self) -> usize

Return the number of nodes in the graph.

Source

pub const fn len(&self) -> usize

Return the number of nodes in the graph.

Source

pub const fn is_empty(&self) -> bool

Return true if the graph contains no nodes.

Source

pub fn successors(&self, node: usize) -> &[(usize, C)]

Return the adjacency list for node.

Source

pub fn adjacency(&self) -> &[Vec<(usize, C)>]

Return all adjacency lists.

Source

pub fn astar<FH, FS>( &self, start: usize, heuristic: FH, success: FS, ) -> Option<(Vec<usize>, C)>
where C: Zero + Ord + Copy, FH: FnMut(usize) -> C, FS: FnMut(usize) -> bool,

Run A* from start to a node satisfying success.

Source

pub fn astar_bag<FH, FS>( &self, start: usize, heuristic: FH, success: FS, ) -> Option<(impl Iterator<Item = Vec<usize>>, C)>
where C: Zero + Ord + Copy, FH: FnMut(usize) -> C, FS: FnMut(usize) -> bool,

Run A* and return all shortest paths as an iterator.

Source

pub fn astar_bag_collect<FH, FS>( &self, start: usize, heuristic: FH, success: FS, ) -> Option<(Vec<Vec<usize>>, C)>
where C: Zero + Ord + Copy, FH: FnMut(usize) -> C, FS: FnMut(usize) -> bool,

Run A* and collect all shortest paths into a vector.

Source

pub fn bfs<FS>(&self, start: usize, success: FS) -> Option<Vec<usize>>
where FS: FnMut(usize) -> bool,

Run BFS from start to a node satisfying success.

Source

pub fn bfs_loop(&self, start: usize) -> Option<Vec<usize>>

Run BFS and return the shortest loop starting and ending at start.

Source

pub fn bfs_bidirectional(&self, start: usize, end: usize) -> Option<Vec<usize>>

Run bidirectional BFS from start to end.

Source

pub fn bfs_reach(&self, start: usize) -> impl Iterator<Item = usize> + '_

Iterate over nodes reachable from start using BFS order.

Source

pub fn dfs<FS>(&self, start: usize, success: FS) -> Option<Vec<usize>>
where FS: FnMut(usize) -> bool,

Run DFS from start to a node satisfying success.

Source

pub fn dfs_reach(&self, start: usize) -> impl Iterator<Item = usize> + '_

Iterate over nodes reachable from start using DFS order.

Source

pub fn iddfs<FS>(&self, start: usize, success: FS) -> Option<Vec<usize>>
where FS: FnMut(usize) -> bool,

Run IDDFS from start to a node satisfying success.

Source

pub fn idastar<FH, FS>( &self, start: usize, heuristic: FH, success: FS, ) -> Option<(Vec<usize>, C)>
where C: Zero + Ord + Copy, FH: FnMut(usize) -> C, FS: FnMut(usize) -> bool,

Run IDA* from start to a node satisfying success.

Source

pub fn dijkstra<FS>(&self, start: usize, success: FS) -> Option<(Vec<usize>, C)>
where C: Zero + Ord + Copy, FS: FnMut(usize) -> bool,

Run Dijkstra from start to a node satisfying success.

Source

pub fn dijkstra_all(&self, start: usize) -> Vec<Option<(usize, C)>>
where C: Zero + Ord + Copy,

Compute shortest paths to all reachable nodes from start.

Source

pub fn dijkstra_partial<FS>( &self, start: usize, stop: FS, ) -> (Vec<Option<(usize, C)>>, Option<usize>)
where C: Zero + Ord + Copy, FS: FnMut(usize) -> bool,

Compute shortest paths until stop returns true.

Source

pub fn dijkstra_reach( &self, start: usize, ) -> impl Iterator<Item = (usize, Option<usize>, C)> + '_
where C: Zero + Ord + Copy + Hash,

Iterate over nodes reached by Dijkstra, yielding the node, parent, and total cost.

Source

pub fn bmssp<FS>(&self, start: usize, success: FS) -> Option<(Vec<usize>, C)>
where C: Zero + Ord + Copy, FS: FnMut(usize) -> bool,

Run the BMSSP-based SSSP algorithm from start.

Source

pub fn bmssp_all(&self, start: usize) -> Vec<Option<(usize, C)>>
where C: Zero + Ord + Copy,

Compute BMSSP parents and costs for all reachable nodes from start.

Source

pub fn fringe<FH, FS>( &self, start: usize, heuristic: FH, success: FS, ) -> Option<(Vec<usize>, C)>
where C: Bounded + Zero + Ord + Copy, FH: FnMut(usize) -> C, FS: FnMut(usize) -> bool,

Run Fringe search from start to a node satisfying success.

Source

pub fn topological_sort(&self) -> Result<Vec<usize>, usize>

Compute a topological ordering of all nodes.

§Errors

Returns Err(node) when a cycle is detected.

Source

pub fn strongly_connected_components(&self) -> Vec<Vec<usize>>

Compute strongly connected components for all nodes.

Source

pub fn strongly_connected_components_from( &self, start: usize, ) -> Vec<Vec<usize>>

Compute strongly connected components reachable from start.

Source

pub fn strongly_connected_component(&self, node: usize) -> Vec<usize>

Compute the strongly connected component containing node.

Source

pub fn count_paths<FS>(&self, start: usize, success: FS) -> usize
where FS: FnMut(usize) -> bool,

Count the number of paths from start to nodes satisfying success.

Source

pub fn yen<FS>( &self, start: usize, success: FS, k: usize, ) -> Vec<(Vec<usize>, C)>
where C: Zero + Ord + Copy, FS: FnMut(usize) -> bool,

Compute k-shortest paths using Yen’s algorithm.

Source

pub fn edmonds_karp( &self, source: usize, sink: usize, ) -> (Vec<((usize, usize), C)>, C, Vec<((usize, usize), C)>)
where C: Copy + Zero + Signed + Ord + Bounded,

Compute the maximum flow and minimum cut using Edmonds-Karp.

§Panics

Panics if source or sink are out of bounds.

Source§

impl<C: Copy> IndexedGraph<C>

Source

pub fn from_adjacency_matrix( matrix: &[Vec<Option<C>>], ) -> Result<Self, IndexedInputError>

Build a directed indexed graph from a square adjacency matrix.

Every Some(weight) cell becomes a directed edge from the row index to the column index. None cells produce no edge.

§Errors

Returns IndexedInputError::RaggedMatrix if rows have different lengths and IndexedInputError::NonSquareAdjacencyMatrix if the matrix is not square.

§Example
use pathfinding_indexed::IndexedGraph;

let graph = IndexedGraph::from_adjacency_matrix(&[
    vec![None, Some(3), None],
    vec![None, None, Some(2)],
    vec![Some(1), None, None],
])
.unwrap();

assert_eq!(graph.successors(0), &[(1, 3)]);
assert_eq!(graph.successors(1), &[(2, 2)]);
assert_eq!(graph.successors(2), &[(0, 1)]);

Trait Implementations§

Source§

impl<C: Clone> Clone for IndexedGraph<C>

Source§

fn clone(&self) -> IndexedGraph<C>

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<C: Debug> Debug for IndexedGraph<C>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<C> Freeze for IndexedGraph<C>

§

impl<C> RefUnwindSafe for IndexedGraph<C>
where C: RefUnwindSafe,

§

impl<C> Send for IndexedGraph<C>
where C: Send,

§

impl<C> Sync for IndexedGraph<C>
where C: Sync,

§

impl<C> Unpin for IndexedGraph<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for IndexedGraph<C>

§

impl<C> UnwindSafe for IndexedGraph<C>
where C: 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> 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.