Parallel algorithms for minimum spanning trees

From HandWiki

In graph theory a minimum spanning tree (MST) [math]\displaystyle{ T }[/math] of a graph [math]\displaystyle{ G = (V, E) }[/math] with [math]\displaystyle{ |V| = n }[/math] and [math]\displaystyle{ |E| = m }[/math] is a tree subgraph of [math]\displaystyle{ G }[/math] that contains all of its vertices and is of minimum weight.

MSTs are useful and versatile tools utilised in a wide variety of practical and theoretical fields. For example, a company looking to supply multiple stores with a certain product from a single warehouse might use an MST originating at the warehouse to calculate the shortest paths to each company store. In this case the stores and the warehouse are represented as vertices and the road connections between them - as edges. Each edge is labelled with the length of the corresponding road connection.

If [math]\displaystyle{ G }[/math] is edge-unweighted every spanning tree possesses the same number of edges and thus the same weight. In the edge-weighted case, the spanning tree, the sum of the weights of the edges of which is lowest among all spanning trees of [math]\displaystyle{ G }[/math], is called a minimum spanning tree (MST). It is not necessarily unique. More generally, graphs that are not necessarily connected have minimum spanning forests, which consist of a union of MSTs for each connected component.

As finding MSTs is a widespread problem in graph theory, there exist many sequential algorithms for solving it. Among them are Prim's, Kruskal's and Borůvka's algorithms, each utilising different properties of MSTs. They all operate in a similar fashion - a subset of [math]\displaystyle{ E }[/math] is iteratively grown until a valid MST has been discovered. However, as practical problems are often quite large (road networks sometimes have billions of edges), performance is a key factor. One option of improving it is by parallelising known MST algorithms.[1]

Prim's algorithm

This algorithm utilises the cut-property of MSTs. A simple high-level pseudocode implementation is provided below:

[math]\displaystyle{ T \gets \emptyset }[/math]
[math]\displaystyle{ S \gets \{s\} }[/math] where [math]\displaystyle{ s }[/math] is a random vertex in [math]\displaystyle{ V }[/math]
repeat [math]\displaystyle{ |V| - 1 }[/math] times
    find lightest edge [math]\displaystyle{ (u,v) }[/math] s.t. [math]\displaystyle{ u \in S }[/math] but [math]\displaystyle{ v \in (V \setminus S) }[/math]
    [math]\displaystyle{ S \gets S \cup \{v\} }[/math]
    [math]\displaystyle{ T \gets T \cup \{(u,v)\} }[/math]
return T

Each edge is observed exactly twice - namely when examining each of its endpoints. Each vertex is examined exactly once for a total of [math]\displaystyle{ O(n + m) }[/math] operations aside from the selection of the lightest edge at each loop iteration. This selection is often performed using a priority queue (PQ). For each edge at most one decreaseKey operation (amortised in [math]\displaystyle{ O(1) }[/math]) is performed and each loop iteration performs one deleteMin operation ([math]\displaystyle{ O(\log n) }[/math]). Thus using Fibonacci heaps the total runtime of Prim's algorithm is asymptotically in [math]\displaystyle{ O(m + n \log n) }[/math].

It is important to note that the loop is inherently sequential and can not be properly parallelised. This is the case, since the lightest edge with one endpoint in [math]\displaystyle{ S }[/math] and on in [math]\displaystyle{ V \setminus S }[/math] might change with the addition of edges to [math]\displaystyle{ T }[/math]. Thus no two selections of a lightest edge can be performed at the same time. However, there do exist some attempts at parallelisation.

One possible idea is to use [math]\displaystyle{ O(n) }[/math] processors to support PQ access in [math]\displaystyle{ O(1) }[/math] on an EREW-PRAM machine,[2] thus lowering the total runtime to [math]\displaystyle{ O(n + m) }[/math].

Kruskal's algorithm

Kruskal's MST algorithm utilises the cycle property of MSTs. A high-level pseudocode representation is provided below.

