# Reachability

Jump to navigation Jump to search

In graph theory, reachability refers to the ability to get from one vertex to another within a graph. We say that a vertex ${\displaystyle s}$ can reach a vertex ${\displaystyle t}$ (or that ${\displaystyle t}$ is reachable from ${\displaystyle s}$) if there exists a sequence of adjacent vertices (i.e. a path) which starts with ${\displaystyle s}$ and ends with ${\displaystyle t}$.

In an undirected graph, it is sufficient to identify the connected components, as any pair of vertices in such a graph can reach each other if and only if they belong to the same connected component. The connected components of a graph can be identified in linear time. The remainder of this article focuses on reachability in a directed graph setting.

## Definition

For a directed graph ${\displaystyle G=(V,E)}$, with vertex set ${\displaystyle V}$ and edge set ${\displaystyle E}$, the reachability relation of ${\displaystyle G}$ is the transitive closure of ${\displaystyle E}$, which is to say the set of all ordered pairs ${\displaystyle (s,t)}$ of vertices in ${\displaystyle V}$ for which there exists a sequence of vertices ${\displaystyle v_{0}=s,v_{1},v_{2},...,v_{k}=t}$ such that the edge ${\displaystyle (v_{i-1},v_{i})}$ is in ${\displaystyle E}$ for all ${\displaystyle 1\leq i\leq k}$.[1]

If ${\displaystyle G}$ is acyclic, then its reachability relation is a partial order; any partial order may be defined in this way, for instance as the reachability relation of its transitive reduction.[2] A noteworthy consequence of this is that since partial orders are anti-symmetric, if ${\displaystyle s}$ can reach ${\displaystyle t}$, then we know that ${\displaystyle t}$ cannot reach ${\displaystyle s}$. Intuitively, if we could travel from ${\displaystyle s}$ to ${\displaystyle t}$ and back to ${\displaystyle s}$, then ${\displaystyle G}$ would contain a cycle, contradicting that it is acyclic. If ${\displaystyle G}$ is directed but not acyclic (i.e. it contains at least one cycle), then its reachability relation will correspond to a preorder instead of a partial order. [3]

## Algorithms

Algorithms for determining reachability fall into two classes: those that require preprocessing and those that do not.

If you have only one (or a few) queries to make, it may be more efficient to forgo the use of more complex data structures and compute the reachability of the desired pair directly. This can be accomplished in linear time using algorithms such as breadth first search or iterative deepening depth-first search.[4]

If you will be making many queries, then a more sophisticated method may be used; the exact choice of method depends on the nature of the graph being analysed. In exchange for preprocessing time and some extra storage space, we can create a data structure which can then answer reachability queries on any pair of vertices in as low as ${\displaystyle O(1)}$ time. Three different algorithms and data structures for three different, increasingly specialized situations are outlined below.

### Floyd-Warshall Algorithm

The Floyd–Warshall algorithm[5] can be used to compute the transitive closure of any directed graph, which gives rise to the reachability relation as in the definition, above.

The algorithm requires ${\displaystyle O(|V|^{3})}$ time and ${\displaystyle O(|V|^{2})}$ space in the worst case. This algorithm is not solely interested in reachability as it also computes the shortest path distance between all pairs of vertices. For graphs containing negative cycles, shortest paths may be undefined, but reachability between pairs can still be noted.

### Thorup's Algorithm

For planar digraphs, a much faster method is available, as described by Mikkel Thorup in 2004.[6] This method can answer reachability queries on a planar graph in ${\displaystyle O(1)}$ time after spending ${\displaystyle O(n\log {n})}$ preprocessing time to create a data structure of ${\displaystyle O(n\log {n})}$ size. This algorithm can also supply approximate shortest path distances, as well as route information.

The overall approach is to associate with each vertex a relatively small set of so-called separator paths such that any path from a vertex ${\displaystyle v}$ to any other vertex ${\displaystyle w}$ must go through at least one of the separators associated with ${\displaystyle v}$ or ${\displaystyle w}$. An outline of the reachability related sections follows.

