Algorithm Search⌘K

Proofs: Hopcroft-Tarjan planarity test

These are the human proofs of Hopcroft-Tarjan planarity test, at full length.

Correctness of the Hopcroft-Tarjan planarity test full

This proof shows: correctness, termination.

The proof follows the segment formulation of Mehlhorn. Line numbers refer to the pseudocode on the main page.

Reduction to one block

Lines L1 and L2. A simple planar graph with n≥3n \ge 3 vertices has at most 3n−63n - 6 edges (planar edge bound), so line L1 never rejects a planar graph. A graph is planar if and only if each of its biconnected components is planar: one direction holds because subgraphs of planar graphs are planar; for the other, embeddings of the blocks can be glued at cut vertices, one block at a time, by putting the block on the outer face of its cut vertex in the drawing so far. A block with at most 4 vertices is a subgraph of K4K_4, which is planar. So it suffices to show that lines L3 to L6 accept a biconnected graph BB with k≥5k \ge 5 vertices exactly when BB is planar.

The palm tree

Fix the DFS of line L3; vertices are their preorder numbers, and T(v)T(v) is the subtree of vv. In an undirected DFS every non-tree edge joins a vertex and one of its ancestors, so it becomes a back arc v→wv \to w with ww a proper ancestor of vv that is not its parent.

Fact 1. In a biconnected graph with at least 3 vertices, vertex 0 has the single child 1, every vertex v≥1v \ge 1 has an arc other than the arc from its parent, and lowpt1(c)<p\mathrm{lowpt}_1(c) < p for every tree arc p→cp \to c with p≠0p \ne 0.

Proof. A root with two children is a cut vertex. A vertex v≥1v \ge 1 of degree 1 would make its neighbour a cut vertex. If lowpt1(c)≥p\mathrm{lowpt}_1(c) \ge p for p≠0p \ne 0, then no back arc leaves T(c)T(c) above pp, and pp separates T(c)T(c) from 00. □\square

Fact 2 (spines). After the sort of line L5, the first arc out of every vertex v≥1v \ge 1 goes to a child cc with lowpt1(c)=lowpt1(v)\mathrm{lowpt}_1(c) = \mathrm{lowpt}_1(v), or is a back arc to lowpt1(v)\mathrm{lowpt}_1(v). So line L8 is well defined, the spine of x→yx \to y ends with a back arc wk→w0w_k \to w_0 where w0=lowpt1(y)w_0 = \mathrm{lowpt}_1(y), every vertex on the spine has lowpt1=w0\mathrm{lowpt}_1 = w_0, and w0<xw_0 < x unless x=0x = 0.

Proof. lowpt1(v)\mathrm{lowpt}_1(v) is the minimum of vv, of the ends of back arcs out of vv, and of lowpt1(c)\mathrm{lowpt}_1(c) over the children. The cost of an arc is 2w2w or 2w+12w + 1 where ww is the end of the back arc or lowpt1(c)\mathrm{lowpt}_1(c), so the arc of least cost reaches the minimum of these, which is below vv by Fact 1. The walk goes down the tree, so it ends; it ends at a back arc. The last statement is Fact 1. □\square

Segments and their attachments. For a tree arc e=x→ye = x \to y, S(e)S(e) is T(y)T(y) with the arc ee and all back arcs out of T(y)T(y); its attachments Att(e)\mathrm{Att}(e) are the ends of those back arcs that are smaller than yy. For a back arc e=v→we = v \to w, S(e)S(e) is the arc and Att(e)={w}\mathrm{Att}(e) = \{w\}. Every attachment of S(x→y)S(x \to y) lies on the tree path from w0w_0 to xx, the stem P(e)P(e), because it is an ancestor of yy and at least lowpt1(y)=w0\mathrm{lowpt}_1(y) = w_0. Let G(e)=S(e)∪P(e)G(e) = S(e) \cup P(e).

Definition. S(e)S(e) is strongly planar if G(e)G(e) has a planar embedding in which every edge and vertex of P(e)P(e) lies on the boundary of the outer face. For e=0→1e = 0 \to 1, S(e)S(e) is all of BB and P(e)P(e) is the single vertex 0, so BB is planar if and only if S(0→1)S(0 \to 1) is strongly planar (any face can be made the outer face).

Decomposition along the spine

