Struct Graph

Source
pub struct Graph<V: Vertex, E: Edge> { /* private fields */ }
Expand description

A directed graph.

Implementations§

Source§

impl<V, E> Graph<V, E>
where V: Vertex, E: Edge,

Source

pub fn new() -> Graph<V, E>

Source

pub fn num_vertices(&self) -> usize

Source

pub fn has_vertex(&self, index: usize) -> bool

Returns true if the vertex with the given index exists in this graph

Source

pub fn remove_vertex(&mut self, index: usize) -> Result<(), Error>

Removes a vertex, and all edges associated with that vertex.

Source

pub fn remove_unreachable_vertices(&mut self, head: usize) -> Result<(), Error>

Removes all unreachable vertices from this graph. Unreachable means that there is no path from head to the vertex.

Source

pub fn has_edge(&self, head: usize, tail: usize) -> bool

Returns true if the edge with the given head and tail index exists in this graph

Source

pub fn remove_edge(&mut self, head: usize, tail: usize) -> Result<(), Error>

Removes an edge

Source

pub fn insert_vertex(&mut self, v: V) -> Result<(), Error>

Inserts a vertex into the graph.

§Errors

Error if the vertex already exists by index.

Source

pub fn insert_edge(&mut self, edge: E) -> Result<(), Error>

Inserts an edge into the graph.

§Errors

Error if the edge already exists by indices.

Source

pub fn successors(&self, index: usize) -> Result<Vec<&V>, Error>

Returns all immediate successors of a vertex from the graph.

Source

pub fn predecessors(&self, index: usize) -> Result<Vec<&V>, Error>

Returns all immediate predecessors of a vertex from the graph.

Source

pub fn successor_indices(&self, index: usize) -> Result<Vec<usize>, Error>

Returns the indices of all immediate successors of a vertex from the graph.

Source

pub fn predecessor_indices(&self, index: usize) -> Result<Vec<usize>, Error>

Returns the indices of all immediate predecessors of a vertex from the graph.

Source

pub fn vertices_without_predecessors(&self) -> Vec<&V>

Returns all vertices which don’t have any predecessors in the graph.

Source

pub fn vertices_without_successors(&self) -> Vec<&V>

Returns all vertices which don’t have any successors in the graph.

Source

pub fn unreachable_vertices( &self, index: usize, ) -> Result<FxHashSet<usize>, Error>

Computes the set of vertices unreachable from the given index.

Source

pub fn reachable_vertices( &self, index: usize, ) -> Result<FxHashSet<usize>, Error>

Computes the set of vertices reachable from the given index.

Source

pub fn compute_pre_order(&self, root: usize) -> Result<Vec<usize>, Error>

Compute the pre order of all vertices in the graph

Source

pub fn compute_post_order(&self, root: usize) -> Result<Vec<usize>, Error>

Source

pub fn compute_dominance_frontiers( &self, start_index: usize, ) -> Result<FxHashMap<usize, FxHashSet<usize>>, Error>

Computes the dominance frontiers for all vertices in the graph

Source

pub fn compute_immediate_dominators( &self, root: usize, ) -> Result<FxHashMap<usize, usize>, Error>

Computes immediate dominators for all vertices in the graph

This implementation is based on the Semi-NCA algorithm described in: Georgiadis, Loukas: Linear-Time Algorithms for Dominators and Related Problems (thesis) https://www.cs.princeton.edu/research/techreps/TR-737-05

Source

pub fn compute_dominators( &self, start_index: usize, ) -> Result<FxHashMap<usize, FxHashSet<usize>>, Error>

Computes dominators for all vertices in the graph

Source

pub fn compute_dominator_tree( &self, start_index: usize, ) -> Result<Graph<NullVertex, NullEdge>, Error>

Creates a dominator tree with NullVertex and NullEdge

Source

pub fn compute_predecessors( &self, ) -> Result<FxHashMap<usize, FxHashSet<usize>>, Error>

Computes predecessors for all vertices in the graph

The resulting sets include all predecessors for each vertex in the graph, not just immediate predecessors.

Given A -> B -> C, both A and B will be in the set for C.

