Fixed-Parameter Algorithms for Longest Heapable Subsequence and Maximum Binary Tree

Karthekeyan Chandrasekaran∗ Elena Grigorescu† Gabriel Istrate‡

Shubhang Kulkarni† Young-San Lin† Minshen Zhu†

November 23, 2020

Abstract A heapable sequence is a sequence of numbers that can be arranged in a min-heap data structure. Finding a longest heapable subsequence of a given sequence was proposed by Byers, Heeringa, Mitzenmacher, and Zervas (ANALCO 2011) as a generalization of the well-studied longest increasing subsequence problem and its complexity still remains open. An equivalent formulation of the longest heapable subsequence problem is that of finding a maximum-sized binary tree in a given permutation (permutation DAG). In this work, we study parameterized algorithms for both longest heapable subsequence as well as maximum-sized binary tree. We show the following results: 1. The longest heapable subsequence problem can be solved in kO(log k)n time, where k is the number of distinct values in the input sequence. 2. We introduce the alphabet size as a new parameter in the study of computational prob- lems in permutation DAGs. Our result on longest heapable subsequence implies that the maximum-sized binary tree problem in a given permutation DAG is fixed-parameter tractable when parameterized by the alphabet size. 3. We show that the alphabet size with respect to a fixed topological ordering can be com- puted in polynomial time, admits a min-max relation, and has a polyhedral description. 4. We design a fixed-parameter algorithm with run-time wO(w)n for the maximum-sized bi- nary tree problem in undirected graphs when parameterized by w. Our results make progress towards understanding the complexity of the longest heapable sub- sequence and maximum-sized binary tree in permutation DAGs from the perspective of param- eterized algorithms. We believe that the parameter alphabet size that we introduce is likely to be useful in the context of optimization problems defined over permutation DAGs.

1 Introduction

The longest increasing subsequence is a fundamental computational problem that has led to nu- merous discoveries in algorithms as well as combinatorics. The motivation behind this work is a

∗University of Illinois, Urbana-Champaign, Email: [email protected] †Purdue University, Email: {elena-g, kulkar17, lin532, zhu628}@purdue.edu ‡West University of Timişoara, Romania, and the e-Austria Research Institute. Email: [email protected]

1 generalization of the longest increasing subsequence problem, known as the longest heapable subse- quence problem, introduced by Byers, Heeringa, Mitzenmacher, and Zervas [BHMZ11]. We begin by defining this problem. A rooted tree whose nodes are labeled with values has the heap prop- erty if the value of every node is at least that of its parent; a sequence of natural numbers is heapable if the elements can be sequentially placed one at a time to form a binary tree with the heap property. For example, the sequence 1, 5, 3, 2, 4 is not heapable while the sequence 1, 3, 3, 2, 4 is heapable. Throughout this work, we will be interested in sequences whose elements are natural numbers. In the longest heapable subsequence problem, the goal is to find a longest heapable sub- sequence of a given sequence. Although the longest increasing subsequence problem is solvable in polynomial-time, the complexity of the longest heapable subsequence problem is still open. The problem of verifying if a given sequence is heapable, although non-trivial, is solvable effi- ciently using a greedy approach [BHMZ11]. In order to address the longest heapable subsequence problem, Porfilio [Por15] observed a connection to a graph problem on directed acyclic graphs (DAGs). The permutation DAG associated with a sequence σ = (σ(1), σ(2), . . . , σ(n)), denoted PermDAG(σ), is obtained by introducing a vertex ti for every sequence element i ∈ [n], and arcs (tj, ti) for every i, j ∈ [n] such that i < j and σ(i) ≤ σ(j). We recall that a directed graph G is a permutation DAG if there exists a sequence τ such that G is isomorphic to PermDAG(τ). We need the notion of a binary tree in a given directed graph G: a subgraph T of G is an r-rooted binary tree if r is the unique vertex in T with no outgoing edges, every vertex in T has a unique directed to r in T , and every vertex in T has in-degree at most 2 in T ; the size of T is the number of vertices in T . Porfilio showed that a longest heapable subsequence of a given sequence σ is equivalent to a maximum-sized binary tree in PermDAG(σ). This result raises the question of whether one can efficiently find a maximum-sized binary tree in a given permutation DAG. The complexity of this problem also remains open. In an earlier work [CGI+20], we showed that maximum-sized binary tree in arbitrary input directed graphs is fixed-parameter tractable when parameterized by the solution size: we gave a 2knO(1) time algorithm, where k is the size of the largest binary tree and n is the number of vertices in the input graph. This also implies that the longest heapable subsequence problem is fixed-parameter tractable when parameterized by the solution size. In this work, we consider two alternative parameterizations for the maximum-sized binary tree/longest heapable subsequence problem. Firstly, we show that the longest heapable subsequence problem is fixed-parameter tractable when parameterized by the number of distinct values in the input sequence. Next, we introduce alphabet size as a new parameter in the study of computational problems in permutation DAGs. Our algorithmic result for longest heapable subsequence problem implies that the maximum-sized binary tree problem in a given permutation DAG is fixed-parameter tractable when parameterized by the alphabet size. We currently do not know how to compute the alphabet size of a given permutation DAG. As a stepping stone towards computing alphabet size, we show that alphabet size with respect to a fixed topological ordering can be computed efficiently and it also admits a min-max relation and a polyhedral description. Our results suggest that alphabet size is an interesting parameterization for computational problems defined on permutation DAGs and merits a thorough study. Finally, we design a fixed-parameter algorithm for the maximum-sized binary tree problem in undirected graphs when parameterized by treewidth. We elaborate on our contributions now.

2 1.1 Results Our first result shows that the longest heapable subsequence problem is fixed-parameter tractable when parameterized by the number of distinct values in the sequence. Theorem 1. There exists an algorithm that takes as input an n-length sequence τ with k distinct values and returns a longest heapable subsequence of τ in time (k + 1)! · k · O(n). Equivalently, our algorithm returns a maximum-sized binary tree in PermDAG(τ). We emphasize that our algorithm also works in the streaming model of computation—i.e., when the input sequence arrives one by one and the algorithm has to find the longest heapable subsequence of the input that has arrived so far (in particular, the algorithm is not allowed to store the entire input sequence). The space complexity of our algorithm is (k + 1)! · k · O(log n) and is logarithmic for constant k. Theorem1 can also be viewed as a fixed-parameter algorithm to find a maximum-sized binary tree in a given permutation DAG when parameterized by alphabet size. We define this parameter now. We note that for a fixed permutation DAG G, there could be several sequences τ such that G(τ) is isomorphic to G. This motivates our parameterization for permutation DAGs: The alphabet size of a permutation DAG G, denoted α(G), is defined as follows (see Figure1 for an example):

α(G) := min{k : ∃ τ ∈ [k]n with PermDAG(τ) being isomorphic to G}.

We recall that directed graphs G = (V,A) and G0 = (V,0 ,A0) are isomorphic if there exists a bijection φ : V 0 → V such that (u0, v0) ∈ A0 if and only if (φ(u0), φ(v0)) ∈ A. Theorem1 also implies that there exists an algorithm that takes as input, an n-vertex permutation DAG G and a sequence τ with α(G) = k distinct values such that G(τ) is isomorphic to G and returns a maximum-sized binary tree in G in time (k + 1)! · k · O(n).

b

a d c

G

a b c d c a d b

PermDAG(τ1 = (2, 3, 1, 2)) PermDAG(τ2 = (2, 1, 2, 1))

Figure 1: Let G be the input permuation DAG. The graph G is isomorphic to PermDAG(τ1) and PermDAG(τ2). The sequence τ1 = (2, 1, 2, 1) uses only two distinct values, which turns out to be the minimum, so α(G) = 2.

Next, we explore algorithmic aspects of our newly defined parameter, namely the alphabet size. A natural question is whether the alphabet size of a given permutation DAG can be computed in polynomial-time. Currently, we do not know the answer to this question. However, there is a natural related problem that seems like a stepping stone towards resolving the complexity of computing the alphabet size of permutation DAGs. We define this related problem now.

3 We recall that every DAG G = (V,A) admits a topological ordering—a bijection γ : V → [n] corresponding to a permutation of its n vertices such that every arc (v, u) ∈ A has γ(u) < γ(v) (i.e., all edges are oriented in the backward direction with respect to the ordering defined by γ). For a fixed topological ordering γ : V → [n] of an n-vertex permutation DAG G, we define the γ-alphabet size of G, denoted α(G, γ), as follows (see Figure2 for an example illustrating the definition):  n 0 α(G, γ) := min k : ∃ τ ∈ [k] with PermDAG(τ) = ({t1, . . . , tn},A ) being isomorphic to G −1 o under the mapping φ : {t1, . . . , tn} → V (G) given by φ(ti) = γ (i) ∀ i ∈ [n] .

We note that the optimization problem α(G, γ) may be infeasible in which case, we use α(G, γ) = ∞ as the convention. The following relationship between alphabet size and γ-alphabet size is immediate for a permutation DAG G: α(G) = min{α(G, γ) : γ is a topological ordering of G}.

c a d b γ−1(1) γ−1(2) γ−1(3) γ−1(4)

φ(t1) φ(t2) φ(t3) φ(t4)

t1 t2 t3 t4

Figure 2: The graph at the top corresponds to G in topological order γ where γ(c) = 1, γ(a) = 2, γ(d) = 3, and γ(b) = 4. The graph at the bottom corresponds to PermDAG(τ = (2, 1, 2, 1)). Note that the two graphs are isomorphic under the mapping φ : {t1, t2, t3, t4} → V (G) given by −1 −1 −1 −1 φ(t1) = γ (1) = c, φ(t2) = γ (2) = a, φ(t3) = γ (3) = d, and φ(t4) = γ (4) = b (shown by dotted lines). We have that α(G, γ) = 2 and is achieved by the sequence τ.