Fix a tree arc e=x→ye = x \to y with spine w1,…,wkw_1, \ldots, w_k and closing back arc wk→w0w_k \to w_0. Let CC be the cycle formed by the stem, the arc ee, the spine and the arc wk→w0w_k \to w_0. Call the arcs out of spine vertices that are not on CC the emanating arcs e1,…,ere_1, \ldots, e_r, with segments Si=S(ei)S_i = S(e_i).

Fact 3. G(e)=C∪S1∪⋯∪SrG(e) = C \cup S_1 \cup \cdots \cup S_r, two different SiS_i meet only in vertices of CC, and every SiS_i meets CC exactly in its base (the spine vertex it leaves) and its attachments. So the SiS_i are the bridges of CC in G(e)G(e).

Proof. The subtrees of the children of spine vertices that are not on the spine are disjoint and disjoint from CC, and each back arc from them ends at an ancestor at least w0w_0 (by Fact 2 applied to the child, lowpt1≥w0\mathrm{lowpt}_1 \ge w_0), which lies on CC. Each tree-arc segment is connected through its subtree. □\square

Two bridges SiS_i and SjS_j conflict if their contact sets with CC (base and attachments) have 3 vertices in common, or contain 4 vertices u1,u2,u3,u4u_1, u_2, u_3, u_4 in this cyclic order on CC with u1,u3u_1, u_3 in one set and u2,u4u_2, u_4 in the other. This is the classical overlap relation of bridges, and the following facts are the standard bridge lemma.

Fact 4. In a planar embedding of C∪Si∪SjC \cup S_i \cup S_j in which SiS_i and SjS_j are on the same side of CC, they do not conflict.

Proof. By the Jordan curve theorem, CC bounds a disc on that side, and SiS_i and SjS_j are drawn inside it. In the alternating case, SiS_i contains a path from u1u_1 to u3u_3 through the disc, which cuts the disc into two parts, with u2u_2 and u4u_4 on different boundary arcs; a path in SjS_j from u2u_2 to u4u_4 would have to cross it. In the case of 3 common vertices a,b,ca, b, c, each bridge contains a vertex joined to a,b,ca, b, c by internally disjoint paths (a subdivided claw); two such claws inside a disc with the same 3 ends on the boundary must cross, since the first claw cuts the disc into three regions and no region has all of a,b,ca, b, c on its boundary. □\square

Fact 5. Suppose bridges S1,…,SqS_1, \ldots, S_q pairwise do not conflict, and each C∪SiC \cup S_i has a planar embedding with SiS_i inside CC. Then C∪S1∪⋯∪SqC \cup S_1 \cup \cdots \cup S_q has a planar embedding with all of them inside CC.

Proof. Induction on qq. Embed C∪S1∪⋯∪Sq−1C \cup S_1 \cup \cdots \cup S_{q-1} with all bridges inside. The faces inside CC are bounded by arcs of CC and paths through bridges. Because SqS_q conflicts with no SiS_i, all contact vertices of SqS_q lie on the boundary of one of these faces: if they did not, two of them would be separated by some SiS_i, which gives an alternating quadruple, or all three regions around a branch vertex of SiS_i would be needed, which gives 3 common contact vertices. This face is a disc whose boundary meets CC in an arc that contains all contact vertices of SqS_q in their order along CC, so the embedding of SqS_q inside CC can be copied into it, after a homeomorphism of discs that matches the contact vertices. □\square

Lemma 1 (decomposition). S(e)S(e) is strongly planar if and only if

  1. every SiS_i is strongly planar, and
  2. the bridges S1,…,SrS_1, \ldots, S_r can be split into two classes LL and RR with no conflict inside a class, such that every bridge with an attachment aa with w0<a<xw_0 < a < x is in LL.

Proof. (Only if.) Take an embedding of G(e)G(e) with the stem on the outer face. By Fact 3 and the Jordan curve theorem, each bridge is drawn inside or outside CC; let RR be the bridges on the side of CC that contains the outer face and LL the others. By Fact 4 there is no conflict inside a class. A bridge in RR with an attachment aa strictly inside the stem contains a path from aa to its base, which lies off the stem; with CC, this path cuts the side of CC that holds the outer face so that the stem edges before aa and after aa are on the boundaries of different faces, which contradicts the choice of the embedding. So such bridges are in LL. For condition 1, restrict the embedding to C∪SiC \cup S_i: the side of CC without SiS_i is a face with all of CC on its boundary, so the stem of SiS_i, which is a subpath of CC from its lowest attachment to its base, is on the boundary of one face, and G(ei)G(e_i) is a subgraph whose stem is still on that face.