[math]\displaystyle{ T \gets }[/math] forest with every vertex in its own subtree
foreach [math]\displaystyle{ (u, v) \in E }[/math] in ascending order of weight
    if [math]\displaystyle{ u }[/math] and [math]\displaystyle{ v }[/math] in different subtrees of [math]\displaystyle{ T }[/math]
        [math]\displaystyle{ T \gets T \cup \{(u,v)\} }[/math]
return T

The subtrees of [math]\displaystyle{ T }[/math] are stored in union-find data structures, which is why checking whether or not two vertices are in the same subtree is possible in amortised [math]\displaystyle{ O(\alpha(m, n)) }[/math] where [math]\displaystyle{ \alpha(m, n) }[/math] is the inverse Ackermann function. Thus the total runtime of the algorithm is in [math]\displaystyle{ O(sort(n) + \alpha(n)) }[/math]. Here [math]\displaystyle{ \alpha(n) }[/math] denotes the single-valued inverse Ackermann function, for which any realistic input yields an integer less than five.

Approach 1: Parallelising the sorting step

Similarly to Prim's algorithm there are components in Kruskal's approach that can not be parallelised in its classical variant. For example, determining whether or not two vertices are in the same subtree is difficult to parallelise, as two union operations might attempt to join the same subtrees at the same time. Really the only opportunity for parallelisation lies in the sorting step. As sorting is linear in the optimal case on [math]\displaystyle{ O(\log n) }[/math] processors, the total runtime can be reduced to [math]\displaystyle{ O(m \alpha(n)) }[/math].

Approach 2: Filter-Kruskal

Another approach would be to modify the original algorithm by growing [math]\displaystyle{ T }[/math] more aggressively. This idea was presented by Osipov et al.[3][4] The basic idea behind Filter-Kruskal is to partition the edges in a similar way to quicksort and filter out edges that connect vertices that belong to the same tree in order to reduce the cost of sorting. A high-level pseudocode representation is provided below.

filterKruskal([math]\displaystyle{ G }[/math]):
if [math]\displaystyle{ m \lt  }[/math] KruskalThreshold:
    return kruskal([math]\displaystyle{ G }[/math])
pivot = chooseRandom([math]\displaystyle{ E }[/math])
[math]\displaystyle{ (E_{\leq} }[/math], [math]\displaystyle{ E_{\gt }) \gets  }[/math]partition([math]\displaystyle{ E }[/math], pivot)
[math]\displaystyle{ A \gets }[/math] filterKruskal([math]\displaystyle{ E_{\leq} }[/math])
[math]\displaystyle{ E_{\gt } \gets }[/math] filter([math]\displaystyle{ E_{\gt } }[/math])
[math]\displaystyle{ A \gets A }[/math] [math]\displaystyle{ \cup }[/math] filterKruskal([math]\displaystyle{ E_{\gt } }[/math])
return [math]\displaystyle{ A }[/math]

partition([math]\displaystyle{ E }[/math], pivot):
[math]\displaystyle{ E_{\leq} \gets \emptyset  }[/math] 
[math]\displaystyle{ E_{\gt } \gets \emptyset }[/math]
foreach [math]\displaystyle{ (u, v) \in E }[/math]:
    if weight([math]\displaystyle{ u, v }[/math]) [math]\displaystyle{ \leq }[/math] pivot:
        [math]\displaystyle{ E_{\leq} \gets E_{\leq} \cup {(u, v)} }[/math] 
    else
        [math]\displaystyle{ E_{\gt } \gets E_{\gt } \cup {(u, v)}  }[/math]
return ([math]\displaystyle{ E_{\leq} }[/math], [math]\displaystyle{ E_{\gt } }[/math])

filter([math]\displaystyle{ E }[/math]):
[math]\displaystyle{ E_{filtered} \gets \emptyset  }[/math]
foreach [math]\displaystyle{ (u, v) \in E }[/math]:
    if find-set(u) [math]\displaystyle{ \neq }[/math] find-set(v):
        [math]\displaystyle{ E_{filtered} \gets E_{filtered} \cup {(u, v)} }[/math]
return [math]\displaystyle{ E_{filtered} }[/math]

Filter-Kruskal is better suited for parallelisation, since sorting, partitioning and filtering have intuitively easy parallelisations where the edges are simply divided between the cores.

Borůvka's algorithm

