- Source: Parallel algorithms for minimum spanning trees
In graph theory a minimum spanning tree (MST)
T
{\displaystyle T}
of a graph
G
=
(
V
,
E
)
{\displaystyle G=(V,E)}
with
|
V
|
=
n
{\displaystyle |V|=n}
and
|
E
|
=
m
{\displaystyle |E|=m}
is a tree subgraph of
G
{\displaystyle G}
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
G
{\displaystyle G}
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
G
{\displaystyle G}
, 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
E
{\displaystyle E}
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.
Prim's algorithm
This algorithm utilises the cut-property of MSTs. A simple high-level pseudocode implementation is provided below:
T
←
∅
{\displaystyle T\gets \emptyset }
S
←
{
s
}
{\displaystyle S\gets \{s\}}
where
s
{\displaystyle s}
is a random vertex in
V
{\displaystyle V}
repeat
|
V
|
−
1
{\displaystyle |V|-1}
times
find lightest edge
(
u
,
v
)
{\displaystyle (u,v)}
s.t.
u
∈
S
{\displaystyle u\in S}
but
v
∈
(
V
∖
S
)
{\displaystyle v\in (V\setminus S)}
S
←
S
∪
{
v
}
{\displaystyle S\gets S\cup \{v\}}
T
←
T
∪
{
(
u
,
v
)
}
{\displaystyle T\gets T\cup \{(u,v)\}}
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
O
(
n
+
m
)
{\displaystyle O(n+m)}
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
O
(
1
)
{\displaystyle O(1)}
) is performed and each loop iteration performs one deleteMin operation (
O
(
log
n
)
{\displaystyle O(\log n)}
). Thus using Fibonacci heaps the total runtime of Prim's algorithm is asymptotically in
O
(
m
+
n
log
n
)
{\displaystyle O(m+n\log n)}
.
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
S
{\displaystyle S}
and on in
V
∖
S
{\displaystyle V\setminus S}
might change with the addition of edges to
T
{\displaystyle T}
. 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
O
(
n
)
{\displaystyle O(n)}
processors to support PQ access in
O
(
1
)
{\displaystyle O(1)}
on an EREW-PRAM machine, thus lowering the total runtime to
O
(
n
+
m
)
{\displaystyle O(n+m)}
.
Kruskal's algorithm
Kruskal's MST algorithm utilises the cycle property of MSTs. A high-level pseudocode representation is provided below.
T
←
{\displaystyle T\gets }
forest with every vertex in its own subtree
foreach
(
u
,
v
)
∈
E
{\displaystyle (u,v)\in E}
in ascending order of weight
if
u
{\displaystyle u}
and
v
{\displaystyle v}
in different subtrees of
T
{\displaystyle T}
T
←
T
∪
{
(
u
,
v
)
}
{\displaystyle T\gets T\cup \{(u,v)\}}
return T
The subtrees of
T
{\displaystyle T}
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
O
(
α
(
m
,
n
)
)
{\displaystyle O(\alpha (m,n))}
where
α
(
m
,
n
)
{\displaystyle \alpha (m,n)}
is the inverse Ackermann function. Thus the total runtime of the algorithm is in
O
(
s
o
r
t
(
n
)
+
α
(
n
)
)
{\displaystyle O(sort(n)+\alpha (n))}
. Here
α
(
n
)
{\displaystyle \alpha (n)}
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
O
(
log
n
)
{\displaystyle O(\log n)}
processors, the total runtime can be reduced to
O
(
m
α
(
n
)
)
{\displaystyle O(m\alpha (n))}
.
= Approach 2: Filter-Kruskal
=Another approach would be to modify the original algorithm by growing
T
{\displaystyle T}
more aggressively. This idea was presented by Osipov et al. 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(
G
{\displaystyle G}
):
if
m
<
{\displaystyle m<}
KruskalThreshold:
return kruskal(
G
{\displaystyle G}
)
pivot = chooseRandom(
E
{\displaystyle E}
)
(
E
≤
{\displaystyle (E_{\leq }}
,
E
>
)
←
{\displaystyle E_{>})\gets }
partition(
E
{\displaystyle E}
, pivot)
A
←
{\displaystyle A\gets }
filterKruskal(
E
≤
{\displaystyle E_{\leq }}
)
E
>
←
{\displaystyle E_{>}\gets }
filter(
E
>
{\displaystyle E_{>}}
)
A
←
A
{\displaystyle A\gets A}
∪
{\displaystyle \cup }
filterKruskal(
E
>
{\displaystyle E_{>}}
)
return
A
{\displaystyle A}
partition(
E
{\displaystyle E}
, pivot):
E
≤
←
∅
{\displaystyle E_{\leq }\gets \emptyset }
E
>
←
∅
{\displaystyle E_{>}\gets \emptyset }
foreach
(
u
,
v
)
∈
E
{\displaystyle (u,v)\in E}
:
if weight(
u
,
v
{\displaystyle u,v}
)
≤
{\displaystyle \leq }
pivot:
E
≤
←
E
≤
∪
(
u
,
v
)
{\displaystyle E_{\leq }\gets E_{\leq }\cup {(u,v)}}
else
E
>
←
E
>
∪
(
u
,
v
)
{\displaystyle E_{>}\gets E_{>}\cup {(u,v)}}
return (
E
≤
{\displaystyle E_{\leq }}
,
E
>
{\displaystyle E_{>}}
)
filter(
E
{\displaystyle E}
):
E
f
i
l
t
e
r
e
d
←
∅
{\displaystyle E_{filtered}\gets \emptyset }
foreach
(
u
,
v
)
∈
E
{\displaystyle (u,v)\in E}
:
if find-set(u)
≠
{\displaystyle \neq }
find-set(v):
E
f
i
l
t
e
r
e
d
←
E
f
i
l
t
e
r
e
d
∪
(
u
,
v
)
{\displaystyle E_{filtered}\gets E_{filtered}\cup {(u,v)}}
return
E
f
i
l
t
e
r
e
d
{\displaystyle E_{filtered}}
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
{
u
,
v
}
{\displaystyle \{u,v\}}
is contracted by first removing
v
{\displaystyle v}
from the graph and then redirecting every edge
{
w
,
v
}
∈
E
{\displaystyle \{w,v\}\in E}
to
{
w
,
u
}
{\displaystyle \{w,u\}}
. 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.
T
←
∅
{\displaystyle T\gets \emptyset }
while
|
V
|
>
1
{\displaystyle |V|>1}
S
←
∅
{\displaystyle S\gets \emptyset }
for
v
∈
V
{\displaystyle v\in V}
S
←
S
{\displaystyle S\gets S}
∪
{\displaystyle \cup }
lightest
{
u
,
v
}
∈
E
{\displaystyle \{u,v\}\in E}
for
{
u
,
v
}
∈
S
{\displaystyle \{u,v\}\in S}
contract
{
u
,
v
}
{\displaystyle \{u,v\}}
T
←
T
∪
S
{\displaystyle T\gets T\cup S}
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
O
(
m
)
{\displaystyle O(m)}
. 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
log
n
{\displaystyle \log n}
iterations, leading to a total runtime in
O
(
m
log
n
)
{\displaystyle O(m\log n)}
.
= Parallelisation
=One possible parallelisation of this algorithm yields a polylogarithmic time complexity, i.e.
T
(
m
,
n
,
p
)
⋅
p
∈
O
(
m
log
n
)
{\displaystyle T(m,n,p)\cdot p\in O(m\log n)}
and there exists a constant
c
{\displaystyle c}
so that
T
(
m
,
n
,
p
)
∈
O
(
log
c
m
)
{\displaystyle T(m,n,p)\in O(\log ^{c}m)}
. Here
T
(
m
,
n
,
p
)
{\displaystyle T(m,n,p)}
denotes the runtime for a graph with
m
{\displaystyle m}
edges,
n
{\displaystyle n}
vertices on a machine with
p
{\displaystyle p}
processors. The basic idea is the following:
while
|
V
|
>
1
{\displaystyle |V|>1}
find lightest incident edges //
O
(
m
p
+
log
n
+
log
p
)
{\displaystyle O({\frac {m}{p}}+\log n+\log p)}
assign the corresponding subgraph to each vertex //
O
(
n
p
+
log
n
)
{\displaystyle O({\frac {n}{p}}+\log n)}
contract each subgraph //
O
(
m
p
+
log
n
)
{\displaystyle O({\frac {m}{p}}+\log n)}
The MST then consists of all the found lightest edges.
This parallelisation utilises the adjacency array graph representation for
G
=
(
V
,
E
)
{\displaystyle G=(V,E)}
. This consists of three arrays -
Γ
{\displaystyle \Gamma }
of length
n
+
1
{\displaystyle n+1}
for the vertices,
γ
{\displaystyle \gamma }
of length
m
{\displaystyle m}
for the endpoints of each of the
m
{\displaystyle m}
edges and
c
{\displaystyle c}
of length
m
{\displaystyle m}
for the edges' weights. Now for vertex
i
{\displaystyle i}
the other end of each edge incident to
i
{\displaystyle i}
can be found in the entries between
γ
[
Γ
[
i
−
1
]
]
{\displaystyle \gamma [\Gamma [i-1]]}
and
γ
[
Γ
[
i
]
]
{\displaystyle \gamma [\Gamma [i]]}
. The weight of the
i
{\displaystyle i}
-th edge in
Γ
{\displaystyle \Gamma }
can be found in
c
[
i
]
{\displaystyle c[i]}
. Then the
i
{\displaystyle i}
-th edge in
γ
{\displaystyle \gamma }
is between vertices
u
{\displaystyle u}
and
v
{\displaystyle v}
if and only if
Γ
[
u
]
≤
i
<
Γ
[
u
+
1
]
{\displaystyle \Gamma [u]\leq i<\Gamma [u+1]}
and
γ
[
i
]
=
v
{\displaystyle \gamma [i]=v}
.
Finding the lightest incident edge
First the edges are distributed between each of the
p
{\displaystyle p}
processors. The
i
{\displaystyle i}
-th processor receives the edges stored between
γ
[
i
m
p
]
{\displaystyle \gamma [{\frac {im}{p}}]}
and
γ
[
(
i
+
1
)
m
p
−
1
]
{\displaystyle \gamma [{\frac {(i+1)m}{p}}-1]}
. Furthermore, each processor needs to know to which vertex these edges belong (since
γ
{\displaystyle \gamma }
only stores one of the edge's endpoints) and stores this in the array
p
r
e
d
{\displaystyle pred}
. Obtaining this information is possible in
O
(
log
n
)
{\displaystyle O(\log n)}
using
p
{\displaystyle p}
binary searches or in
O
(
n
p
+
p
)
{\displaystyle O({\frac {n}{p}}+p)}
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.
v
←
{\displaystyle v\gets }
find(
i
m
p
{\displaystyle {\frac {im}{p}}}
,
Γ
{\displaystyle \Gamma }
)
for
e
←
i
m
p
;
e
<
(
i
+
1
)
m
p
−
1
;
e
+
+
{\displaystyle e\gets {\frac {im}{p}};e<{\frac {(i+1)m}{p}}-1;e++}
if
Γ
[
v
+
1
]
=
e
{\displaystyle \Gamma [v+1]=e}
v
+
+
{\displaystyle v++}
if
c
[
e
]
<
c
[
p
r
e
d
[
v
]
]
{\displaystyle c[e]
p
r
e
d
[
v
]
←
e
{\displaystyle pred[v]\gets e}
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
p
r
e
v
{\displaystyle prev}
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
O
(
log
p
)
{\displaystyle O(\log p)}
. Thus the total runtime of this step is in
O
(
m
p
+
log
n
+
log
p
)
{\displaystyle O({\frac {m}{p}}+\log n+\log p)}
.
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
v
∈
V
{\displaystyle v\in V}
w
←
p
r
e
d
[
v
]
{\displaystyle w\gets pred[v]}
if
p
r
e
d
[
w
]
=
v
∧
v
<
w
{\displaystyle pred[w]=v\land v
p
r
e
d
[
v
]
←
v
{\displaystyle pred[v]\gets v}
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
∃
v
∈
V
:
p
r
e
d
[
v
]
≠
p
r
e
d
[
p
r
e
d
[
v
]
]
{\displaystyle \exists v\in V:pred[v]\neq pred[pred[v]]}
forAll
v
∈
V
{\displaystyle v\in V}
p
r
e
d
[
v
]
←
p
r
e
d
[
p
r
e
d
[
v
]
]
{\displaystyle pred[v]\gets pred[pred[v]]}
Now every subgraph is a star. With some advanced techniques this step needs
O
(
n
p
+
log
n
)
{\displaystyle O({\frac {n}{p}}+\log n)}
time.
Contracting the subgraphs
In this step each subgraph is contracted to a single vertex.
k
←
{\displaystyle k\gets }
number of subgraphs
V
′
←
{
0
,
…
,
k
−
1
}
{\displaystyle V'\gets \{0,\dots ,k-1\}}
find a bijective function
f
:
{\displaystyle f:}
star root
→
{
0
,
…
,
k
−
1
}
{\displaystyle \rightarrow \{0,\dots ,k-1\}}
E
′
←
{
(
f
(
p
r
e
d
[
v
]
)
,
f
(
p
r
e
d
[
w
]
)
,
c
,
e
o
l
d
)
:
(
v
,
w
)
∈
E
∧
p
r
e
d
[
v
]
≠
p
r
e
d
[
w
]
}
{\displaystyle E'\gets \{(f(pred[v]),f(pred[w]),c,e_{old}):(v,w)\in E\land pred[v]\neq pred[w]\}}
Finding the bijective function is possible in
O
(
n
p
+
log
p
)
{\displaystyle O({\frac {n}{p}}+\log p)}
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
E
′
{\displaystyle E'}
in
O
(
m
p
+
log
p
)
{\displaystyle O({\frac {m}{p}}+\log p)}
time.
= Complexity
=Each iteration now needs
O
(
m
p
+
log
n
)
{\displaystyle O({\frac {m}{p}}+\log n)}
time and just like in the sequential case there are
log
n
{\displaystyle \log n}
iterations, resulting in a total runtime of
O
(
log
n
(
m
p
+
log
n
)
)
{\displaystyle O(\log n({\frac {m}{p}}+\log n))}
. If
m
∈
Ω
(
p
log
2
p
)
{\displaystyle m\in \Omega (p\log ^{2}p)}
the efficiency of the algorithm is in
Θ
(
1
)
{\displaystyle \Theta (1)}
and it is relatively efficient. If
m
∈
O
(
n
)
{\displaystyle m\in O(n)}
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
O
(
log
n
)
{\displaystyle O(\log n)}
. Bader and Cong presented an MST-algorithm, that was five times quicker on eight cores than an optimal sequential algorithm.
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
References
Kata Kunci Pencarian:
- Parallel algorithms for minimum spanning trees
- Minimum spanning tree
- Prim's algorithm
- Euclidean minimum spanning tree
- Spanning tree
- Parallel algorithm
- Kruskal's algorithm
- Borůvka's algorithm
- Priority queue
- Minimum degree spanning tree