Keywords

These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.

1 Introduction

Mathematical morphology provides a set of filtering and segmenting tools that are very useful in applications to image analysis. There is a growing interest for considering digital objects not only composed of points but also composed of elements lying between them and carrying structural information about how the points are glued together. The simplest of these representations are the graphs. The domain of an image is considered as a graph (which can be planar or not) whose vertex set is made of the pixels and whose edge set is given by an adjacency relation on these pixels. Note that this adjacency relation can be either spatially invariant or spatially variant leading to operators that are either spatially invariant or spatially variant. Graphs are also useful to process other kinds of discrete structures defined for instance on 3-dimensional meshes. In this context, it becomes relevant to consider morphological transformations acting on the subsets of vertices, the subsets of edges and the subgraphs of a graphs and not only those acting on the set of all subsets of pixels.

Mathematical morphology on graphs was pioneered by Vincent [12] who proposes operators relying on a dilatation (and its adjunct erosion) that act on the vertices of a graph. More recently, [3, 6] introduce basic dilatations and erosions that map a set of vertices to a set of edges and a set of edges to a set of vertices. It was shown in [3] that these operators can be combined in order to obtain operators acting on the subsets of edges, on the subsets of vertices and on the subgraphs of a given graph. In particular, interesting openings and closings (and then the associated alternate sequential filters) are obtained by iteration of the basic operators. The number of iterations constitutes a filtering parameter related to the size of the features to be preserved or removed. Therefore, based on the straightforward definition, the time-complexity of the associated algorithm increases with the size parameter. More precisely, for a parameter value of \(\lambda \) the algorithm runs in \(O(\lambda .n)\) time, where n is the size of the underlying graph. In this article, our main contributions are twofold: we first propose to use distance maps in order to avoid the dependence to the parameter \(\lambda \) when computing the results of the operators of [3]; and then we propose a parallelization strategy leading to fast computation, in particular, for multicore/multithread architectures.

After presenting background notions about morphology and graphs in Sect. 2, we investigate in Sect. 3 some distance maps that lead to characterizations of the dilations and erosions presented in [3]. Since we are interested in operators that map sets of edges to sets of vertices and sets of vertices to sets of edges, we introduce edge-vertex and vertex-edge distance maps. Given a set of edges (resp. vertices), the edge-vertex (resp. vertex-edge) distance map provides for each vertex (resp. edge) a geodesic distance to the closest edge (resp. vertex) of the input set. In order to computed these distance maps, we adapt classical linear-time algorithm for distance maps in unweighted graphs. These algorithms derive from breadth first search. Whatever the size parameter, any dilation, erosion, opening and closing of [3] can be obtained by thresholding these distance maps. Therefore, the time complexity of the associated algorithms is linear with respect to the size of the graph, without any dependence to the size parameter.

In Sect. 4, we propose a parallel algorithm to compute the proposed distance maps, hence the morphological operators of [3]. Parallel and/or separable algorithms for morphological operators and distance maps on images have been widely studied [1, 2, 5, 711]. Based on the regular structure of the space, such computations use a static partitioning of the image into rows, columns or blocks processed in parallel. In order to handle the non-regular structure of a graph, our parallelization strategy is based on dynamic partitioning which depends on the input set and which is iteratively computed during the execution. The time complexity of our parallel algorithm is analyzed. In particular it depends of the complexity of two auxiliary functions to manage the dynamic partitions. These functions are presented in Sect. 5. Under some reasonable assumptions about the graph and set under consideration, our algorithm runs in \(O(n/p + K \log _2 p)\) time, where n, p, and K are the size of the underlying graph, the number of available processors and the number of distinct level sets of the distance map, respectively. In the considered practical cases, this complexity is dominated by the O(n / p) term.

2 Background Notions for Morphology on Graphs

A (undirected) graph is a pair \(X=({X}^{\bullet },{X}^{\times })\) where \({X}^{\bullet }\) is a set and \({X}^{\times }\) is composed of unordered pairs of distinct elements in \({X}^{\bullet }\), i.e.\({X}^{\times }\) is a subset of \(\left\{ \{x,y\} \subseteq {X}^{\bullet } \; | \;x \ne y \right\} \). Each element of \({X}^{\bullet }\) is called a vertex or a point (of X), and each element of \({X}^{\times }\) is called an edge (of X).