The main idea behind Borůvka's algorithm is edge contraction. An edge [math]\displaystyle{ \{u, v\} }[/math] is contracted by first removing [math]\displaystyle{ v }[/math] from the graph and then redirecting every edge [math]\displaystyle{ \{w, v\} \in E }[/math] to [math]\displaystyle{ \{w, u\} }[/math]. These new edges retain their old edge weights. If the goal is not just to determine the weight of an MST but also which edges it comprises, it must be noted between which pairs of vertices an edge was contracted. A high-level pseudocode representation is presented below.

[math]\displaystyle{ T \gets \emptyset }[/math]
while [math]\displaystyle{ |V| \gt  1 }[/math]
    [math]\displaystyle{ S \gets \emptyset }[/math] 
    for [math]\displaystyle{ v \in V }[/math]
        [math]\displaystyle{ S \gets S }[/math] [math]\displaystyle{ \cup }[/math] lightest [math]\displaystyle{ \{u, v\} \in E }[/math]
    for [math]\displaystyle{ \{u, v\} \in S }[/math]
        contract [math]\displaystyle{ \{u, v\} }[/math]
    [math]\displaystyle{ T \gets T \cup S }[/math]
return T

It is possible that contractions lead to multiple edges between a pair of vertices. The intuitive way of choosing the lightest of them is not possible in [math]\displaystyle{ O(m) }[/math]. However, if all contractions that share a vertex are performed in parallel this is doable. The recursion stops when there is only a single vertex remaining, which means the algorithm needs at most [math]\displaystyle{ \log n }[/math] iterations, leading to a total runtime in [math]\displaystyle{ O(m \log n) }[/math].

Parallelisation

One possible parallelisation of this algorithm[5][6][7] yields a polylogarithmic time complexity, i.e. [math]\displaystyle{ T(m, n, p) \cdot p \in O(m \log n) }[/math] and there exists a constant [math]\displaystyle{ c }[/math] so that [math]\displaystyle{ T(m, n, p) \in O(\log^c m) }[/math]. Here [math]\displaystyle{ T(m, n, p) }[/math] denotes the runtime for a graph with [math]\displaystyle{ m }[/math] edges, [math]\displaystyle{ n }[/math] vertices on a machine with [math]\displaystyle{ p }[/math] processors. The basic idea is the following:

while [math]\displaystyle{ |V| \gt  1 }[/math]
    find lightest incident edges // [math]\displaystyle{ O(\frac{m}{p} + \log n + \log p) }[/math]
    assign the corresponding subgraph to each vertex // [math]\displaystyle{ O(\frac{n}{p} + \log n) }[/math]
    contract each subgraph // [math]\displaystyle{ O(\frac{m}{p} + \log n) }[/math]

The MST then consists of all the found lightest edges.

This parallelisation utilises the adjacency array graph representation for [math]\displaystyle{ G = (V, E) }[/math]. This consists of three arrays - [math]\displaystyle{ \Gamma }[/math] of length [math]\displaystyle{ n + 1 }[/math] for the vertices, [math]\displaystyle{ \gamma }[/math] of length [math]\displaystyle{ m }[/math] for the endpoints of each of the [math]\displaystyle{ m }[/math] edges and [math]\displaystyle{ c }[/math] of length [math]\displaystyle{ m }[/math] for the edges' weights. Now for vertex [math]\displaystyle{ i }[/math] the other end of each edge incident to [math]\displaystyle{ i }[/math] can be found in the entries between [math]\displaystyle{ \gamma [\Gamma [i-1]] }[/math] and [math]\displaystyle{ \gamma [\Gamma[i]] }[/math]. The weight of the [math]\displaystyle{ i }[/math]-th edge in [math]\displaystyle{ \Gamma }[/math] can be found in [math]\displaystyle{ c[i] }[/math]. Then the [math]\displaystyle{ i }[/math]-th edge in [math]\displaystyle{ \gamma }[/math] is between vertices [math]\displaystyle{ u }[/math] and [math]\displaystyle{ v }[/math] if and only if [math]\displaystyle{ \Gamma[u] \leq i \lt \Gamma[u + 1] }[/math] and [math]\displaystyle{ \gamma[i] = v }[/math].

