Struct WeightedAdjacencyList

Source
pub struct WeightedAdjacencyList { /* private fields */ }
Expand description

A graph represented by a weighted adjacency list. Under the hood, a weighted adjacency list is a Vec of Vec of Edges. For an adjacency list g, g[i] is a Vec of edges pointing from i to other nodes (vertices). Thus, the number of nodes is implied by the len of the (outer) Vec. For each node i that do not have outgoing edges, g[i] is an empty vector.

Implementations§

Source§

impl WeightedAdjacencyList

Source§

impl WeightedAdjacencyList

Source

pub fn dfs(&self, start: usize) -> usize

Perform a depth first search on a graph with n nodes from a starting point to count the number of nodes in a given component.

In this particular implementation we just increment a counter each time we visit a new node. This, by itself, is not of much use, but you’ll soon see that many other advanced algorithms are based on this DFS prototype.

Source§

impl WeightedAdjacencyList

Source§

impl WeightedAdjacencyList

Source§

impl WeightedAdjacencyList

Source

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

Source§

impl WeightedAdjacencyList

Source

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

Source§

impl WeightedAdjacencyList

Source

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

Source

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

Imagine building a program with dependencies

Source

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

Source§

impl WeightedAdjacencyList

Source

pub fn with_size(n: usize) -> Self

Initialize an empty adjacency list that can hold up to n nodes.

Source

pub fn is_empty(&self) -> bool

Is the graph devoid of vertices?

Source

pub fn add_directed_edge(&mut self, u: usize, v: usize, weight: f64)

Add a directed edge from node u to node v with weight weight.

Source

pub fn add_undirected_edge(&mut self, u: usize, v: usize, weight: f64)

Add an undirected edge between nodes u and v.

Source

pub fn new_directed(size: usize, edges: &[(usize, usize, f64)]) -> Self

Source

pub fn new_undirected(size: usize, edges: &[(usize, usize, f64)]) -> Self

Source

pub fn new_directed_unweighted(size: usize, edges: &[[usize; 2]]) -> Self

Source

pub fn new_undirected_unweighted(size: usize, edges: &[[usize; 2]]) -> Self

Source

pub fn edges(&self) -> impl Iterator<Item = (usize, usize, f64)> + '_

Iterates over all edges in the gragh. Each item is a tuples of 3: (from, to, weight)

Source

pub fn edge_count(&self) -> usize

Number of edges in the graph

Source

pub fn nodes(&self) -> impl Iterator<Item = (usize, &Vec<Edge>)>

Iterates over all nodes in the graph. Each item is a tuple of the node id and a Vec of all its outgoing edges

Source

pub fn node_count(&self) -> usize

Number of nodes (vertices) in the graph

Trait Implementations§

Source§

impl Debug for WeightedAdjacencyList

Source§

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

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

impl Display for WeightedAdjacencyList

Pretty-prints a small graph represented by a weighted adjacency list The graph is first converted to a WeightedAdjacencyMatrix before being printed

Source§

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

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

impl From<WeightedAdjacencyList> for WeightedAdjacencyMatrix

For convinience

Source§

fn from(inp: WeightedAdjacencyList) -> Self

Converts to this type from the input type.
Source§

impl From<WeightedAdjacencyList> for WeightedUndirectedAdjacencyMatrixCondensed

Source§

fn from(inp: WeightedAdjacencyList) -> Self

Converts to this type from the input type.
Source§

impl Index<usize> for WeightedAdjacencyList

Allows the outgoing edges of a node to be accessed easily.

Source§

type Output = Vec<Edge>

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. 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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.