Important Remark. Hereafter, the workspace is a graph \(\mathbb {G}=({\mathbb {G}}^{\bullet },{\mathbb {G}}^{\times })\) and we consider the sets \({\mathcal {G}}^{\bullet }\), \({\mathcal {G}}^{\times }\) and \(\mathcal {G}\) of respectively all subsets of \({\mathbb {G}}^{\bullet }\), all subsets of \({\mathbb {G}}^{\times }\) and all subgraphs of \(\mathbb {G}\).

Mathematical morphology on graphs, as introduced in [3], relies on four basic operators. The operators \({\delta }^{\bullet }\) and \({\epsilon }^{\bullet }\) are defined from \({\mathcal {G}}^{\times }\) to \({\mathcal {G}}^{\bullet }\) by:

$$\begin{aligned} {\delta }^{\bullet }({X}^{\times })&= \{x \in {\mathbb {G}}^{\bullet } \; | \;\exists \{x,y\} \in {X}^{\times }\}, \text { for any~} {X}^{\times } \subseteq {\mathbb {G}}^{\times } \text {; and} \end{aligned}$$
(1)
$$\begin{aligned} {\epsilon }^{\bullet }({X}^{\times })&= \{x \in {\mathbb {G}}^{\bullet } \; | \;\forall \{x,y\} \in {\mathbb {G}}^{\times }, \{x,y\} \in {X}^{\times }\}, \text { for any }~ {X}^{\times } \subseteq {\mathbb {G}}^{\times }. \end{aligned}$$
(2)

The operators \({\epsilon }^{\times }\), and \({\delta }^{\times }\) are defined from \({\mathcal {G}}^{\bullet }\) to \({\mathcal {G}}^{\times }\) by:

$$\begin{aligned} {\epsilon }^{\times }({X}^{\bullet })&= \{ \{x,y\} \in {\mathbb {G}}^{\times } \; | \;x \in {X}^{\bullet } \text { and } y \in {X}^{\bullet }\},&\text { for any }~ {X}^{\bullet } \subseteq {\mathbb {G}}^{\bullet }\text {; and} \end{aligned}$$
(3)
$$\begin{aligned} {\delta }^{\times }({X}^{\bullet })&= \{ \{x,y\} \in {\mathbb {G}}^{\times } \; | \;x \in {X}^{\bullet } \text { or } y \in {X}^{\bullet }\},&\text { for any }~{X}^{\bullet } \subseteq {\mathbb {G}}^{\bullet }. \end{aligned}$$
(4)

In order to obtain efficient filters (opening, closings, and associated alternate sequential filters), which are parametrized by a integer value related to a notion of size of the features to be preserved or removed, one needs to consider iterated versions of the basic building blocks presented above. Let \(\alpha \) be an operator acting on \({\mathcal {G}}^{\bullet }\) or on \({\mathcal {G}}^{\times }\) and let i be a non negative integer. The operator \(\alpha ^i\) is defined by the identity when \(i = 0\) and by \(\alpha \circ \alpha ^{i-1}\) otherwise.

Since the operators defined above map the elements of \({\mathcal {G}}^{\bullet }\) (i.e., subsets of vertices) to those of \({\mathcal {G}}^{\times }\) (i.e., subsets of edges) or the elements of \({\mathcal {G}}^{\times }\) to those of \({\mathcal {G}}^{\bullet }\), they cannot be directly iterated. However, any composition of an operator acting from \({\mathcal {G}}^{\bullet }\) to \({\mathcal {G}}^{\times }\) (resp. from \({\mathcal {G}}^{\times }\) to \({\mathcal {G}}^{\bullet }\)) with an operator from \({\mathcal {G}}^{\times }\) to \({\mathcal {G}}^{\bullet }\) (resp.from \({\mathcal {G}}^{\bullet }\) to \({\mathcal {G}}^{\times }\)) leads to an operator on \({\mathcal {G}}^{\bullet }\) (resp. on \({\mathcal {G}}^{\times }\)). Then, such composition can be iterated and eventually followed again by an operator from \({\mathcal {G}}^{\bullet }\) to \({\mathcal {G}}^{\times }\) (resp. from \({\mathcal {G}}^{\times }\) to \({\mathcal {G}}^{\bullet }\)). Therefore, to define iterated operators on graphs, two cases can be distinguish depending whether a final composition with an operator from \({\mathcal {G}}^{\bullet }\) to \({\mathcal {G}}^{\times }\) (resp. from \({\mathcal {G}}^{\times }\) to \({\mathcal {G}}^{\bullet }\)) is considered or not.

