<<

Proceedings of the 13th WSEAS International Conference on APPLIED MATHEMATICS (MATH'08)

Parallel Algorithm for Finding an Eulerian in an Undirected Graph

AKIO TADA DAISABURO YOSHIOKA Sojo University Sojo University Faculty of Computer and Information Sciences Faculty of Computer and Information Sciences Ikeda 4-22-1, Kumamoto City Ikeda 4-22-1, Kumamoto City JAPAN JAPAN [email protected] [email protected]

EIICHI MUKAI YOSHIMI MATSUMOTO Sojo University Sojo University Faculty of Computer and Information Science Faculty of Computer and Information Science Ikeda 4-22-1, Kumamoto City Ikeda 4-22-1, Kumamoto City JAPAN JAPAN [email protected]

Abstract: There is Fleury’s algorithm as a sequential algorithm for finding an Eulerian path in an undirected graph. However, we cannot find a parallel algorithm for this problem. In this paper, we proposed an efficient parallel algorithm for finding an Eulerian path in an undirected graph with n vertices and m edges on a CREW-PRAM model. Namely, the proposed algorithm initially determines whether or not a given graph has an Eulerian path. Next, we divide each vertex into several new vertices which have a maximum of 2 edge degrees, and connect between new vertices by edges. Consequently, we find one Eulerian path in the given graph. The proposed algorithm requires O(n + m) processors and O(log(n + m)) time on a CREW-PRAM model.

Key–Words: Parallel algorithm, Eulerian Path, Undirected graph, CREW PRAM model,Fleury’s algorithm

1 Introduction As a result, we find one Eulerian path in the given graph. The proposed algorithm requires O(n + m) An Eulerian path is a path in which every edge is used processors and O(log(n + m)) time on a CREW- precisely once on a connected graph. The conditions PRAM model. of a graph with an Eulerian path are that the num- ber of vertices with odd- is just 2 or degrees of all vertices are even [1, 2]. There is Fleury’s al- 2 The Proposed Algorithm gorithm as a sequential algorithm for finding an Eu- lerian path in an undirected graph [1]. However, we A given graph is an undirected connected graph, cannot find a parallel algorithm for this problem. As which contains self-loop and/or multiple edges. Each one of important applications for this problem, there vertex in a given graph is serially numbered in order is a sequence design on information theory. Finding of a large number of degrees. If each vertex num- an Eulerian path is identical to the design of maxi- ber is given arbitrarily, then a given graph is recon- mum length sequences with finite precisions, such as structed by renumbering the vertex numbers in order De Bruijn sequences. of a large number of degrees. The following theorem In this paper, we proposed an efficient parallel determines whether or not an undirected graph is an algorithm for finding an Eulerian path in an undi- Eulerian path [2]. rected graph with n vertices and m edges on a CREW- PRAM model using only the basic parallel algorithms Theorem 1 An undirected graph has an Eulerian [6, 7]. Namely, the proposed algorithm initially deter- path if and only if it is connected and the number of mines whether or not a given graph has an Eulerian vertices with odd-degree is 2 or 0. path. Next, we divide each vertex into several new vertices which have a maximum of 2 edge degrees The proposed parallel algorithm is composed of the [3, 4, 5] and connect between new vertices by edges. following two stages:

ISSN: 1790-2769 82 ISBN: 978-960-474-034-5 Proceedings of the 13th WSEAS International Conference on APPLIED MATHEMATICS (MATH'08)

5 procedure Stage-1(V1[1..m],V2[1..m]); { Parallel algorithm for Stage-1 } begin 2 1 { Step[1.1] Calculate the number of degrees for each vertex } for e := 1 to m in parallel do begin V1V2[2(e-1)+1] ← V1[e]; 6 V1V2[2e] ← V2[e] 3 end; { Sort vertex numbers with index in ascending order } Para_Sort( V1V2[1..2m]); 4 { Find the boundaries of different vertex numbers } for e := 1 to 2m-1 in parallel do 1 2 3 4 5 6 7 8 9 if V1V2[e] ≠ V1V2[e+1] then B[V1V2[e]] ← e; V1 1 1 1 1 2 2 2 3 4 B[V1V2[2m]] ← 2m; V2 2 3 5 6 3 4 5 4 6 D[1] ← B[1]; for v := 2 to n in parallel do D[v] ← B[v] - B[v-1]; Figure 1: Example of given undirected connected {Find the number of degrees for each vertex } graph and array representation of edges. { Step[1.2] Determine whether or not a given graph has an Eulerian path } for v := 1 to n in parallel do DD[v] ← D[v] mod 2; • Stage 1: Determine whether or not a given graph Para_Prefixsum( DD[1..n]); { Calculate partial sum in DD[n] } if ( DD[n] ≠ 0 AND DD[n] ≠ 2 ) then exit(0); is an Eulerian path if ( DD[n] = 0) then begin Delete the head edge and decrease the number of degrees • Stage 2: Divide each vertex into several new ver- for vertices of the deleted edge tices and find one Eulerian path end; DD[0] ← 0; for v := 1 to n in parallel do An undirected connected graph with n vertices if ( DD[v] = 1 AND DD[v-1] ≠ 1) then odd ← v and m edges is given in the form of two arrays end; V1[1..m] and V2[1..m], which indicate the vertex numbers. Each i-th undirected edge is represented by Figure 2: Stage-1 Algorithm a pair of V1[i] and V2[i], where V1[i] ≦ V2[i] (1 ≦ i ≦ m). Moreover vertices in the array V1 are sorted in order of the vertex number. An example of an undi- rected connected graph and its array representation of Parallel Merge Sort [8]. At this time, the values undirected edges is shown in Figure 1. in V1 and V2 are placed alternately in V1V2 be- In the following subsections the details of Stages fore sorted. Next, in the array V1V2 we find the 1 and 2 are respectively described. boundaries of different vertex numbers and store an index number every vertex number in an array B[1..n]. Subsequently, we can obtain the num- 2.1 Stage 1: Determine whether or not a ber of degrees for each vertex in an array D[1..n] given graph is an Eulerian path by calculating the difference between B[v] and In this stage, we initially examine the number of de- B[v-1] where v is each vertex number. This step grees for each vertex in order to determine whether requires O(log m) time and O(m) processors be- or not a given graph is an Eulerian path. Subse- cause the vertex numbers are sorted. quently, we decide if a given graph satisfies Theorem 1. Namely we examine the number of vertices with Step 1.2 Determine whether or not a given graph odd-degree 2 or 0. If so, a given graph has an Eulerian has an Eulerian path path, otherwise our algorithm stops. In the case that A graph which has an Eulerian path contains 0 degrees of all vertices are even, one edge is deleted or 2 vertices with odd-degree. So, we divide the then two vertices with odd-degree appear. In the fol- number of degrees for each vertex by 2 and find lowing, we proceed with the processing based on a the remainder. Subsequently, we calculate the graph having two vertices with odd-degree. This stage sum of the remainders using Parallel Prefixsum is composed of the following two steps: Algorithm [6]. If the sum of the remainders is 0 or 2, then a given graph has an Eulerian path. If Step 1.1 Calculate the number of degrees for each the sum of the remainders is 0, namely, the de- vertex grees of all vertices are even, we delete the head At first, we concatenate two input arrays V1 and edge in the input array temporally and store the V2 into a single array V1V2[1..2m] and sort this vertex number of which the vertex has a smaller array with index numbers in ascending order by number of vertices with odd-degree in the vari-

ISSN: 1790-2769 83 ISBN: 978-960-474-034-5 Proceedings of the 13th WSEAS International Conference on APPLIED MATHEMATICS (MATH'08)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ray D by 2, in order to divide each given ver- V1V2 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 6 6 tex (we call it the old vertex) into some new ver-

1 2 3 4 5 6 tices with a maximum of 2 edge degrees. When B 4 8 11 14 16 18 vertices have odd degrees, round up fractions. These values also represent the number of new 1 2 3 4 5 6 vertices generated from each old vertex. Subse- D 4 4 3 3 2 2 quently, we calculate the prefix sum in the array D and store the values in an array S[1..n]. The Figure 3: The number of degrees for each vertex. newly generated vertices are serially numbered to retain the same order of the old vertex num- ber in the given graph. Here a correspondence table N[1..S[n]], in which the new vertex num- able odd. Subsequently, we proceed with the fol- ber u corresponds to the old vertex number v is lowing processing as the graph has 2 odd-degree prepared, where S[n] is the total number of all vertices. This step requires O(log n) time and new vertices after division and S[n] = O(n+m). O(n) processors. In the array N, the values of elements are the old vertex numbers and the indices are the new Therefore, the complexity of Stage 1 requires vertex numbers. The variable odd is updated O(log m) O(m) time and processors. The algorithm by the new vertex number. This step requires of Stage 1 is shown by the pseudo-codes in Figure 2. O(log(n + m)) time and O(n + m) processors. As a result of Stage 1, the sorted input array V1V2, the boundary edge array B and the degree of Step 2.2 Substitute the old vertex numbers in the each vertex array D are illustrated as Figure 3. In the given graph for the new vertex numbers array D, vertices which have an odd-degree are ver- The old vertex numbers in Figure 1 are substi- tex number 3 and 4. Therefore, a given graph has an tuted for the new vertex numbers. Firstly, the ele- Eulerian path. In the variable odd, 3 is stored. ments, which are arranged by the old vertex num- ber at Step [1.1] in the array V1V2 are simulta- 2.2 Stage 2: Divide each vertex into several neously substituted for the new vertex numbers new vertices and find one Eulerian path using the array B at Step [1.1] and the array S at There is Fleury’s algorithm as a sequential algorithm Step [2.1] as follows: for finding an Eulerian path. This algorithm avoids the edge becoming a , as is possible in the case for e:=1 to 2m in parallel do of an edge selection of multiple branches. if odd=e then We propose an algorithm which resembles NV1V1[e] Fleury’s algorithm when linking edges. Firstly, we di- S[V1V2[e]-1] vide each vertex into several new vertices which have +[e+1-B[V1V2[e]-1]]/2 a maximum of 2 edge degrees and replace the old ver- else NV1V2[e] tex numbers in the input array by the divided new ver- S[V1V2[e]-1] tex numbers. Subsequently, we find one Eulerian path +[e-B[V1V2[e]-1]]/2; in the given graph. When the old vertex numbers are replaced by the where the array NV1V2 shows the result of sub- new vertex numbers, the edges are linked in descend- stitution for the new vertex numbers. ing order to the number of degrees of each vertex be- At this time, the same old vertex numbers are re- cause the vertex numbers in an input graph are as- placed by the same new vertex number two by signed in descending order to the number of degrees two from the top. However, concerning the odd of each vertex. Therefore this algorithm resembles vertex, which has the number stored in the vari- Fleury’s algorithm. This stage is composed of the fol- able odd, only the top vertex number is initially lowing two steps: replaced by the new vertex number and then by the same new vertex number two by two. Step 2.1 Make a correspondence table in which Secondly, the old vertex numbers in the given the new vertex number corresponds to the old input arrays V1 and V2 are substituted for the vertex number new vertex numbers by referring to index num- Firstly, we divide the values of elements which bers in the array NV1V2, whereupon one Eule- represent edge degrees of each vertex in the ar- rian path will be constructed. If the given graph

ISSN: 1790-2769 84 ISBN: 978-960-474-034-5 Proceedings of the 13th WSEAS International Conference on APPLIED MATHEMATICS (MATH'08)

procedure Stage-2( var D[1..n], B[1..n], V1V2[1..2m]); 1 2 3 4 5 6 { Parallel algorithm for Stage-2 } D 2 2 2 2 1 1 begin { Step[2.1] 1 2 3 4 5 6 Make a correspondence table which corresponds 2 4 6 8 9 10 the new/old vertex numbers } S for v := 1 to m in parallel do D[v] ← D[v]/2; { the number of new vertices } 1 2 3 4 5 6 7 8 9 10 { Calculate the prefix sum of new vertices } Para_Prefixsum( D[1..n], S[1..n]); N 1 1 2 2 3 3 4 4 5 6 N[1] ← 1; { Create the correspondence table N } for v := 2 to n in parallel do begin Figure 5: The correspondence table N u ← S[v-1] + 1; N[u] ← v end; { If N[u]=0 then fill in the old vertex numbers } Boadcast-1(N[1..S[n]); 5 { Step[2.2] ᡪ Substitute the old vertex number for the new one } B[0] ← 0; S[0] ← 0; for e := 1 to 2m in parallel do ᡤ ᡢ if odd = e

then NV1V2[e] ← S[V1V2[e]-1] 2 ᡥ ᡣ 1 + [e+1 - B[V1V2[e] - 1] ]/2 else NV1V2[e] ← S[V1V2[e]-1]

+ [e - B[V1V2[e] - 1] ]/2; ᡦ The old vertex numbers in the input arrays V1 and V2 3 ᡧ ᡫ are substituted for the new vertex numbers by referring 6 to index numbers in the array NV1V2. If the given graph has even degrees of all vertices, restore the deleted edge and substitute the old vertex ᡨ ᡩ numbers of its edge for the new vertex numbers. end; 4

Figure 4: Stage-2 Algorithm Figure 6: Divided new vertices.

has even degrees of all vertices, we restore the deleted edge and substitute the old vertex num- bers of its edge for the new vertex numbers. This number 5 because there is 3 in the variable odd. Sub- step requires O(1) time and O(m) processors. sequently, the new input arrays V1 and V2 shows the result of substitution of the old vertex numbers in the Therefore, the complexity of Stage 2 requires input arrays for the new vertex numbers using indices O(log(n + m)) time and O(n + m) processors. The in the array NV1V2. algorithm of Stage 2 is shown by the pseudo-codes in According to Figure 7, Figure 8 is illustrated and Figure 4. we obtain one Eulerian path such as (5)-(1)-(3)-(6)- As a result of Step [2.1], the number of new ver- (7)-(4)-(9)-(2)-(10)-(8). Namely in the given graph, tices D, the prefix sum of new vertices S and the cor- we obtain one Eulerian path such as 3-1-2-3-4-2-5-1- respondence table N are illustrated as Figure 5. In the 6-4. correspondence table N, the values of elements rep- resent the old vertex numbers and the index numbers represent the new vertex numbers. Therefore, the old 3 Conclusion vertices in the given graph are divided into some new vertices as Figure 6. In this figure, the number in- We have proposed a parallel algorithm for finding an cluded in each circle is the new vertex number and the Eulerlian path in an undirected graph using only the number outside each rectangular area is the old vertex basic parallel algorithms. This parallel algorithm re- number. quires O(log(n + m)) time and O(n + m) processors In Step [2.2], Figure 7 shows the array NV1V2 on a CREW-PRAM model. and the new input arrays V1 and V2. The array The complexity of Fleury’s algorithm, which is NV1V2 shows the result of substitution of the old a sequential algorithm for finding an Eulerian path, vertex numbers in the array V1V2 for the new ver- is O(n + m) and a cost of the proposed parallel al- tex numbers using the boundary edge array B and the gorithm is O((n + m) log(n + m)). Therefore, our prefix sum array S. With reference to the old vertex algorithm is not an optimal parallel algorithm. How- number 3, only one is substituted for the new vertex ever, it is easy to optimize our algorithm using Brent’s

ISSN: 1790-2769 85 ISBN: 978-960-474-034-5 Proceedings of the 13th WSEAS International Conference on APPLIED MATHEMATICS (MATH'08)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [7] Xavier, C. and Iyegar, S.S.: Introduction to Par- NV1V2 1 1 2 2 3 3 4 4 5 6 6 7 7 8 9 9 10 10 allel Algorithms, pp.108-140, Wiley-inter sci- ence (1998). 1 2 3 4 5 6 7 8 9 [8] Cole, R. ”Parallel Merge Sort,” SIAM J. Com- V1 1 1 2 2 3 4 4 6 8 put., Vol.17, No.4, pp.770-785 (1988). V2 3 5 9 10 6 7 9 7 10

Figure 7: New input array.

5ᡪ ᡤ ᡢ ᡥ 2 ᡣ 1 ᡦ

3 ᡧ ᡫ 6 ᡨ ᡩ 4

Figure 8: One Eulerian path.

theorem [6]. Namely, it is possible by assigning the number of log(n + m) elements to each processor be- cause we use an array as a data structure.

References: [1] Ichimori, T.: (in Japanese) pp.59- 72, Kyoritu Shuppan(2002). [2] Gibbons, A.: Algorithmic Graph Theory, pp.152-162, Cambridge University Press, Cam- bridge (1985). [3] Tada, A., Migita M. and Nakamura, R. ”Parallel Topological Sorting Algorithm,” (in Japanese) IPSJ Journal, Vol.45, No.4, pp.1102-1111 (2004). [4] Migita, M., Tada, A., Itokawa, T. and Nakamura, R. ”Parallel Algorithm for Determining Critical Paths in PERT Chart,” (in Japanese) IPSJ Jour- nal, Vol.47, No.7, pp.2212-2223 (2006). [5] Tada, A., Mukai, E., Migita, M. and Itokawa, T. ”Parallel Algorithm for Finding the Mini- mum Edges to build a Strongly Connected Di- rected Graph,” 12th WSEAS Int. Conf. on Ap- plied Mathematics, pp.100-104 (2007). [6] Gibbons A. and Rytter, W.: Efficient parallel al- gorithm, pp.6-18, Cambridge University Press, Cambridge (1988).

ISSN: 1790-2769 86 ISBN: 978-960-474-034-5