Given a graph ${\displaystyle G}$, the algorithm begins by organizing the vertices into layers starting from an arbitrary vertex ${\displaystyle v_{0}}$. The layers are built in alternating steps by first considering all vertices reachable from the previous step (starting with just ${\displaystyle v_{0}}$) and then all vertices which reach to the previous step until all vertices have been assigned to a layer. By construction of the layers, every vertex appears at most two layers, and every directed path, or dipath, in ${\displaystyle G}$ is contained within two adjacent layers ${\displaystyle L_{i}}$ and ${\displaystyle L_{i+1}}$. Let ${\displaystyle k}$ be the last layer created, that is, the lowest value for ${\displaystyle k}$ such that ${\displaystyle \bigcup _{i=0}^{k}=V}$.

The graph is then re-expressed as a series of digraphs ${\displaystyle G_{0},G_{1},\ldots ,G_{k-1}}$ where each ${\displaystyle G_{i}=r_{i}\cup L_{i}\cup L_{i+1}}$ and where ${\displaystyle r_{i}}$ is the contraction of all previous levels ${\displaystyle L_{0}\ldots L_{i-1}}$ into a single vertex. Because every dipath appears in at most two consecutive layers, and because each ${\displaystyle G_{i}}$ is formed by two consecutive layers, every dipath in ${\displaystyle G}$ appears in its entirety in at least one ${\displaystyle G_{i}}$ (and no more than 2 consecutive such graphs)

For each ${\displaystyle G_{i}}$, three separators are identified which, when removed, break the graph into three components which each contain at most ${\displaystyle 1/2}$ the vertices of the original. As ${\displaystyle G_{i}}$ is built from two layers of opposed dipaths, each separator may consist of up to 2 dipaths, for a total of up to 6 dipaths over all of the separators. Let ${\displaystyle S}$ be this set of dipaths. The proof that such separators can always be found is related to the Planar Separator Theorem of Lipton and Tarjan, and these separators can be located in linear time.

For each ${\displaystyle Q\in S}$, the directed nature of ${\displaystyle Q}$ provides for a natural indexing of its vertices from the start to the end of the path. For each vertex ${\displaystyle v}$ in ${\displaystyle G_{i}}$, we locate the first vertex in ${\displaystyle Q}$ reachable by ${\displaystyle v}$, and the last vertex in ${\displaystyle Q}$ that reaches to ${\displaystyle v}$. That is, we are looking at how early into ${\displaystyle Q}$ we can get from ${\displaystyle v}$, and how far we can stay in ${\displaystyle Q}$ and still get back to ${\displaystyle v}$. This information is stored with each ${\displaystyle v}$. Then for any pair of vertices ${\displaystyle u}$ and ${\displaystyle w}$, ${\displaystyle u}$ can reach ${\displaystyle w}$ via ${\displaystyle Q}$ if ${\displaystyle u}$ connects to ${\displaystyle Q}$ earlier than ${\displaystyle w}$ connects from ${\displaystyle Q}$.

Every vertex is labelled as above for each step of the recursion which builds ${\displaystyle G_{0}\ldots ,G_{k}}$. As this recursion has logarithmic depth, a total of ${\displaystyle O(\log {n})}$ extra information is stored per vertex. From this point, a logarithmic time query for reachability is as simple as looking over each pair of labels for a common, suitable ${\displaystyle Q}$. The original paper then works to tune the query time down to ${\displaystyle O(1)}$.

In summarizing the analysis of this method, first consider that the layering approach partitions the vertices so that each vertex is considered only ${\displaystyle O(1)}$ times. The separator phase of the algorithm breaks the graph into components which are at most ${\displaystyle 1/2}$ the size of the original graph, resulting in a logarithmic recursion depth. At each level of the recursion, only linear work is needed to identify the separators as well as the connections possible between vertices. The overall result is ${\displaystyle O(n\log n)}$ preprocessing time with only ${\displaystyle O(\log {n})}$ additional information stored for each vertex.