Definition 1

(Iterated Dilations/Erosions). Let \(\lambda \) be a nonnegative integer.

Case 1 (Even Values of \(\lambda \) ). If \(\lambda \) is even, the operators \(\delta _{\lambda /2}\) and \(\epsilon _{\lambda /2}\) are defined on \({\mathcal {G}}^{\bullet }\) by \(\delta _{\lambda /2} = ({\delta }^{\bullet } \circ {\delta }^{\times })^{\lambda /2}\) and \(\epsilon _{\lambda /2} = ({\epsilon }^{\bullet } \circ {\epsilon }^{\times })^{\lambda /2}\); the operators \(\varDelta _{\lambda /2}\) and \(\varepsilon _{\lambda /2}\) are defined on \({\mathcal {G}}^{\times }\) by \(\varDelta _{\lambda /2} = ({\delta }^{\times } \circ {\delta }^{\bullet })^{\lambda /2}\) and \(\varepsilon _{\lambda /2} = ({\epsilon }^{\times } \circ {\epsilon }^{\bullet })^{\lambda /2}\).

Case 2 (Odd Values of \(\lambda \) ). If \(\lambda \) is odd, the operators \(\delta _{\lambda /2}\) and \(\epsilon _{\lambda /2}\) are defined from \({\mathcal {G}}^{\bullet }\) to \({\mathcal {G}}^{\times }\) by \(\delta _{\lambda /2} = {\delta }^{\times } \circ ({\delta }^{\bullet } \circ {\delta }^{\times })^{(\lambda -1)/2}\) and \(\epsilon _{\lambda /2} = {\epsilon }^{\times } \circ ({\epsilon }^{\bullet }\circ {\epsilon }^{\times })^{(\lambda -1)/2}\); the operators \(\varDelta _{\lambda /2}\) and \(\varepsilon _{\lambda /2}\) are defined from \({\mathcal {G}}^{\times }\) to \({\mathcal {G}}^{\bullet }\) by \(\varDelta _{\lambda /2} = {\delta }^{\bullet } \circ ({\delta }^{\times } \circ {\delta }^{\bullet })^{(\lambda -1)/2}\) and \(\varepsilon _{\lambda /2} = {\epsilon }^{\bullet } \circ ({\epsilon }^{\times } \circ {\epsilon }^{\bullet })^{(\lambda -1)/2}\).

Illustrations of the operators \(\delta _{\lambda /2}\) and \(\varDelta _{\lambda /2}\) are provided in Fig. 1 for \(\lambda =3\).

Fig. 1.
figure 1

Illustration of some morphological operators on graphs and of vertex-edge and edge-vertex distance maps.

The operators \({\delta }^{\bullet }, {\delta }^{\times }, \delta _{\lambda /2}\) and \(\varDelta _{\lambda /2}\) are all morphological dilations and the operators \({\epsilon }^{\times }, {\epsilon }^{\bullet }, \epsilon _{\lambda /2}\) and \(\varepsilon _{\lambda /2}\) are their adjunct erosions. Thus, any composition of one of these dilations with its adjunct erosion leads to a morphological filter which is either an opening or a closing depending on the composition order. In particular, when the integer parameter \(\lambda \) is even (resp. odd), the compositions of \(\delta _{\lambda /2} \circ \epsilon _{\lambda /2}\) and \( \epsilon _{\lambda /2} \circ \delta _{\lambda /2}\) (resp. \(\varDelta _{\lambda /2} \circ \epsilon _{\lambda /2}\) and \( \varepsilon _{\lambda /2} \circ \delta _{\lambda /2}\)) filters on \({G}^{\bullet }\) and the compositions of \(\varDelta _{\lambda /2} \circ \varepsilon _{\lambda /2}\) and \( \varepsilon _{\lambda /2} \circ \varDelta _{\lambda /2}\) (resp. \(\delta _{\lambda /2} \circ \varepsilon _{\lambda /2}\) and \( \epsilon _{\lambda /2} \circ \varDelta _{\lambda /2}\)) filters on \({G}^{\times }\). The simultaneous application of these compositions on the vertices and on the edges of any element in \(\mathcal {G}\) (i.e., on any subgraph of \(\mathbb {G}\)) leads to a subgraph of \(\mathbb {G}\), hence morphological filtering on subgraphs.

