Algorithm 797: Fortran Subroutines for Approximate Solution of Graph Planarization Problems Using GRASP CELSO C. RIBEIRO Catholic University of Rio de Janeiro and MAURICIO G. C. RESENDE AT&T Labs Research
We describe Fortran subroutines for finding approximate solutions of the maximum planar subgraph problem (graph planarization) using a Greedy Randomized Adaptive Search Procedure (GRASP). The design and implementation of the code are described in detail. Computational results with the subroutines illustrate the quality of solutions found as a function of number of GRASP iterations. Categories and Subject Descriptors: D.3.2 [Programming Languages]: Language Classifications—Fortran 77; G.2.1 [Discrete Mathematics]: Combinatorics—Combinatorial algorithms; G.m [Mathematics of Computing]: Miscellaneous General Terms: Algorithms, Performance Additional Key Words and Phrases: Automatic graph drawing, combinatorial optimization, graph planarization, GRASP, local search
1. INTRODUCTION A graph is said to be planar if it can be drawn on the plane in such a way that no two of its edges cross. Given a graph G 5 ~ V, E ! with vertex set V and edge set E , the objective of graph planarization is to find a minimum cardinality subset of edges F # E such that the graph G9 5 ~ V, E\F ! resulting from the removal of the edges F from G is planar. This problem is also known as the maximum planar subgraph problem. A maximal planar
Authors’ addresses: C. C. Ribeiro, Department of Computer Science, Catholic University of Rio de Janeiro, Rio de Janeiro, RJ 22453-900, Brazil; email:
[email protected]; M. G. C. Resende, Information Sciences Research Center, AT&T Labs Research, Room C241, 180 Park Avenue, Florham Park, NJ 07932; email:
[email protected]. Permission to make digital / hard copy of part or all of this work for personal or classroom use is granted without fee provided that the copies are not made or distributed for profit or commercial advantage, the copyright notice, the title of the publication, and its date appear, and notice is given that copying is by permission of the ACM, Inc. To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specific permission and / or a fee. © 2000 ACM 0098-3500/99/0900 –0341 $5.00 ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999, Pages 341–352.
342
•
C. C. Ribeiro and M. G. C. Resende
subgraph is a planar subgraph G9 5 ~ V9, E9 ! of G 5 ~ V, E ! , such that the addition of any edge e [ E\E9 to G9 destroys the planarity of the subgraph. Graph planarization is known to be NP-hard [Liu and Geldmacher 1977], and most algorithms to date attempt to find good approximate solutions. Several graph planarization heuristics have been proposed in the literature. Jayakumar et al. [1989] describe two O ~? V ? 2 ! planarization algorithms, based on the Lempel, Even, and Cederbaum planarity testing algorithm [Lempel et al. 1966]. Cai et al. [1993] proposed an O ~? E ? log? V ?! algorithm for finding a maximal planar subgraph based on the Hopcroft and Tarjan planarity testing algorithm [Hopcroft and Tarjan 1974]. Another O ~? E ? log? V ?! algorithm was also proposed by Di Battista and Tamassia [1989], based on an O ~ log? V ?! time-per-operation strategy to the problem of maintaining a planar graph under edge additions. Takefuji and Lee [1989] described a parallel heuristic for planarization based on neural networks. They use an arbitrary sequencing of the vertices, placing them along a line, followed by the use of a neural network for determining two sets of edges that may be represented without crossings above and below that line, respectively. The two-phase approach of Takefuji and Lee was extended and improved by Goldschmidt and Takvorian [1994]. Jünger and Mutzel [1996] designed an exact branch-and-cut algorithm for the maximum planar subgraph problem using facet-defining inequalities. A heuristic based on the branch-and-cut algorithm stops with a feasible, though not necessarily optimal, solution. Resende and Ribeiro [1997] further extended the two-phase approach, applying the framework of the GRASP (Greedy Randomized Adaptive Search Procedure) metaheuristic. The Fortran subroutines described in this article are an implementation of that GRASP. We next describe the organization of this article. Section 2 describes the GRASP for graph planarization. The Fortran implementation is outlined in Section 3, and its usage is described in Section 4. In Section 5, we present some computational results illustrating the effectiveness of this approximate solution procedure. 2. GRASP FOR GRAPH PLANARIZATION A GRASP [Feo and Resende 1995] is an iterative process, where each iteration consists of two phases: construction and local search. The construction phase builds a feasible solution, whose neighborhood is explored by local search. The best solution over all iterations is returned as the result. Figure 1 illustrates a generic GRASP implementation in pseudocode. The GRASP takes, as input, parameters for setting the candidate list size, maximum number of iterations, and the seed for the randomnumber generator. After reading the instance data (line 1), the iterations are carried out in lines 2– 6. Each iteration consists of the construction phase (line 3), the local search phase (line 4), and, if necessary, the incumbent solution update (line 5). ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.
Algorithm 797: Approximate Solution of Graph Planarization Problems
Fig. 1.
•
343
A generic GRASP pseudocode.
In the graph planarization problem, the construction phase (see Figure 2) of each GRASP iteration consists in devising a sequence P of the set of vertices V of the input graph G . The vertices of G are placed on a line according to this sequence P . The procedure takes as input the graph G 5 ~ V, E ! to be planarized, the restricted candidate list (RCL) parameter a (0 # a # 1 ), and a seed for the pseudorandom-number generator. Let degG ~ v ! be the degree of vertex v with respect to G , and let d 5 min $ degG ~ v !% and d# 5 max $ degG ~ v !% . The first vertex in the sev[V
v[V
quence is determined in lines 1–3, where all vertices having degree in the range @ d, a ~ d# 2 d ! 1 d # are placed in the RCL and where a single vertex
is selected at random from the list. The working vertex set 9 and graph G 1 are defined in line 4. The loop from line 5 to 15 determines the sequence of the remaining ? V ? 2 1 vertices. To assign the k th vertex (iteration k of the loop) two cases can occur. Define G k to be the graph induced on G by V\ $ v 1 , v 2 , · · ·, v k % . Let ADJG k21 ~ v k21 ! be the set of vertices of G k21 adjacent to v k21 in G . The RCL is made up of all vertices in ADJG k21 ~ v k21 ! having degree in the range @ d, a ~ d# 2 d ! 1 d # in G k . Otherwise, if ADJG ~ v k21 ! 5 0 , the RCL is k21
made up of all unselected vertices having degree in the range @ d, a ~ d# 2 d ! 1 d # in G k . In line 13, the k th vertex in the sequence is determined by selecting a vertex, at random, from the RCL. The greedy function is adapted in line 14 where the working vertex set and graph are updated. The vertex sequence P 5 ~ v 1 , v 2 , . . . , v ? V ? ! is returned, where p ~ v ! denotes the relative position of vertex v [ V within vertex sequence P . Furthermore, let e 1 5 ~ a, b ! and e 2 5 ~ c, d ! be two edges of G , such that, without loss of generality, p ~ a ! , p ~ b ! and p ~ c ! , p ~ d ! . These edges are said to cross with respect to sequence P if p ~ a ! , p ~ c ! , p ~ b ! , p ~ d ! or p~c! , p~a! , p~d! , p~b!. The vertex ordering obtained at the end of the construction phase may be improved by attempting to reduce the number of crossing edges by locally searching a neighborhood of the current vertex sequence prior to the second phase. We next describe such a local search procedure. To do so, let us ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.
344
•
C. C. Ribeiro and M. G. C. Resende
Fig. 2.
Pseudocode of GRASP construction phase.
Fig. 3.
Pseudocode of GRASP local search phase.
define the neighborhood 1 ~ P ! of the vertex sequence P to be formed by all vertex sequences P9 differing in exactly two positions, i.e.,
1~P! 5 $P9 5 ~v91, v92, . . . , v9?V?! : v9i 5 vi, @i Þ j, k, v9j 5 vk, v9k 5 vj%. Let x ~ P ! be the number of edge-pairs that cross with respect to vertex sequence P . The pseudocode in Figure 3 describes the local search procedure used in the GRASP. In our implementation, we use a slightly more restricted neighborhood, in which only consecutive vertices can be exchanged. Let H 5 ~ E, I ! be a graph where each of its vertices corresponds to an edge of the input graph G . Vertices e 1 and e 2 of H are connected by an edge if the corresponding edges of G cross with respect to sequence P . A graph is called an overlap graph if its vertices can be placed in one-to-one correspondence with a family of intervals on a line. Two intervals are said to overlap if they cross and none is contained in the other. Two vertices of the overlap graph are connected by an edge if and only if their corresponding intervals overlap. Hence, the graph H as constructed above is the overlap graph associated with the representation of G defined by sequence P . ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.
Algorithm 797: Approximate Solution of Graph Planarization Problems
Fig. 4.
•
345
Pseudocode of the two-coloring procedure.
Following Goldschmidt and Takvorian [1994], the next phase of our procedure consists in two-coloring a maximum number of the vertices of the overlap graph H such that each of the two color classes @ (blue) and 5 (red) forms an independent set. Equivalently, the second phase seeks a maximum induced bipartite subgraph of the overlap graph H , i.e., a bipartite subgraph having the largest number of vertices. This problem is equivalent to drawing the edges of the input graph G above or below the line where its vertices have been placed, according to sequence P . Since the decision version of the problem of finding a maximum induced bipartite subgraph of an overlap graph is NP-complete [Sarrafzadeh and Lee 1989], a greedy algorithm is used in GT to construct a maximal induced bipartite subgraph of the overlap graph. This algorithm finds a maximum independent set @ # E of the overlap graph H 5 ~ E, I ! , reduces the overlap graph by removing from it the vertices in @ and all edges incident to vertices in @ , and then finds a maximum independent set 5 # E\@ in the remaining overlap graph H9 5 ~ E\@, I9 ! . The two independent sets obtained induce a bipartite subgraph of the original overlap graph, not necessarily with a maximum number of vertices. This procedure has polynomial time complexity, since finding the maximum independent set of an overlap graph has been shown by Gavril [1973] to be polynomially solvable in time O ~? E 3 ?! , where ? E ? is the number of vertices of the overlap graph H 5 ~ E, I ! (see also Golumbic [1980] for another description of this algorithm). Pseudocode for the two-coloring procedure is given in Figure 4. The two-coloring heuristic outputs three edge sets: @ (blue edges), 5 (red edges), and 3 (the remaining edges, which we refer to as the pale edges). By construction, @ , 5 , and 3 are such that no red or pale edge can be colored blue. Likewise, pale edges cannot be colored red. However, if there exists a pale edge p [ 3 such that all blue edges that cross with p (let @ ˆ p # @ be the set of those blue edges) do not cross with any red edge r [ 5 , then all blue edges b [ @ ˆ p can be colored red and p can be colored blue. This reassignment of color classes increases the size of the planar subgraph by one edge. Figure 5 shows pseudocode for procedure EnlargePlanar that seeks pale and blue edges allowing the above color class reassignment and enlarges the planar subgraph when such edges are encountered. The pale edges are scanned in do loop 1–12, while loop 3–10 ˆ p , whose elements are saved in scans the blue edges to construct the set @ ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.
346
•
C. C. Ribeiro and M. G. C. Resende
Fig. 5.
Pseudocode of local search procedure to enlarge planar subgraph.
line 5. If a blue edge b [ @ ˆ p crosses any red edge, the set @ ˆ p is discarded (line 7), and a new pale edge is scanned. If no edge b [ @ ˆ p crosses red ˆ p are colored red, and the pale edge p is edges, then all blue edges b [ @ colored blue in line 11. This procedure has time complexity O ~? E 3 ?! . By incorporating the two-coloring heuristic and the graph-enlarging procedure to the construction and local search phases, we have a GRASP for graph planarization, whose pseudocode is given in Figure 6. The procedure is repeated MaxIter times. In each iteration, a greedy randomized solution (vertex sequence P ) is constructed in line 2. In line 3, the local search attempts to produce a vertex sequence that has fewer edge-pair crossings than the one generated in line 2. The vertex sequence P is given as input to the two-coloring heuristic in line 4 to produce a planar subgraph of G . In lines 5 and 6 the procedure to enlarge the current planar subgraph is applied. If the cardinality of the edge-set of the subgraph found in line 6 is the largest found so far, it is recorded in line 7. 3. DESIGN AND IMPLEMENTATION OF THE SUBROUTINES The code is implemented in ANSI standard Fortran 77, and runs without any modifications on UNIX environments. It has also run on other environments with little or no change. Input, output, variable, and array declarations and parameter settings are all performed in a separate driver module. Therefore, usage and experimentation with the code are relatively simple. The optimization package consists of three files: Makefile, driver.f, and gmpsg.f. Also included in the distribution is a sample input data file, sample.dat. 4. USAGE OF THE SUBROUTINES The subroutines in file gmpsg.f carry out the approximate optimization of the maximum planar subgraph problem. The user interface with them is ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.
Algorithm 797: Approximate Solution of Graph Planarization Problems
Fig. 6.
•
347
Pseudocode of GRASP for graph planarization.
subroutine gmpsg, which must be called from a driver program. In addition to a number of auxiliary arrays, the driver passes (1) the following representation of the input graph n: Number of nodes in graph m: Number of arcs in graph arc1: Integer array where arc1(i) is node 1 of arc i arc2: Integer array where arc2(i) is node 2 of arc i and (2) the following array dimensions: nmax: Dimension of number of nodes declared in driver.f, mmax: Dimension of number of arcs declared in driver.f. The sample driver program for gmpsg included in the distribution (driver.f) is set for problems of dimension n # 200 vertices and m # 1600 edges. The input/output parameters that define Fortran input/output devices are set to the standard values in 5 5 and iout 5 6 . These parameters can be set by the user for problems of different dimension or if an alternate input or output device is required. All variables and arrays needed by subroutine gmpsg are defined in the main program in driver.f. Subroutines readp and outsol, also provided in driver.f, are examples of code that can be used for input and output, respectively. The driver program calls readp, which reads the input data and returns an error condition errcnd 5 0 if no error in the input data is detected or errcnd 5 1 if the declared number of nodes n . nmax, errcnd 5 2 if the declared number of arcs m . mmax, or errcnd 5 9 if the number of arcs provided in the input is less than the declared number of arcs m . If errcnd . 0 , then driver.f calls subroutine errmsg (provided in file gmpsg.f) which prints the error message, and execution is halted. Five parameters that control the execution of the algorithm need to be set before the optimization module is called: alpha, the RCL parameter, is a real number that must satisfy 0 # alpha # 1 ; look4, a stopping parameter that forces GRASP to stop if a planar subgraph of size at least look4 is ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.
348
•
C. C. Ribeiro and M. G. C. Resende
Fig. 7.
Fig. 8.
Input file of graph planarization instance g1.
Sample output of driver for graph planarization instance g1.
found, is an integer that must satisfy 0 # look4 # m ; maxitr, the maximum number of GRASP iterations, is an integer such that maxitr . 0 ; prttyp, the output option parameter, is an integer that is set to 0 (silent run; gmpsg does not write anything), 1 (gmpsg prints out solution improvements), or 2 (gmpsg prints out the solution found in each GRASP iteration); and seed, the pseudorandom-number generator seed, an integer such that 1 # seed # 2 31 2 1 . The default settings for alpha, look4, maxitr, prttyp, and seed are, respectively, 0.1 , m , 2048 , 1 , and 270001 . If errcnd 5 0 is returned by readp, then driver.f calls the optimization subroutine gmpsg, which attempts to find a large planar subgraph of the input graph. Subroutine gmpsg returns error condition errcnd indicating the status of the optimization. Error condition errcnd can have the following values:
errcnd 5 1 if a n . nmax; errcnd 5 2 if a m . mmax; ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.
Algorithm 797: Approximate Solution of Graph Planarization Problems
•
349
Fig. 9. Incumbent solutions as a function of GRASP iteration. Dotted line is average incumbent solution.
errcnd 5 3 if an input node is less than 1 or greater than m ; errcnd 5 4 if alpha , 0 or alpha . 1 ; errcnd 5 5 if look4 , 1 or look4 . m ; errcnd 5 6 if maxitr , 1 ; errcnd 5 7 if prttyp Þ 0,1,2 ; errcnd 5 8 if seed , 1 or seed . 2147483647 . As an example, consider the instance g1 of dimension 10 vertices by 22 arcs (of Goldschmidt and Takvorian [1994]) with input file shown in Figure 7. Running the driver program using the default settings on instance g1 ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.
350
•
Table I.
C. C. Ribeiro and M. G. C. Resende Number of Arcs in Incumbent Solution as a Function of GRASP Iteration: Part 1 GRASP Iteration
Name
2
8
32
128
512
2048
g2.cimi g13 g14 g15 g16 g17
160 129 141 142 184 218
160 130 141 143 187 225
165 131 143 144 190 231
165 133 143 144 190 231
165 134 143 144 191 231
165 135 143 144 193 234
rg50.1 rg50.2 rg50.3 rg50.4 rg50.5
83 91 98 98 98
87 91 98 98 101
87 92 100 100 102
87 92 100 100 102
90 94 101 100 105
90 95 103 101 105
rg75.1 rg75.2 rg75.3 rg75.4 rg75.5
122 126 129 132 131
126 127 129 134 136
126 128 130 137 139
126 129 130 137 139
126 130 131 138 141
126 131 132 140 143
rg100.1 rg100.2 rg100.3 rg100.4 rg100.5
150 158 153 164 172
153 159 161 169 178
156 163 162 170 182
159 163 162 172 187
159 163 163 173 187
161 164 164 173 187
rg150.1 rg150.2 rg150.3 rg150.4 rg150.5
217 218 226 226 232
217 219 230 228 234
222 219 231 232 239
222 221 234 235 239
225 223 234 235 239
226 224 237 236 240
rg200.1 rg200.2 rg200.3 rg200.4 rg200.5
276 277 293 302 300
276 277 296 302 303
278 279 303 308 307
281 283 303 308 307
281 283 303 309 311
284 285 304 310 311
produces the output shown in Figure 8. The output lists each iteration for which an improving solution was found and describes the best solution found by listing the vertex permutation, and the blue and red arcs. 5. COMPUTATIONAL RESULTS In this section, we illustrate the effectiveness of the subroutines by running the planarization procedure on a subset of the test problems used by Resende and Ribeiro [1997]. The experiment was limited to all of those test problems having at least 50 and at most 200 nodes with at most 750 arcs (a total of 51 instances). Each run of GRASP was done using the default ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.
Algorithm 797: Approximate Solution of Graph Planarization Problems Table II.
351
•
Number of Arcs in Incumbent Solution as a Function of GRASP Iteration: Part 2 GRASP Iteration
Name
2
8
32
128
512
2048
tg100.1 tg100.2 tg100.3 tg100.4 tg100.5 tg100.6 tg100.7 tg100.8 tg100.9 tg100.10
239 227 237 235 241 231 219 227 230 219
247 247 248 244 244 231 229 232 235 229
257 252 252 249 244 237 240 236 235 230
257 253 252 254 250 242 244 247 237 235
258 265 256 254 252 244 244 247 239 236
258 265 262 262 254 253 246 248 241 244
tg200.1 tg200.2 tg200.3 tg200.4 tg200.5 tg200.6 tg200.7 tg200.8 tg200.9 tg200.10
463 435 463 462 448 462 426 443 434 443
463 460 478 469 458 462 431 449 451 443
477 471 485 469 463 462 446 458 451 443
477 478 495 476 473 468 450 461 453 461
491 480 502 488 474 477 452 492 461 468
491 488 504 493 486 482 466 492 465 468
parameter settings as defined in file driver.f of the distribution, i.e., alpha 5 0.1 , look4 5 m , maxitr 5 2048 , prttyp 5 1 , and seed 5 270001 . For each problem considered in the experiment, Tables I and II show the size (number of arcs) of the incumbent solution as a function of GRASP iterations. The incumbent solutions at iterations 2, 8, 32, 128, 512, and 2048 are listed. Figure 9 shows all incumbents at iterations 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, and 2048, as well as the average values of the incumbent solutions at those iterations (dotted line). REFERENCES CAI, J., HAN, X., AND TARJAN, R. E. 1993. An O(m log n)-time algorithm for the maximal planar subgraph problem. SIAM J. Comput. 22, 6 (Dec. 1993), 1142–1162. DI BATTISTA, G. AND TAMASSIA, R. 1989. Incremental planarity testing. In Proceedings of the 30th IEEE Symposium on Foundations of Computer Science (FOCS ’89, Research Triangle Park, NC, Oct. 30 –Nov. 1) IEEE Computer Society Press, Los Alamitos, CA, 436 – 441. FEO, T. AND RESENDE, M. 1995. Greedy randomized adaptive search procedures. J. Global Optim. 6, 109 –133. GAVRIL, F. 1973. Algorithms for a maximum clique and a maximum independent set of a circle graph. Networks 3, 261–273. GOLDSCHMIDT, O. AND TAKVORIAN, A. 1994. An efficient graph planarization two-phase heuristic. Networks 24, 69 –73. GOLUMBIC, M. 1980. Algorithmic Graph Theory and Perfect Graphs. Academic Press, Inc., New York, NY. HOPCROFT, J. AND TARJAN, R. 1974. Efficient planarity testing. J. ACM 21, 4 (Oct.), 549 –568. ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.
352
•
C. C. Ribeiro and M. G. C. Resende
JAYAKUMAR, R., THULASIRAMAN, K., AND SWAMI, N. 1989. O ~ n 2 ! algorithms for graph planarization. IEEE Trans. Comput.-Aided Des. 8, 257–267. JÜNGER, M. AND MUTZEL, P. 1996. Maximum planar subgraphs and nice embeddings: Practical layout tools. Algorithmica 16, 33–59. LEMPEL, A., EVEN, S., AND CEDARBAUM, I. 1966. An algorithm for planarity testing of graphs. In Proceedings of the International Symposium on Theory of Graphs (Rome) Gordon and Breach Science Publishers, Inc., Langhorn, PA, 215–232. LIU, P. AND GELDMACHER, R. 1977. On the deletion of nonplanar edges of a graph. In Proceedings of the 10th Southeastern Conference on Combinatorics, Graph Theory, and Computing (Boca Raton, FL) 727–738. RESENDE, M. G. C. AND RIBEIRO, C. C. 1997. A GRASP for graph planarization. Networks 29, 173–189. SARRAFZADEH, M. AND LEE, D. 1989. A new approach to topological via minimization. IEEE Trans. Comput.-Aided Des. 8, 890 –900. TAKEFUJI, Y. AND LEE, K. 1989. A near-optimum parallel planarization algorithm. Science 245, 1221–1223. Received: September 1997;
revised: March 1999;
accepted: May 1999
ACM Transactions on Mathematical Software, Vol. 25, No. 3, September 1999.