Contents
Each chapter starts with a page that explains its ideas; the problems follow. To find a problem by the function that solves it, see the index by API; to find a word, use the search at the top of the page.
1 Building and reading graphs
This chapter shows how a graph comes into being, where its data lives, and how to read its nodes and edges.
- 1.1 Build a graph from a list of edgesYou build a graph from pairs of node IDs, given in any order and with duplicates.
- 1.2 Build a graph from objects of your ownYou give your objects node IDs, keep an array to find each object again, and handle input that is numbered elsewhere.
- 1.3 Build trees and paths in a few callsYou add children and parents with their edges, paths through several nodes, and the edges between one node and several others.
- 1.4 Combine several graphs into oneYou put graphs side by side in one graph, with edges between them, and extend a graph by new edges.
- 1.5 Keep data on nodes and edgesYou keep names, weights and anything else about nodes and edges in arrays indexed by node ID and edge index.
- 1.6 Find the out-nodes and in-nodes of a nodeYou read the nodes that a node has edges to, the nodes that have edges to it, and both together.
- 1.7 Work with the lists Graphity returnsYou work with sorted lists of nodes, and with lists of edges with their sources, targets and edge indices.
- 1.8 Use arrays you already have as a graphYou make a graph from the two arrays of a compressed sparse row (CSR) layout, and get the arrays of a graph for code of your own.
2 Order and cycles
This chapter finds orders in which every node comes before its out-nodes, and the cycles that prevent them.
- 2.1 Find out whether a graph has a cycle, and show oneYou find out whether a graph is a DAG (directed acyclic graph), and get one of its cycles as a list of nodes.
- 2.2 Put the nodes in dependency orderYou get a topological order, in which every node comes before its out-nodes.
- 2.3 Find which nodes can be processed in parallelYou turn a DAG (directed acyclic graph) into the stages of a schedule: you get the level of each node and a topological order level by level.
- 2.4 Find the starting points of a graphYou get the root nodes, which no edge leads to.
- 2.5 Classify the edges of a depth-first searchYou sort the edges of a graph into tree, back, forward and cross edges, which tells you which edges close a cycle and where the others lead.
- 2.6 Remove edges to break every cycleYou get the back edges of a depth-first search, whose removal leaves a DAG (directed acyclic graph).
3 Strongly connected components
This chapter shows where the cycles of a directed graph are, and which nodes reach each other through them.
- 3.1 Find the groups of nodes that reach each otherYou divide a graph into its strongly connected components, see which of them contain a cycle, and check whether two given nodes are in the same one.
- 3.2 Condense each group into one node to get a DAGYou condense each strongly connected component into one node and get a graph without cycles that keeps the edges between the components.
- 3.3 Put the nodes in dependency order although the graph has cyclesYou get an order of the strongly connected components in which each comes after the ones it depends on.
- 3.4 Check whether every node reaches every otherYou check whether a graph is strongly connected, and use that to check a control-flow graph for blocks that never run or never reach the exit.
- 3.5 Find the edges and nodes whose removal splits a strongly connected componentYou find the strong bridges and strong articulation points of a graph, without which a strongly connected component splits into several.
4 Reachability and distances
This chapter answers which nodes a node reaches, which nodes reach it, and how many edges lie in between.
- 4.1 Find everything a node reachesYou get the nodes that a path of edges leads to from one node or from several.
- 4.2 Find everything that reaches a nodeYou get the nodes from which a path of edges leads to a node - the callers of callers, the users of users.
- 4.3 Find a shortest path between two nodesYou get the distance from a node to every other node, and a path with the fewest edges.
- 4.4 Find the distances from several start nodes, and the largest distanceYou get the distance of each node from the nearest of several start nodes, and the largest distance a search reaches.
- 4.5 Answer many reachability questions at onceYou build the transitive closure, which is a graph with an edge wherever a path leads.
- 4.6 Remove edges that other paths make redundantYou get the transitive reduction of a DAG (directed acyclic graph), in which every node reaches the same nodes as before, with as few edges as possible.
5 Connectivity when direction does not matter
This chapter reads graphs as undirected: their weakly connected components, the edges and nodes whose removal splits a component, their biconnected components, and whether they are bipartite, trees or forests.
- 5.1 Find the components of a graph read as undirectedYou get the weakly connected components of a graph: the groups of nodes that paths connect when the direction of the edges does not matter.
- 5.2 Find the edges and nodes whose removal splits a graphYou find the bridges and articulation points: the edges and nodes whose removal splits a component of a graph read as undirected.
- 5.3 Divide the edges of a graph into biconnected componentsYou get the biconnected components of a graph read as undirected, also called blocks: groups of edges in which any two edges lie on a common cycle.
- 5.4 Find out whether a graph is bipartiteYou find out whether the nodes of a graph read as undirected split into two sets so that every edge runs from one set to the other.
- 5.5 Find out whether an undirected graph is a tree or a forestYou find out whether a graph read as undirected has no cycles apart from self-loops, and whether it is connected as well.
6 Trees and hierarchies
This chapter covers trees and forests: their shape, the parent and depth of each node, ancestors, lowest common ancestors, and the descendants of a node.
- 6.1 Find out whether a graph is a tree, a forest or a pathYou find out whether a graph has no cycles and at most one parent per node, whether it also has at most one root, and whether every node has at most one child.
- 6.2 Find the parent and the depth of each nodeYou build an ancestor index that gives the parent and the depth of every node of a forest.
- 6.3 Find the lowest common ancestor of two nodesYou find the lowest common ancestor of two nodes: the deepest node that is an ancestor of both.
- 6.4 Test many times whether one node is an ancestor of anotherYou test whether a node is an ancestor of another, in logarithmic time with the ancestor index and in constant time with the index with the preorder.
- 6.5 List a node and all its descendantsYou get the subtree of a node, its size, and the preorder of the whole forest.
7 Dominance
This chapter finds the nodes that every path from an entry passes through, the tree they form, the same towards an exit, and dominators from several entries.
- 7.1 Find the nodes that every path from the entry passes throughYou get the immediate dominator of each node: the nearest other node that every path from the entry to it passes through.
- 7.2 Build the dominator tree and ask it questionsYou build the tree of immediate dominators as a graph, and answer questions about dominance with an ancestor index.
- 7.3 Find the nodes that every path to the exit passes throughYou find what is certain to happen after a node: its post-dominators, and the post-dominator tree they form.
- 7.4 Find dominators from several entriesYou get the dominators of a graph with several entries, and the dominator forest they form.
8 Control-flow analysis
This chapter shows the graph of the paths through a function, and what compilers and analyzers read from it: loops, dominance frontiers, phi-nodes and control dependence.
- 8.1 Find the loops of a function and how they nestYou find which blocks of a control-flow graph form a loop, which block is the header of each loop, and which loop lies in which.
- 8.2 Find loops that can be entered in more than one placeYou find the loops of a control-flow graph that can be entered at more than one block, which are called irreducible, and see how Graphity reports them.
- 8.3 Find the blocks where the dominance of a block endsYou get the dominance frontier of each block of a control-flow graph, which holds the blocks where the dominance of that block ends.
- 8.4 Find the blocks that need a phi-node for a variableYou find the blocks where SSA form (static single assignment) needs a phi-node for a variable: the iterated dominance frontier of the blocks that assign it.
- 8.5 Find which conditions decide whether a block runsYou get the control dependence of a graph: for each block, the branches that decide whether it runs.
9 Transforming graphs and carrying data along
This chapter derives subgraphs, renumbered and transposed graphs, and groups of your own from a graph, and shows how the data on nodes and edges follows them.
- 9.1 Take the subgraph of some nodesYou take a graph of selected nodes and the edges between them, with a mapping back to the original nodes.
- 9.2 Number the nodes in a different orderYou renumber a graph in depth-first order or in an order of your own, such as a topological one.
- 9.3 Carry node data over to a transformed graphYou gather data on nodes into the numbering of a transformed graph, translate node IDs both ways, and compose mappings.
- 9.4 Carry edge data over to a transformed graphYou get the edge mapping between a transformed graph and the original, and gather edge data through it.
- 9.5 Turn every edge aroundYou turn every edge around without copying the graph, and get the edge indices and edge data of the result.
- 9.6 Condense groups of your own choiceYou divide the nodes into groups you choose, and get the condensed graph between the groups.
- 9.7 Reduce the data of the nodes or edges of a groupYou reduce the values of the nodes or edges of a group to one value, such as a sum, a maximum or a list, and get the edges behind an edge of a condensed graph.
10 Storing and comparing graphs
This chapter saves graphs to files and loads them again, recognizes a graph that has not changed, and compares two versions edge by edge.
- 10.1 Save a graph to a file and load it againYou write a graph in a compact binary form to a file or any other output, and read it back.
- 10.2 Find out cheaply whether a graph has changedYou get a 64-bit hash of the nodes and edges of a graph, and every run gives the same hash.
- 10.3 Find the edges added and removed between two versionsYou list the edges added and removed between two graphs over the same nodes.
11 Showing graphs
This chapter turns graphs into text for logs and tests, and into pictures drawn by Graphviz and by Mermaid, with labels, styles and frames from your own data.
- 11.1 Print a graph as textYou print the whole graph as an adjacency list, or a short summary for logs.
- 11.2 Draw a graph with GraphvizYou write a graph in the DOT language of Graphviz, with labels from your own data.
- 11.3 Highlight nodes, edges and groups in GraphvizYou set colors, shapes and line styles from your data, draw groups of nodes as frames, leave nodes out, and show undirected graphs.
- 11.4 Draw a graph as a Mermaid flowchartYou write a graph as a Mermaid flowchart, with labels, a direction and node shapes.
- 11.5 Highlight nodes, edges and groups in MermaidYou set classes and styles from your data, draw groups of nodes as frames, leave nodes out, and show undirected graphs in a Mermaid flowchart.
12 Writing searches of your own
This chapter shows how to write your own depth-first and breadth-first searches on Graphity's traversers without recursion, and how to loop over the arrays of a graph.
- 12.1 Write a depth-first search without recursionYou write a depth-first search of your own, with hooks that are called when a node is entered and when it is left.
- 12.2 React to every edge of a depth-first searchYou run a depth-first search that reports every edge with the state of its target and lets you follow the edge, skip it or stop.
- 12.3 Write a breadth-first searchYou write a search of your own that works off a queue, level by level - breadth-first, or following only the nodes you choose.
- 12.4 Loop over the arrays of a graph directlyYou read the out-nodes and in-nodes of a graph from the arrays of its CSR layout (compressed sparse row), for the fastest loops over all edges.