When the size parameter \(\lambda \) is an even integer, the operators \(\delta _{\lambda /2}\) and \(\epsilon _{\lambda /2}\) correspond to the dilation and erosion proposed in [12]. It is known [12] that the result of these operators can be obtained by thresholding a (geodesic) distance map instead of iterating the basic dilation or erosion.

Let x and y be two vertices in \({\mathbb {G}}^{\bullet }\). A (vertex-vertex) path from x to y is a sequence \((x_0, u_0, \ldots , u_{\ell -1}, x_\ell )\) such that \(x_0 = x\)\(x_\ell =y\), and, for any i in \(\{0, \ldots , \ell -1\}\), we have \(u_i = \{x_i,x_{i+1}\}\). The length of a path \(\pi = (x_0, u_0, \ldots , u_{\ell -1}, x_\ell )\) is the number of its elements minus one, i.e., the integer value \(2 \ell \). A shortest path from x to y is a path of minimal length from x to y. We denote by L(xy) the length of a shortest path from x to y. The (vertex-vertex)distance map \(D_{{X}^{\bullet }}\) to a set \({X}^{\bullet } \subseteq {\mathbb {G}}^{\bullet }\) is the map from \({\mathbb {G}}^{\bullet }\) to the set of integers such that:

$$\begin{aligned} D_{{X}^{\bullet }}(x) = \min \{L(x,y) \; | \;y \in {X}^{\bullet }\},&\text { for any }~x \in {X}^{\bullet }. \end{aligned}$$
(5)

Then, when \(\lambda \) is even, the following relation characterizes the dilation \(\delta _{\lambda /2}\):

$$\begin{aligned} \delta _{\lambda /2}({X}^{\bullet })&= \{x \in {\mathbb {G}}^{\bullet } \; | \;{D}^{\bullet }_{{X}^{\bullet }}(x) \le \lambda \}, \text { for any }~ {X}^{\bullet } \in {\mathcal {G}}^{\bullet }. \end{aligned}$$
(6)

Based on Eq. 6, to obtain the dilation of a set of vertices, one needs to compute a distance map and to threshold it. An advantage, compared to the computation based on the iterative definition, is to avoid the dependence to the parameter \(\lambda \) in the algorithm time-complexity. More precisely, it is known that the distance map and thresholding computations (see e.g. Algorithm 1 for distance map) can be done in linear-time with respect to the size of the graph \(\mathbb {G}\). In particular, Algorithm 1 is a variation on breadth-first search, which is a linear-time algorithm with respect to the size of \(\mathbb {G}\). Observe that at line 8 of Algorithm 1, the distance value given to y is equal to the one of its predecessor x plus two. This is indeed correct with respect to the above definition of the length of a path, for which, e.g., the distance between two neighbors is equal to two.

It can be deduced from the duality properties stated in [3] that \((\delta _{\lambda /2},\epsilon _{\lambda /2})\) and \((\varDelta _{\lambda /2},\varepsilon _{\lambda /2})\) are pairs of dual operators, meaning that one operator in the pair can be easily computed from the other due to complementation operations. Hence, in order to provide efficient algorithms for these operators, we only need to focus on the operators \(\delta _{\lambda /2}\) and \(\varDelta _{\lambda /2}\) and deduce the others by duality. For instance, the erosion \(\epsilon _{\lambda /2}({X}^{\bullet })\) can be obtained with the same algorithm as \(\delta _{\lambda /2}({X}^{\bullet })\) provided a complementation on both the input and output of the dilation algorithm. It is also straightforward to obtain a similar linear-time algorithms, based on an edge-edge distance map, for the edge dilation \(\varDelta _{\lambda /2}\) and erosion \(\varepsilon _{\lambda /2}\) when \(\lambda \) is even.