(If.) For each SiS_i, take an embedding of G(ei)G(e_i) with its stem PiP_i on the outer face. The rest of CC is a path between the two ends of PiP_i that avoids SiS_i; draw it in the outer face, close to the boundary, which gives an embedding of C∪SiC \cup S_i with SiS_i on one side, and by a reflection on either side. Apply Fact 5 to LL inside CC and to RR outside CC (outside is a disc on the sphere). The stem has no attachment from RR in its interior, so after the bridges of RR are drawn, all stem edges still border one face outside CC; make it the outer face. □\square

Conflicts along the spine. By Fact 3 and the order of numbers along CC (the stem and spine are a tree path with increasing numbers, closed by the arc from wkw_k to w0w_0), conflict has a simple form. Say SjS_j is handled after SiS_i by line L11, so the base bjb_j of SjS_j is at most the base bib_i of SiS_i. Let aj=min⁡Att(Sj)a_j = \min \mathrm{Att}(S_j).

Lemma 2. SiS_i and SjS_j conflict if and only if SiS_i has an attachment cc with aj<c<bja_j < c < b_j.

Proof. (If.) If bj<bib_j < b_i, the four vertices aj<c<bj<bia_j < c < b_j < b_i alternate, with aj,bja_j, b_j in SjS_j and c,bic, b_i in SiS_i. If bj=bi=vb_j = b_i = v, then SiS_i came first in the sort, so lowpt1\mathrm{lowpt}_1 of SiS_i is at most aja_j. If it is smaller, the vertices lowpt1(Si)<aj<c<v\mathrm{lowpt}_1(S_i) < a_j < c < v alternate. If it equals aja_j, then SiS_i has the attachment cc strictly between aja_j and vv, so its cost is odd, and SjS_j, which comes later with the same lowpt1\mathrm{lowpt}_1, has odd cost too: it has an attachment dd with aj<d<va_j < d < v. If c=dc = d, the common vertices aj,c,va_j, c, v are three; otherwise aj,min⁡(c,d),max⁡(c,d),va_j, \min(c, d), \max(c, d), v alternate between the two bridges. (A back arc counts as a segment with even cost, which comes first in its bucket.)

(Only if.) Suppose every attachment of SiS_i is at most aja_j or at least bjb_j. All contact vertices of SjS_j lie in the interval [aj,bj][a_j, b_j] of CC, and all contact vertices of SiS_i lie outside the open interval (aj,bj)(a_j, b_j). Then two vertices of SiS_i never separate two vertices of SjS_j on CC, and the only possible common vertices are aja_j and bjb_j. So they do not conflict. □\square

The stack of blocks

Consider one call SEGMENT(x,y)(x, y). Say that a bridge is seen once line L13 has handled it, and that its live attachments are those below the current vertex ww of line L10. Line L14 removes, when the walk moves from ww to its parent, exactly the attachments equal to the parent, so the lists on SS hold exactly the live attachments of seen bridges, if each list is sorted with the largest first (removal at the heads then removes all copies of the parent).

Invariant. Before and after each call of ADD-BLOCK:

  • (a) every list on SS is sorted with the largest first;
  • (b) each block holds the live attachments of a set of seen bridges, split into left and right lists by a 2-colouring with no conflict inside a colour, and the sets of the blocks are the connected components of the conflict graph of the seen bridges that still have live attachments;
  • (c) every attachment in a block is at most every attachment in every block above it.

Proof that ADD-BLOCK keeps it. Let the new bridge be SjS_j with list AA, sorted by induction (for a back arc, AA has one element). Its minimum is aja_j. While SS is not empty, look at the top block TT and let μ\mu be the minimum of the current left list of BB.

First, μ=aj\mu = a_j as long as the left list of BB has only taken left lists that are empty or have all entries at most aja_j; and by (c) every entry of a block below TT is at most every entry of TT. So if the loop has combined a block with a nonempty left list, then μ\mu is the minimum of that list, and every remaining block has all entries at most μ\mu, so the loop ends at line L22 in the next round. Hence the comparisons with μ\mu give the same result as comparisons with aja_j for every block that is compared while it matters.

