Skip to main content

Crate graphity_core

Crate graphity_core 

Source
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

§Components and connectivity

§Orders, cycles and traversals

§Reachability and distances

§Trees and dominators

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

§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.
BiAdjacentGraph
A Graph that 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_of returns 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 DfsTraverser and DfsEdgeTraverser, which share this type.
DfsEdgeTraverser
The base of a depth-first search of your own over a Graph, with a hook for every edge - Java’s abstract class DfsEdgeTraverser. 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 class DfsTraverser. A type of yours implements the hooks enter_node and leave_node of DfsHooks, and a run method 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.
EdgeMapping
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.
EdgePartition
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.
EdgeTypes
The type of every edge of a graph in a depth-first search: tree, back, forward or cross edge, as EdgeType defines them.
Edges
A list of edges of a graph, each with its source, target and edge index. Algorithms such as back_edges::of and Biconnectivity::bridges return it.
FrontierControl
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’s FrontierTraverser.
FrontierTraverser
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 class FrontierTraverser. A type of yours implements the hooks of FrontierHooks, and a run method starts the search with them.
Graph
A directed graph, which is the input of the algorithms in this library.
GraphBuilder
A builder for a Graph, from nodes and edges added in any order.
GraphDiff
The edges added and removed between two graphs over the same nodes, as graphs::diff returns them.
NodeMapping
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.
NodePartition
A partition of the nodes of a graph into groups, such as its components.
PreorderAncestry
An Ancestry that 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_of returns them.
SortedIntView
A list of u32 values in ascending order and without duplicates, as a view of a range of an array.
StrongConnectivity
The strong bridges and strong articulation points of a graph, as components::strong_connectivity_of returns them.
TopOrderAndLevels
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§

EdgeAction
What the search does with an edge that visit_edge reports.
EdgeType
The type of an edge in a DFS (depth-first search), as EdgeTypes classifies the edges of a graph.
TargetState
The state of the target of an edge when visit_edge reports the edge.

Constants§

NONE
The value for “none” in the arrays this crate takes, such as the group_of of NodePartition::of: a node in no group, or a node that maps to no node. It has the same bits as Java’s -1 and is never a valid node ID or edge index.

Traits§

DfsEdgeHooks
The hooks of a depth-first search of your own that looks at edges, which a DfsEdgeTraverser runs: 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 DfsTraverser runs: a type of yours implements enter_node and, if needed, leave_node.
FrontierHooks
The hooks of a search of your own that works off a queue of nodes, which a FrontierTraverser runs: a type of yours implements visit_node and, if needed, leave_level.
IndexMapping
A mapping from the indices 0..index_count() to the indices of another numbering - Java’s abstract class IndexMapping. It is the common base of the node and edge mappings and of the partitions.
IndexPartition
A partition of indices into groups: the common base of NodePartition and EdgePartition - Java’s abstract class IndexPartition.