An efficient implementation of a planarity testing and maximal planar subgraph algorithm by Zhongyuan Li A thesis submitted in partial fulfillment of the requirements for the degree of Master of Science in Computer Science Montana State University © Copyright by Zhongyuan Li (1996) Abstract: This thesis presents efficient implementations of a planarity testing and a maximal planar subgraph algorithm. The algorithms are based on the methods of Shih and Hsu [10] and Hsu [9]. The algorithms use the vertex addition method. The key of the algorithms is to add vertices according to a postordering obtained from a depth-first search tree. The algorithms run in linear time. The implementations were developed based on Shih and Hsu’s methods, and were implemented in the C language. Empirical analysis shows that the programs run in linear time. The maximal planar subgraph algorithm was also compared with several other algorithms. In two tables, the sizes of the maximal planar subgraphs obtained from several special graphs and twenty random graphs are listed. A n E fficient Im p lem en ta tio n o f a P la n a r ity T estin g and M axim a l P lan ar Subgraph A lg o rith m by Zhongyuan Li A thesis submitted in partial fulfillment of the requirements for the degree of M aster of Science in Computer Science Montana State University Bozeman, Montana July 1996 ,V 3 7 8 ii UU-IScICL A PPRO V A L of a thesis submitted by Zhongyuan Li This thesis has been read by each member of the thesis committee and has been found to be satisfactory regarding content, English usage, format, citations, bibliographic style, and consistency, and is ready for submission to the College of Graduate Studies. ~ Date Chairperson, Graduate Committee Approved for the Major Department Date Head/Major Dejwtment Approved for the College of Graduate Studies Date Graduate fJean Ill STATEM ENT OF PERM ISSION TO USE In presenting this thesis in partial fulfillment of the requirements for a master’s degree at Montana State University, I agree that the Library shall make it available to borrowers under rules of the Library. If I have indicated my intention to copyright this thesis by including a copyright notice page, copying is allowable only for scholarly purposes, con­ sistent with “fair use” as prescribed in the U.S. Copyright Law. Requests for permission for extended quotation from or reproduction of this thesis in whole or in parts may be granted only by the copyright holder. — Signature Date iu U d i x m -Z ACKNOW LEDGM ENTS I would like to take this opportunity to thank my graduate committee mem­ bers, Dr. Robert Cimikowski, Dr. Denbigh Starkey, and Prof. Anne DeFrance, and the rest of the faculty members from the Department of Computer Science for their help and guidance during my graduate program. I would especially like to thank my advisor, Dr. Robert Cimikowski, for his encouragement and support during the whole work. C ontents Table of Contents v List of Tables vii List of Figures viii Abstract x 1 Introduction I 2 3 3 4 5 Preliminaries 2.1 Definitions...................................................................................... 3 2.2 Some Useful T h eo rem s................................................................ 7 Planarity Testing and Maximal Planar Subgraph Algorithms 8 3.1 Overview . . ................................................................................ 8 3.2 Theorems . . ........................................ D ata Structures 13 25 4.1 Representations of G ra p h s .......................................................... 25 4.2 Representation of the Depth-First Search T r e e ......................... 26 Shih and H su’s Planarity Testing Algorithm 5.1 Outline of the A lgorithm ................................................... v 28 28 vi 5.2 6 Time Complexity of the A lg o rith m .......................................... H su’s M aximal Planar Subgraph Algorithm 30 32 6.1 Outline of the A lgorithm ............................................................. 32 6.2 Time Complexity of the A lg o rith m .......................................... 34 7 Test Results 35 7.1 Test G ra p h s................................................................................... 35 7.2 Comparison with Other A lgorithm s........................................... 42 Bibliography 45 List o f Tables 7.1 Heuristics applied to dense nonplanar graphs . . . 7.2 Heuristics applied to random graphs ............... 44 ........................................ 44 List o f Figures 2.1 A directed graph and an undirected g r a p h ............................... 3 2.2 A multigraph and a p se u d o g rap h .............................................. 4 2.3 A cut vertex and separation pair .............................................. 5 2.4 Graphs K 5 and K 3t5 ................................................................... 5 2.5 A planar graph and an embedding ........................................... 6 2.6 A graph G and the subdivision graph of G ............................... 6 3.1 A graph, its DFS tree and 4th ite ratio n ..................................... 9 3.2 Cut vertex i and the subtrees .................................................... 11 3.3 A tree before contraction and after contraction......................... 12 3.4 A terminal v-node and a terminal c-node .................................. 13 3.5 An intermediate c-node, Zii and h3, upward and downward di­ rection ............................................................................................ 14 3.6 A forbidden structure ................................................................ 15 3.7 A forbidden structure ................................................................ 16 3.8 A forbidden structure ................................................................ 16 3.9 A forbidden structure ................................................................ 17 .......................................................... ... . 18 3.11 An inner cycle Ci and an outer cycle C3 .................................. 19 3.12 Border of cycle Ci with one terminal v - n o d e ............................ 20 3.13 Border of cycle Ci with one terminal c - n o d e ............................ 21 3.10 A forbidden structure viii ix 3.14 Two terminal v-nodes ui, 112 and v-node u. 3.15 Two terminal v-nodes U1, 22 u 2 and u is a c-node........................... 23 3.16 Two terminal nodes, U1, U 2 , in different c-node and ^ is a c-node. 24 4.1 The input file of K 5 and its adjacency list representation . . . 26 4.2 Representation of a depth-first search t r e e ............................... 27 7.1 Graph gl ..............................., ............................................. 36 7.2 A maximal planar subgraph of g l .............................................. 36 7.3 Graph g 2 37 7.4 A maximal planar subgraph of g 2 .............................................. 37 7.5 Graph gZ 37 7.6 A maximal planar subgraph of g Z .............................................. 38 7.7 Graph #4 ...................................................................................... 38 7.8 A maximal planar subgraph of < /4.............................................. 39 7.9 Graph g5 . . ................................................................................ 40 7.10 A maximal planar subgraph of g b .............................................. 40 7.11 Graph g 6 ...................................................................................... 41 7.12 A maximal planar subgraph of <76.............................................. 41 ....................................................... ...................................................................... Abstract This thesis presents efficient implementations of a planarity testing and a maximal planar subgraph algorithm. The algorithms are based on the meth­ ods of Shih and Hsu [10] and Hsu [9]. The algorithms use the vertex addition method. The key of the algorithms is to add vertices according to a postorder­ ing obtained from a depth-first search tree. The algorithms run in linear time. The implementations were developed based on Shih and Hsu’s methods, and were implemented in the C language. ■Empirical analysis shows that the programs run in linear time. The maximal planar subgraph algorithm was also compared with several other algorithms. In two tables, the sizes of the maximal planar subgraphs obtained from several special graphs and twenty random graphs are listed. C hapter I Introduction A graph is planar if it can be drawn in the plane without any crossing edges. Planar graph problems can be found in many applications, e.g., in VLSI and printed circuit board design, in network design and analysis, and in determin­ ing the isomorphism of chemical structures. The planarity testing problem is to determine whether a given graph can be drawn in the plane without any crossing edges. Planarity testing algorithms are divided into two major types: path ad­ dition methods and vertex addition methods. The path addition algorithm was originally developed by Auslander and Parter [I]; Hopcroft and Tarjan [2] presented a linear time implementation. The vertex addition algorithm was presented first by Lempel, Even and Cederbaum [3],,-and improved to a linear algorithm by Booth and Lueker [4], , If a graph is not planar, then the next problem that arises is how to find a planar subgraph that is as close as possible to the given graph. For a given graph (7, a maximal planar subgraph G' is a planar subgraph of G such that adding any edge of Cr - Cr' to G1 will result in a nonplanar graph. A maximal planar, subgraph which has the maximum number of edges is called a maximum planar subgraph. I 2 Since finding a maximum planar subgraph has been shown to be NPcomplete [5], research has focused on computing a maximal planar subgraph G' of G. For a given graph G which has m edges and n vertices, the first algorithm for this problem has an 0{m n) worst-case time bound [6]. Recently Cai, Han and Tarjan developed an 0{m log n) algorithm [7] for the maximal planar subgraph problem, based on the Hopcroft-Tarjan planarity testing al­ gorithm. An algorithm with the same complexity bound of 0 (m log n) can also be derived from the incremental planarity testing algorithm of Di Battista and Tamassia [13]. Jayakumar, Thulasiraman and Swamy [15] presented an 0 (n 2) planarization algorithm based on PQ-trees. However, this algorithm contains some mistakes. Kant’s correct version [16] of the algorithm also runs in 0 (n 2). Djidjev [12] presented the first linear time algorithm for the maximal planar subgraph problem.. 0. Goldschmidt and A. Takvorian’s planarization algorithm used the cycle-packing method [14]. According to Cimikowski [8], it gives the best results for these algorithms but runs very slowly for a large graph. This implementation is based on Shih and Hsu’s linear time planarity test­ ing algorithm [10] and maximal planar subgraph algorithm [9]. This algorithm uses the vertex addition approach. The key to this algorithm is to add vertices according to a postordering obtained from a depth-first tree. This implemen­ tation is coded in the C language. This thesis presents some useful definitions and theorems in Chapter 2. Chapter 3 gives an overview of the algorithms. Chapter 4 describes the main data structures of the implementations. Chapter 5 discusses the details of the planarity ,testing algorithm and its time complexity analysis. Chapter 6 discusses the details of the maximal'planar subgraph algorithm. Finally, the last chapter discusses the test cases and results. C hapter 2 Prelim inaries 2.1 D efin itio n s A graph G — (V, E) consists of a finite nonempty vertex set V and a finite edge set E. Each edge is a pair of distinct vertices. The number of vertices in the graph G is n= |V |, and the number of edges in the graph G is m = |5 |. Any vertex pair (it, v) G S is called an edge and is said to join the vertices u and v. The graph G — (I/, E) is a directed graph if E is defined as a set of ordered pairs of distinct vertices. If E is defined as a set of unordered pairs of distinct vertices, the graph G is called an undirected graph. Figure 2.1: A directed graph and an undirected graph 3 4 A simple graph G is a graph which has no multiple edges or loops. The multiple edges are the edges which connect the same pair of vertices. A loop is the edge which joins a vertex to itself. If a graph G has some multiple edges but no loops, it is called a multigraph. If a graph G has multiple edges or loops, it is called a pseudograph. Figure 2.2: A multigraph and a pseudograph A walk of a graph G is an alternating sequence of vertices and edges, beginning and ending with vertices. If all vertices on a walk are distinct, the walk is called a path. A graph G is connected if every pair of vertices is connected by a path. A connected component of a graph is a maximal connected subgraph. A cut vertex is a vertex whose deletion increases the number of components. A graph G is 2-connected if it is connected and has no cut vertex. A separation pair of a 2-connected graph G is a pair of vertices whose deletion disconnects the graph G. If a graph G has no cut vertex or separation pair then it is 3-connected. Usually a 2-connected graph is called biconnected, and a 3-connected graph is called triconnected. Figure 2.3 shows a cut vertex and separation pair. A graph G in which every pair of distinct vertices is adjacent is called a complete graph. The complete graph on n vertices is denoted by K n. 5 cut vertex separation pair u,v Figure 2.3: A cut vertex and separation pair A graph G is called bipartite if its vertex set V can be partitioned into two subsets, Vi and V2, such that every edge of G joins Vj with V2. If the graph G contains every edge joining V l with V2, then it is called a complete bipartite graph. If V\ and V2 have m and n vertices, the graph G is denoted by Krritn. Figure 2.4: Graphs Kr1 and Zf3i3 A graph G is said to be embedded in a surface S when it is drawn on S so that no two edges intersect. A graph G is planar if it can be embedded in the plane. A subdivision of G is a graph obtained from G by subdividing some of the edges, that is, by replacing the edges by paths having at most their end vertices in common. 6 Figure 2.5: A planar graph and an embedding Figure 2.6: A graph G and the subdivision graph of G 7 2.2 S om e U sefu l T h eorem s K u ra to w sk i’s T heorem . A graph is planar if and only if it does not contain a subdivision of AT5 or AT3i3. Kuratowski’s Theorem can be proven by using Euler’s Polyhedron Formula and its corollaries and Lemma 1.1. These corollaries and lemma are listed below. The details are given in Nishizeki and Chiba [11]. E u le r’s P olyhedron Form ula. Let G be a connected plane graph, and let n, m and / denote, respectively, the number of vertices, edges, and faces of G. Then n - m + f = 2. C orollary 1.1 If G is a planar graph with n (> 3) vertices and m edges, then m < 3n —6. Moreover, the equality holds if G is a maximal planar graph. C orollary 1.2 If G is a planar bipartite graph with n (> 3) vertices and m edges, then m < 2 n — 4. L em m a 1.1 If G is a 3-connected graph having five or more vertices, then G contains an edge e such that the graph G' obtained from G by contracting / ■ e is 3-connected. C hapter 3 P lanarity T esting and M axim al Planar Subgraph A lgorithm s This chapter discusses the details of Shih and Hsu’s planarity testing algorithm [10] and Hsu’s maximal planar subgraphs algorithm [9]. These algorithms work on a given undirected and biconnected graph. First, an overview is given of the algorithms. Then several definitions and theorems used in the algorithms are discussed. 3.1 O verview As was mentioned in Chapter I, the planarity testing problem is to determine whether there exists a way to draw a graph in the plane without any crossing edges. Given an undirected graph G, finding a maximal planar subgraph means finding a planar subgraph G' of G such that no edge oi G — G1 can be added to G' without destroying planarity. Basically there are two types of planarity testing and maximal planar sub­ graph algorithms: one uses a path addition approach, while the other uses a vertex addition approach. Shih and Hsu’s [10] planarity testing and maximal planar subgraph algo­ rithms are based on the vertex addition approach. In the Hopcroft and Tarjan 8 9 [2] and Lempel [3] algorithms, the subgraphs constructed at each iteration are connected, but Shih and Hsu’s algorithms keep the subgraph induced by the vertices that have not been added connected. In Shih and Hsu’s algorithm, a depth-first search tree is first created, and each vertex is given a number based on postordering. Then the vertices are added one by one in ascending order of the postordering. This means that each vertex will not be considered for inclusion until all of its children have been considered. By using postordering of a depth-first search tree, the graph induced by the vertices that have not been added into the subgraphs is connected. Let T be the depth-first search tree of G. All edges in T are called tree edges, and the other edges of G are called back edges. In a depth-first search tree, if a vertex in the tree has a back edge, then this back edge must connect to one of its ancestors. Also, one back edge can create at most one biconnected component. In the algorithm, once the biconnected component is embedded, it will never change. Figure 3.1 shows a graph G, its depth-first search tree, and the subgraphs after inserting the vertex i. Figure 3.1: A graph, its DFS tree and 4th iteration 10 'The graph created in iteration i is denoted as G1-; the forest which is gen­ eralized in iteration i is denoted as Fi] the trees in the forest Fi is denoted as Ti, T2, ... Tfcj and their roots is denoted as T11, r 2, ... r&. Now consider the ith iteration of the algorithm. In this iteration, we will insert the vertex i into graph G1--I. As was mentioned before, if there is a vertex in the tree T7- of forest T1-I which has a back edge to i then vertex i has a tree edge to the vertex r,, the root of T7-. Now we list the trees of T _i whose roots are adjacent to vertex i as Ti, T2, ... Ti. For any subset Vj. of V, the subgraph G' induced on Vj is the maximal subgraph of G with vertex set Vj. Denote the graph G1 induced on Vj as GlVi]. Clearly the vertex i is a cut vertex of the induced subgraph G1 = G[{z} U Ti U T2 U ... U T]- This means G1 is planar if and only if each G[{i} U T7-] is planar, j = I, 2, ... I. So the problem can be solved by determining the planarity of each G[{i} U T7]. Figure 3.2 shows the situation at iteration i. For a given undirected and biconnected graph G, if every G1, i = I, 2, ... n, is planar, then the given graph G is planar. If there exists a G1- which is not planar then the given graph G is not planar. Some edges (determined by the theorems) can be removed to make the G1 planar. After all the vertices are inserted, the final graph will be a maximal planar subgraph of the given graph G. For each graph G1 created at the ith iteration, the vertex in G1 is defined as a v-node if the vertex is an original vertex of the given graph G. And a biconnected component of G1 is defined as a c-node. At the time a c-node is created, the embedding of the biconnected component of G1 is determined and will never change. A marked vertex in Fi is a vertex which is adjacent to vertex i. At the 11 su b tr e e 2 s u b tr e e 3 Figure 3.2: Cut vertex i and the subtrees beginning of each iteration, the vertices which are adjacent to vertex i are marked. If a marked vertex is a v-node, then the v-node is called a marked v-node. If the vertex is in a c-node then the c-node is called a marked c-node. The external degree of a vertex v at the ith iteration is the number of its neighbors among {i+1, ... n}. A vertex with an external degree of 0 has no back edges to vertices {i+1, ... n}. A vertex contraction procedure eliminates the vertices which are leaves and have an external degree of 0. At the beginning of each iteration, the external degree of each vertex which is adjacent to vertex i will be reduced by one. Then the vertex contraction procedure is performed to contract the vertices which have external degree 0. During the vertex contraction procedure, if we find a leaf vertex u with an external degree of 0 then we delete the vertex and connect its parent node with the vertex i. This new edge will represent the path parent(u), u, i. During the vertex contraction procedure, if any vertex 12 with an external degree of 0 becomes a leaf then the vertex can be contracted. Because each marked vertex is checked in ascending order, it can be guaranteed that every vertex will not be checked before all of its marked children have been checked. Figure 3.3 shows how the vertex contraction procedure works. edge u represents all contracted vertices vertices with an external degree of 0 and not indicent to a tree edge Figure 3.3: A tree before contraction and after contraction A marked v-node is called a terminal node if none of its descendants is marked. A marked c-node is called a terminal node if none of its descendants is marked. Figure 3.4 shows a terminal v-node and a terminal c-node. A c-node is called an intermediate c-node if the c-node appears on the tree path from the last inserted vertex to one of the marked nodes. In each inter­ mediate c-node, there exist exactly two vertices adjacent to the two tree edges. We call these two vertices high P-vertex hi and low P-vertex Zi2, respectively, according to their postordering number. Both the high P-vertex h\ and the low P-vertex A2 are referred to as P-vertex. All other vertices in the c-node are 13 a term inal v-n od e a term inal c-n od e Figure 3.4: A terminal v-node and a terminal c-node called non-P-vertex. For every non-P-vertex in an intermediate c-node, the upward direction is defined as the direction which leads from the non-P-vertex to the h\ first, and the downward direction is defined as the direction which leads from the non-P-vertex to the h 2 first. Any tree edge, once it is included into a c-node, will never be called a tree edge again. Figure 3.5 shows an intermediate c-node, hi and Zi2, and the upward and downward directions. 3.2 T h eorem s This section discusses several theorems. These theorems are the basis for this implementation. Theorem I. If G is planar, then there exist at most two terminal nodes in Tj. Proof: Let G be a graph which has n vertices. Suppose at the ith iteration, there exist more than two terminal nodes. Pick three of them and denote them 14 terminal c-node upward downward Figure 3.5: An intermediate c-node, Zi1 and h2, upward and downward direc­ tion as I, 2, and 3, respectively. By the definition of the terminal nodes, it is known there are some children adjacent to the vertices which are among the vertices {i+1,..., n}. Choose three from these vertices for I, 2, and 3, respectively, and label the vertex which contains the medium postordering number as t. Let the least common ancestor of I, 2, and 3 be r. Then a subgraph homeomorphic to ATgiS can be found. By Kuratowski’s Theorem, it is known that this graph is not planar. Figure 3.6 shows the forbidden structure. □ Theorem 2.1. Each marked non-P-vertex %in a c-node must either be adjacent to the high P-vertex h\, or every vertex between u and Zi1 must have an external degree of 0 and must not be incident to a tree edge. Proof: Let G be a graph which has n vertices. At the itfl iteration, suppose the vertex u is a marked vertex in the c-node. Consider two cases: (I) the c-node is an intermediate c-node, or (2) the c-node is an end c-node. Case (1): Because the c-node is an intermediate c-node, from vertex u in the downward direction, the low-P-vertex h2 will be met first. This means u cannot be adjacent to the high-P-vertex hi from the downward direction. Now suppose there is a vertex u ’ between the vertex u and the high-P-vertex hi 15 '3,3 Figure 3.6: A forbidden structure in the upward direction. Figure 3.7 shows that a subgraph homeomorphic to 7^3,3 can be found. Case (2): In this case, because the c-node is an end c-node, there is no IowP-vertex in the c-node. The marked non-P-vertex u may be adjacent to the high P-vertex in either direction. Now suppose there are two vertices, ti'and u ”, between the non-P-vertex u and the high-P-vertex Zi1 in either direction, respectively. Figure 3.8 shows that a subgraph homeomorphic to K 3i3 can be found. □ Theorem 2.2. If a c-node is an intermediate c-node, then Zi1 and Zi2 must either be adjacent to each other or all vertices between them must have an extern degree of 0 and must not be incident to a tree edge. Proof: Suppose there is an intermediate c-node, and Zi1, Zi2 are not adjacent to each other. Then there must exist two non-P-vertices, U1 and H2, arranged as Zi1U1Zi2H2 in the c-node, and both H1 and H2 have either a non-zero extern 16 Figure 3.7: A forbidden structure i hi u’ u u" t K3,3 Figure 3.8: A forbidden structure 17 degree or induce to a tree edge. That means both u\ and M2 have a path to one of the vertices {i+1, n}. Figure 3.9 shows that a subgraph homeomorphic to Tf3i3 can be found. □ i u vertices-.will be u’ hdded in.future K3,3 Figure 3.9: A forbidden structure Theorem 3. At the ith iteration, if there are two terminal nodes U\ and m2, let the least common ancestor be M3 . Then all vertices between vertex M3 and i must have an external degree of 0 and must not be incident to a tree edge. Proof: Suppose there is a vertex v between vertex M3 and i with a non-zero external degree. This means vertex v is adjacent to one of the vertices {i+1, ..., n}. Figure 3.10 shows that a subgraph homeomorphic to Tf3 3 can be found. Similarly, if vertex v is incident to a tree edge then vertex v connects to one of the vertices {i+1, ..., n} via a child vertex. We can get the same subgraph of Figure 3.10.0 At the ith iteration, if there are two terminal nodes M3 and M2 , and their 18 vertices w ill be -added in future Figure 3.10: A forbidden structure least common ancestor is a c-node, then two cycles will be found. In the c-node there exist two vertices, Vi and ug, which connect with U1 and u2, respectively. The inner cycle C1 is defined as the cycle which contains the path U1V1V2U2 with the back edges (ui,i), (u 2,i), and not passing through Ii1. The outer cycle C2 is defined as the cycle which contains the path U1V1Ii1V2U2V2 with the back edges (u 2,i). Figure 3.11 shows an example. Theorem 4. At the ith iteration, if cycles C1 and C2 are found, and if there is a vertex v which is on C1 but which does not appear on C2, then the vertex v must have an external degree of 0 and must not be incident to a tree edge. The proof of Theorem 4 is similar to the proof of Theorem 3. Theorem 5. For a given graph G, if Gi (the graph which contains vertices {1, 2, ..., i} satisfies the previous four theorems at each vertex-adding iteration, then the graph G can be embedded in the plane. 19 inner outer " cy cle C 2 Figure 3.11: An inner cycle C\ and an outer cycle C2 20 P roof: If an embedding for each Gi can be found, such that all vertices which are adjacent to vertices {i+1, n} are kept on the border of the cycle Ci which is created at the ith iteration, then the graph G can be embedded in the plane. Now consider two cases: (I) there is only one terminal node; (2) there are two terminal nodes. (I) There is only one terminal node u. First assume u is a v-node. Let P be the path from root rj to u. The path P is arranged with the back edge (i,u) and tree edge (i,rj) functioning as the border of the cycle. If, on the path P, there is some c-node, then by Theorem 2.2, hi and must be adjacent. Therefore, all vertices of the c-node can be arranged on the border of C*. See Figure 3.12. intermediate c-node the border Figure 3.12: Border of cycle Ci with one terminal v-node 21 Otherwise, if u is in a c-node, then according to Theorem 2.1, u must be adjacent to the vertex Zi1. Path UZi2Zi1 is chosen with the path from Tj Zi1 and the back edge (i,u) and the tree edge (ZjTj ) to be the border of C,. See Figure 3.13. b o rd er o f c y c le C - term in a l c -n o d e Figure 3.13: Border of cycle Ci with one terminal c-node (2) There are two terminal nodes U1 and u 2. (a) If both terminal nodes are v-nodes and the least common ancestor u is a v-node, then the border of the cycle will contain the path UljUjU2 and back edges (UljZ)j (u2jz). From Theorem 3, it is known that every vertex between z and u will have an external degree of 0 and must not be incident to a tree edge. Therefore, every vertex which is adjacent to vertices {i+1, ... n} will be on the border of Ci- See Figure 3.14. (b) If both terminal nodes are v-nodes and the least common ancestor u is a c-node, then two cycles C1 and C2 will be formed. C2 is used as the border of Ci- From the definition of C2 and the Theorem 4, every vertex which is 22 Z ^ vertices with an border of external degree- cycle Cj of 0 and not incident to any tree edge Figure 3.14: Two terminal v-nodes Ui , Ug and v-node u. adjacent to vertices {i+1, ... n} will be on the border of Ci- See Figure 3.15. The case of both terminal nodes Ui , Ug in the same c-node is similar to the above case. (c) If two terminal nodes, U1, U2, are in different c-nodes and the least common ancestor it is a c-node, then cycle C2 is used as the border of CiFrom the definition of C2 and Theorem 4, it is known that every vertex which is adjacent to vertices {i+1, ... n} will be on the border of Ci- See Figure 3.16. Finally, there could exist two terminal nodes, It1, u2, in different c-nodes and the least common ancestor is a v-node. This case is similar to the above case.O 23 border o f c y c le Cj c -n o d e u ul / Figure 3.15: Two terminal v-nodes Ui , U2 and tt is a c-node. 24 y ~ ^ border of cycle Cj c-node Figure 3.16: Two terminal nodes, Mi, U2, in different c-node and u is a c-node. C hapter 4 D ata Structures This chapter describes the data structures used in the implementation. Like other implementations of planarity testing and maximal planar subgraph al­ gorithms, the data structures are complex and play an important role in the efficiency of the implementation. 4.1 R ep resen ta tio n s o f G raphs In this implementation, an adjacency list is used to represent the graph. Let G = (V, E) be a graph with n vertices and m edges. The adjacency list representation is an array Adj of pointers. The array Adj has n members. Each vertex u in the graph G has an entry Adj[u] which contains a pointer to a chain of all the vertices which are adjacent to vertex u. The input to this program is a text file which contains the number of vertices n in the graph on the first line; each subsequent line represents a vertex in the graph. Vertex u is represented by the (it + l)th line. This line contains the vertex numbers of the vertices which are adjacent to vertex u. Those numbers are separated by spaces, and the end of the list is marked with a 0. Figure 4.1 shows an example of the input file and adjacency list represen­ 25 26 tation of K 5. 5 23450 1 13450 2 12450 3 1235 0 4 12340 5 Figure 4.1: The input file of K 5 and its adjacency list representation Another common way to represent a graph is using an adjacency matrix representation. The adjacency matrix representation of a graph G = (F, E) is an Ti x n matrix A. For each if (i,j) 6 E then = I else = 0. When the graph is a sparse graph, the adjacency list representation is more efficient in memory usage than the adjacency matrix representation. The major disadvantage of the adjacency list representation is that there is no quick way to determine if a given edge (u,v) is present in the graph. We have to search Adj[u] to determine that. 4.2 R ep resen ta tio n o f th e D e p th -F ir st Search Tree Figure 4.2 shows the basic structure of the representation of the depth-first search tree. In each tree node, a chain for tree edges and a chain for back edges is kept. Each chain is sorted in ascending order of the postordering numbers of the vertices. Each node in the tree can be visited by the tree edge chain. 27 n ew N o d es 1 back 2 tree back back n back Figure 4.2: Representation of a depth-first search tree A pointer array, newNodes, is used to store the pointers to each tree node. The index into the pointer array newNodes is the postordering number of the vertex. Thus, if the postordering number of the vertex which the program wants to visit is known, the program can visit the vertex directly by using the array newNodes. By using the newNodes array, we can avoid travel along the tree edges when finding a vertex. In each tree node we also store the external degree, the parent’s postorder­ ing number, the node type, and the “removed” mark. C hapter 5 Shih and H su ’s Planarity T esting A lgorithm This chapter describes the details' of the planarity testing algorithm and dis­ cusses the time complexity of the algorithm. The major goal of this implemen­ tation is to have the algorithm run in linear time. This means that for each edge, the algorithm can only traverse the edge a constant number of times. 5.1 O u tlin e o f th e A lg o rith m Following is the outline of the algorithm: 1. Create a depth-first search tree; 2. Generate a postordering of the vertices in the depth-first search tree; 3. Create a sorted tree edge chain and a sorted back edge chain for each vertex; 4. Set a contraction step number by a preorder traversal of the depth-first search tree. If the contraction step number of a vertex v is k, v cannot be contracted before vertex k added to the subgraph; 5. Add vertices one by one according to the postordering; 5.1 Mark every vertex which is adjacent to the newly-added vertex i; 28 29 5.2 Perform the vertex contraction procedure on each marked vertex ac­ cording to the postordering; 5.3 Check each vertex v which is adjacent to the newly-added vertex i by a back edge according to the postordering; 5.3.1 Choose an unchecked marked vertex v which has the smallest pos­ tordering number; 5.3.2 If vertex v is in a c-node, check if it is adjacent to the high P-vertex of the c-node. If it is not adjacent to the high P-vertex of the c-node then, output “nonplanar” (Theorem 2.1) and halt; 5.3.3 If vertex v is adjacent to the high P-vertex of the c-node and at least one of its children has not been contracted, check iNodes. If iNodes = 2, then output “nonplanar” (Theorem I and the definition of the terminal nodes) and halt else set iNodes = tNodes+1, remember the vertex v as one of the two terminal nodes, and travel from vertex v to i along the tree path, marking the label of every vertex encountered in the path to i. If any c-node is found along this path then check if the high P-vertex and low P-vertex are adj acent to each other. If it is not then output “nonplanar” (Theorem 2.2) and halt, else jump to the high P-vertex to continue the traversal; 5.3.4 If the label of the vertex v is not i, then check the number of terminal nodes iNodes. If iNodes = 2, then output “nonplanar”(Theorem I), else set iNodes — tNodespl, remember the vertex v as one of the two terminal nodes, and travel from vertex v to vertex i along the tree path, marking the label of every vertex encountered in the path to vertex i. If any c-node is found during the traversal, then check if the high P-vertex and low P-vertex are adjacent to each other. If not then output “nonplanar” (Theorem 2.2) and halt, else jump to the high P-vertex to continue the traversal; 5.3.5 Create a new c-node by using the terminal nodes found in previous 30 checking; If there is only one terminal node, then create the c-node by including all vertices along the tree path from v to i] If there are two terminal nodes Ui, U2 and the least common ancestor u3 is a v-node, then create the c-node from the two terminal nodes and the least common ancestor. The c-node will include all vertices along the tree path Ui to u 2. If there are any uncontracted vertices between the least common ancestor U3 and vertex i, then output “nonplanar” (Theorem 3) and halt; If there are two terminal nodes U i, U2 and the least common ancestor U 3 is a c-node, then create the c-node from the two terminal nodes and the least common ancestor. The c-node will include all vertices along the tree path u% to u i , with U2 to u3, where U i, U 2, Ui , U2 are the vertices in the c-node and are connected respectively. If there are any uncontracted vertices on cycle C\ - C2, then output “nonplanar” (Theorem 4) and halt. 5.3.6 If all vertices have been added to the subgraph then output “The graph is planar” else go to 5.3.1. 5.2 T im e C o m p lex ity o f th e A lg o rith m Suppose a given graph G = (V, E) has n vertices and m edges. Step I, creating a depth first tree, will take 0 (m + n) steps. Step 2, postordering each vertex in the depth-first search tree by using a depth-first search, will take 0 (m + n) steps. Step 3, creating the sorted tree and back edge chain, means the vertices must be visited in ascending order of postordering. Each vertex is visited and each edge is traversed once. Therefore, the time complexity of this step is 0 (m + n). 31 Step 4 can be done by a preorder traversal on the depth-first search tree, which means the time complexity of this step is 0 (m + n). Step 5 is the most important step in the algorithm. The vertex contraction process will take at most 0 (n) steps because each vertex can be contracted at most once. Step 5.3.2 always requires a constant number of steps. From step 5.3.3 and 5.3.4, it can be seen that all tree edges will be traversed at most a constant number of times because any tree edge which is included in a c-node will never be traversed again. In step 5.3.5, checking if the least common ancestor U3 is adjacent to the newly-added vertex i or checking if there is any vertex in Ci - Cg is only done a constant number of times. The c-node creating phase will traverse each tree edge between the root and the marked vertex once, so the number of times a tree edge is traversed is a constant. Summing up all the steps, this program has the time complexity 0 (m + n). C hapter 6 H su ’s M axim al Planar Subgraph A lgorithm This chapter describes the details of the maximal planar subgraph algorithm. This algorithm is based on the planarity testing algorithm. The major differ­ ence is that whenever a back edge whose addition causes the planarity testing algorithm to halt, instead of stopping, the edge is marked as “removed”. When the final planar subgraph is output, these edges will be marked as removed edges. 6.1 O u tlin e o f th e A lg o rith m Following is the outline of the algorithm: 1. Create a depth-first search tree; 2. Generate a postordering of the vertices in the depth-first search tree; 3. Create a sorted tree edge chain and a sorted back edge chain for each vertex; 4. Set a contraction step number by a preorder traversal of the depth-first search tree. If the contraction step number of a vertex v is k, v cannot be contracted before vertex k has been added to the subgraph; 5. Add vertices one by one according to the postordering; 32 33 5.1 Mark every vertex which is adjacent to the newly-added vertex i] 5.2 Perform the vertex contraction procedure on each marked vertex ac­ cording to the postordering; 5.3 Check each vertex v which is adjacent to the newly-added vertex i by a back edge according to the postordering. 5.3.1 Choose an unchecked marked vertex v which has the smallest pos­ tordering number; 5.3.2 If vertex v is marked “removed” then mark the back edge (v,i) as “removed” in the back edge chain of vertex i, and go to 5.3.1; 5.3.3 If vertex v is in a c-node, then check if it is adjacent to the high Pvertex of the c-node. If it is not adjacent to the high P-vertex of the c-node, then mark the back edge (v,i) as “removed” (Theorem 2.1) in the back edge chain of vertex i, and go to 5.3.1; 5.3.4 If vertex v is adjacent to the high P-vertex of the c-node and all of its children have not been contracted, check iNodes. If tNod,es=2, then mark the back edge (v,i) as “removed” (Theorem I and the definition of the terminal nodes) and go 5.3.1, else set iNodes= iNodes+1, remember the vertex v as one of the two terminal nodes and travel from vertex v to i along the tree path, marking the label of every vertex encountered in the path to i. If any c-node is found along this path then check if the high P-vertex and low P-vertex are adjacent to each other. If they are not, then mark the back edge (v,i) as “removed” (Theorem 2.2) and go to 5.3.1; 5.3.5 If the label of the vertex v is not equal to i then check iNodes. If iNodes = 2, then mark the back edge (v,i) as “removed” (Theorem I) and go to 5.3.1, else set iNodes= tNodes+1, remember the vertex v as one of the two terminal nodes and travel from vertex v to vertex i along the tree path, marking the label of every vertex encountered in the traversal to vertex i. If 34 any c-node is found during the traversal then check if the high P-vertex and low P-vertex are adjacent to each other. If they are not then mark the back edge (v,i) as “removed” (Theorem 2.2) and go to 5.3.1; 5.3.6 Create a new c-node by using the terminal nodes found in previous checking; If there is only one terminal node, then create the c-node by including all vertices along the tree path from v to t; If there are two terminal nodes, u\, U2, and the least common ancestor U3 is a v-node, then create the c-node from the two terminal nodes and the least common ancestor. The c-node will include all vertices along the tree path U1 to u 2. Mark all vertices between the least common ancestor U 3 and the vertex i as “removed” (Theorem 3) and go to 5.3.1; If there are two terminal nodes, U1, u2, and the least common ancestor U 3 is a c-node, then create the c-node from the two terminal nodes and the least common ancestor. The c-node will include all vertices along the tree path U 1 to U1, U1 , u 2 to U2 , where U1 , U2 are the vertices in the c-node and connected with u 2, respectively. Mark all vertices on cycle C1 - C2 as “removed” (Theorem 4) and go to 5.3.1; 6. Output the final graph. In this step, the final maximal planar subgraph will be output. The vertices will be printed in ascending order of postordering number. Every tree edge will be included in the final graph. Every back edge removed by the algorithm will be output with a mark ‘r ’. 6.2 T im e C o m p lex ity o f th e A lg o rith m This analysis is similar to the analysis of the time complexity of the planarity testing algorithm. The maximal planar subgraph algorithm also runs in linear time. C hapter 7 Test R esu lts 7.1 T est G raphs A few special graphs were used for testing and the results were compared with other algorithms. The first test case is graph gl, shown in Figure 7.1. Graph gl has a maximum planar subgraph obtained by removing edges (4,8) and (7,8). This maximal planar subgraph program obtained a maximal planar subgraph by removing edges (2,7), (2,8), (2,9), (3,7), (3,8), (3,9), (4,7), (4,8), and (4,9). Figure 7.2 shows the result. The second test case is graph </2, shown in Figure 7.3. Graph g2 has a maximum planar subgraph obtained by removing edge (u,v). This maximal planar subgraph program obtained, a maximal planar subgraph by removing the ten edges which are crossing with edge (u,v). Figure 7.4 shows the result. The third test case is graph g3, shown in Figure 7.5. Graph gZ has a max­ imum planar subgraph obtained by removing edges (1,28) and (9,12). This maximal planar subgraph program obtained a maximal planar subgraph by rem ovin g edges (2 ,1 9 ), (2,20), (3,20), (3 ,2 1 ), (4,21), (4,22), (5 ,2 2 ), (5,23), (6 ,2 3 ), (7 ,2 3 ), (7 ,2 4 ), (8,11), (12,24), (13,24), (13,25), (14,25), (1 4 ,26), (15,26), (15,27), and (16,27). Figure 7.6 shows the result. 35 36 10 Figure 7.1: Graph gl io Figure 7.2: A maximal planar subgraph of gl 37 Figure 7.3: Graph g2 Figure 7.4: A maximal planar subgraph of g2 Figure 7.5: Graph g2> 38 Figure 7.6: A maximal planar subgraph of <78 The fourth test case is graph g4, shown in Figure 7.7. Graph #4 has a maximum planar subgraph formed by removing edges (1,4) and (8,10), This maximal planar subgraph program obtained a maximal planar subgraph by removing edges (2,8), (2,9), (3,9), and (4,8). Figure 7.8 shows the result. Figure 7.7: Graph g4 The fifth test case is graph g5, shown in Figure 7.9. Graph gb has a maximum planar subgraph obtained by removing edges (1,2), (3,4) and (4,5). This maximal planar subgraph program obtained a maximal planar subgraph by removing edges (2,44), (3,43), (4,42), (5,41), (6,40), (8,41), (9,42), (10,43), 39 Figure 7.8: A maximal planar subgraph of y4 and (11,44). Figure 7.10 shows the result. The sixth test case is graph g6 , shown in Figure 7.11. Graph g6 has a maximum planar subgraph obtained by removing edges (1,9), (1,31), (22,23) and (23,32). This maximal planar subgraph program obtained a maximal planar subgraph by removing edges (19,40), (19,36), (22,35), (23,32), (4,38), (4,26), and (5,35). Figure 7.12 shows the result. 40 Figure 7.9: Graph g5 Figure 7.10: A maximal planar subgraph of g5 41 Figure 7.11: Graph 56 Figure 7.12: A maximal planar subgraph of 56 42 7.2 C om p arison w ith O ther A lg o rith m s The maximal planar subgraph solution of our algorithm H SU were compared against solutions obtained by other algorithms. Two kinds of graphs were used: the special graphs gl to. <76 and a set of 20 random graphs. The random graphs are generated from K 3. We start from K 3 and at each step we insert a new vertex and three new edges inside an arbitrary face. We created ten 100-vertex graphs and ten 200-vertex graphs. Each of them contained a maximum planar subgraph of size 3n —6. Then k (nonplanar) edges were added randomly, with k ranging from 10 to 100. This section is based on the results of Cimikowski [8]. Table 7.1 shows the size of the maximal planar subgraphs found from special graphs gl to gQ by each algorithm. Table 7.2 shows the size of the maximal planar subgraphs found from the random graphs. From the table, we can see that the Goldschmidt-Takvorian algorithm ob­ tained the best solutions for these algorithms. H SU did not perform as well as the PQ-tree heuristic and the Goldschmidt-Takvorian heuristic but our im­ plementation has a faster running time. In comparing the running times of these algorithms, we tried a 300-vertex, 1507-edge graph. We used a DEC Alpha 3000/500 system for our testing. We ran each program 5 times and computed the average running time. HSU found a solution in 0.12 CPU seconds. CHT found a solution in 0.135 CPU seconds. The remaining algorithms took a very long time to find solutions. KEYS : H T = Hopcroft-Tarjan heuristic C H T = Cai-Han-Tarjan heuristic PQ = PQ-tree heuristic 43 IN C = Incremental heuristic GT = Goldschmidt-Takvorian heuristic H SU = Hsu heuristic 44 graph gl g2 g3 g4 g5 g6 n m opt.1 HT 19 16 10 21 60 166 165 160 57 73 28 75 20 16 10 22 82 77 45 85 59 43 63 59 Size of planar subgraph PQ CHT IN C GT HSU 12 12 18 17 19 164 154 165 149 156 71 55 73 73 55 19 19 18 19 19 80 76 55 80 76 54 56 55 50 56 1 optimum solution Table 7.1: Heuristics applied to dense nonplanar graphs k1 10 20 30 40 50 60 70 80 90 100 100 vertices2 Size of planar subgraph H T C H T PQ I N C 219 196 238 237 224 207 203 206 209 190 224 227 212 213 177 184 221 207 169 192 194 217 162 171 201 217 174 186 198 202 171 177 215 184 206 164 194 187 161 174 obtained GT H S U 203 235 223 208 224 188 228 183 181 234 242 168 239 168 164 221 235 180 173 224 200 vertices3 Size of planar subgraph H T C H T PQ I N C 479 481 401 475 490 446 487 410 445 405 449 444 448 372 460 429 420 375 433 416 452 360 430 422 423 358 408 403 345 417 397 416 409 401 414 358 396 324 416 402 1 the number of edges added to the maximum planar graph 2 the optimum solution of the graph is 294 3 the optimum solution of the graph is 594 Table 7.2: Heuristics applied to random graphs obtained GT H S U 461 412 439 414 451 409 464 371 376 439 334 411 408 345 328 423 417 357 323 461 B ibliography [1] L. Auslander, and S. V. Barter, On embedding graphs in the plane, J. Math. Mech. 11 (1961), pp. 517-523. [2] J. Hop croft, and R. E. Tarj an, Efficient planarity testing, J. ACM 21 (1974),vpp. 549-568. [3] A. Lempel, S. Even and I. Cederbaum, An algorithm for planarity testing of graphs, Theory of Graphs, Int. Symp. Rome (1966), pp. 215-232. [4] K. S. Booth, and G. S. Lueker, Testing for the consecutive ones property, interval graphs and graph planarity testing using PQ-tree algorithms, J. Computer & System Sciences 13 (1976), pp. 335-379. [5] M. R. Garey, and D. S. Johnson, A Guide to the Theory of NP-completeness, Freeman Co., San Francisco, 1979. [6] T. Chiba, I. Nishioka and I. Shirakawa, An algorithm of maximal planarization of graphs, Proc. 1979 IEEE Int. Symp. on Circuits and Systems (1979), pp. 649-652. [7] J. Cai, X. Han and R. E. Tarjan, An 0{rn log n)-timealgorithm for the maximal planar subgraph problem, SIAM J. Comput. Vol. 22 (6)(1993), pp. 1142-1162. [8] R. Cimikowski, An analysis of heuristics for graph pla­ narization, manuscript, 1995. [9] W.-L. Hsu, A linear time algorithm for finding maximal planar subgraphs, Proc. Sixth Annul Int. Symp. on Algo­ rithms & Computation, Cairns, Australia, 1995. [10] W.-K..Shih and W.-L. Hsu, A simple test for planar graphs, , manuscript, 1993. 45 46 [11] T. Nishizeki and N. Chiba, Planar graphs : theory and algorithms, North-Holland 1988. [12] H. N. Djidjev, A linear algorithm for the maximal planar subgraph problem, Tech. Report TR95-247, Dept of Com­ puter Science, Rice Univ. 1995. [13] G. Di Battista and R. Tamassia, Incremental planarity test­ ing, Proc. IEEE Symp. Found. Comp. Sci. (1989), pp. 598611. [14] 0 . Goldschmidt and A. Takvorian, An efficient graph pla­ narization two-phase heuristic, Networks, vol. 24, pp. 69-73, 1994. [15] R. Jayakumar, K. Thilasiraman and M. N. S. Swamy, 0 (n 2) algorithms for graph planarization, IEEE Trans, on Gomput.-Aided Design, vol. 8, pp. 257-267, 1989. [16] G. Kant, An 0 (n 2) maximal planarization algorithm based on PQ-trees, Tech. Report RUU-CS-92-03, CS Dept.,. Univ. Utrecht, Netherlands, Jan., 1992.