pub struct Graph<V: Vertex, E: Edge> { /* private fields */ }
Expand description
A directed graph.
Implementations§
Source§impl<V, E> Graph<V, E>
impl<V, E> Graph<V, E>
pub fn new() -> Graph<V, E>
pub fn num_vertices(&self) -> usize
Sourcepub fn has_vertex(&self, index: usize) -> bool
pub fn has_vertex(&self, index: usize) -> bool
Returns true if the vertex with the given index exists in this graph
Sourcepub fn remove_vertex(&mut self, index: usize) -> Result<(), Error>
pub fn remove_vertex(&mut self, index: usize) -> Result<(), Error>
Removes a vertex, and all edges associated with that vertex.
Sourcepub fn remove_unreachable_vertices(&mut self, head: usize) -> Result<(), Error>
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.
Sourcepub fn has_edge(&self, head: usize, tail: usize) -> bool
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
Sourcepub fn insert_vertex(&mut self, v: V) -> Result<(), Error>
pub fn insert_vertex(&mut self, v: V) -> Result<(), Error>
Sourcepub fn insert_edge(&mut self, edge: E) -> Result<(), Error>
pub fn insert_edge(&mut self, edge: E) -> Result<(), Error>
Sourcepub fn successors(&self, index: usize) -> Result<Vec<&V>, Error>
pub fn successors(&self, index: usize) -> Result<Vec<&V>, Error>
Returns all immediate successors of a vertex from the graph.
Sourcepub fn predecessors(&self, index: usize) -> Result<Vec<&V>, Error>
pub fn predecessors(&self, index: usize) -> Result<Vec<&V>, Error>
Returns all immediate predecessors of a vertex from the graph.
Sourcepub fn successor_indices(&self, index: usize) -> Result<Vec<usize>, Error>
pub fn successor_indices(&self, index: usize) -> Result<Vec<usize>, Error>
Returns the indices of all immediate successors of a vertex from the graph.
Sourcepub fn predecessor_indices(&self, index: usize) -> Result<Vec<usize>, Error>
pub fn predecessor_indices(&self, index: usize) -> Result<Vec<usize>, Error>
Returns the indices of all immediate predecessors of a vertex from the graph.
Sourcepub fn vertices_without_predecessors(&self) -> Vec<&V>
pub fn vertices_without_predecessors(&self) -> Vec<&V>
Returns all vertices which don’t have any predecessors in the graph.
Sourcepub fn vertices_without_successors(&self) -> Vec<&V>
pub fn vertices_without_successors(&self) -> Vec<&V>
Returns all vertices which don’t have any successors in the graph.
Sourcepub fn unreachable_vertices(
&self,
index: usize,
) -> Result<FxHashSet<usize>, Error>
pub fn unreachable_vertices( &self, index: usize, ) -> Result<FxHashSet<usize>, Error>
Computes the set of vertices unreachable from the given index.
Sourcepub fn reachable_vertices(
&self,
index: usize,
) -> Result<FxHashSet<usize>, Error>
pub fn reachable_vertices( &self, index: usize, ) -> Result<FxHashSet<usize>, Error>
Computes the set of vertices reachable from the given index.
Sourcepub fn compute_pre_order(&self, root: usize) -> Result<Vec<usize>, Error>
pub fn compute_pre_order(&self, root: usize) -> Result<Vec<usize>, Error>
Compute the pre order of all vertices in the graph
pub fn compute_post_order(&self, root: usize) -> Result<Vec<usize>, Error>
Sourcepub fn compute_dominance_frontiers(
&self,
start_index: usize,
) -> Result<FxHashMap<usize, FxHashSet<usize>>, Error>
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
Sourcepub fn compute_immediate_dominators(
&self,
root: usize,
) -> Result<FxHashMap<usize, usize>, Error>
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
Sourcepub fn compute_dominators(
&self,
start_index: usize,
) -> Result<FxHashMap<usize, FxHashSet<usize>>, Error>
pub fn compute_dominators( &self, start_index: usize, ) -> Result<FxHashMap<usize, FxHashSet<usize>>, Error>
Computes dominators for all vertices in the graph
Sourcepub fn compute_dominator_tree(
&self,
start_index: usize,
) -> Result<Graph<NullVertex, NullEdge>, Error>
pub fn compute_dominator_tree( &self, start_index: usize, ) -> Result<Graph<NullVertex, NullEdge>, Error>
Creates a dominator tree with NullVertex and NullEdge
Sourcepub fn compute_predecessors(
&self,
) -> Result<FxHashMap<usize, FxHashSet<usize>>, Error>
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.
Sourcepub fn compute_dfs_tree(
&self,
start_index: usize,
) -> Result<Graph<NullVertex, NullEdge>, Error>
pub fn compute_dfs_tree( &self, start_index: usize, ) -> Result<Graph<NullVertex, NullEdge>, Error>
Creates a DFS tree with NullVertex and NullEdge
Sourcepub fn compute_acyclic(
&self,
start_index: usize,
) -> Result<Graph<NullVertex, NullEdge>, Error>
pub fn compute_acyclic( &self, start_index: usize, ) -> Result<Graph<NullVertex, NullEdge>, Error>
Creates an acyclic graph with NullVertex and NullEdge
Sourcepub fn is_acyclic(&self, root: usize) -> bool
pub fn is_acyclic(&self, root: usize) -> bool
Determines if the graph is acyclic
Sourcepub fn is_reducible(&self, head: usize) -> Result<bool, Error>
pub fn is_reducible(&self, head: usize) -> Result<bool, Error>
Determines if the graph is reducible.
Sourcepub fn compute_loops(&self, head: usize) -> Result<Vec<Loop>, Error>
pub fn compute_loops(&self, head: usize) -> Result<Vec<Loop>, Error>
Computes the set of natural loops in the graph
Sourcepub fn compute_loop_tree(&self, head: usize) -> Result<LoopTree, Error>
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.
Sourcepub fn compute_topological_ordering(&self) -> Result<Vec<usize>, Error>
pub fn compute_topological_ordering(&self) -> Result<Vec<usize>, Error>
Computes the topological ordering of all vertices in the graph
pub fn vertices_mut(&mut self) -> Vec<&mut V>
Sourcepub fn vertex(&self, index: usize) -> Result<&V, Error>
pub fn vertex(&self, index: usize) -> Result<&V, Error>
Fetches an index from the graph by index.
pub fn vertex_mut(&mut self, index: usize) -> Result<&mut V, Error>
pub fn edge(&self, head: usize, tail: usize) -> Result<&E, Error>
pub fn edge_mut(&mut self, head: usize, tail: usize) -> Result<&mut E, Error>
Sourcepub fn edges_out(&self, index: usize) -> Result<Vec<&E>, Error>
pub fn edges_out(&self, index: usize) -> Result<Vec<&E>, Error>
Return all edges out for a vertex