Expand description
Directed graphs and their analysis, for tools that see a program as graphs: components, orders and cycles, reachability, trees and dominators, and graphs derived from other graphs.
This crate is the counterpart of the Java module graphity-core, package
com.kobayami.graphity; the crates graphity-controlflow, graphity-dot and graphity-mermaid
build on it and are released with it at the same version. A Graph has the nodes
0..node_count() and holds nothing but its edges, in 4 bytes per node and per edge; names and
other data on nodes and edges stay in arrays of the caller, indexed by node ID or edge index.
GraphBuilder builds a graph from a model of the caller, and the algorithms take the graph and
return immutable results. None of them recurses, so a deep graph does not overflow the call stack.
Here, the packages of a small program and the packages each of them uses become a graph, which
has a cycle:
// The packages are the nodes 0 to 7, and uses[p] lists the packages that package p
// uses.
let names = [
"app", "cli", "ui", "service", "model", "storage", "json", "util",
];
let uses: [&[u32]; 8] = [
&[1, 2, 3],
&[7],
&[3, 4, 7],
&[4, 5],
&[5],
&[4, 6, 7],
&[7],
&[],
];
// Build the graph: an edge p -> q for every package q that p uses.
let mut builder = GraphBuilder::new();
builder.add_nodes(uses.len() as u32);
for (pkg, used) in uses.iter().enumerate() {
builder.add_out_edges(pkg as u32, used);
}
let dependencies = builder.build();
// Find a cycle.
let cycle = graphs::find_cycle(&dependencies);
// cycle: [4, 5] - model and storage use each other§Graphs
Graph- a directed graph with the out-nodes of every node;BiAdjacentGraph- one that also holds the in-nodes, for the algorithms that follow edges against their direction. It gives the transposed graph as well.GraphBuilder- builds a graph from edges in any order;Graph::oftakes the arrays of its CSR (compressed sparse row) layout, whichcsrhands to algorithm code of your own.Graph::write_toandGraph::read_from- store a graph in a stream and load it again.SortedIntView- the sorted list of node IDs or edge indices that lookups such asGraph::out_nodesreturn;Edges- a list of edges, each with its source, target and edge index.graphs::adjacency_list_of,graphs::topology_hashandgraphs::diff- a whole graph as text, a hash to keep, and the edges added and removed between two versions of a graph.
§Components and connectivity
components::sccs_of- the SCCs (strongly connected components), asSccs.components::wccs_of- the weakly connected components, which are the components of the graph read as undirected.components::biconnectivity_of- the bridges, articulation points and biconnected components, asBiconnectivity.components::strong_connectivity_of- the strong bridges and strong articulation points, asStrongConnectivity.graphs::is_strongly_connectedandgraphs::is_weakly_connected- the same questions for the whole graph, answered without the components.
§Orders, cycles and traversals
top_order- a topological order of a DAG (directed acyclic graph);TopOrderAndLevels- one with a level per node.graphs::is_dagandgraphs::find_cycle- whether a graph has a cycle, and one of its cycles.EdgeTypes- the tree, back, forward and cross edges of a depth-first search;back_edges- the back edges alone.DfsTraverser,DfsEdgeTraverserandFrontierTraverser- depth-first and breadth-first searches of your own, with the hooks ofDfsHooks,DfsEdgeHooksandFrontierHooks.
§Reachability and distances
graphs::reachable_fromandgraphs::reachable_from_sources- the nodes reachable from one or several nodes;graphs::root_nodes- the nodes with in-degree 0, called root nodes.BfsDistances- distances and shortest paths from one or several nodes.graphs::transitive_closureandgraphs::transitive_reduction- an edge for every path, and the fewest edges for the same paths.
§Trees and dominators
Ancestry- parent, depth and lowest common ancestor in a forest;PreorderAncestry- the ancestor test in O(1) and the nodes of each subtree.graphs::is_tree,graphs::is_forestandgraphs::is_path- shape checks, and their counterparts for the graph read as undirected, such asgraphs::is_undirected_tree.Dominators- the dominators of a control-flow graph, and on its transposed graph the post-dominators;graphs::dominator_tree- the dominator tree as a graph.
Loops, dominance frontiers, control dependence and the placement of phi-nodes are in the crate
graphity-controlflow. The crate graphity-mermaid writes a graph as a Mermaid flowchart, and
graphity-dot writes it as text in DOT (the language of Graphviz).
§Derived graphs and the data on them
graphs::remappedwith aNodeMapping- a subgraph or a graph with renumbered nodes;EdgeMappingcarries data on the edges over to it.NodePartition::condense- the condensed graph of a partition of the nodes into groups;EdgePartition- the edges behind each of its edges.BiAdjacentGraph::transposed- the transposed graph, without copying;graphs::transposed_edge_mappingcarries data on the edges over to it.
§Rust and Java
The names are Java’s in snake case, and the numbers are u32. Where Java returns -1 for “none”,
a single lookup returns Option<u32>, and an array of the caller uses NONE. An operation that
checks a whole input returns a Result with a GraphError; a single argument out of its range
panics, like an out-of-range slice index. The error module describes how both keep Java’s
distinction between IndexOutOfBoundsException and IllegalArgumentException. Every type is
Send and Sync.
Re-exports§
pub use error::ErrorKind;pub use error::GraphError;
Modules§
- back_
edges - The back edges of a graph: the edges that a depth-first search finds pointing back to a node on
its current path, self-loops included - Java’s class
BackEdges. - components
- The algorithms for the components of a graph, and for the edges and nodes whose removal splits
them - Java’s class
Components. - csr
- Access to the arrays in which a graph keeps its edges, for algorithm code that loops over all
edges - Java’s class
Csr. These arrays are called the CSR (compressed sparse row) layout of the graph. - error
- The error type of the operations that validate whole inputs, and the panics of the others.
- graphs
- Operations on graphs, in five families: derived graphs, edge mappings, node sets, shape checks,
and a graph as text, hash or diff - Java’s class
Graphs, with every operation under its Java name in snake case. - top_
order - The topological order of a DAG (directed acyclic graph), in which every node comes before its
out-nodes - Java’s class
TopOrder.
Structs§
- Ancestry
- An index of a forest for ancestor queries: the parent and depth of a node, whether one node is an ancestor of another, and the lowest common ancestor of two nodes.
- BfsDistances
- The distances of the nodes of a graph from one or several sources, with a shortest path to every node reached.
- BiAdjacent
Graph - A
Graphthat also holds the in-nodes of every node, for algorithms that follow edges against their direction. - Biconnectivity
- The bridges, articulation points and biconnected components of a graph read as undirected, as
components::biconnectivity_ofreturns them. - DfsControl
- What a hook may ask or do during a run of a depth-first search: read the graph, ask whether a node
is visited or on the current path, and stop the run - the protected members of Java’s
DfsTraverserandDfsEdgeTraverser, which share this type. - DfsEdge
Traverser - The base of a depth-first search of your own over a
Graph, with a hook for every edge - Java’s abstract classDfsEdgeTraverser. The hook also sees the edges to nodes already visited. - DfsTraverser
- The base of a depth-first search of your own over a
Graph- Java’s abstract classDfsTraverser. A type of yours implements the hooksenter_nodeandleave_nodeofDfsHooks, and arunmethod starts the search with them. - Dominators
- The dominators of a graph with one or more entries, such as the control flow of a method, given by the immediate dominator of every node.
- Edge
Mapping - A mapping from the edge indices of one graph to those of another, to carry data on edges over to a subgraph, a renumbered graph or the transposed graph.
- Edge
Partition - A partition of the edges of a graph into groups: the edges behind each edge of a condensed graph, or the blocks of a graph.
- Edge
Types - The type of every edge of a graph in a depth-first search: tree, back, forward or cross edge, as
EdgeTypedefines them. - Edges
- A list of edges of a graph, each with its source, target and edge index. Algorithms such as
back_edges::ofandBiconnectivity::bridgesreturn it. - Frontier
Control - What a hook may ask or do during a run of a
FrontierTraverser: put nodes into the frontier, ask whether a node is visited and which level is being visited, keep nodes out of the search, and stop the run - the protected members of Java’sFrontierTraverser. - Frontier
Traverser - The base of a breadth-first search of your own over a
Graph, or of any search that works off a queue of nodes and chooses which nodes enter it - Java’s abstract classFrontierTraverser. A type of yours implements the hooks ofFrontierHooks, and arunmethod starts the search with them. - Graph
- A directed graph, which is the input of the algorithms in this library.
- Graph
Builder - A builder for a
Graph, from nodes and edges added in any order. - Graph
Diff - The edges added and removed between two graphs over the same nodes, as
graphs::diffreturns them. - Node
Mapping - A mapping from the node IDs of one graph to those of another, to extract a subgraph or to renumber the nodes of a graph.
- Node
Partition - A partition of the nodes of a graph into groups, such as its components.
- Preorder
Ancestry - An
Ancestrythat also holds the preorder of its forest, for an ancestor test in O(1) and the nodes of every subtree. - Sccs
- The SCCs (strongly connected components) of a graph, as
components::sccs_ofreturns them. - Sorted
IntView - A list of
u32values in ascending order and without duplicates, as a view of a range of an array. - Strong
Connectivity - The strong bridges and strong articulation points of a graph, as
components::strong_connectivity_ofreturns them. - TopOrder
AndLevels - A topological order of a DAG (directed acyclic graph) with the level of each node. A root node has level 0, and every other node has one more than the highest level of its in-nodes.
Enums§
- Edge
Action - What the search does with an edge that
visit_edgereports. - Edge
Type - The type of an edge in a DFS (depth-first search), as
EdgeTypesclassifies the edges of a graph. - Target
State - The state of the target of an edge when
visit_edgereports the edge.
Constants§
- NONE
- The value for “none” in the arrays this crate takes, such as the
group_ofofNodePartition::of: a node in no group, or a node that maps to no node. It has the same bits as Java’s-1and is never a valid node ID or edge index.
Traits§
- DfsEdge
Hooks - The hooks of a depth-first search of your own that looks at edges, which a
DfsEdgeTraverserruns: a type of yours implements the hooks it needs - all three have defaults. - DfsHooks
- The hooks of a depth-first search of your own, which a
DfsTraverserruns: a type of yours implementsenter_nodeand, if needed,leave_node. - Frontier
Hooks - The hooks of a search of your own that works off a queue of nodes, which a
FrontierTraverserruns: a type of yours implementsvisit_nodeand, if needed,leave_level. - Index
Mapping - A mapping from the indices
0..index_count()to the indices of another numbering - Java’s abstract classIndexMapping. It is the common base of the node and edge mappings and of the partitions. - Index
Partition - A partition of indices into groups: the common base of
NodePartitionandEdgePartition- Java’s abstract classIndexPartition.