Namespace Graphity
Graphity.Core
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 package is the core of Graphity for .NET, with the namespace Graphity. The packages Graphity.ControlFlow, Graphity.Dot and Graphity.Mermaid build on it and are released with it at the same version, and Graphity.All brings in all four. A Graph has the nodes 0..NodeCount-1 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:
using System.Collections.Immutable;
using Graphity;
// The packages are the nodes 0..7, and uses[p] lists the packages that package p
// uses.
string[] names =
["app", "cli", "ui", "service", "model", "storage", "json", "util"];
int[][] uses = [[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.
var builder = new GraphBuilder();
builder.AddNodes(uses.Length);
for (int pkg = 0; pkg < uses.Length; pkg++)
{
builder.AddOutEdges(pkg, uses[pkg]);
}
Graph dependencies = builder.Build();
// Find a cycle.
ImmutableArray<int> cycle = Graphs.FindCycle(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.WriteTo(Stream)andGraph.ReadFrom(Stream)- store a graph in a stream and load it again, in the binary format of the Java library.SortedIntView- the sorted list of node IDs or edge indices that lookups such asGraph.OutNodesreturn;Edges- a list of edges, each with its source, target and edge index.Graphs.AdjacencyListOf,Graphs.TopologyHashandGraphs.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.SccsOf- the SCCs (strongly connected components), asSccs.Components.WccsOf- the weakly connected components, which are the components of the graph read as undirected.Components.BiconnectivityOf- the bridges, articulation points and biconnected components, asBiconnectivity.Components.StrongConnectivityOf- the strong bridges and strong articulation points, asStrongConnectivity.Graphs.IsStronglyConnectedandGraphs.IsWeaklyConnected- the same questions for the whole graph, answered without the components.
Orders, cycles and traversals
TopOrder- a topological order of a DAG (directed acyclic graph);TopOrderAndLevels- one with a level per node.Graphs.IsDagandGraphs.FindCycle- whether a graph has a cycle, and one of its cycles.EdgeTypes- the tree, back, forward and cross edges of a depth-first search;BackEdges- the back edges alone.DfsTraverser,DfsEdgeTraverserandFrontierTraverser- the base classes of depth-first and breadth-first searches of your own.
Reachability and distances
Graphs.ReachableFrom- the nodes reachable from one or several nodes;Graphs.RootNodes- the nodes with in-degree 0, called root nodes.BfsDistances- distances and shortest paths from one or several nodes.Graphs.TransitiveClosureandGraphs.TransitiveReduction- 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.IsTree,Graphs.IsForestandGraphs.IsPath- shape checks, and their counterparts for the graph read as undirected, such asGraphs.IsUndirectedTree.Dominators- the dominators of a control-flow graph, and on its transposed graph the post-dominators;Graphs.DominatorTree- the dominator tree as a graph.
Loops, dominance frontiers, control dependence and the placement of phi-nodes are in the package Graphity.ControlFlow. The package 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.TransposedEdgeMappingcarries data on the edges over to it.
No parameter, return value or element in this package is null unless its type is nullable; a null argument where the type does not allow one throws ArgumentNullException. The XML documentation of every type and member states its contract: what it returns and throws, and its cost in time and memory.
Classes
- 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.
- BackEdges
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.
- 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 BiconnectivityOf(BiAdjacentGraph) returns them.
- Components
The algorithms for the components of a graph, and for the edges and nodes whose removal splits them.
- Csr
Access to the arrays in which a graph keeps its edges, for algorithm code that loops over all edges. These arrays are called the CSR (compressed sparse row) layout of the graph.
- DfsEdgeTraverser
The base class of a depth-first search of your own over a Graph, with a hook for every edge. The hook also sees the edges to nodes already visited.
- DfsTraverser
The base class of a depth-first search of your own over a Graph. A subclass overrides the hooks EnterNode(int) and LeaveNode(int), and a
Runmethod starts the search.
- 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 Of(Graph) and Bridges return it.
- FrontierTraverser
The base class 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.
- 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 Diff(Graph, Graph) returns them.
- Graphs
Operations on graphs, in five families: derived graphs, edge mappings, node sets, shape checks, and a graph as text, hash or diff.
- IndexMapping
A mapping from the indices
0..IndexCount-1to the indices of another numbering. 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.
- 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 SccsOf(Graph) returns them.
- StrongConnectivity
The strong bridges and strong articulation points of a graph, as StrongConnectivityOf(BiAdjacentGraph) returns them.
- TopOrder
The topological order of a DAG (directed acyclic graph), in which every node comes before its out-nodes.
- 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.
Structs
- SortedIntView
A list of
intvalues in ascending order and without duplicates, as a view of a range of an array.
- SortedIntView.Enumerator
An enumerator over the values of a SortedIntView, in ascending order.
Enums
- DfsEdgeTraverser.EdgeAction
What the search does with an edge that VisitEdge(int, int, int, TargetState) reports.
- DfsEdgeTraverser.TargetState
The state of the target of an edge when VisitEdge(int, int, int, TargetState) reports the edge.
- EdgeType
The type of an edge in a DFS (depth-first search), as EdgeTypes classifies the edges of a graph.
Delegates
- EdgeConsumer
An action on an edge, given its source, target and edge index: the callback of ForEachEdge(EdgeConsumer).