By Lemma 2, SjS_j conflicts with a seen bridge exactly when that bridge has a live attachment above aja_j. (Attachments that are not live are at least the current vertex w=bjw = b_j, and cannot lie in (aj,bj)(a_j, b_j).) So SjS_j conflicts with some bridge on the left of TT if and only if the head of the left list of TT exceeds aja_j, and the same on the right.

  • If both sides of TT have such a bridge, the component of TT with SjS_j has an odd cycle: a path inside the component between two bridges of different colours has odd length, and SjS_j closes it with two edges. The conflict graph is not bipartite, and lines L19 and L20 fail. By Lemma 1, S(e)S(e) is not strongly planar, and failing is correct.
  • If only the left side conflicts, line L19 swaps the sides of TT, which is another valid colouring of the same component.
  • If the right side conflicts after this, TT joins the component of SjS_j on the side opposite to SjS_j, and line L21 merges it: the left lists contain SjS_j and the non-conflicting colour of TT, the right lists the conflicting colour. The concatenations stay sorted, because the left list of TT has all entries at most μ\mu (otherwise line L20 would have failed) and the right list of BB comes from blocks above TT, whose entries are at least those of TT by (c).
  • If neither side conflicts, then by (c) no lower block conflicts either, because all their entries are at most the entries of TT, which are at most aja_j. The loop ends.

The pushed block holds SjS_j and every component that conflicts with it, so (b) holds. Its entries are at least μ\mu, and everything below it is at most μ\mu, so (c) holds. Line L14 removes only heads and pops empty blocks, which keeps (a), (b) and (c): a bridge without live attachments conflicts with no later bridge by Lemma 2. □\square

Lemma 3. Assume that every recursive call made by SEGMENT(x,y)(x, y) returns the attachments of its segment below its base, sorted, when the segment is strongly planar, and fails otherwise. Then SEGMENT(x,y)(x, y) does the same for S(x→y)S(x \to y).

Proof. If some SiS_i is not strongly planar, a recursive call fails, and by Lemma 1 so does S(e)S(e). Otherwise, by the invariant, line L13 fails exactly when the conflict graph of the bridges is not bipartite; the conflicts are those of Lemma 2, and a bridge whose attachments have all left the lists conflicts with no later bridge, so dropping it changes nothing. If the loop of L10 ends, the conflict graph is bipartite, and the blocks on SS are the components of the bridges that still have attachments below xx; the colouring of each component can be chosen independently, up to a swap. A bridge with an attachment strictly between w0w_0 and xx is a bridge with a live entry above w0w_0 at the end. Condition 2 of Lemma 1 can be met if and only if no component has such bridges in both colours, which is the test of line L15. So line L15 fails exactly when S(e)S(e) is not strongly planar.

When it does not fail, line L16 returns the attachments of S(e)S(e) below xx: in each block, the list with no entry above w0w_0 contains only copies of w0w_0, since all live entries are at least w0w_0, so taking the other list loses only copies of w0w_0, and w0w_0 is appended when w0≠xw_0 \ne x. The result is sorted by (a) and (c). The bridges whose attachments all left the lists contribute attachments at least xx, which are not attachments of S(e)S(e) below xx. □\square

Theorem. HT-PLANAR returns true if and only if GG is planar.

Proof. By induction on the size of the subtree of yy, Lemma 3 holds for every call; the innermost calls have only back arcs as bridges, where the hypothesis on recursive calls is empty. For the call SEGMENT(0,1)(0, 1), the segment is the whole block, so line L6 fails exactly when the block is not planar. With the reduction to blocks, the algorithm returns true exactly when every block, and so the graph, is planar. □\square

Termination

The search, the low points and the sort are finite loops. Each tree arc starts at most one call of SEGMENT, and each call walks a finite spine. Each round of the loop in ADD-BLOCK either pops a block or leaves the loop, and each block is pushed once. Line L14 deletes list entries, which were created once each. So the algorithm stops, in O(n+m)O(n + m) steps by the count on the main page. ■\blacksquare

Back to Hopcroft-Tarjan planarity test