Finding the lightest incident edge

First the edges are distributed between each of the [math]\displaystyle{ p }[/math] processors. The [math]\displaystyle{ i }[/math]-th processor receives the edges stored between [math]\displaystyle{ \gamma[\frac{i m}{p}] }[/math] and [math]\displaystyle{ \gamma[\frac{(i + 1)m}{p} - 1] }[/math]. Furthermore, each processor needs to know to which vertex these edges belong (since [math]\displaystyle{ \gamma }[/math] only stores one of the edge's endpoints) and stores this in the array [math]\displaystyle{ pred }[/math]. Obtaining this information is possible in [math]\displaystyle{ O(\log n) }[/math] using [math]\displaystyle{ p }[/math] binary searches or in [math]\displaystyle{ O(\frac{n}{p} + p) }[/math] using a linear search. In practice the latter approach is sometimes quicker, even though it is asymptotically worse.

Now each processor determines the lightest edge incident to each of its vertices.

[math]\displaystyle{ v \gets }[/math] find([math]\displaystyle{ \frac{i m}{p} }[/math], [math]\displaystyle{ \Gamma }[/math])
for [math]\displaystyle{ e \gets \frac{i m}{p}; e \lt  \frac{(i+1) m}{p} - 1; e++ }[/math]
    if [math]\displaystyle{ \Gamma[v+1] = e  }[/math]
        [math]\displaystyle{ v++ }[/math]
    if[math]\displaystyle{ c[e] \lt  c[pred[v]] }[/math]
        [math]\displaystyle{ pred[v] \gets e }[/math]

Here the issue arises some vertices are handled by more than one processor. A possible solution to this is that every processor has its own [math]\displaystyle{ prev }[/math] array which is later combined with those of the others using a reduction. Each processor has at most two vertices that are also handled by other processors and each reduction is in [math]\displaystyle{ O(\log p) }[/math]. Thus the total runtime of this step is in [math]\displaystyle{ O(\frac{m}{p} + \log n + \log p) }[/math].

Assigning subgraphs to vertices

Observe the graph that consists solely of edges collected in the previous step. These edges are directed away from the vertex to which they are the lightest incident edge. The resulting graph decomposes into multiple weakly connected components. The goal of this step is to assign to each vertex the component of which it is a part. Note that every vertex has exactly one outgoing edge and therefore each component is a pseudotree - a tree with a single extra edge that runs in parallel to the lightest edge in the component but in the opposite direction. The following code mutates this extra edge into a loop:

parallel forAll [math]\displaystyle{ v \in V }[/math] 
    [math]\displaystyle{ w \gets pred[v] }[/math]
    if [math]\displaystyle{ pred[w] = v \land v \lt  w }[/math] 
        [math]\displaystyle{ pred[v] \gets v }[/math]

Now every weakly connected component is a directed tree where the root has a loop. This root is chosen as the representative of each component. The following code uses doubling to assign each vertex its representative:

while [math]\displaystyle{ \exists v \in V: pred[v] \neq pred[pred[v]] }[/math]
    forAll [math]\displaystyle{ v \in V }[/math] 
        [math]\displaystyle{ pred[v] \gets pred[pred[v]] }[/math]

Now every subgraph is a star. With some advanced techniques this step needs [math]\displaystyle{ O(\frac{n}{p} + \log n) }[/math] time.

Contracting the subgraphs

In this step each subgraph is contracted to a single vertex.

[math]\displaystyle{ k \gets }[/math] number of subgraphs
[math]\displaystyle{ V' \gets \{0, \dots , k-1\} }[/math]
find a bijective function [math]\displaystyle{ f: }[/math] star root [math]\displaystyle{ \rightarrow \{0, \dots, k-1\} }[/math] 
[math]\displaystyle{ E' \gets \{(f(pred[v]), f(pred[w]), c, e_{old}): (v, w) \in E \land pred[v] \neq pred[w]\} }[/math]

Finding the bijective function is possible in [math]\displaystyle{ O(\frac{n}{p} + \log p) }[/math] using a prefix sum. As we now have a new set of vertices and edges the adjacency array must be rebuilt, which can be done using Integersort on [math]\displaystyle{ E' }[/math] in [math]\displaystyle{ O(\frac{m}{p} + \log p) }[/math] time.

Complexity

Each iteration now needs [math]\displaystyle{ O(\frac{m}{p} + \log n) }[/math] time and just like in the sequential case there are [math]\displaystyle{ \log n }[/math] iterations, resulting in a total runtime of [math]\displaystyle{ O(\log n(\frac{m}{p} + \log n)) }[/math]. If [math]\displaystyle{ m \in \Omega(p \log^2 p) }[/math] the efficiency of the algorithm is in [math]\displaystyle{ \Theta(1) }[/math] and it is relatively efficient. If [math]\displaystyle{ m \in O(n) }[/math] then it is absolutely efficient.

Further algorithms

There are multiple other parallel algorithms that deal the issue of finding an MST. With a linear number of processors it is possible to achieve this in [math]\displaystyle{ O(\log n) }[/math].[8][9] Bader and Cong presented an MST-algorithm, that was five times quicker on eight cores than an optimal sequential algorithm.[10]

Another challenge is the External Memory model - there is a proposed algorithm due to Dementiev et al. that is claimed to be only two to five times slower than an algorithm that only makes use of internal memory[11]

References

  1. Sanders; Dietzfelbinger; Martin; Mehlhorn; Kurt; Peter (2014-06-10). Algorithmen und Datenstrukturen Die Grundwerkzeuge. Springer Vieweg. ISBN 978-3-642-05472-3. 
  2. Brodal, Gerth Stølting; Träff, Jesper Larsson; Zaroliagis, Christos D. (1998), "A Parallel Priority Queue with Constant Time Operations", Journal of Parallel and Distributed Computing 49 (1): 4–21, doi:10.1006/jpdc.1998.1425 
  3. Osipov, Vitaly; Sanders, Peter; Singler, Johannes (2009), "The filter-kruskal minimum spanning tree algorithm", Proceedings of the Eleventh Workshop on Algorithm Engineering and Experiments (ALENEX). Society for Industrial and Applied Mathematics: 52–61 
  4. Sanders, Peter. "Algorithm Engineering script". http://algo2.iti.kit.edu/sanders/courses/algen17/skript.pdf. 
  5. Sanders, Peter. "Parallel Algorithms script". https://algo2.iti.kit.edu/sanders/courses/paralg18/skript.pdf. 
  6. Zadeh, Reza. "Distributed Algorithms and Optimization". https://stanford.edu/~rezab/dao/notes/lecture06/cme323_lec6.pdf. 
  7. Chun, Sun; Condon, Anne (1996). "Parallel implementation of Bouvka's minimum spanning tree algorithm". Proceedings of International Conference on Parallel Processing. pp. 302–308. doi:10.1109/IPPS.1996.508073. ISBN 0-8186-7255-2. 
  8. Chong, Ka Wong; Han, Yijie; Lam, Tak Wah (2001), "Concurrent threads and optimal parallel minimum spanning trees algorithm", Journal of the Association for Computing Machinery 48 (2): 297–323, doi:10.1145/375827.375847 
  9. Pettie, Seth; Ramachandran, Vijaya (2002), "A randomized time-work optimal parallel algorithm for finding a minimum spanning forest", SIAM Journal on Computing 31 (6): 1879–1895, doi:10.1137/S0097539700371065, http://www.eecs.umich.edu/~pettie/papers/sicomp-randmst.pdf 
  10. Bader, David A.; Cong, Guojing (2006), "Fast shared-memory algorithms for computing the minimum spanning forest of sparse graphs", Journal of Parallel and Distributed Computing 66 (11): 1366–1378, doi:10.1016/j.jpdc.2006.06.001 
  11. Dementiev, Roman; Sanders, Peter; Schultes, Dominik; Sibeyn, Jop F. (2004), "Engineering an external memory minimum spanning tree algorithm", Proc. IFIP 18th World Computer Congress, TC1 3rd International Conference on Theoretical Computer Science (TCS2004), pp. 195–208, http://algo2.iti.kit.edu/dementiev/files/emst.pdf .