figure a

The next section presents an approach based on distance maps to obtain linear time algorithms for \(\varDelta _{\lambda /2}\) and \(\delta _{\lambda /2}\) when \(\lambda \) is odd. Then, Sect. 4 presents a parallelization strategy leading to efficient parallel algorithms for all morphological operators on graphs presented in [3].

3 Vertex-Edge and Edge-Vertex Distance Maps

When considering an odd value of \(\lambda \), an important difference with the even case is that the results and arguments of the dilations \(\delta _{\lambda /2}\) and \(\varDelta _{\lambda /2}\) are not homogeneous: one of them is a set of edges whereas the other one is a set of vertices. In order to deal with this inhomogeneity, we introduce the edge-vertex and vertex-edge distance maps. Given a set of edges (resp. vertices), the edge-vertex (resp. vertex-edge) distance map provides for each vertex (resp. edge) of \(\mathbb {G}\) a distance to the closest edge (resp. vertex) of the input set. These distance maps allow us to characterize (by thresholding) the dilations \(\delta _{\lambda /2}\) and \(\varDelta _{\lambda /2}\) when \(\lambda \) is odd. Finally, Algorithm 1 is adapted to compute these distance maps.

The distance maps considered in this section rely on the lengths of shortest paths from vertices to edges. A (vertex-edge) path from a vertex x of \(\mathbb {G}\) to an edge u of \(\mathbb {G}\) is a sequence \((x_0,u_0,\ldots ,x_\ell ,u_\ell )\) such that \(u_\ell = u\)\(x_\ell \in u_\ell \), and \((x_0,u_0,\ldots ,x_\ell )\) is a vertex-vertex path from x to \(x_\ell \). The length of a path \((x_0,u_0,\ldots ,x_l,u_\ell )\) is the number of its elements minus one, i.e., the integer value \(2\ell +1\). A shortest path from a vertex x of \(~\mathbb {G}\) to an edge u of \(~\mathbb {G}\) is a path of minimal length from x to u. We denote by L(xu) the length of a shortest path from x to u. Finally, given a subset \(X^{\bullet }\) of vertices of \(\mathbb {G}\), we define the vertex-edge distance map to \({X^\bullet }\) as the map \(D^{\times }_{X^\bullet }\) from \({\mathbb {G}}^{\times }\) to the set of integers such that:

$$\begin{aligned} D^{\times }_{X^\bullet }(u) = \min \{L(x,u) \; | \;x \in X^\bullet \}, \text {for any } u \in X^{\times }. \end{aligned}$$
(7)

Dualy, given a subset \({X}^{\times }\) of edges, the edge-vertex distance map to the set \({X}^{\times }\) is the map \({D}^{\bullet }_{{X}^{\times }}\) from \({{\mathbb {G}}^{\bullet }}\) to the set of integers such that:

$$\begin{aligned} {D}^{\bullet }_{{X}^{\times }}(x) = \min \{L(x,u) \; | \;u \in {X}^{\times }\}, \text { for any } x \in {X}^{\bullet }. \end{aligned}$$
(8)

Edge-vertex and vertex-edge distance maps are illustrated in Fig. 1.

The next property states that the dilatations \(\delta _{\lambda /2}\) and \(\varDelta _{\lambda /2}\) can also be characterized with distance maps when \(\lambda \) is odd.

Property 2

Let \(\lambda \) be any odd positive integer. The following relations hold true:

$$\begin{aligned} \delta _{\lambda /2} ({X}^{\bullet })&= \{u \in {\mathbb {G}}^{\times } \; | \;{D}^{\times }_{{X}^{\bullet }}(u) \le \lambda \}, \text { for any }~ {X}^{\bullet } \in {\mathcal {G}}^{\bullet } \text {; and}\\ \varDelta _{\lambda /2} ({X}^{\times })&= \{x \in {\mathbb {G}}^{\bullet } \; | \;{D}^{\bullet }_{{X}^{\times }}(x) \le \lambda \}, \text { for any }~ {X}^{\times } \in {\mathcal {G}}^{\times }. \end{aligned}$$

