Skip to main content

Module graphs

Module graphs 

Source
Expand description

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.

use graphity_core::{graphs, GraphBuilder, NodeMapping};

let mut b = GraphBuilder::new();
b.add_nodes(4);
b.add_edge_pairs(&[0, 1, 0, 2, 1, 3]);
let graph = b.build();

assert!(graphs::is_dag(&graph));
assert!(graphs::is_tree(&graph));
assert!(!graphs::is_path(&graph));
assert!(graphs::is_bipartite(&graph));

let reached = graphs::reachable_from(&graph, 1);
let sub = graphs::remapped(&graph, &NodeMapping::for_subgraph_sorted(&reached).unwrap());
assert_eq!(graphs::adjacency_list_of(&sub), "0 -> [1]\n1 -> []\n");

Data on nodes and edges follows a renumbered, transposed or condensed graph through the mappings. Like the node mappings that the factories of NodeMapping make, the edge mappings point from new to old: from the derived graph to the original one. Their gather functions carry an array of data over to the derived graph. An EdgePartition groups the edges of the original by the condensed edge they became, and its reduce functions combine their data.

A shape check without a prefix reads the graph as directed. One prefixed undirected reads it as undirected, as components::ccs_of does: each edge connects its source and target both ways, and the edges a -> b and b -> a count as one. is_bipartite and is_weakly_connected read it so too, without the prefix, because neither property has a directed reading. The checks of the undirected reading use union-find, whose cost grows with alpha(node_count). The inverse Ackermann function alpha is below 5 for any graph. No shape check needs memory in proportion to the edge count.

Every result is computed anew on every call, not cached, and shares no array with the arguments: keep a result to use it more than once. No function recurses, so deep graphs do not overflow the call stack.

Functions§

adjacency_list_of
Returns the whole graph as text: one line per node with its out-nodes, in ascending order.
diff
Returns the edges added and removed between before and after. Both are graphs over the same nodes.
dominator_tree
Returns the dominator tree of dominators as a graph, with an edge d -> n for every node n whose immediate dominator is d.
edge_mapping
Returns the edge mapping from new_graph to old_graph that goes with the node mapping new_to_old.
edge_partition
Returns the edge partition that goes with the node partition partition, from the edges of the partitioned graph to the edges of condensed_graph.
find_cycle
Returns the nodes of a cycle of graph in the order of its edges, or an empty vector if graph is a DAG.
is_bipartite
Returns whether graph is bipartite: its nodes split into two sets such that every edge runs from one set to the other.
is_dag
Returns whether graph is a DAG: a graph without cycles. A self-loop is a cycle.
is_forest
Returns whether graph is a forest: a DAG in which every node has in-degree at most 1, so that the edges point away from the root nodes.
is_path
Returns whether graph is a path: a tree in which every node has out-degree at most 1.
is_strongly_connected
Returns whether graph is strongly connected: every node reaches every other along the edges.
is_tree
Returns whether graph is a tree: a forest with at most one root node.
is_undirected_forest
Returns whether graph is a forest when read as undirected: without cycles.
is_undirected_tree
Returns whether graph is a tree when read as undirected: connected and without cycles.
is_weakly_connected
Returns whether graph is weakly connected: connected when the direction of the edges is ignored.
reachable_from
Returns the nodes reachable from source, in ascending order and with source included. Equivalent to reachable_from_sources(graph, &[source]) - Java’s overload reachableFrom(Graph, int).
reachable_from_sources
Returns the nodes reachable from at least one of sources, in ascending order and with the sources included - Java’s overload reachableFrom(Graph, IntList).
remapped
Returns graph with its nodes renumbered by new_to_old: node i of the result is node new_to_old.get(i) of graph.
root_nodes
Returns the root nodes of graph in ascending order. A root node has in-degree 0.
topology_hash
Returns a 64-bit hash of the nodes and edges of graph, consistent with ==.
transitive_closure
Returns the transitive closure of graph: the same nodes, with an edge u -> v wherever a path of one or more edges leads from u to v.
transitive_reduction
Returns the transitive reduction of dag: the same nodes, keeping each edge u -> v unless another path leads from u to v.
transposed_edge_mapping
Returns the edge mapping from the transposed graph of graph to graph.