As a stepping stone towards understanding α(G), we show that α(G, γ) for a given topological ordering γ of G (i.e., the γ-alphabet size of G) can be computed in polynomial time. Theorem 2. There exists a polynomial-time algorithm that takes a permutation DAG G and a topological ordering γ of G as input and detects if α(G, γ) is finite and if so, then returns a sequence that achieves α(G, γ). Our algorithm underlying Theorem2 also reveals a min-max relation for γ-alphabet size that we describe now. Let G = (V,A) be a permutation DAG with n vertices and let γ : V → [n] be a −→ topological ordering of G such that α(G, γ) is finite. Let E := {(u, v) : γ(u) < γ(v) and (v, u) 6∈ A} −→ −→ and H(G, γ) := (V,A ∪ E ). We note that H(G, γ) is a tournament.1 Also, let w : A ∪ E → {0, 1} be an arc weight function for H(G, γ) defined as follows: ( 0 if e ∈ A, w(e) := −→ 1 if e ∈ E.

1A tournament is a directed graph H = (V,A) in which we have exactly one of the two arcs (v, u) and (u, v) for every pair of vertices u, v ∈ V .

4 Then, we have the following min-max relation for the minimization problem corresponding to α(G, γ).

Theorem 3. Let G = (V,A) be a permutation DAG and γ be a topological ordering of V such that α(G, γ) is finite. Then,   X  α(G, γ) = 1 + max w(e) : P is a path in H(G, γ) . e∈P 

In addition to the algorithm and the min-max relation, we give a polyhedral result (see Theorem 5 in Section 3.3) that also leads to an LP-based algorithm to compute α(G, γ). We believe that alphabet size, as a parameter, is likely to be useful in the context of permutation DAGs and consequently, merits a thorough study. We view Theorems2 and3 as stepping stones towards the problem of efficiently computing the alphabet size of a given permutation DAG and Theorem1 to be an application of this parameter. Resolving the complexity of computing the alphabet size is an intriguing open problem. Next, we address the maximum binary tree problem in undirected graphs with bounded treewidth. Here, we are given an undirected graph G and the goal is to find a subgraph that is a binary tree with maximum number of nodes. An undirected graph is said to be a binary tree if the graph is acyclic and every vertex has degree at most 3. We observe that the existence of a binary tree can be expressed as a monadic second order logic property and hence, extensions of Courcelle’s theorem [DF12] can be used to obtain an algorithm for maximum-sized binary tree that runs in time f(w)n for n-vertex undirected graphs with treewidth w for some function f(w). However, the run-time dependence f(w) is at least doubly exponential on the treewidth w in this approach. We improve this dependence substantially.

Theorem 4. Given a tree decomposition of an n-vertex undirected graph G with treewidth w, there exists an algorithm to find a maximum-sized binary tree in G in time wO(w)n.

1.2 Related Work Heapability of integer sequences was introduced in [BHMZ11] and has been investigated further in [IB15, Por15, IB16, BGGS16, BS18, BIR18, BBD+20]. Heapability of integer sequences can be decided by a simple greedy algorithm [BHMZ11] (see also [IB16] for an alternate approach based on integer programming, and [BBD+20] for connections with Dilworth’s theorem and an algorithm based on network flows). Besides introducing the longest heapable subsequence problem, [BHMZ11] also showed that deciding if a sequence can be arranged in a complete binary heap is NP-complete. Heapable sequences of integers can be regarded as “loosely increasing”. The celebrated Ulam- Hammesley problem aims to understand the length of the longest increasing sequence of a random permutation. This has a long history with deep connections to many areas of science (e.g., see [Rom15]). [BHMZ11] studied the counterpart of this problem for heapability: they showed that the longest heapable subsequence of a random permutatoin of length n is of size n − o(n) with high probability and it can also be found in an online fashion. As mentioned earlier, Porfilio [Por15] showed that the longest heapable subsequence is equiva- lent to solving the maximum-sized binary tree problem in permutation DAGs. In an earlier work [CGI+20], we showed that the maximum-sized binary tree problem is NP-hard in DAGs and showed

5 further inapproximability results. We also gave a fixed-parameter algorithm for the maximum binary tree problem when parameterized by the solution size. Furthermore, we designed a polynomial-time algorithm to solve the maximum-sized binary tree problem in the special class of bipartite permu- tation graphs. It is also known that maximum-sized binary tree problem in DAGs induced by sets of intervals can be solved in polynomial time [BBD+20].

Organization. In Section2, we present the fixed-parameter algorithm for longest heapable sub- sequence when parameterized by the alphabet size and prove Theorem1. In Section3, we address the problem of computing γ-alphabet size, present a min-max relation, and a polyhedral description for the same. In Section4, we present a fixed-parameter algorithm for computing maximum-sized binary tree in a given undirected graph when parameterized by treewidth.

2 LHS for small alphabet sizes

In this section we prove Theorem1. Theorem 1. There exists an algorithm that takes as input an n-length sequence τ with k distinct values and returns a longest heapable subsequence of τ in time (k + 1)! · k · O(n). Equivalently, our algorithm returns a maximum-sized binary tree in PermDAG(τ). Before explaining our algorithm, we first establish certain useful definitions. We will denote a directed binary tree where each node is labeled by some natural number in [k] such that the labels on every leaf to root path is non-increasing as a heap over alphabet [k]. Definition 1 (Extended Binary Tree). Given a rooted non-empty binary tree T , we define the extended binary tree Ext(T ) by introducing new leaf nodes in a way that makes every node in T have exactly 2 children. The nodes in T are also referred to as internal nodes, and the new leaf nodes are referred to as external nodes. Definition 2 (Shape). Given a heap H over alphabet [k], we define its shape as a tuple x = (x0, x1, . . . , xk−1, xk), where xi is the number of external nodes whose parents have label i in Ext(H). We also follow the convention that the shape of an empty heap is (1, 0,..., 0).

1

3 c

a b

Figure 3: Given a binary tree T composed of two nodes 1 and 3, the extended binary tree Ext(T ) has two internal nodes 1 and 3, and three new external nodes a, b, and c. Suppose k = 4. The shape of the heap above is x = (x0, x1, x2, x3, x4) = (0, 1, 0, 2, 0) because the parent of a and b has label 3 and the parent of c has label 1.

Intuitively, an external node represents the location of a potential future insertion into the heap. Since an insertion is effectively replacing an external node with a new internal node (thus introducing

6 two new external nodes), it is captured by simple manipulations of shapes. This naturally leads us to defining insertions with respect to shapes. Given a shape x = (x0, . . . , xk) and labels a ≤ b, the shape obtained by inserting b under a, denoted x(a ← b), is defined as  (x0, . . . , xa−1, xa + 1, xa+1, . . . , x ) if xa > 0 and a = b,  k x (a ← b) := (x0, . . . , xa−1, xa − 1, xa+1, . . . , xb−1, xb + 2, xb+1, . . . , xk) if xa > 0 and a < b,  ⊥ if xa = 0.

For example, consider the shape x = (0, 1, 0, 2, 0). The shape x (1 ← 2) is (0, 0, 2, 2, 0), and the shape x(2 ← 3) is ⊥. Given a heap H and a sequence a = (a1, . . . , an), consider a longest subsequence of a which can be sequentially inserted to H as leaf nodes while maintaining the heap property. We will call such a subsequence a longest heapable subsequence starting from H. We observe that the longest heapable subsequence of a given sequence starting from H depends only the shape of H and not the precise structure of H (i.e., the optimum does not change for two different heaps H1 and H2 sharing the same shape). Therefore, it is equivalent and also convenient to consider the longest heapable subsequence problem starting from an initial shape instead of an initial heap. This line of thought also suggests a natural approach where the subproblems are specified by shapes. To analyze the running time, we need to upper bound the number of subproblems, which is the same as the number of distinct shapes. As a starting point, the number of distinct shapes can be upper bounded by nO(k). This is because in any n-node heap H there are exactly n + 1 external nodes in Ext(H) (an elementary property of binary trees). Therefore the number of shapes is bounded by the number of non-negative integral solutions to x0 + x1 + ... + xk = n + 1, which is nO(k). Although this estimate seems like a very crude upper bound, bringing down the estimate into the fixed-parameter regime (i.e., f(k)nO(1)) seems very difficult. We employ additional ideas to design a fixed-parameter algorithm. Consider the longest heapable subsequence problem starting from initial shape x = (x0, x1, . . . , xk). Suppose that the initial shape also satisfies the condition that xj ≥ k − j + 1 for some j ∈ [k]. Our key observation is that all elements with labels at least j are heapable from x: we can reserve an external node attached to j for each label v ∈ {j, j + 1, . . . , k}, which can then be used to form a chain of elements with the same label v. Essentially, once we have reached the shape x, there are “infinitely” many external nodes available for future elements with label at least j, and hence, we no longer need to keep track of the precise values of xj, xj+1, . . . , xk. This motivates the following notion of refined shapes.

Definition 3 (Refined shapes). A tuple (x0, x1, . . . , xk) is a refined shape (over alphabet size k) if for each j ∈ {0, 1, . . . , k} we have xj ∈ {0, 1, . . . , k − j} ∪ {∞}, and xj = ∞ implies x` = ∞ for all ` > j. We will write Xk for the set of all refined shapes over alphabet size k. The operation refine(·) introduced below formalizes the intuition discussed earlier.

Definition 4. Let x = (x0, . . . , xk) be such that xj ∈ N ∪ {∞} for all j. Let ( x if x ≤ k − j for all j refine(x) := j . (x0, . . . , xj0−1, ∞, ∞,...) j0 is the smallest j such that xj ≥ k − j + 1

7 We remark that refine(x) ∈ Xk for any x. Next we define insertions with respect to refined shapes. Given a refined shape x = (x0, . . . , xk) and labels a ≤ b, the shape obtained by inserting b under a, denoted x(a ← b), is defined as

 refine (x0, . . . , xa−1, xa + 1, xa+1, . . . , x ) if xa > 0 and a = b,  k−1 x (a ← b) := refine (x0, . . . xa−1, xa − 1, xa+1, . . . , xb−1, xb + 2, xb+1, . . . , xk−1) if xa > 0 and a < b,  ⊥ if xa = 0. where we followed the convention that ∞ > 0 and ∞ + c = ∞ for any constant c. Now we are ready to state the dynamic programming algorithm. In the following, we fix (a1, a2, . . . , an) as the input sequence. For x ∈ Xk and i ∈ [n] define LHS[i, x] to be the length of the longest heapable subsequence in the prefix sequence (a1, a2, . . . , ai), with an additional con- straint that the refined shape of the heap constructed from the subsequence should be x. We write LHS[i, x] = −∞ if there is no feasible solution (i.e. shape x is not reachable by any subsequence of (a1, . . . , ai)). With this definition, the longest heapable subsequence of the given sequence has length maxx∈Xk LHS[n, x]. Our goal now is to compute LHS[n, x] for each x ∈ Xk. For a label v ∈ {1, 2, . . . , k} and two refined shapes x and x0, we say that x is reachable from x0 via an insertion of v if there exists b ≤ v such that x0(b ← v) = x. We denote by prev(x, v) the set of refined shapes from which x is reachable via an insertion of v. We show that LHS satisfies the following recurrence relation.

Lemma 1. For every i ∈ [n] and x ∈ Xk, we have that ( ) LHS[i, x] = max LHS[i − 1, x], max LHS[i − 1, x0] + 1 . 0 x ∈prev(x,ai)

n  0 o 0 Proof. We will show that LHS[i, x] ≤ max LHS[i − 1, x], maxx ∈prev(x,ai) LHS[i − 1, x ] + 1 as the other direction is trivial. Let us fix an optimal heapable subsequence s of (a1, . . . , ai). If ai does not belong to s, it must be the case that s is also an optimal heapable subsequence of (a1, . . . , ai−1). In this case LHS[i, x] = LHS[i−1, x]. If ai belongs to s, we further fix an optimal heap H (with refined shape x) and assume that ai is inserted under an element with value b in H. Removing ai from H 0 0 0 0 results in a heap H with a shape x satisfying x (b ← ai) = x. In particular, x ∈ prev(x, ai). In 0  0 0 this case, LHS[i, x] = LHS[i − 1, x ] + 1 ≤ maxx ∈prev(x,ai) LHS[i − 1, x ] + 1. Proof of Theorem1. Given Lemma1, it remains to show that the recurrence relation can be im- plemented in time (k + 1)! · k · O(n). We observe that the number of subproblems is bounded by 0 O(n|Xk|). The set prev(x, ai) can be enumerated in time O(k) by inverting the operation x (b ← ai) for each b ≤ ai. Therefore it suffices to show that |Xk| = O((k + 1)!). In order to bound the size of Xk, we observe that for every x = (x0, x1, . . . , xk) ∈ Xk, we have that x0 = 0 unless x = (1, 0,..., 0), and that xj ∈ {0, 1, . . . , k − j} ∪ {∞} for j ≥ 1. Therefore Qk |Xk| ≤ 1 + j=1(k − j + 2) = (k + 1)! + 1. An implementation of this dynamic programming algorithm is given by Algorithm1. This implementation requires space complexity O((k + 1)!n · log n), which can be optimized to O((k + 1)! · log n) using a standard rolling array technique: we observe that in the recurrence relation,

8 LHS[i, x] depends only on LHS[i − 1, x0] but not on LHS[j, x0] for any j < i − 1. Therefore the values LHS[i − 2, x] become obsolete and the space can be recycled to store new values. Essentially, we only need two arrays LHS1[x] and LHS2[x] and store new values alternately between them. Remark. We note that our dynamic programming algorithm also works in the streaming model, where the elements of the input sequence have to be processed one by one without storing them in memory and the goal is to find the length of a longest heapable subsequence of the input that has arrived so far. For constant alphabet size k, the space complexity of our algorithm is O (log n).

Input: A sequence a = (a1, . . . , an) such that ∀i ∈ [n], ai ∈ {1, 2, . . . , k}. Output: The length of longest heapable subsequence in a.

LHS(a1, a2, . . . , an) : 1: X ← (1, 0,..., 0) . X maintains a set of reachable refined shapes 2: LHS ← integer array of size n × (k + 1) × k × ... 2 × 1 3: LHS[0, (1, 0,..., 0)] ← 0 4: for i ← 1 to n do . DP main body 5: for x ∈ X do 6: LHS[i, x] ← dp[i − 1, x] . Discard ai 7: for x ∈ X do  0 0 8: for b ∈ b : 0 ≤ b ≤ ai, xb0 > 0 do 0 0 9: x ← x(b ← ai) . Insert ai under b to reach refined shape x 10: if x0 ∈/ X then . First time reaching shape x0 11: X ← X ∪ x0 12: LHS[i, x0] ← LHS[i − 1, x] + 1 13: else if LHS[i, x0] < LHS[i − 1, x] + 1 then 14: LHS[i, x0] ← LHS[i − 1, x] + 1 return max LHS[n, x]: x ∈ X Algorithm 1: Longest Heapable Subsequence for Alphabet Size k

3 Alphabet Size of Permutation DAGs

In this section, we consider the problem of computing the γ-alphabet size of a permutation DAG where γ is a given topological ordering of G. We give an efficient algorithm in Section 3.1, a min-max relation in Section 3.2, and a polyhedral description in Section 3.3. We recall that a directed graph G is a permutation DAG if there exists a sequence σ such that PermDAG(σ) is isomorphic to G. We note that permutation DAGs are transitively closed, i.e., for a permutation DAG G = (V,A), if (u, v), (v, w) ∈ A, then (u, w) ∈ A. In order to recognize if a given DAG is a permutation DAG, we need the notion of umbrella-free ordering defined below (see Figure4 for an example). This notion will also help us recognize if α(G, γ) is finite.

Definition 5 (Umbrella-free Order). Let G = (V,A) be an n-vertex DAG. An order γ : V → [n] of V is umbrella-free if for all (v, u) ∈ A and for every vertex w ∈ V with γ(u) < γ(w) < γ(v), either (w, u) ∈ A or (v, w) ∈ A (or both).

9 u w v a b c d a b d c (a) (b)

Figure 4: (a) Depicts the scenario when the triple (u, w, v) is an umbrella. (b) Shows two topological orderings of the same DAG. The order (a, b, c, d) is not umbrella-free due to the (highlighted) umbrella (b, c, d), while the the order (a, b, d, c) is umbrella-free.

The following lemma characterizes permutation DAGs in terms of the existence of an umbrella- free topological ordering.

Lemma 2 ([PLE71, Gol80]). Let G = (V,A) be a transitively closed DAG. Then G is a permutation DAG if and only if there exists an umbrella-free topological ordering of G. Moreover, there exists a polynomial-time algorithm to verify if a given DAG is a permutation DAG and if so, then construct an umbrella-free topological ordering.

Lemma2 implies that α(G, γ) is finite if and only if γ is an umbrella-free topological ordering of G.

3.1 Computing γ-alphabet size In this section, we address the problem of computing the γ-alphabet size of a given permutation DAG, where γ is a topological ordering of G. The following is the main result of this section.

Theorem 2. There exists a polynomial-time algorithm that takes a permutation DAG G and a topological ordering γ of G as input and detects if α(G, γ) is finite and if so, then returns a sequence that achieves α(G, γ).

We note that umbrella-freeness of a given topological ordering can be verified in polynomial- time, so we may henceforth assume that the input γ is in fact an umbrella-free topological ordering of G. We will give an iterative algorithm to compute α(G, γ). We observe that computing α(G, γ) involves assigning a value to each vertex of G such that the sequence obtained by ordering the values of the vertices in the same order as γ gives the same permutation DAG as G. At each iteration, our algorithm will choose a vertex of G and assign a value to it. The next definition will allow us to formally define the choice of this vertex.

Definition 6 (Fully Suffix Connected Vertex). Let G = (V,A) be a permutation DAG and γ be a topological ordering of G. A vertex u ∈ V is fully suffix connected if for all v ∈ V such that γ(v) > γ(u), we have (v, u) ∈ A. The γ-least fully suffix connected (γ-LFSC) vertex is the fully suffix connected vertex u with smallest γ(u).

See Figure5 for an example showing fully suffix connected vertices. We note that γ-LFSC is unique. The following lemma states a useful property of the γ-LFSC vertex.

Lemma 3. Let G = (V,A) be a permutation DAG and γ be an umbrella-free topological ordering of G. Then, the γ-LFSC vertex has no outgoing arcs in G.

10 Figure 5: The DAG in the given topological order γ has 3 fully suffix connected vertices that are depicted as filled circles. The leftmost fully suffix connected vertex is the (unique) γ-LFSC vertex.

Proof. Let v ∈ V be the γ-LFSC and suppose for contradiction that v has an outgoing arc in G. Let u be the vertex with largest γ(u) such that (v, u) ∈ A. We note that γ(u) < γ(v) since γ is a topological ordering. We will show that such a vertex u is fully suffix connected and hence contradicts the γ-least fully suffix connected property of vertex v. We first show that for every vertex w ∈ V such that γ(w) ≥ γ(v), we have (w, u) ∈ A. For w = v, this follows since (v, u) ∈ A by the choice of u. Let w be a vertex such that γ(w) > γ(v). Since v is fully suffix connected, we have that (w, v) ∈ A. Also, since G is a permutation DAG, it is transitively closed. Hence, (v, u) ∈ A implies that (w, u) ∈ A. Next, we show that for every vertex w ∈ V such that γ(u) < γ(w) < γ(v), we have (w, u) ∈ A. Let w be a vertex such that γ(u) < γ(w) < γ(v). By assumption, the ordering γ is umbrella-free. Thus, at least one of (w, u) or (v, w) must exist in A. However, (v, w) 6∈ A as otherwise, w will contradict the choice of vertex u. Therefore, (w, u) ∈ A.

We now discuss a high level overview of our iterative greedy algorithm for computing α(G, γ). During the first iteration, the algorithm greedily chooses the γ-LFSC vertex v1 (say) in G1 := G to assign the smallest alphabet, namely σ(v1) = 1. The vertex v1 and its incident edges are deleted from G1 to form G2, and the remaining n − 1 vertices V \{v} are ordered in the same relative order as γ—denote this ordering as γ2. In the second iteration, our algorithm greedily chooses the γ2-LFSC vertex v2 (say) in G2 to assign the next smallest alphabet—the next smallest alphabet is chosen based on whether v2 lies to the left or right of v1: if v2 lies to the left of v1 with respect to γ, then we set σ(v2) = σ(v1) + 1, otherwise we set σ(v2) = σ(v1). This iterative removal and assignment process continues for n iterations, i.e., until all vertices are removed from G. The final output sequence will just be the sequence of assigned values in the order of vertices in γ. Before presenting our final complete algorithm (Algorithm2), we introduce a definition to formalize the reordering of vertices after removing a vertex from G — this will allow us to obtain γi+1 from γi. Definition 7 (Projected order). Let G = (V,A) be an n-vertex DAG, and γ be a topological ordering of V . Let H = G − v. Then the projection of γ onto H, denoted by ProjH [γ] : V \{v} → [n − 1], is defined as ( γ(u) if γ(u) < γ(v), Proj [γ](u) = H γ(u) − 1 if γ(u) > γ(v).

Armed with the notions of fully suffix connected vertices and projected order, we state our algorithm below.

11 Input: Permutation DAG G = (V,A) on n vertices in umbrella-free topological order γ : V → [n]. Output: Sequence σ of length n.

GreedyAssign(G, γ):

1: Initialize α ← 1; G1 ← G; γ(v0) ← −∞; γ1 ← γ 2: for i ← 1 to n do 3: vi ← γi-LFSC in Gi 4: if γ(vi) < γ(vi−1) then α ← α + 1

5: Gi+1 ← Gi − vi 6: γi+1 ← ProjGi+1 [γi] 7: σ(vi) ← α 8: Return σ ← (σ(γ−1(1)) . . . σ(γ−1(n))) Algorithm 2: GreedyAssign algorithm to compute α(G, γ)

The algorithm can be implemented to run in polynomial-time since a γ-LFSC vertex in G can be computed in polynomial-time. We now prove the correctness of the algorithm. Let G = (V,A) be an n-vertex permutation DAG, and γ be an umbrella-free topological ordering of G. Let v1, . . . , vn be the sequence of vertices chosen in the execution of GreedyAssign(G, γ). Let αi,Gi and γi denote the alphabet size α at the end of the ith iteration, the remaining subgraph at the start of th the i iteration, and γ projected onto Gi respectively. Finally, let σ be the sequence returned by GreedyAssign(G, γ). We have the following observations about the execution of the algorithm.

Observation 1. The vertex vi has no outgoing arcs in Gi for all i ∈ [n].

Observation 2. If γ(vi+1) < γ(vi) then σ(vi+1) = σ(vi)+1 and (vi, vi+1) 6∈ A, otherwise σ(vi+1) = σ(vi) and (vi+1, vi) ∈ A. Thus, alphabet assignments by GreedyAssign are non-decreasing with increasing iterations i.e. σ(vi) ≤ σ(vj) for all i, j ∈ [n] with i < j. Observation1 directly follows from Lemma3. Observation2 is due to the conditional increment of the alphabet size, α, in GreedyAssign. The next two lemmas show feasibility and optimality of GreedyAssign respectively. Theorem2 then immediately follows from Lemmas4 and5. 0 Lemma 4 (Feasibility of GreedyAssign). Let PermDAG(σ) = ({t1 . . . tn},A ) Then PermDAG(σ) −1 is isomorphic to G under the mapping φ : {u1 . . . un} → V given by φ(ui) = γ (i). Proof. We will prove isomorphism of the two graphs under φ by showing that (u, v) ∈ A if and only if (φ−1(u), φ−1(v)) ∈ A0. For the forward direction, it suffices to show that σ(u) ≤ σ(v) whenever (u, v) ∈ A. We observe that if (u, v) ∈ A, then γ(v) < γ(u). By Observation1, GreedyAssign must assign σ(v) before σ(u). Observation2 then implies that σ(u) ≤ σ(v). Next we show the contrapositive of the converse direction. Assume that (u, v) 6∈ A. We first −1 −1 consider the case when γ(v) > γ(u). Let φ (u) = tγ(u) and φ (v) = tγ(v). By definition of permutation DAGs, PermDAG(σ) does not have arc (ti, tj) when i < j. Thus (tγ(u), tγ(v)) 6∈ A. Next, we consider the case when γ(v) < γ(u). For this, it suffices to show that σ(u) < σ(v). Since (u, v) 6∈ A, the vertex v will never become fully suffix connected before the removal of u. Thus GreedyAssign sets σ(u) before σ(v). Thus, by Observation2, we have that σ(u) ≤ σ(v). Let u = vi and v = vj, where i, j ∈ [n] are the iteration numbers during which GreedyAssign assigns σ(u) and σ(v) respectively. Then, there exists k such that i ≤ k < j and γ(vk+1) < γ(vk) as otherwise, Observation2 would imply that γ(vi) < γ(vj), a contradiction. Thus, σ(u) < σ(v).

12 ∗ Lemma 5 (Optimality of GreedyAssign). Let σ be a sequence achieving α(G, γ). Then, σ(vi) ≤ ∗ σ (vi) for all i ∈ [n]. Proof. We will show this by induction on i. For the base case of i = 1, GreedyAssign always sets ∗ σ(v1) = 1, the smallest possible alphabet assignment. Thus σ(v1) ≤ σ (v1) holds. For the induction step, let i ≥ 2. We have the following two cases based on whether GreedyAssign incremented the alphabet size while assigning vi.

1. Suppose σ(vi) = σ(vi−1). By the description of the algorithm GreedyAssign, we have that vi−1 is fully suffix connected in Gi−1 and γ(vi) > γ(vi−1). Thus, the arc (vi, vi−1) must exist in Gi−1 and so also in G. It follows that

∗ ∗ σ(vi) = σ(vi−1) ≤ σ (vi−1) ≤ σ (vi).

Here, the first inequality is by the induction hypothesis, while the second inequality is due to the observation that (vi, vi−1) ∈ A.

2. Suppose σ(vi) 6= σ(vi−1). By the description of the algorithm GreedyAssign, we have that γ(vi) < γ(vi−1). Thus by Observation1, the arc (vi−1, vi) does not exist in Gi−1 and hence, does not exist in G. It follows that

∗ ∗ σ(vi) = σ(vi−1) + 1 ≤ σ (vi−1) + 1 ≤ σ (vi).

The equality relation is due to Observation2. The first inequality is due to the induction hypothesis while the second inequality is due to our observation that (vi−1, vi) 6∈ A.

Remark. Algorithm2 can be implemented to run in O(|V | + |A|) time. This can be done by using a priority queue data structure initialized as a stack. All fully suffix connected vertices should be added to the priority queue with priorities being position in γ. The choice of vertex to assign is the vertex with the minimum priority. The alphabet size should be incremented whenever a vertex removal results in new vertices becoming fully suffix connected.

3.2 Min-Max Relation Min-max relations are significant in optimization literature as they are strong indicators for the existence of a polynomial-time algorithm. In the context of algorithm design for optimization problems, min-max relations bring the optimization problem into NP ∩ coNP, thus providing strong evidence for the existence of polynomial-time algorithms. In this section, we give a min-max relation for α(G, γ). An immediate consequence of our min-max relation will be an alternative linear time algorithm for computing α(G, γ). We believe that the min-max relation could be a useful tool towards computing α(G). Let G = (V,A) be a permutation DAG and γ be an umbrella-free topological ordering of its −→ vertices. We recall that E := {(u, v) : γ(u) < γ(v) and (v, u) 6∈ A} and H(G, γ) is the graph −→ −→ (V,A ∪ E ). We also recall the arc weight function w : A ∪ E → {0, 1} given by: ( 0 if e ∈ A, w(e) := −→ 1 if e ∈ E.

13 We observe that H(G, γ) is a tournament. We now restate and prove the min-max relation. Our proof of the min-max relation will rely on our algorithm from Section 3.1.

Theorem 3. Let G = (V,A) be a permutation DAG and γ be a topological ordering of V such that α(G, γ) is finite. Then,   X  α(G, γ) = 1 + max w(e) : P is a path in H(G, γ) . e∈P 

Proof. We will show the equation by showing inequality in both directions. We begin by showing the lower bound on α(G, γ). Let P be any path in H(G, γ), and σ be any sequence such that 0 PermDAG(σ) = ({t1, . . . , tn},A ) is isomorphic to G under the mapping φ : {t1, . . . , tn} → V given −1 −→ by φ(ti) = γ (i). For every arc (φ(ti), φ(tj)) ∈ P such that (φ(ti), φ(tj)) ∈ E , we have the −1 −1 0 following two observations. First, the arc (φ (tj), φ (ti)) 6∈ A as the arc (φ(tj), φ(ti)) 6∈ A. Second, σ(φ(ti)) ≥ σ(φ(tj)) + 1 as i < j. It follows that X X X w(P ) = w(u, v) = w(u, v) ≤ σ(u) − σ(v) ≤ α(G, γ) − 1. −→ −→ (u,v)∈P (u,v)∈P ∩E (u,v)∈P ∩E

The first and second equations are by definition of w(P ) and the weight function w respectively. −→ The first inequality is due to our observation that σ(u) ≥ σ(v) + 1 whenever (u, v) ∈ E . Let a and b be the first and last vertices on P . Then the final inequality follows from σ(a) ≥ 1 and σ(b) ≤ α(G, γ). Next, we show the upper bound on α(G, γ). We recall that v1, . . . , vn is the order in which GreedyAssign processes vertices of G. Consider P = (vn, vn−1, . . . , v1). To prove the upper bound, it suffices to show that (1) P is a path in H(G, γ); and (2) w(P ) ≥ α(G, γ) − 1. To prove (1), −→ we show that (vi, vi−1) ∈ A ∪ E for each i ≥ 2. Consider the case when γ(vi) > γ(vi−1). Since v was γ -LFSC in G , the arc (v , v ) ∈ A. Next, consider the case when γ(v ) < γ(v ). i−1 i−1 i i i−1 −→ i i−1 By Observation1, we have that the arc (v , v ) 6∈ A. Thus, the arc (v , v ) ∈ E by definition −→ i−1 i −→i i−1 of E . We now prove (2). By Observation2, and definitions of w and E , we have w(vi, vi−1) = σ(vi) − σ(vi−1) It follows that

n n X X w(P ) = w(vi, vi−1) = σ(vi) − σ(vi−1) = α(G, γ) − 1. i=2 i=2 The second equality is due to our previous observation. The final equality is due to the GreedyAssign assignments σ(vn) = α(G, γ) and σ(v1) = 1. We remark that although the RHS problem in the min-max relation given in Theorem3 is the longest path problem in a directed graph, it can be solved in the graph H(G, γ) owing to the following lemma. Lemma6 allows the optimization problem in the RHS of Theorem3 to be solved in O(|V | + |A|) time by the classical dynamic programming algorithm for maximum weight path in a DAG. This leads to an alternative algorithm for computing α(G, γ).

Lemma 6. H(G, γ) is a DAG.

14 Proof. Suppose for contradiction that H(G, γ) contains a cycle. Let C = (u , u , . . . u , u ) be a −→ 1 2 k 1 cycle with the smallest number of vertices. If (u , u ) ∈ A ∪ E , then C0 = (u , u , . . . u , u ) is a 1 3 1 3 k −→1 cycle, contradicting our choice of C. Since H(G, γ) is a tournament, the arc (v3, v1) ∈ A ∪ E , and 0 C = (u1, u2, u3, u1) is also a cycle i.e. k = 3. Recall that the subgraph (V,A) is transitively closed. Thus, at most one edge of C can belong to A. To get the required contradiction, it suffices to show −→ −→ that the subgraph (V, E ) is transitively closed. Suppose for contradiction that E is not transitively −→ −→ −→ closed. Then, there exist arcs (u, v), (v, w) ∈ E such that the arc (u, w) 6∈ E . By definition of E , we have that γ(u) < γ(v) < γ(w). It follows that the arc (w, u) ∈ A, and the triple (u, v, w) is an umbrella in G ordered by γ. This contradicts that γ is umbrella-free.

3.3 Polyhedral Description In this section, we give a polyhedral description for the convex-hull of sequences that are feasible for α(G, γ). As a consequence, it leads to an LP-based algorithm to compute α(G, γ). We emphasize that our polyhedral result is stronger than giving an LP-based algorithm to compute α(G, γ): it implies that one can efficiently compute an integer-valued sequence σ = (σ(1), . . . , σ(n)) with Pn minimum weight i=1 wiσ(i) for any given non-negative weights w1, . . . , wn such that G(σ) is −1 isomorphic to G under the mapping φ : {t1, . . . , tn} → V given by φ(ti) = γ (i) for every i ∈ [n]. The following is the main result of this section. Theorem 5. Let G = (V,A) be an n-vertex permutation DAG and γ be an umbrella-free topological n ordering of its vertices. Let Q(G, γ) be the convex-hull of indicator vectors of x ∈ N whose sequence 0 σ := (x1, . . . , xn) is such that PermDAG(σ) = ({t1, . . . , tn},A ) is isomorphic to G under the mapping −1 φ : {t1, . . . , tn} → V given by φ(ti) = γ (i) for all i ∈ [n]. Then,   x ≤ x ∀(v, u) ∈ A,  γ(u) γ(v)  n Q(G, γ) = x ∈ R xγ(v) ≤ xγ(u) − 1 ∀(v, u) 6∈ A with γ(u) < γ(v), and .    xi ≥ 1 ∀ i ∈ [n]  For notational convenience, let P (G, γ) denote the polyhedron defined in the RHS of Theorem 5. Before proving Theorem5, we describe how α(G, γ) can be obtained by optimizing over P (G0, γ0) for a graph G0 and an ordering γ0 obtained from G and γ. Let G0 = (V 0,A0) be obtained from G by adding a vertex t with edges (t, u) for all u ∈ V and γ0 : V 0 → [n + 1] be defined as γ0(u) = γ(u) if u ∈ V and γ0(t) = n + 1. We note that if G is a permutation DAG and γ is an umbrella-free topological ordering of G, then G0 is also a permutation DAG and γ0 is an umbrella-free topological ordering of G0. Moreover, we also have that

n 0 0 o α(G, γ) = min xγ0(n+1) : x ∈ Q(G , γ ) . Thus, by Theorem5, the γ-alphabet size of G, i.e., α(G, γ), can be computed by optimizing along n+1 0 0 the objective direction (0,..., 0, 1) ∈ R over the polyhedron P (G , γ ). We now prove Theorem5.

Proof of Theorem5. We recall that a point x is an extreme point of a polyhedron if x cannot be expressed as a convex combination of any two distinct points in the polyhedron. Any extreme point x of Q(G, γ) satisfies the constraints defining P (G, γ). Thus, Q(G, γ) ⊆ P (G, γ). In order to show equality, it suffices to show that all extreme points of P (G, γ) are integral. Lemma7 shows that all extreme points of P (G, γ) are integral, thus completing the proof of Theorem5.

15 Lemma 7. Let G = (V,A) be an n-vertex DAG and γ be a topological ordering of its vertices. If x n is an extreme point of P (G, γ), then x ∈ Z . Proof. Suppose for contradiction that x is non-integral. We will show the existence of two points in P (G, γ) such that x is a convex combination of these points. Let S := {i : xi 6∈ Z}. We note that the set S is non-empty due to our choice of x. Let  ∈ R be as follows   := min min(xi − bxic , dxie − xi) . i∈S

 n Since S is non-empty, we have  > 2 > 0. Let y ∈ R be defined as follows: ( /2 if i ∈ S, yi := 0 otherwise.

1 1 We note that x = 2 (x + y) + 2 (x − y). It suffices to show that x + y, x − y ∈ P . We will show that the point x + y ∈ P and remark that the proof of x − y ∈ P is along very similar lines. We observe that y ≥ 0. Constraint (3) is always satisfied as xi + yi ≥ xi ≥ 1. We first focus on constraint (1). Consider any arc (v, u) ∈ A. Since y ≥ 0, the constraint is easily seen to be satisfied in the cases where (1) xγ(u), xγ(v) ∈ Z; (2) xγ(u), xγ(v) 6∈ Z; and (3) xγ(u) ∈ Z but xγ(v) 6∈ Z. Consider the case when xγ(u) 6∈ Z but xγ(v) ∈ Z. Then, we have that l m xγ(u) + yγ(u) < xγ(u) +  ≤ xγ(u) ≤ xγ(v) = xγ(v) + yγ(v).

The first inequality is by yi ≤ /2 for all i ∈ [n]. The second inequality is by definition of . The third inequality is due to x ∈ P and our case assumption that xγ(v) ∈ Z. The equality relation is by definition of y. Next, we consider constraint (2). Let γ(u) < γ(v) but (v, u) 6∈ A. Similar to the above analysis, the constraint is easily seen to be satisfied in the cases where (1) xγ(u), xγ(v) ∈ Z; (2) xγ(u), xγ(v) 6∈ Z; and (3) xγ(u) 6∈ Z but xγ(v) ∈ Z. Consider the case when xγ(u) ∈ Z but xγ(v) 6∈ Z. Then, we have that l m xγ(v) + yγ(v) < xγ(v) +  ≤ xγ(u) ≤ xγ(u) = xγ(u) + yγ(u).

The first inequality is due to yi ≤ /2 for all i ∈ [n]. The second inequality is by definition of . The third inequality is due to x ∈ P and our case assumption that xγ(u) ∈ Z. The equality relation is by definition of y.

Based on Lemma7, it is natural to wonder if the integral extreme points of P (G, γ) have any combinatorial interpretation when G is an arbitrary DAG and γ is an arbitrary topological ordering of G. The following lemma shows that integrality of P (G, γ) is useful only when G is a permutation DAG and γ is an umbrella-free topological ordering of G. Lemma 8. Let G be a DAG and γ be a topological ordering of G. Then, P (G, γ) is non-empty if and only if G is a permutation DAG and γ is umbrella-free.

Proof. The reverse direction follows from the correctness of GreedyAssign (Lemma4). We focus on proving the forward direction. Let x ∈ P (G, γ) be a feasible point. It suffices to show that G is transitively closed and γ is umbrella-free.

16 First, assume for contradiction that G is not transitively closed. Then, there exist arcs (u, v), (v, w) ∈ A such that the arc (u, w) 6∈ A. Since x is feasible, we have the following: (1) xγ(v) ≤ xγ(u); (2) xγ(w) ≤ xγ(v); and (3) xγ(u) ≤ xγ(w) − 1. However, these inequalities do not admit any feasible solution, a contradiction. Next, assume for contradiction that γ is not umbrella-free. Then, there exists a triple (u, v, w) such that γ(u) < γ(v) < γ(w), and the arc (w, u) ∈ A, but the arcs (v, u), (w, v) 6∈ A. Since the point x is feasible, we have the following: xγ(w) ≤ xγ(v) − 1; (2) xγ(v) ≤ xγ(u) − 1; and (3) xγ(u) ≤ xγ(w). However, these inequalities do not admit any feasible solution, a contradiction.

4 An efficient algorithm for finding a maximum binary tree in bounded treewidth graphs

In this section, we prove Theorem4 by designing an efficient dynamic programming algorithm to find a maximum binary tree in graphs with bounded treewidth. Given an undirected graph G = (V,E), we say that a subgraph T , where V (T ) ⊆ V and E(T ) ⊆ E, is a binary tree in G if T is connected, acyclic, and degT (v) ≤ 3 for every vertex v ∈ V (T ). The problem of interest is the following.

UndirMaxBinaryTree Given: An undirected graph G. Goal: A binary tree in G with maximum number of vertices.

We recall that if the maximum degree requirement for each vertex is two instead of three, then this problem is exactly the longest path problem. Our algorithm is modified from the standard dynamic programming approach for longest path in bounded treewidth graphs. Our main modifica- tion to the algorithm is to address the degree requirement for each vertex in the subproblems. We note that our technique is also applicable to find a maximum-sized bounded degree tree in a given graph of bounded treewidth, where the degree bounds are constant. We will focus on the rooted version of the problem as defined below.

rooted-UndirMaxBinaryTree Given: An undirected graph G and a root s ∈ V (G).

Goal: A binary tree in G containing s where degT (s) ≤ 2 with maximum number of vertices.

Now we introduce the definition of treewidth.

Definition 8. A tree decomposition of an undirected graph G is given by T = (T, {Xt}t∈V (T )), where T is a tree in which every tree node i ∈ V (T ) is assigned a subset Xi ⊆ V (G) of vertices of G, called a bag, such that the following conditions are satisfied:

(T1) ∪i∈V (T )Xi = V (G). (T2) For each {u, v} ∈ E(G), there is a tree node i ∈ V (T ) in the tree T such that both u and v are in Xi.

17 (T3) For each vertex u ∈ V (G), if there exist two tree nodes i, j ∈ V (T ) in the tree T such that u is in both Xi and Xj, then u ∈ Xk for every tree node k ∈ V (T ) on the unique path between i and j.

For ease of distinction, we will denote i ∈ V (T ) as a tree node and the set Xi as a bag of the tree decomposition T . The width of a tree decomposition is maxi∈V (T ){|Xi| − 1}. The treewidth wG of the graph G is the minimum width among all possible tree decompositions. A tree decomposition w O(w2 ) with treewidth wG can be found in (wG2 G ) G |V (G)| time [Bod96].

4.1 Outline of the algorithm Our algorithm consists of two parts:

1. Design a linear time algorithm to solve rooted-UndirMaxBinaryTree.

2. Reduce UndirMaxBinaryTree to rooted-UndirMaxBinaryTree.

For the first part, we will show the following theorem in the next sections.

Theorem 6. Let s ∈ V (G) be a root. Given a tree decomposition of G with width wG, rooted-- O(w ) UndirMaxBinaryTree can be solved in wG G |V (G)| time.

For the second part, by Theorem6, given a tree decomposition of G with width wG, we can find O(wG) 2 a maximum binary tree of G in wG |V (G)| time by solving rooted-UndirMaxBinaryTree for every possible choice of the root vertex in G. The can be brought down to linear as shown in the proof below. Corollary1 proves Theorem4.

Corollary 1. Given a tree decomposition of G with width wG, UndirMaxBinaryTree can be O(w ) solved in wG G |V (G)| time.

Proof. UndirMaxBinaryTree with G as the input graph can be solved by the following reduction to rooted-UndirMaxBinaryTree in a graph whose treewidth is one larger than the treewidth of G:

1. Construct G0 by adding a vertex s and a binary tree B such that:

(a) s is adjacent to all the vertices in V (G), (b) s is adjacent to the root of B, and (c) |E(B)| = |E(G)|.

2. Solve rooted-UndirMaxBinaryTree on G0 rooted at s to obtain the maximum binary tree T 0 of G0.

3. Let v ∈ V (G) be the vertex such that {v, s} ∈ E(T 0). Pick the subtree of T 0 rooted at v.

This is a generic reduction from UndirMaxBinaryTree to rooted-UndirMaxBinaryTree (but not approximation-preserving). We note that the treewidth of the graph G0 is one larger than the treewidth of G.

18 4.2 Proof of Theorem6 We will prove Theorem6 in this section. We begin with a convenient form of tree decompositions and associated notations in Section 4.2.1. We describe the subproblems of the dynamic program along with the recursive expressions for these subproblems and analyze the run-time in Section 4.2.2. We prove the correctness of the recursive expressions in Section 4.2.3.

4.2.1 Special tree decompositions In this subsection, we will introduce a modified form of tree decomposition that will be convenient for our dynamic program. We also introduce the associated notations and prove a crucial edge disjointness property (Lemma9). Given a tree decomposition T with an arbitrarily chosen root, the parent-child and ancestor- descendant relationships between the tree nodes are defined naturally. With these relationships, it will be convenient to think of nice tree decompositions (defined below) as rooted trees. Definition 9. A nice tree decomposition of an undirected graph G is a tree decomposition T = (T, {Xt}t∈V (T )) where T is rooted at a tree node r ∈ V (T ) such that:

1. Xl = ∅ for every leaf tree node l ∈ V (T ) and Xr = ∅. 2. Every non-leaf tree node i is one of the following four types:

Introduce vertex: Tree node i has only one child j with Xi = Xj ∪ {v} for some v ∈ V (G) \ Xj. In this scenario, we say that vertex v is introduced at tree node i.

Drop: Tree node i has only one child j with Xi = Xj \{v} for some v ∈ Xj. In this scenario, we say that vertex v is dropped at tree node i.

Join: Tree node i has two children j and k with Xi = Xj = Xk. Introduce edge: Tree node i is labeled by an edge {u, v} ∈ E(G) such that both u and v belong to Xi, and i has only one child j with Xi = Xj. In this scenario, we say that edge {u, v} is introduced at tree node i. 3. Every edge of G is introduced at exactly one tree node. 4. Every edge {u, v} is introduced at tree node i which is between tree nodes j and k such that: (a) Node j is an ancestor of node k. (b) Node j either drops u or drops v. (c) All tree nodes between node j and node k are of type introduce edge and they introduce edges incident at the vertex that j drops. (d) Node k is not of type introduce edge.

Given a tree decomposition with width wG, a nice tree decomposition with O(wG|V (G)|) tree 2 + nodes can be computed in O(wG max{|V (G)|, |V (T )|}) time [CFK 15]. We note that by (T3), each vertex in V (G) is dropped only once, but may be introduced several times in a nice tree decomposition. In order to solve rooted-UndirMaxBinaryTree, we construct Gs from G by adding a pen- n o dant vertex s0 and edge {s, s0}. Formally, Gs := (V (G) ∪ {s0},E(G) ∪ s, s0 ).

19 Observation 3. The maximum binary tree in G rooted at s can be obtained by finding a maximum binary tree in Gs rooted at s0 and removing the vertex s0 and the edge {s, s0} from the tree.

Now we introduce a special tree decomposition of Gs.

Definition 10. Given a nice tree decomposition of G, an s0-special tree decomposition of Gs is a tree decomposition obtained by the following steps:

1. Obtain a nice tree decomposition of G with width wG. 2. Add s0 to each bag and insert a tree node that introduces the edge s, s0 between the tree node that drops s and its child.

The idea behind this tree decomposition is to ensure that every bag contains the new root s0, which is useful in the definition of the subproblems for the dynamic program. 0 s For brevity, we will denote the resulting s -special tree decomposition of G as T = (T, {Xt}t∈V (T )). Let r be the root tree node of T . Since we will only care about the optimal structure that is stored locally with respect to a tree node, we will use the following notations.

Definition 11. For a tree node i, let

1. Si denote the set of tree nodes consisting of all its descendants (including i), 2. V := S X be the descendant vertices, i j∈Si j  3. Ei := e ∈ E(G) : e is introduced at some tree nodes in the subtree rooted at i be the descen- dent edges, and

4. Gi := (Vi,Ei). With the above notations, we show a disjointness property for the join tree nodes.

Lemma 9. In an s0-special tree decomposition, suppose a join tree node i has two children j and k with Xi = Xj = Xk. Then (Vj \ Xi) ∩ (Vk \ Xi) = ∅ and Ej ∩ Ek = ∅.

Proof. First, we show that (Vj \ Xi) ∩ (Vk \ Xi) = ∅. Assume for the sake of contradiction that there is a vertex v ∈ (Vj \ Xi) ∩ (Vk \ Xi). Then there is a descendent tree node dj of j, such that v ∈ Xdj \ Xi. Similarly, there is a descendent tree node dk of k, such that v ∈ Xdk \ Xi. From (T3), the vertex v must belong to the bag of every tree node that is on the unique path between tree nodes dj and dk, and this includes tree node i. Hence, v ∈ Xi, a contradiction. Next, we show that Ej ∩ Ek = ∅. By Definition9, a tree node that drops vertex v is followed by descendant tree nodes that introduce edges with v as an endvertex until a descendant tree node that is not of type introduce edge is reached. Therefore, the edge {u, v} where u ∈ Xi and v ∈ Xi is not yet included in Ei. Consequently, any edge in Ej cannot have both endvertices in Xj = Xi, i.e. it must have an endvertex in Vj \ Xj = Vj \ Xi. Similar argument implies that any edge in Ek cannot have both endvertices in Xk = Xi, i.e., it must have an endvertex in Vk \ Xk = Vk \ Xi. From (Vj \ Xi) ∩ (Vk \ Xi) = ∅, we must have Ej ∩ Ek = ∅.

20 4.2.2 The dynamic program Our algorithm solves subproblems in a bottom-up fashion beginning with the leaf tree nodes. For every tree node i and for all possible (X, P,D), where

1. X ⊆ Xi,

2. P is a partition {P1,P2, ..., Pq} of X with each part of P being non-empty, and 3. D : X → {0, 1, 2, 3} is a function specifying the degree constraints on the vertices of X, let MBTi(X, P,D) be the maximum number of edges in a binary forest F satisfying the following five constraints:

(DP1) F is a subgraph of Gi, (DP2) s0 ∈ X,

(DP3) Xi ∩ V (F ) = X, i.e. the set of vertices in F from Xi is exactly X,

(DP4) F has exactly q connected components (trees) T1, ..., Tq such that V (Tp) ∩ X = Pp for each p ∈ {1, 2, ..., q}, i.e. each part of P corresponds to a single tree of F , and

(DP5) degF (v) = D(v) for each v ∈ X, i.e. each vertex v ∈ X has exactly D(v) edges incident to it in F .

A forest is feasible for MBTi(X, P,D) if it satisfies properties (DP1), (DP2), (DP3), (DP4), and (DP5). If a forest has the maximum number of edges among the feasible forests, then we say that this forest is optimal for MBTi(X, P,D). If there is no feasible forest for MBTi(X, P,D), then we call the subproblem to be infeasible. For example, MBTi(X, P,D) where D(v) = 0 for a vertex v ∈ X ⊆ Xi in a part P ∈ P with |P | ≥ 2 is infeasible. We will define MBTi(X, P,D) = −∞ when the subproblem is infeasible. We show an example in Figure6. Suppose at tree node i, Xi = {v1, ..., v6}, Vi = {v1, ..., v10}, and Gi = (Vi,Ei) where Ei consists of solid and dashed edges. All the edges in Ei are introduced in the subtree rooted at i where the solid edges are selected in the binary forest F while the dashed edges are not. Suppose the given input for tree node i is (X,P,D) where X = {v1, ..., v5},  P = {v1, v2} , {v3, v5} , {v4} , D(v1) = D(v2) = D(v5) = 1, D(v4) = 0, and D(v3) = 2. In this case OPT (i, X, P, D) = 6.

i v1 v2 v3 X v4 v5 v6

v7 v8 v9 v10

Figure 6: MBTi(X, P,D)

We recall that r is the root of the s0-special tree decomposition of Gs. The number of edges in s 0 0 n 0 o 0 the maximum binary tree of G rooted at s is exactly MBTr(X = {s }, P = s ,D(s ) = 1).

21 We now provide the recurrence relations for the dynamic programming algorithm for each type of tree node.

Leaf: Suppose i is a leaf tree node. Then,  n o 0 if X = s0 , P = s0 , and D(s0) = 0, MBTi(X, P,D) = (DP-L) −∞ otherwise.

Introduce vertex: Suppose i introduces vertex v and j is the child of i with Xi = Xj ∪ {v} for some v ∈ V (G) \ Xj. Consider the following conditions: (IntroVertex 1) v ∈ X and either {v} is not a part in P or D(v) > 0.

(IntroVertex 2) v ∈ X, {v} is a part in P, and D(v) = 0. For D : X → {0, 1, 2, 3}, let D0 : X \{v} → {0, 1, 2, 3} be obtained by setting D0(u) = D(u) for every u ∈ X \{v}. Then,  −∞ if (IntroVertex 1),   0 MBTi(X, P,D) = MBTj(X \{v} , P \ {v} ,D ) if (IntroVertex 2), (DP-IV)  MBTj(X, P,D) otherwise.

Introduce edge: Suppose i introduces edge {u, v} and j is the child of i with Xi = Xj. Consider the following condition:

(IntroEdge1) u and v are both in X and in the same part P ∈ P.

Suppose (IntroEdge1) holds. Then let P0 be a partition of X obtained from P by splitting P 0 into two disjoint sets Pu and Pv such that u ∈ Pu and v ∈ Pv, i.e. P = P \{P } ∪ {Pu,Pv}, and D0 : X → {0, 1, 2, 3} be obtained by setting D0(w) = D(w) for w ∈ X \{u, v}, D0(u) = D(u)−1 ≤ 3, D(u) ≥ 1, D0(v) = D(v) − 1 ≤ 3, and D(v) ≥ 1. Then

( 0 0 max{maxP0,D0 MBTj(X, P ,D ) + 1, MBTj(X, P,D)} if (IntroEdge1), MBTi(X, P,D) = MBTj(X, P,D) otherwise. (DP-IE) 0 Drop: Suppose i drops vertex v and j is the child of i with Xi = Xj \{v} for some v ∈ Xj. Let P be a partition obtained by adding v to one of the existing parts of P and D0 : X ∪ {v} → {0, 1, 2, 3} is obtained by setting D0(u) = D(u) for u ∈ X and D0(v) is set to some value in {1, 2, 3}. Then    0 0 MBTi(X, P,D) = max max MBTj(X ∪ {v} , P ,D ) , MBTj(X, P,D) . (DP-D) P0,D0

Join: Suppose i has two children j and k with Xi = Xj = Xk. For arbitrary partitions Pj, Pk of X, we obtain a graph H(Pj, Pk) and a partition Q(Pj, Pk) as follows: for b ∈ {j, k} we first construct an auxiliary graph Hb that contains vertices for every vertex in X and vertices for every part in Pb, with the vertex corresponding to a part of Pb being adjacent

22 to all vertices that are in that part. We note that V (Hj)∩V (Hk) = X and E(Hj)∩E(Hk) = ∅. We j k 0 consider the merged graph H(P , P ) := (V (Hj) ∪ V (Hk),E(Hj) ∪ E(Hk)). Let Q be a partition of V (H(Pj, Pk)) where vertices in the same connected component of H belong to the same part of Q0. Let Q(Pj, Pk) be the partition of X obtained from Q0 by restricting each part of Q0 to elements in X. Namely, Q(Pj, Pk) = {Q ∩ X | Q ∈ Q0}. We note that Q(Pj, Pk) can be found efficiently by running a depth first search on H(Pj, Pk). Let (X, P,D) be the input at tree node i. Let Pj and Pk be partitions such that the graph H(Pj, Pk) is a forest and the resulting partition Q(Pj, Pk) = P. Let Dj,Dk : X → {0, 1, 2, 3} be functions such that D(v) = Dj(v) + Dk(v) for every v ∈ X. Then

( j j k k maxPj ,Pk,Dj ,Dk MBTj(X, P ,D ) + MBTk(X, P ,D ), MBTi(X, P,D) = (DP-J) −∞ if there are no such Pj, Pk,Dj, and Dk.

j  We show an example in Figure7. Suppose X = {v1, ..., v6}, P = {v1, v2} , {v3, v4} , {v5} , {v6} , k j k and P = {{v1} , {v2, v3, v5}, {v4} , {v6}}, the merged graph H(P , P ) is a forest. The resulting par- tition is {{v1, v2, v3, v4, v5} , {v6}}.

Hj

i v1 v2 v3 v4 v5 v6

Hk

Figure 7: H(Pj, Pk)

When the root tree node r is reached, the maximum number of edges of the binary tree in Gs is 0 0 0 MBTr(X = {s },P = {{s }},D(s ) = 1). To find the optimal binary tree, we can simply backtrack through the subproblems to obtain an optimal solution. This concludes the description of the algorithm. The proof of correctness is in Section 4.2.3. We proceed to analyze the running time. The bag of each tree node has at most wG + 2 vertices, w +2 O(w ) so the number of subproblems per tree node is at most (8wG + 16) G = wG G , since for a |X | |X| |X| tree node i there are 2 i subsets X ⊆ Xi, at most |X| partitions of X, and at most 4 degree requirements. Solving a subproblem at a tree node requires considering at most all states from the O(w ) 2 O(w ) children of that tree node, which takes (wG G ) = wG G time. Thus, the running time for O(w ) computing all the MBT values of a tree node is wG G . We have thus proved Theorem6.

Theorem 6. Let s ∈ V (G) be a root. Given a tree decomposition of G with width wG, rooted-- O(w ) UndirMaxBinaryTree can be solved in wG G |V (G)| time.

4.2.3 Correctness of the dynamic program We prove the correctness of the recursion expressions in the dynamic program described in the previous section.

23 Lemma 10. MBTi(X, P,D) satisfies the recurrence relations given in DP-L, DP-IV, DP-IE, DP-D, and DP-J.

Proof. The proof consists of showing two parts: If MBTi(X, P,D) is feasible, then the right hand side (RHS) of the recurrence formula should be the same as the left hand side (LHS), or equivalently, RHS is an upper bound and lower bound of LHS. In addition, if MBTi(X, P,D) is infeasible, then the recurrence should return MBTi(X, P,D) = −∞. We will show the upper and lower bound and the infeasible case with a proof by induction depending on the tree node type.

0 Leaf: For the base case, i.e. when i is a leaf tree node, the only vertex in Gi is s . The only input 0 n 0 o 0 for which MBTi(X, P,D) is feasible is X = {s }, P = s , and D(s ) = 0. For the feasible input, the node s0 must be included in the optimal binary forest F otherwise constraint (DP2) will be violated. Since no edges have been introduced in the subtree rooted at tree node i, the degree of 0 s in the optimal binary forest F will be zero. Therefore, MBTi(X, P,D) = 0. If MBTi(X, P,D) is infeasible, then it is set to −∞.

Suppose that MBTj(X, P,D) is correct for all possible inputs (X, P,D) for all children j of a non-leaf tree node i. We will show that MBTi(X, P,D) computed using the recurrence relation based on its children is correct. We consider the various types for tree node i.

Introduce vertex: Suppose that i has one child j with Xi = Xj ∪ {v} for some v ∈ V (G) \ Xj. We recall that none of the edges incident to v have been introduced in the subtree rooted at i, so the vertex v is isolated in Gi, i.e. degGi (v) = 0. Suppose that MBTi(X, P,D) is feasible with F being an optimal binary forest. We have two cases: Case 1: Suppose that v ∈ X. Then {v} must be a part of P and D(v) = 0 (i.e., condition (IntroVertex 2) is satisfied), otherwise MBTi(X, P,D) is infeasible. If (IntroVertex 2) holds, then 0 0 (V (F ) \{v},E(F )) is a feasible solution for MBTj(X \{v}, P \ {{v}},D ), where D : X \{v} → 0 {0, 1, 2, 3} is obtained by setting D (u) = D(u) for every u ∈ X \{v}. Hence, MBTi(X, P,D) ≤ 0 0 MBTj(X \{v}, P \ {{v}},D ). Since there is a feasible forest Fj for MBTj(X \{v}, P \ {{v}},D ), a feasible forest for MBTi(X, P,D) can be obtained by adding an isolated vertex v to V (Fj). Therefore, 0 MBTi(X, P,D) ≥ MBTj(X \{v}, P \ {{v}},D ). Case 2: Suppose v∈ / X. Then v is not in F . The forest F is a feasible solution for MBTj(X, P,D). Hence, MBTi(X, P,D) ≤ MBTj(X, P,D). Since there is a feasible forest Fj for MBTj(X, P,D), and Fj is also feasible for MBTi(X, P,D), we have MBTi(X, P,D) ≥ MBTj(X, P,D). Next, suppose that MBTi(X, P,D) is infeasible. If (IntroVertex 1) holds, then we are done. 0 Suppose (IntroVertex 1) fails, but (IntroVertex 2) holds. In this case, MBTj(X \{v}, P \ {{v}},D ), where D0 : X \{v} → {0, 1, 2, 3} is obtained by setting D0(u) = D(u) for every u ∈ X \{v}, 0 is infeasible. Hence, MBTi(X, P,D) = MBTj(X \{v}, P \ {{v}},D ) = −∞. Suppose that both (IntroVertex 1) and (IntroVertex 2) fail. In this case, MBTj(X, P,D) is infeasible. Hence, MBTi(X, P,D) = MBTj(X, P,D) = −∞.

Introduce edge: Suppose i introduces edge {u, v} and has one child j such that Xi = Xj. Suppose that MBTi(X, P,D) is feasible with F being an optimal binary forest. We have two cases: Case 1: Suppose u and v are both in X and also in the same part of P (i.e., condition (In- troEdge1) is satisfied). Then, the forest F can either include or exclude {u, v}.

24 If {u, v} is excluded, then F is also feasible for MBTj(X, P,D) and hence MBTi(X, P,D) ≤ MBTj(X, P,D). There is a feasible forest for MBTj(X, P,D) which is also feasible for MBTi(X, P,D). Therefore, MBTi(X, P,D) ≥ MBTj(X, P,D). If {u, v} is included, then P must have a part P that can be partitioned into two disjoint parts Pu and Pv where u ∈ Pu and v ∈ Pv. Hence, the forest (V (F ) \{u, v},E(F ) \ {{u, v}}) is also feasible 0 0 0 0 for MBTj(X, P ,D ) for some P = P \{P } ∪ {Pu,Pv} with P = Pu ∪ Pv, and D : X → {0, 1, 2, 3} obtained by setting D0(w) = D(w) for w ∈ X \{u, v}, D0(u) = D(u) − 1, and D0(v) = D(v) − 1. 0 0 Consequently, MBTi(X, P,D) ≤ maxP0,D0 MBTj(X, P ,D ) + 1. On the other hand, a feasible forest 0 0 for MBTi(X, P,D) can be obtained by adding edge {u, v} to a feasible forest for MBTj(X, P ,D ), 0 0 so MBTi(X, P,D) ≥ maxP0,D0 MBTj(X, P ,D ) + 1. Case 2: Otherwise, either (1) u∈ / X or v∈ / X, or (2) u and v are both in X but not in the same part of P. In this case {u, v} cannot be in F . Hence, the forest F is a feasible solution for the subproblem MBTj(X, P,D) and hence MBTi(X, P,D) ≤ MBTj(X, P,D). There is a feasible forest for MBTj(X, P,D) which is also feasible for MBTi(X, P,D). Therefore, MBTi(X, P,D) ≥ MBTj(X, P,D). Next, suppose that MBTi(X, P,D) is infeasible. Then, MBTj(X, P,D) is also infeasible and hence, MBTj(X, P,D) = −∞. We consider (IntroEdge1) now: In this case, for every partition 0 0 P = P \{P } ∪ {Pu,Pv} with P = Pu ∪ Pv, and for D : X → {0, 1, 2, 3} obtained by setting D0(w) = D(w) for w ∈ X \{u, v}, D0(u) = D(u) − 1, and D0(v) = D(v) − 1, we have that 0 0 0 0 MBTj(X, P ,D ) is infeasible. Therefore maxP0,D0 MBTj(X, P ,D ) = −∞.

Drop: Suppose i drops v and has a child j such that Xi = Xj \{v} for some v ∈ Xj. Suppose that MBTi(X, P,D) is feasible with F being an optimal binary forest. If F excludes v, then F is also a feasible solution for MBTj(X, P,D) and hence MBTi(X, P,D) ≤ MBTj(X, P,D). There is a feasible forest for MBTj(X, P,D) which is also feasible for MBTi(X, P,D). Therefore, MBTi(X, P,D) ≥ MBTj(X, P,D). 0 0 Suppose that F includes v, then the forest F is a feasible solution for some MBTj(X∪{v} , P ,D ), where P0 is a partition obtained by adding v to one of the existing parts of P and D0 : X ∪ {v} → {0, 1, 2, 3} is obtained by setting D0(u) = D(u) for u ∈ X and D0(v) is set to some value in 0 0 {1, 2, 3}. Hence, MBTi(X, P,D) ≤ maxP0,D0 MBTj(X ∪ {v} , P ,D ). On the other hand, a feasible 0 0 forest for MBTi(X, P,D) can be obtained by selecting a feasible forest for MBTj(X, P ,D ), so 0 0 MBTi(X, P,D) ≥ maxP0,D0 MBTj(X ∪ {v} , P ,D ). Next, suppose that MBTi(X, P,D) is infeasible. Then, MBTj(X, P,D) is also infeasible and 0 hence MBTj(X, P,D) = −∞. Moreover, for every partition P obtained by adding v to one of the existing parts of P and every D0 : X ∪{v} → {0, 1, 2, 3} obtained by setting D0(u) = D(u) for u ∈ X 0 0 0 and every choice of D (v) ∈ {1, 2, 3}, there is no feasible forest for MBTj(X ∪ {v} , P ,D ). Hence, 0 0 maxP0,D0 MBTj(X ∪ {v} , P ,D ) = −∞.

Join: Suppose i has two children j and k and Xi = Xj = Xk. Suppose that MBTi(X, P,D) is feasible with F being an optimal binary forest. First we show that LHS ≤ RHS. By Lemma9, the edges in F are from disjoint edge sets Ej and Ek. Hence, the forest F decomposes into two binary forests Fj := (V (F ) ∩ Vj,E(F ) ∩ Ej) and Fk := (V (F )∩Vk,E(F )∩Ek). In particular Fj and Fk are subgraphs of Gj and Gk, respectively. Let j k X := V (Fj)∩Xj be the set of vertices in Xj that belong to Fj and similarly, let X := V (Fk)∩Xk. Claim 1. Xj = Xk = X.

25 Proof. We will show that Xj = X. The proof for Xk = X will also follow by the same argument. j We first have that X ⊆ X because X ⊆ V (F ) and X ⊆ Xi = Xj ⊆ Vj imply X ⊆ V (F ) ∩ j j Vj ∩ Xj = X . Next, suppose that there exists a vertex v ∈ X \ X. Then v ∈ Xj \ X = Xi \ X. j Besides, v ∈ V (Fj) ⊆ V (F ). However, these two statements violate (DP3) so X ⊆ X. We show that there exist partitions Pj and Pk of X, degree requirements Dj, Dk : X → {0, 1, 2, 3}, and a decomposition of the forest F into F j and F k such that

(J1) the merged graph H(Pj, Pk) is a forest,

(J2) the partition Q(Pj, Pk) is exactly P,

(J3) Dj(v) + Dk(v) = D(v) for v ∈ X, and

j j k k (J4) Fj is feasible for MBTj(X, P ,D ) and Fk is feasible for MBTk(X, P ,D ). Let b ∈ {j, k}. We obtain the partition Pb of X as follows: vertices of X in the same connected b 0 component of Fb will be in the same part of P , i.e., if Rb is a partition of V (Fb) based on the b 0 0 0 connected components of Fb, then P = {R ∩ X|R ∈ Rb}. We recall that Hb is an auxiliary graph that contains vertices for every vertex in X and vertices for every part in Pb, with the vertex corresponding to a part of Pb being adjacent to all vertices j k that are in that part. The merged graph H(P , P ) := (V (Hj) ∪ V (Hk),E(Hj) ∪ E(Hk)) where V (Hj) ∩ V (Hk) = X is such that E(Hj) ∩ E(Hk) = ∅. Let u, v ∈ X. For brevity, we denote u-v path as a path between u and v. From the fact that (1) u and v are in the same part of Pj (or Pk) j if and only if their is a unique u-v path in Fj (or Fk) and (2) u and v are in the same part of P (or k P ) if and only if their is a unique u-v path in Hj (or Hk), we have the following observation:

Observation 4. Let u, v ∈ X, then there is a unique u-v path in Fj (or Fk) if and only there is a unique u-v path in Hj (or Hk). Claim 2. The merged graph H(Pj, Pk) satisfies (J1).

Proof. The proof consists of showing two parts: (1) if u, v ∈ X and there is a u-v path in H(Pj, Pk), j k then this path is unique, and (2) there is no cycle in H(P , P ). We will use the notation u − Fj − v to denote that there is a unique u-v path in Fj and such a path may have intermediary vertices from X \{u, v}. We will use similar notations for Fk, Hj, and Hk. j k Let u, v ∈ X. Suppose that there is a u-v path in H(P , P ). Since E(Hj) ∩ E(Hk) = ∅ and (V (Hj) \ X) ∩ (V (Hk) \ X) = ∅, the path must be one of the following:

1. u − Hj − w1 − Hk − w2 − ... − wn−1 − Hk − wn − Hj − v,

2. u − Hj − w1 − Hk − w2 − ... − wn−1 − Hj − wn − Hk − v,

3. u − Hk − w1 − Hj − w2 − ... − wn−1 − Hk − wn − Hj − v, or

4. u − Hk − w1 − Hj − w2 − ... − wn−1 − Hj − wn − Hk − v

j k where w1, ..., wn ∈ X \{u, v}. In short, a u-v path in H(P , P ) must alternate edges between Hj j k and Hk. By Observation4, if the u-v path in H(P , P ) is the first case, then there is a u-v path u − Fj − w1 − Fk − w2 − ... − wn−1 − Fk − wn − Fj − v in F . The argument is similar for the other three cases. We note that the mapping of a u-v path in H(Pj, Pk) to a u-v path in F is a bijection.

26 If there are two u-v paths in H(Pj, Pk), then there are two u-v paths in F and F is not a forest, a contradiction. Therefore, we have the following observation: Observation 5. Let u, v ∈ X. There is no u-v path in F if and only if there is no u-v path in H(Pj, Pk). If there is a u-v path in F , then there is a bijection between the unique u-v path in F and the unique u-v path in H(Pj, Pk). This implies that if u, v ∈ X and there is a u-v path in H(Pj, Pk), then this path is unique. Next, suppose that there is a cycle in H(Pj, Pk). Then this cycle has even length in H(Pj, Pk) since H(Pj, Pk) is bipartite with X on one side and vertices for every part in Pj or Pk on the other side. Furthermore, the cycle has length at least four and alternates vertices from X and from parts in Pj or Pk. This indicates that there must be two vertices u, v ∈ X in the cycle, which contradicts the fact that if there is a u-v path in H(Pj, Pk), then such a path must be unique.

Claim 3. The partition Q(Pj, Pk) satisfies (J2).

Proof. Let Q0 be a partition of V (H(Pj, Pk)) where vertices in the same connected component of H(Pj, Pk) belong to the same part of Q0. We recall that Q(Pj, Pk) is the partition of X obtained from Q0 by restricting each part of Q0 to elements in X. That is, Q(Pj, Pk) = {Q ∩ X | Q ∈ Q0}. We prove (J2) by showing that the following statements are equivalent for u, v ∈ X:

(P1) u and v are in the same part of P,

(P2) there is a unique u-v path in F ,

(P3) there is a unique u-v path in H(Pj, Pk), and

(P4) u and v are in the same part of Q(Pj, Pk).

(P1) ⇐⇒ (P2): This is by the definition of P. The vertices u and v are in the same part of P if and only if they are in the same connected component of F . Since F is a forest, the vertices u and v are in the same connected component of F if and only if there is a unique u-v path in F . (P2) ⇐⇒ (P3): This is by Observation5. (P3) ⇐⇒ (P4): Since H(Pj, Pk) is a forest, there is a unique u-v path in H(Pj, Pk) if and only if u and v are in the same connected component of H(Pj, Pk). By the definition of Q(Pj, Pk), u and v are in the same connected component of H(Pj, Pk) if and only if they are in the same part of Q(Pj, Pk). From (P1) ⇐⇒ (P4), we know that Q(Pj, Pk) = P.

For (J3), we set Dj(v) := deg (v) and Dk(v) := deg (v) for v ∈ X. By the edge disjointness Fj Fk of F and F , we have Dj(v) + Dk(v) = deg (v) + deg (v) = deg (v) = D(v). j k Fj Fk F Now we show (J4). j j k k Claim 4. Fj is feasible for MBTj(X, P ,D ) and Fk is feasible for MBTk(X, P ,D ).

j j Proof. We will show that Fj is feasible for MBTj(X, P ,D ). The proof for Fk being feasible for k k MBTk(X, P ,D ) would follow by the same argument. We need to show that properties (DP1), (DP2), (DP3), (DP4), and (DP5) hold. (DP1) holds 0 j because Fj := (V (F ) ∩ Vj,E(F ) ∩ Ej) is a subgraph of Gj. (DP2) holds since s ∈ X = X where j X := V (Fj) ∩ Xj. This is obtained from the fact that F is feasible for MBTi(X, P,D), which

27 implies s0 ∈ X, and Xj = X by Claim1. (DP3) holds because of Claim1. (DP4) holds by the j definition of P : vertices of X in the same connected component of Fj will be in the same part of j j : P . (DP5) holds by the definition D (v) = degFj (v) for every v ∈ X. We have shown that there exist partitions Pj, Pk of X, degree requirements Dj,Dk : X → {0, 1, 2, 3}, and a decomposition of the forest F into F j and F k such that (J1), (J2), (J3), and (J4) j j k k hold. Therefore, MBTi(X, P,D) ≤ maxPj ,Pk,Dj ,Dk MBTj(X, P ,D ) + MBTk(X, P ,D ).

Next, we show that LHS ≥ RHS. Given input (X, P,D) at tree node i, suppose that we have partitions Pj and Pk of X, and degree requirements Dj,Dk : X → {0, 1, 2, 3} such that

1. the merged graph H(Pj, Pk) is a forest,

2. the partition Q(Pj, Pk) is exactly P,

3. Dj(v) + Dk(v) = D(v) for v ∈ X, and

j j k k 4. there are feasible forests Fj for MBTj(X, P ,D ) and Fk for MBTk(X, P ,D ).

0 We show that F := (V (Fj) ∪ V (Fk),E(Fj) ∪ E(Fk)) is a feasible forest for MBTi(X, P,D) with j j k k 0 MBTj(X, P ,D ) + MBTk(X, P ,D ) edges. This consists of showing the following: (1) |E(F )| = j j k k 0 0 MBTj(X, P ,D ) + MBTk(X, P ,D ), (2) F is a forest, and (3) F is feasible for MBTi(X, P,D). For the first part, we note that Fj and Fk are subgraphs of Gj and Gk, respectively. By Lemma9, 0 we have E(Gj) ∩ E(Gk) = ∅ which implies E(Fj) ∩ E(Fk) = ∅ and |E(F )| = |E(Fj)| + |E(Fk)| = j j k k MBTj(X, P ,D ) + MBTk(X, P ,D ). We now prove that F 0 is a forest. Similar to the argument while proving LHS ≤ RHS, we note that Observation4 still holds. Claim 5. F 0 is a forest.

Proof. The proof consists of showing two parts: (1) if u, v ∈ X and there is a u-v path in F 0, then this path is unique, and (2) there is no cycle in F 0. Let u, v ∈ X. Since E(Fj) ∩ E(Fk) = ∅ and (V (Fj) \ Xi) ∩ (V (Fk) \ Xi) = ∅, if there is a u-v path in F 0, it must be one of the following:

1. u − Fj − w1 − Fk − w2 − ... − wn−1 − Fk − wn − Fj − v,

2. u − Fj − w1 − Fk − w2 − ... − wn−1 − Fj − wn − Fk − v,

3. u − Fk − w1 − Fj − w2 − ... − wn−1 − Fk − wn − Fj − v, or

4. u − Fk − w1 − Fj − w2 − ... − wn−1 − Fj − wn − Fk − v

0 where w1, ..., wn ∈ X \{u, v}. By Observation4, if the u-v path in F is the first case, then there j k is a u-v path u − Hj − w1 − Hk − w2 − ... − wn−1 − Hk − wn − Hj − v in H(P , P ). The argument is similar for the other three cases. We note that the mapping of a u-v path in F 0 to a u-v path in H(Pj, Pk) is a bijection. If there are two u-v paths in F 0, then there are two u-v paths in H(Pj, Pk) and H(Pj, Pk) is not a forest, a contradiction. Therefore, we have the following observation:

28 Observation 6. Let u, v ∈ X. There is no u-v path in H(Pj, Pk) if and only if there is no u-v path in F 0. If there is a u-v path in H(Pj, Pk), then there is a bijection between the unique u-v path in H(Pj, Pk) and the unique u-v path in F 0. This implies that if u, v ∈ X and there is a u-v path in F 0, then this path is unique. Now, if F 0 has a cycle, then this cycle must have two vertices u, v ∈ X because (V (Fj)\Xi)∩(V (Fk)\Xi) = ∅. This implies that there are two u-v paths in F 0, a contradiction.

0 Finally, we show that F is feasible for MBTi(X, P,D). 0 Claim 6. The forest F is feasible for MBTi(X, P,D). Proof. We need to show (DP1), (DP2), (DP3), (DP4), and (DP5). (DP1) holds because F 0 := (V (Fj) ∪ V (Fk),E(Fj) ∪ E(Fk)) is a subgraph of Gi = (V (Gj) ∪ V (Gk),E(Gj) ∪ E(Gk)). (DP2) 0 j j k k holds because we know s ∈ X from the feasibility of MBTj(X, P ,D ) (or MBTk(X, P ,D )). 0 0 (DP3) holds because Xi = Xj = Xk and X = Xi ∩ V (F ). To show X = Xi ∩ V (F ), first 0 0 X = Xj ∩ V (Fj) ⊆ Xi ∩ V (F ) and Xi ∩ V (F ) = Xi ∩ (V (Fj) ∪ V (Fk)) = Xi ∩ (X ∪ (V (Fj) \ X) ∪ (V (Fk) \ X)) = Xi ∩ X ⊆ X. (DP5) holds by edge disjointness of Fj and Fk and the fact that D(v) = Dj(v) + Dk(v) for every v ∈ X. Now we prove (DP4), namely for u, v ∈ X, if u and v are in the same connected component of F 0, then u and v are in the same part of P. We show that the following statements are equivalent for u, v ∈ X,

(P10) u and v are in the same connected component of F 0,

(P20) there is a unique u-v path in F 0,

(P30) there is a unique u-v path in H(Pj, Pk), and

(P40) u and v are in the same part of Q(Pj, Pk) = P.

(P10) ⇐⇒ (P20): Since F 0 is a forest, u and v are in the same connected component of F 0 if and only if there is a unique u-v path in F 0. (P20) ⇐⇒ (P30): This is by Observation6. (P30) ⇐⇒ (P40): This is by the definition of Q(Pj, Pk). Vertices u and v are in the same connected component of H(Pj, Pk) if and only if they are in the same part of Q(Pj, Pk). Since H(Pj, Pk) is a forest, there is a unique u-v path in H(Pj, Pk) if and only if u and v are in the same part of Q(Pj, Pk).

0 j j k k Since F is a feasible forest with MBTj(X, P ,D ) + MBTk(X, P ,D ) edges for MBTi(X, P,D), j j k k we have MBTi(X, P,D) ≥ maxPj ,Pk,Dj ,Dk MBTj(X, P ,D ) + MBTk(X, P ,D ). j k j k Next, suppose that MBTi(X, P,D) is infeasible. For every P and P such that H(P , P ) is a for- est, Q(Pj, Pk) = P is the resulting partition on X, and for every Dj,Dk : X → {0, 1, 2, 3} such that j k j j k k D(v) = D (v)+D (v) for every v ∈ X, either MBTj(X, P ,D ) = −∞ or MBTk(X, P ,D ) = −∞, otherwise we can find a feasible solution and this contradicts the assumption that MBTi(X, P,D) j k j k is infeasible. If such P , P ,D , and D do not exist, then MBTi(X, P,D) = −∞.

29 References

[BBD+20] János Balogh, Cosmin Bonchiş, Diana Diniş, Gabriel Istrate, and Ioan Todinca. The heapability of finite partial orders. Discrete Mathematics and Theoretical Computer Science, 22(1), 2020. [BGGS16] Anne-Laure Basdevant, Lucas Gerin, Jean-Baptiste Gouéré, and Arvind Singh. From Hammersley’s lines to Hammersley’s trees. Probability Theory and Related Fields, pages 1–51, 2016. [BHMZ11] John Byers, Brent Heeringa, Michael Mitzenmacher, and Georgios Zervas. Heapable sequences and subseqeuences. In Proceedings of the Eighth Workshop on Analytic Algo- rithmics and Combinatorics, ANALCO ’11, pages 33–44, 2011. [BIR18] Cosmin Bonchiş, Gabriel Istrate, and Vlad Rochian. The language (and series) of Hammersley-type processes. In Proceedings of the Eighth Conference on Machines Com- putation and Universality (MCU’18), volume 10881 of Lecture Notes in Computer Sci- ence, 2018. [Bod96] Hans L Bodlaender. A linear-time algorithm for finding tree-decompositions of small treewidth. SIAM Journal on Computing, 25(6):1305–1317, 1996. [BS18] Anne-Laure Basdevant and Arvind Singh. Almost-sure asymptotic for the number of heaps inside a random sequence. Electronic Communications in Probability, 23(17), 2018. [CFK+15] Marek Cygan, Fedor V. Fomin, Lukasz Kowalik, Daniel Lokshtanov, Daniel Marx, Marcin Pilipczuk, Michal Pilipczuk, and Saket Saurabh. Parameterized Algorithms. Springer, 2015. [CGI+20] Karthekeyan Chandrasekaran, Elena Grigorescu, Gabriel Istrate, Shubhang Kulkarni, Young-San Lin, and Minshen Zhu. The maximum binary tree problem. In Proceedings of the 32nd European Symposium on Algorithms (ESA’20), to appear, 2020. Also arXiv preprint: 1909.07915. [DF12] Rodney G Downey and Michael Ralph Fellows. . Springer Verlag, 2012. [Gol80] Martin Charles Golumbic. Chapter 7 - permutation graphs. In Martin Charles Golumbic, editor, Algorithmic and Perfect Graphs, pages 157 – 170. Academic Press, 1980. [IB15] Gabriel Istrate and Cosmin Bonchiş. Partition into heapable sequences, heap tableaux and a multiset extension of Hammersley’s process. In Proceedings of the 26th Annual Symposium on Combinatorial Pattern Matching (CPM’15), Ischia, Italy, volume 9133 of Lecture Notes in Computer Science, pages 261–271. Springer, 2015. [IB16] Gabriel Istrate and Cosmin Bonchiş. Heapability, interactive particle systems, partial orders: Results and open problems. In Proceedings of the 18th International Conference on Descriptional Complexity of Formal Systems (DCFS’2016), Bucharest, Romania, volume 9777 of Lecture Notes in Computer Science, pages 18–28. Springer, 2016.

30 [PLE71] A. Pnueli, A. Lempel, and S. Even. Transitive orientation of graphs and identification of permutation graphs. Canadian Journal of Mathematics, 23(1):160–175, 1971.

[Por15] Jaclyn Porfilio. A combinatorial characterization of heapability. Master’s thesis, Williams College, 2015.

[Rom15] Dan Romik. The surprising mathematics of longest increasing subsequences. Cambridge University Press, 2015.

31