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 vertices has at most 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 , which is planar. So it suffices to show that lines L3 to L6 accept a biconnected graph with vertices exactly when is planar.
The palm tree
Fix the DFS of line L3; vertices are their preorder numbers, and is the subtree of . In an undirected DFS every non-tree edge joins a vertex and one of its ancestors, so it becomes a back arc with a proper ancestor of 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 has an arc other than the arc from its parent, and for every tree arc with .
Proof. A root with two children is a cut vertex. A vertex of degree 1 would make its neighbour a cut vertex. If for , then no back arc leaves above , and separates from .
Fact 2 (spines). After the sort of line L5, the first arc out of every vertex goes to a child with , or is a back arc to . So line L8 is well defined, the spine of ends with a back arc where , every vertex on the spine has , and unless .
Proof. is the minimum of , of the ends of back arcs out of , and of over the children. The cost of an arc is or where is the end of the back arc or , so the arc of least cost reaches the minimum of these, which is below by Fact 1. The walk goes down the tree, so it ends; it ends at a back arc. The last statement is Fact 1.
Segments and their attachments. For a tree arc , is with the arc and all back arcs out of ; its attachments are the ends of those back arcs that are smaller than . For a back arc , is the arc and . Every attachment of lies on the tree path from to , the stem , because it is an ancestor of and at least . Let .
Definition. is strongly planar if has a planar embedding in which every edge and vertex of lies on the boundary of the outer face. For , is all of and is the single vertex 0, so is planar if and only if is strongly planar (any face can be made the outer face).
Decomposition along the spine
Fix a tree arc with spine and closing back arc . Let be the cycle formed by the stem, the arc , the spine and the arc . Call the arcs out of spine vertices that are not on the emanating arcs , with segments .
Fact 3. , two different meet only in vertices of , and every meets exactly in its base (the spine vertex it leaves) and its attachments. So the are the bridges of in .
Proof. The subtrees of the children of spine vertices that are not on the spine are disjoint and disjoint from , and each back arc from them ends at an ancestor at least (by Fact 2 applied to the child, ), which lies on . Each tree-arc segment is connected through its subtree.
Two bridges and conflict if their contact sets with (base and attachments) have 3 vertices in common, or contain 4 vertices in this cyclic order on with in one set and 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 in which and are on the same side of , they do not conflict.
Proof. By the Jordan curve theorem, bounds a disc on that side, and and are drawn inside it. In the alternating case, contains a path from to through the disc, which cuts the disc into two parts, with and on different boundary arcs; a path in from to would have to cross it. In the case of 3 common vertices , each bridge contains a vertex joined to 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 on its boundary.
Fact 5. Suppose bridges pairwise do not conflict, and each has a planar embedding with inside . Then has a planar embedding with all of them inside .
Proof. Induction on . Embed with all bridges inside. The faces inside are bounded by arcs of and paths through bridges. Because conflicts with no , all contact vertices of lie on the boundary of one of these faces: if they did not, two of them would be separated by some , which gives an alternating quadruple, or all three regions around a branch vertex of would be needed, which gives 3 common contact vertices. This face is a disc whose boundary meets in an arc that contains all contact vertices of in their order along , so the embedding of inside can be copied into it, after a homeomorphism of discs that matches the contact vertices.
Lemma 1 (decomposition). is strongly planar if and only if
- every is strongly planar, and
- the bridges can be split into two classes and with no conflict inside a class, such that every bridge with an attachment with is in .
Proof. (Only if.) Take an embedding of with the stem on the outer face. By Fact 3 and the Jordan curve theorem, each bridge is drawn inside or outside ; let be the bridges on the side of that contains the outer face and the others. By Fact 4 there is no conflict inside a class. A bridge in with an attachment strictly inside the stem contains a path from to its base, which lies off the stem; with , this path cuts the side of that holds the outer face so that the stem edges before and after are on the boundaries of different faces, which contradicts the choice of the embedding. So such bridges are in . For condition 1, restrict the embedding to : the side of without is a face with all of on its boundary, so the stem of , which is a subpath of from its lowest attachment to its base, is on the boundary of one face, and is a subgraph whose stem is still on that face.
(If.) For each , take an embedding of with its stem on the outer face. The rest of is a path between the two ends of that avoids ; draw it in the outer face, close to the boundary, which gives an embedding of with on one side, and by a reflection on either side. Apply Fact 5 to inside and to outside (outside is a disc on the sphere). The stem has no attachment from in its interior, so after the bridges of are drawn, all stem edges still border one face outside ; make it the outer face.
Conflicts along the spine. By Fact 3 and the order of numbers along (the stem and spine are a tree path with increasing numbers, closed by the arc from to ), conflict has a simple form. Say is handled after by line L11, so the base of is at most the base of . Let .
Lemma 2. and conflict if and only if has an attachment with .
Proof. (If.) If , the four vertices alternate, with in and in . If , then came first in the sort, so of is at most . If it is smaller, the vertices alternate. If it equals , then has the attachment strictly between and , so its cost is odd, and , which comes later with the same , has odd cost too: it has an attachment with . If , the common vertices are three; otherwise 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 is at most or at least . All contact vertices of lie in the interval of , and all contact vertices of lie outside the open interval . Then two vertices of never separate two vertices of on , and the only possible common vertices are and . So they do not conflict.
The stack of blocks
Consider one call SEGMENT. Say that a bridge is seen once line L13 has handled it, and that its live attachments are those below the current vertex of line L10. Line L14 removes, when the walk moves from to its parent, exactly the attachments equal to the parent, so the lists on 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 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 with list , sorted by induction (for a back arc, has one element). Its minimum is . While is not empty, look at the top block and let be the minimum of the current left list of .
First, as long as the left list of has only taken left lists that are empty or have all entries at most ; and by (c) every entry of a block below is at most every entry of . So if the loop has combined a block with a nonempty left list, then is the minimum of that list, and every remaining block has all entries at most , so the loop ends at line L22 in the next round. Hence the comparisons with give the same result as comparisons with for every block that is compared while it matters.
By Lemma 2, conflicts with a seen bridge exactly when that bridge has a live attachment above . (Attachments that are not live are at least the current vertex , and cannot lie in .) So conflicts with some bridge on the left of if and only if the head of the left list of exceeds , and the same on the right.
- If both sides of have such a bridge, the component of with has an odd cycle: a path inside the component between two bridges of different colours has odd length, and closes it with two edges. The conflict graph is not bipartite, and lines L19 and L20 fail. By Lemma 1, is not strongly planar, and failing is correct.
- If only the left side conflicts, line L19 swaps the sides of , which is another valid colouring of the same component.
- If the right side conflicts after this, joins the component of on the side opposite to , and line L21 merges it: the left lists contain and the non-conflicting colour of , the right lists the conflicting colour. The concatenations stay sorted, because the left list of has all entries at most (otherwise line L20 would have failed) and the right list of comes from blocks above , whose entries are at least those of by (c).
- If neither side conflicts, then by (c) no lower block conflicts either, because all their entries are at most the entries of , which are at most . The loop ends.
The pushed block holds and every component that conflicts with it, so (b) holds. Its entries are at least , and everything below it is at most , 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.
Lemma 3. Assume that every recursive call made by SEGMENT returns the attachments of its segment below its base, sorted, when the segment is strongly planar, and fails otherwise. Then SEGMENT does the same for .
Proof. If some is not strongly planar, a recursive call fails, and by Lemma 1 so does . 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 are the components of the bridges that still have attachments below ; the colouring of each component can be chosen independently, up to a swap. A bridge with an attachment strictly between and is a bridge with a live entry above 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 is not strongly planar.
When it does not fail, line L16 returns the attachments of below : in each block, the list with no entry above contains only copies of , since all live entries are at least , so taking the other list loses only copies of , and is appended when . The result is sorted by (a) and (c). The bridges whose attachments all left the lists contribute attachments at least , which are not attachments of below .
Theorem. HT-PLANAR returns true if and only if is planar.
Proof. By induction on the size of the subtree of , 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, 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.
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 steps by the count on the main page.