Algorithms 2 and 3 presented below compute these distance maps in linear time with respect to size \(|{\mathbb {G}}^{\bullet }| + |{\mathbb {G}}^{\times }|\) of \(\mathbb {G}\).

figure b
figure c

4 Parallel Algorithm for Distance Maps on Graphs

Contrary to the parallel computation of distance maps on an image, which is often based on a static partitioning of the image into rows, columns or blocks processed in parallel, our parallelization strategy on graphs is based on dynamic partitioning. The partition depends on the input set and is iteratively computed during the execution. More precisely, our strategy iteratively considers the successive level-sets of the distance maps, each level set being partitioned and then traversed in parallel. In this section, our parallel algorithm is presented and its complexity is analyzed assuming that partitioning can be done efficiently. Efficient parallel management of partitions is the topic of the next section.

For the sake of simplicity, we only describe the case of vertex-vertex distance maps, but our strategy can also be adapted to edge-edge, vertex-edge and edge-vertex distance maps computations.

Let us first present our algorithm from a high level point of view. To this end, we recall the notion of a level set. Given an integer \(\lambda \) and a (distance) map D from \({\mathbb {G}}^{\bullet }\) in the set of integers, the \(\lambda \) -level set of D is the set of all elements of value \(\lambda \) for D (i.e., the set \(\{x \in {\mathbb {G}}^{\bullet } \; | \;D(x) = \lambda \}\)). Given a subset \({X}^{\bullet }\) of \({\mathbb {G}}^{\bullet }\), after an initialization step where an integer variable \(\lambda \) is set to 0 and where the elements of \({X}^{\bullet }\) are inserted in a variable set E (hence E is the \((\lambda =0)\)-level-set of \({D}^{\bullet }_{{X}^{\bullet }}\)), our algorithm can be sketched as follows:

  1. 1.

    Partition E (i.e., the \(\lambda \)-level set of \({D}^{\bullet }_{{X}^{\bullet }}\)) into p balanced subsets \(E_1, \ldots , E_p\).

  2. 2.

    Assign each of the p subsets \(E_1, \ldots , E_p\) to one of the p processors

  3. 3.

    Let, in parallel, each processor insert the non already traversed neighbors of the elements in its assigned subset \(E_i\) into a private variable set \(S_i\) and set the distance map value of the elements in \(S_i\) to \(\lambda +2\).

  4. 4.

    Merge the private sets \(\{S_i \; | \;i \in \{1,\ldots , p\} \}\) and store the result in E so that E becomes the \((\lambda +2 )\)-level set of \({D}^{\bullet }_{{X}^{\bullet }}\).

  5. 5.

    Increment \(\lambda \) and repeat steps 1–4 until E becomes empty.

In Step 3, in order to concurrently check if a vertex has been already traversed, we need to equip each vertex with a synchronization Boolean variable that is handled with an atomic test-and-set instruction. The test-and-set instruction sets a given variable to true and returns its old value as a single atomic (i.e., non-interruptible) instruction.

Algorithm 4 provides the precise description of our parallel approach. It uses two auxiliary functions called Partition and Union. In the next section, we provide algorithms for these two functions. The efficiency of Algorithm 4 depends on these functions. As we will see, the function Partition runs in linear time with respect to n / p and the function Union runs in \(O(n/(Kp) + log_2 p)\) amortized time, where np and K are the size of the graph, the number of processors, and the number of level-sets of the produced distance map. Furthermore, any class of the produced partition contains either n / p or \(n/p+1\) elements.

figure d

Finally, in order to state the time complexity of Algorithm 4, we need to make two assumptions about the graph and the set of vertices under consideration.

The degree of a vertex x of \(\mathbb {G}\) is the number of edges that contain x (i.e., the cardinality of the set \(\{ y \in {\mathbb {G}}^{\bullet } \; | \;\{x,y\} \in {\mathbb {G}}^{\times }\}\)). Let \(\beta \) be any positive integers. We say that \(\mathbb {G}\) is \(\beta \) -balanced if the degrees of any two vertices of \(\mathbb {G}\) differ by at most \(\beta \). Let \({X}^{\bullet }\) be a subset of \({\mathbb {G}}^{\bullet }\). We say that \({X}^{\bullet }\) is \(\beta \) -balanced if every nonempty level-set of \({D}^{\bullet }_{{X}^{\bullet }}\) contains at least \(\beta \) elements.

