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.
- Derived graphs:
remappedrenumbers the nodes of a graph by aNodeMapping, which also selects subgraphs;transitive_closureandtransitive_reductionadd and drop the edges that other paths imply;dominator_treeturnsDominatorsinto a graph. - Edge mappings:
edge_mapping,transposed_edge_mappingandedge_partitionrelate the edges of a remapped, transposed or condensed graph to the edges of the graph it came from. - Node sets:
root_nodesandreachable_from_sources. - Shape checks:
is_dag,is_forest,is_tree,is_pathandis_strongly_connectedread the graph as directed, andfind_cyclereturns a cycle that makes a graph no DAG (directed acyclic graph);is_undirected_forest,is_undirected_tree,is_bipartiteandis_weakly_connectedread it as undirected. - Text, hash and diff:
adjacency_list_ofwrites a whole graph as text,topology_hashhashes it into 64 bits to keep, anddiffreturns the edges added and removed between two versions of a graph.
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
beforeandafter. Both are graphs over the same nodes. - dominator_
tree - Returns the dominator tree of
dominatorsas a graph, with an edged -> nfor every nodenwhose immediate dominator isd. - edge_
mapping - Returns the edge mapping from
new_graphtoold_graphthat goes with the node mappingnew_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 ofcondensed_graph. - find_
cycle - Returns the nodes of a cycle of
graphin the order of its edges, or an empty vector ifgraphis a DAG. - is_
bipartite - Returns whether
graphis bipartite: its nodes split into two sets such that every edge runs from one set to the other. - is_dag
- Returns whether
graphis a DAG: a graph without cycles. A self-loop is a cycle. - is_
forest - Returns whether
graphis 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
graphis a path: a tree in which every node has out-degree at most 1. - is_
strongly_ connected - Returns whether
graphis strongly connected: every node reaches every other along the edges. - is_tree
- Returns whether
graphis a tree: a forest with at most one root node. - is_
undirected_ forest - Returns whether
graphis a forest when read as undirected: without cycles. - is_
undirected_ tree - Returns whether
graphis a tree when read as undirected: connected and without cycles. - is_
weakly_ connected - Returns whether
graphis weakly connected: connected when the direction of the edges is ignored. - reachable_
from - Returns the nodes reachable from
source, in ascending order and withsourceincluded. Equivalent toreachable_from_sources(graph, &[source])- Java’s overloadreachableFrom(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 overloadreachableFrom(Graph, IntList). - remapped
- Returns
graphwith its nodes renumbered bynew_to_old: nodeiof the result is nodenew_to_old.get(i)ofgraph. - root_
nodes - Returns the root nodes of
graphin 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 edgeu -> vwherever a path of one or more edges leads fromutov. - transitive_
reduction - Returns the transitive reduction of
dag: the same nodes, keeping each edgeu -> vunless another path leads fromutov. - transposed_
edge_ mapping - Returns the edge mapping from the transposed graph of
graphtograph.