Source

pub fn compute_dfs_tree( &self, start_index: usize, ) -> Result<Graph<NullVertex, NullEdge>, Error>

Creates a DFS tree with NullVertex and NullEdge

Source

pub fn compute_acyclic( &self, start_index: usize, ) -> Result<Graph<NullVertex, NullEdge>, Error>

Creates an acyclic graph with NullVertex and NullEdge

Source

pub fn is_acyclic(&self, root: usize) -> bool

Determines if the graph is acyclic

Source

pub fn is_reducible(&self, head: usize) -> Result<bool, Error>

Determines if the graph is reducible.

Source

pub fn compute_loops(&self, head: usize) -> Result<Vec<Loop>, Error>

Computes the set of natural loops in the graph

Source

pub fn compute_loop_tree(&self, head: usize) -> Result<LoopTree, Error>

Computes the loop tree of all natural loops in the graph

If loop l1 is nested in loop l2, l1 is a child node of l2 in the loop tree.

Source

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

Computes the topological ordering of all vertices in the graph

Source

pub fn vertices(&self) -> Vec<&V>

Returns all vertices in the graph.

Source

pub fn vertices_mut(&mut self) -> Vec<&mut V>

Source

pub fn vertex(&self, index: usize) -> Result<&V, Error>

Fetches an index from the graph by index.

Source

pub fn vertex_mut(&mut self, index: usize) -> Result<&mut V, Error>

Source

pub fn edge(&self, head: usize, tail: usize) -> Result<&E, Error>

Source

pub fn edge_mut(&mut self, head: usize, tail: usize) -> Result<&mut E, Error>

Source

pub fn edges(&self) -> Vec<&E>

Get a reference to every Edge in the Graph.

Source

pub fn edges_mut(&mut self) -> Vec<&mut E>

Get a mutable reference to every Edge in the Graph.

Source

pub fn edges_out(&self, index: usize) -> Result<Vec<&E>, Error>

Return all edges out for a vertex

Source

pub fn edges_in(&self, index: usize) -> Result<Vec<&E>, Error>

Return all edges in for a vertex

Source

pub fn dot_graph(&self) -> String

Returns a string in the graphviz format

Trait Implementations§

Source§

impl<V: Clone + Vertex, E: Clone + Edge> Clone for Graph<V, E>

Source§

fn clone(&self) -> Graph<V, E>

Returns a copy 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<V: Debug + Vertex, E: Debug + Edge> Debug for Graph<V, E>

Source§

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

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

impl<V: Default + Vertex, E: Default + Edge> Default for Graph<V, E>

Source§

fn default() -> Graph<V, E>

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

impl<'de, V, E> Deserialize<'de> for Graph<V, E>
where V: Deserialize<'de> + Vertex, E: Deserialize<'de> + Edge,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<V: Hash + Vertex, E: Hash + Edge> Hash for Graph<V, E>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<V: Ord + Vertex, E: Ord + Edge> Ord for Graph<V, E>

Source§

fn cmp(&self, other: &Graph<V, E>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<V: PartialEq + Vertex, E: PartialEq + Edge> PartialEq for Graph<V, E>

Source§

fn eq(&self, other: &Graph<V, E>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<V: PartialOrd + Vertex, E: PartialOrd + Edge> PartialOrd for Graph<V, E>

Source§

fn partial_cmp(&self, other: &Graph<V, E>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<V, E> Serialize for Graph<V, E>
where V: Serialize + Vertex, E: Serialize + Edge,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<V: Eq + Vertex, E: Eq + Edge> Eq for Graph<V, E>

Source§

impl<V: Vertex, E: Edge> StructuralPartialEq for Graph<V, E>

Auto Trait Implementations§

§

impl<V, E> Freeze for Graph<V, E>

§

impl<V, E> RefUnwindSafe for Graph<V, E>

§

impl<V, E> Send for Graph<V, E>
where V: Send, E: Send,

§

impl<V, E> Sync for Graph<V, E>

§

impl<V, E> Unpin for Graph<V, E>

§

impl<V, E> UnwindSafe for Graph<V, E>

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,