Note that when \({X}^{\bullet }\) is p-balanced, the distance map \({D}^{\bullet }_{{X}^{\bullet }}\) has at most \(|{\mathbb {G}}^{\bullet }|/p\) nonempty level-sets, then the while loop at line 7 is executed at most \(|{\mathbb {G}}^{\bullet }|/p\) times. Furthermore, if a given level set E contains n vertices, any of the \(\{E_i \; | \;i \in \{1, \ldots , p\}\}\) obtained at line 8 contains at most \(n/p + 1\) vertices, which allows us to deduce that the loop line 11 runs in \(O(|{\mathbb {G}}^{\bullet }|/p)\) time since the level-sets of \({D}^{\bullet }_{{X}^{\bullet }}\) partition \({\mathbb {G}}^{\bullet }\). As any of the \(\{E_i \; | \;i \in \{1, \ldots , p\}\}\) contains at most \(n/p + 1\) vertices, when \(\mathbb {G}\) is \(\beta \)-balanced, we can bound the number of edges that contain an element in \(E_i\) by \(m/p + d_{min} + \beta n / p + \beta \), where m is the total number of edges that contain an element in \(E_i\) and where \(d_{min}\) is the minimal degree of a vertex of \(\mathbb {G}\). Thus, we also have \(|S_i| \le m/p + d_{min} + \beta n / p + \beta \), where \(S_i\) is the set obtained after the execution of foreach loop line 11. Hence, since the level sets of \({D}^{\bullet }_{{X}^{\bullet }}\) partition \({\mathbb {G}}^{\bullet }\), it can be shown that the insertion operation on \(S_i\) at line 14 is executed at most \( (3|{\mathbb {G}}^{\times }| + 2\beta |{\mathbb {G}}^{\bullet }|) / p\) times by each of the p processors during the overall execution and the continuation condition of the loop at line 12 must be tested less than \(3 |{\mathbb {G}}^{\times }| / p + 2(\beta +1) |{\mathbb {G}}^{\bullet }| / p\) times. Hence, using an array of linked lists to represent the graph \(\mathbb {G}\) and using simple arrays for all sets, we deduce that the time complexity of the main part (lines 9 to 15) of Algorithm 4 is linear with respect to \((|{\mathbb {G}}^{\times }| + |{\mathbb {G}}^{\bullet }| ) / p\). Considering also the auxiliary functions Union and Partition, the overall time complexity of Algorithm 4 can be established.

Theorem 3

Algorithm 4 outputs a map \({D}^{\bullet }_{{X}^{\bullet }}\) which is the vertex-vertex distance map to the set \({X}^{\bullet }\). Let p be the number of available processors. Let us assume that \(\beta \) is a constant integer such that \(\mathbb {G}\) is \(\beta \)-balanced and that \({X}^{\bullet }\) is p-balanced. Then, Algorithm 4 runs in \(O( (|{\mathbb {G}}^{\bullet }| + |{G}^{\times }|)/p + K \log _2 p)\) time, where K is the number of nonempty level-sets of \({D}^{\bullet }_{{X}^{\bullet }}\).

Under the assumption of Theorem 3, the distance map \(D_X\) contains at most \(|{\mathbb {G}}^{\bullet }|/p\) nonempty level-sets. Thus the time complexity of Algorithm 4 is less than \(O((|{\mathbb {G}}^{\bullet }| + |{G}^{\times }|)/p + |{\mathbb {G}}^{\bullet }| (\log _2 p) / p)\).

The assumptions in Theorem 3 hold, in general, true when the graph-based morphological operators of [3] are applied to image processing. In particular, when we consider a 2-dimensional image equipped with the 4- or 8- adjacency relation, the degrees of any two vertices are the same (except on the image borders), and the number of distinct level-sets is of the order of \(\sqrt{|{G}^{\bullet }|}\), meaning that in average, each level set contains \(\sqrt{|{G}^{\bullet }|}\) vertices. Furthermore, in practice, we generally have \(K.\log _2 p \le (|{\mathbb {G}}^{\bullet }| + |{\mathbb {G}}^{\times }|) / p\). Thus, roughly speaking, we can say that the time-complexity is, in general, dominated by \((|{\mathbb {G}}^{\bullet }| + |{G}^{\times }|)/p\).