### Kameda's Algorithm

The same graph as above after Kameda's algorithm has run, showing the DFS labels for each vertex

An even faster method for pre-processing, due to T. Kameda in 1975,[7] can be used if the graph is planar, acyclic, and also exhibits the following additional properties: all 0-indegree and all 0-outdegree vertices appear on the same face (we will assume this is the outer face), and it is possible to partition the boundary of that face into two parts such that all 0-indegree vertices appear on one part, and all 0-outdegree vertices appear on the other (i.e. the two types of vertices do not alternate).

If ${\displaystyle G}$ exhibits these properties, then we can preprocess the graph in only ${\displaystyle O(n)}$ time, and store only ${\displaystyle O(\log {n})}$ extra bits per vertex, answering reachability queries for any pair of vertices in ${\displaystyle O(1)}$ time with a simple comparison.

Preprocessing performs the following steps. We add a new vertex ${\displaystyle s}$ which has an edge to each 0-indegree vertex, and another new vertex ${\displaystyle t}$ with edges to each 0-outdegree vertex. Note that the properties of ${\displaystyle G}$ allow us to do so while maintaining planarity, that is, there will still be no edge crossings after these additions. For each vertex we store the list of adjacencies (out-edges) in order of the planarity of the graph (for example, clockwise with respect to the graph's embedding). We then initialize a counter ${\displaystyle i=n+1}$ and begin a Depth-First Traversal from ${\displaystyle s}$. During this traversal, the adjacency list of each vertex is visited from left-to-right as needed. As vertices are popped from the traversal's stack, they are labelled with the value ${\displaystyle i}$, and ${\displaystyle i}$ is then decremented. Note that ${\displaystyle t}$ is always labelled with the value ${\displaystyle n+1}$ and ${\displaystyle s}$ is always labelled with ${\displaystyle 0}$. The depth-first traversal is then repeated, but this time the adjacency list of each vertex is visited from right-to-left.

When completed, ${\displaystyle s}$ and ${\displaystyle t}$, and their incident edges, are removed. Each remaining vertex stores a 2-dimensional label with values from ${\displaystyle 1}$ to ${\displaystyle n}$. Given two vertices ${\displaystyle u}$ and ${\displaystyle v}$, and their labels ${\displaystyle L(u)=(a_{1},a_{2})}$ and ${\displaystyle L(v)=(b_{1},b_{2})}$, we say that ${\displaystyle L(u) if and only if ${\displaystyle a_{1}\leq b_{1}}$, ${\displaystyle a_{2}\leq b_{2}}$, and there exists at least one component ${\displaystyle a_{1}}$ or ${\displaystyle a_{2}}$ which is strictly less than ${\displaystyle b_{1}}$ or ${\displaystyle b_{2}}$, respectively.

The main result of this method then states that ${\displaystyle v}$ is reachable from ${\displaystyle u}$ if and only if ${\displaystyle L(u), which is easily calculated in ${\displaystyle O(1)}$ time.

## Related Problems

A related problem is to solve reachability queries with some number ${\displaystyle k}$ of vertex failures. For example: "Can vertex ${\displaystyle u}$ still reach vertex ${\displaystyle v}$ even though vertices ${\displaystyle s_{1},s_{2},...,s_{k}}$ have failed and can no longer be used?" A similar problem may consider edge failures rather than vertex failures, or a mix of the two. The breadth-first search technique works just as well on such queries, but constructing an efficient oracle is more challenging.[8]

Another problem related to reachability queries is in quickly recalculating changes to reachability relationships when some portion of the graph is changed. For example, this is a relevant concern to garbage collection which needs to balance the reclamation of memory (so that it may be reallocated) with the performance concerns of the running application.

## References

1. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
2. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
3. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
4. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
5. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
6. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
7. {{#invoke:citation/CS1|citation |CitationClass=citation }}.
8. {{#invoke:citation/CS1|citation |CitationClass=citation }}.