How to find a local minimum of a complete binary tree?

Consider an $n$-node complete binary tree $T$, where $n = 2^d − 1$ for some $d$. Each node $v \in V(T)$ is labeled with a real number $x_v$. You may assume that the real numbers labeling the nodes are all distinct. A node $v \in V(T)$ is a local minimum if the label $x_v$ is less than the label $x_w$ for all nodes $w$ that are joined to $v$ by an edge.

You are given each a complete binary tree $T$, but the labeling is only specified in the following implicitly way: for each node $v$, you can determine the value $x_v$ by probing the node $v$. Show how to find a local minimum of $T$ using only $O(\log n)$ probes to the nodes of $T$.

Attribution: this seems to be Problem 6 in Chapter 5 "Divide and Conquer" from the book "Algorithm Design" by Jon Kleinberg and Eva Tardos.

  • 3
    $\begingroup$ You're talking about "the" local minimum, but there could be several ones. $\endgroup$ Feb 25 '13 at 4:26
  • 2
    $\begingroup$ There is an algorithm which will find one of the local minima in a complete binary tree in time O(log n). Is this what you're looking for? Here's a hint: call a node a "candidate local minimum" if it's less than its parent. Now, for any candidate local minimum: either it is a local minimum or it has a child which is a candidate local minimum. $\endgroup$
    – rici
    Feb 25 '13 at 18:34
  • $\begingroup$ I see a note saying that the text has been copied from an exercise. Copying a problem from a textbook without attribution is not allowed on this site. (Copying it even with attribution is rather poor form, in my opinion, but copying it without attribution amounts to plagiarism, which is not welcome here.) Let's keep the attribution intact, please. $\endgroup$
    – D.W.
    Oct 26 '14 at 1:35

As the tree is finite and the labels are real numbers, we are assured that there is a least element in the set of labels (this question from math.SE has a proof of this simple property). In this case as all the labels are distinct, it is okay that we require the local minima to be strictly less than their neighbours. If they were, then we would have to relax this condition to "less or equal to", otherwise there may not be a solution. Nonetheless we know that there is at least one local minimum to find.

If the tree is rooted (i.e. we have a notion of a parent-child relation) we can solve the problem marginally cheaper. If the tree is unrooted, then asymptotically we can do as well, but we will likely perform more actual probes.

First we'll deal with the rooted case.

As the tree is a (complete) binary tree, each vertex has at most three neighbours, its parent and two siblings (with the root of course having no parent), so a vertex is a local minimum if its label is less than the labels of its two children and parent. Hence we can determine if a vertex is a local minimum or not with at most four probes (in fact as we will traverse the tree in an ordered fashion, we will need at most three in the rooted case).

To actually find a local minimum, we can traverse the tree, starting with the root as the current vertex, in the following fashion:

  1. Probe the current vertex's label, and the labels of its two children.
  2. If the current label is the smallest, halt and report that the current vertex is a local minimum.
  3. Else set the current vertex to the child with the smallest label, and return to step 1.

As we start from the root, we never need to worry about the parent's label - the root has no parent, and if our current vertex has a parent, then we must have discounted it in the previous iteration. Note that in step 2 it's not strictly required that we choose the smallest label, any that is smaller than the current is sufficient1, picking the smallest just gives a definite choice (again because we have distinct labels).

As we select one vertex out of the two children, our traversal picks out a path from the root to (at furthest) a leaf, thus as we have a binary tree of depth $d < \log n + 1 $ (as it's a complete binary tree with $n = 2^{d}-1$ vertices), we perform at most $3d \in O(\log n)$ probes.

In the unrooted case, we don't have such a convenient starting point, however we still have the tree structure (even if the algorithm doesn't know it). We can use the following algorithm:

  1. Pick any vertex to be the initial current vertex.
  2. Probe the label of the current vertex and all of its neighbours.
  3. If the current vertex has the lowest label, halt and report it as a local minimum.
  4. Else, select the neighbour with the lowest label as the new current vertex and return to step 2.

At each iteration we only perform at most 4 probes, so the question is how many iterations can there be? The key is to observe that we can never backtrack (we know that were we came from had a bigger label), so we must follow a simple path in the graph, but as the graph is actually a complete binary tree, the longest simple path has length $2d$. Thus even in the unrooted case we perform at most $8d \in O(\log n)$ probes.

For correctness we look at the second algorithm (the rooted case is clearly just a special case of the unrooted).

The fact that the algorithm can never backtrack also guarantees termination, as the graph is finite. Now we need only demonstrate that the vertex $v$ we terminate at is in fact a local minimum. Assume for contradiction that it is not, then it has some neighbour $u$ that has a label with a lower value. We have three cases: (1) $u$ is the previous vertex in the path which the algorithm has followed, in which case when the algorithm was at $u$, it would not have selected $v$ as the next vertex; (2) $u$ is appears in the path, but more than one iteration earlier, in this case the graph is not a tree; and (3) $u$ is not in the path the algorithm followed, but then the algorithm would select $u$ as the next vertex in step 4, and the algorithm would not have terminated at $v$. Thus $v$ must be a local minimum.

Another way to see this is to simply observe that the sequence of labels of the vertices selected by the algorithm is monotonically decreasing. From this both the termination and correctness follows immediately.


  1. The property that allows this is that every subtree has a local minimum - again, every finite set which has a total ordering has a least element. Thus by induction, when we extend the subtree with a new root and a sibling-subtree, we have three cases: (1) the new root is smaller than both its children, so the algorithm would terminate the new root; (2) the new root is smaller than one child but not the other, then the algorithm will not choose the larger root and must take the smallest by definition; or (3) both children are smaller, but in this case, as noted, each subtree has at least one local minimum, so we may select either.
  • $\begingroup$ A correctness proof of your algorithm is desirable: Why can we safely throw away the other sub-tree and only select the child with the smallest label? IMO, it is necessary to prove that your algorithm will find some local minimum as long as they do exist. Furthermore, a stronger property may be true: Every complete binary tree has at least one local minimum. $\endgroup$
    – hengxin
    Oct 25 '14 at 2:10
  • $\begingroup$ @hengxin (hopefully) clarified. $\endgroup$ Oct 25 '14 at 3:07
  • $\begingroup$ i think there is a flaw in this reasoning as O(log n) probes require some order in the insertion of the tree values, so that the divide and conquer can discriminate between going to the left and to the right. In this case, on step4 "select the neighbor with the lowest label" is really not choosing the correct path always as there is not ordering in the tree values (the local minima can be in the far right subtree). If there is ordering upon insertion of the tree, then the parent node is always greater than the current node, making the local minima to exist only on the left leaf of the tree. $\endgroup$ Oct 12 '17 at 5:36
  • $\begingroup$ Cormen et at (2011) defines that for a tree to be considered a binary search tree it must have the "binary-search-tree property" which requires a certain order in the tree insertion. $\endgroup$ Oct 12 '17 at 5:40
  • $\begingroup$ @JuanZamora The question isn't about binary search trees, it's just about vertex labelled complete binary trees. Secondly, even if it were about BSTs, it's about finding a local minimum, not the global minimum. $\endgroup$ Oct 12 '17 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.