5 Parallel Partition and Disjoint Union Algorithms

In this section, we present efficient parallel algorithms for the partition and union function used in Algorithm 4 and we analyze their time-complexity.

The parallel partition algorithm (see Algorithm 5) consists of computing in parallel, with p processors, a balanced partition \(\{E_1, \ldots , E_p\}\) of a set E. The partition is balanced in the sense that the k-first sets of the partition contain \(|E|/p+1\) elements whereas the following ones contain |E| / p elements, where k is the remainder in the integer division of |E| by p. The elements of E, stored in an array, are moved to arrays previously allocated for the subsets \(E_1, \ldots , E_p\) in the order of their indices: the first set receives the first elements of the array E and so on (see Fig. 2). Thus, each processor computes the index of the first and of the last element that must be copied (lines 2 to 7) before actually copying the elements of E located between the computed indices (line 8). The computation of the first and of the last indices can be done in constant time and the copying step is done in linear time with respect to |E| / p (each processor moves at most \(|E|/p +1\) elements).

figure e
Fig. 2.
figure 2

Illustration of the Partition algorithm with \(p = 5\) processors.

Our parallel Union algorithm (see Algorithm 6) computes the union of p disjoint sets \(\{S_1, \ldots , S_p\}\) with p processors. The elements of each set are stored in an array and each processor i copies the elements of the array \(S_i\) in the array E. The elements of \(S_i\) are stored in the resulting array E from the index start[i], where start[i] is the sum of the cardinalities of the sets \(S_1, \ldots , S_{i-1}\) (see Fig. 3). Thus, our algorithm first computes the values start[i] for any i in \(\{1, \ldots , p\}\) (line 1) before actually copying the elements into E (line 3). Given the cardinalities \(|S_1|, \ldots ,|S_p|\), computing the values start[i] for any i in \(\{1,\ldots ,p\}\) is known as the prefix-sum problem. It can be solved in parallel with p processors with a \(O(\log _2 p)\) running-time algorithm [4]. Then, each processor i copies (line 3) the elements of \(S_i\) into E at the correct position. Let us consider the amortized-time complexity of this operation for a sequence of calls to Union as used in Algorithm 4, under the assumptions of Theorem 3. Let K be the number of distinct level sets of \({D}^{\bullet }_{{X}^{\bullet }}\). There is one call to Union for each level-set of the distance map \({D}^{\bullet }_X\). Thus, there are K calls to Union. We have seen in Sect. 4 that there are at most \((3|{\mathbb {G}}^{\times }| + 2\beta |{\mathbb {G}}^{\bullet }|) / p\) insertions in \(S_i\). Any element inserted in \(S_i\) is considered exactly once at line 3 of Algorithm 6. Thus, the amortized time-complexity of line 3 is \(O((|{\mathbb {G}}^{\bullet }| + |{\mathbb {G}}^{\times }|)/ (Kp))\) and the one of Algorithm 6 is \(O((|{\mathbb {G}}^{\bullet }| + |{\mathbb {G}}^{\times }|)/ (Kp) + \log _2 p)\).

figure f
Fig. 3.
figure 3

Illustration of the Union algorithm with \(p =5\) processors.

6 Conclusion

In this article efficient sequential and parallel algorithms for the (binary) graph-based mathematical morphology operators defined in [3] have been proposed. These algorithms are based on distance maps computation in unweighted graphs. The sequential algorithms run in linear time with respect to the size of the underlying graph, whereas the parallel algorithms run (under some reasonable assumptions) in \(O( n/p + K \log _2 p)\) time, where n, p, and K are the size of the underlying graph, the number of available processors, and the number of distinct level-sets of the distance map, respectively.

From a computational point of view, future work will include experimental studies of the execution times, variations on our parallel algorithms with improved load balancing, as well as algorithms for the so-called “grayscale case” in order to filter functions as well as binary sets. On the methodological point of view, the use of distance maps in unweighted graphs opens the door towards the investigation of morphological operators on graphs embedded in metric spaces (or more generally on weighted graphs) where the result of an operator depends on the “length” of the edges according to the metric.