PrevNext
Somewhat Frequent
 0/31

Binary Jumping

Authors: Benjamin Qi, Neo Wang, Qi Wang

Contributors: Kevin Sheng, Mihnea Brebenel

Efficiently finding ancestors of a node.

Edit This Page

Terminology

Binary jumping is more commonly referred to as "binary lifting".

Binary Jumping

Focus Problem – try your best to solve this problem before continuing!

Explanation

Binary lifting consists of calculating the 2k2^k-th ancestor of each node for all relevant values of kk and storing them in a table.

With this table, we can then efficiently answer queries regarding the kk-th ancestor of all nodes. This is because any kk can be broken down into a sum of powers of 22 with its binary representation.

This way, instead of directly computing, say, the 1313th ancestor of a node, we can go to the 88th, then 44th, then 11st ancestor of the node. This results in a logarithmic complexity for computing the kkth parent.

Here's an animation of how we jump if you're still confused:

To actually compute our binary jumping table, we start with the 20=12^0=1st parents of each node, which is their direct parent. We then move on and calculate 21=22^1=2nd parents, using the fact that the 22nd parent can be calculated as the 11st parent of the 11st parent. Using similar logic, we move on to 222^2, 232^3, and so on. We stop when 2n2^n is greater than the size of the tree, since at that point we're definitely at the root.

Implementation

Time Complexity: O((N+Q)logN)\mathcal{O}((N+Q)\log N)

C++

#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
class Tree {
private:

Problems

StatusSourceProblem NameDifficultyTags
CSESEasy
Show TagsBinary Jumping
CSESNormal
Show TagsFunctional Graph
CSESNormal
Show Tags2P, Binary Jumping, Binary Search
POINormal
Show TagsBinary Jumping, Sliding Window
CFNormal
Baltic OINormal
Baltic OINormal
PlatinumHard
Show TagsBinary Jumping
Baltic OIVery Hard

Lowest Common Ancestor

Focus Problem – try your best to solve this problem before continuing!

Resources
CPH

Brief description/solution

SansPapyrus683

Alternative implementation

Explanation 1

To find lca(a,b)\textrm{lca}(a, b), we can first lift the lower node of aa and bb to the same depth as the other. Then, we lift both nodes up decrementally. At the end, the parent of either node is the LCA of the two.

Implementation

Time Complexity: O((N+Q)logN)\mathcal{O}((N+Q)\log N)

C++

#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
class Tree {
private:

Explanation 2

We can also use an Euler tour of the tree to help us compute the LCAs as well.

Let start\texttt{start} and end\texttt{end} be the time-in and time-out table for the nodes in the tree. They'll be filled up in the exact same way the Euler tour module does it.

The cool thing is that while we're filling up start\texttt{start} and end\texttt{end}, we can also calculate our binary jumping table in the exact same way we did in the previous solution! We can do this because in a DFS, we're guaranteed to have processed all of a node's parents before the node itself, so all the tables of any node's ancestors will have been filled when we reach the node.

Now, to actually calculate the LCA without the depths of the nodes, we can use the fact that node aa is an ancestor of node bb if start[a]start[b]\texttt{start}[a] \le \texttt{start}[b] and end[b]end[a]\texttt{end}[b] \le \texttt{end}[a].

In our LCA function, we first check if one node is already an ancestor of the other. In that case, we return the ancestor. If it isn't, then we lift up one of the nodes until its an ancestor of the other in a method that's basically the same as our previous binary jumping algorithm. After that, our answer is the parent of the node we lift up.

Implementation

Time Complexity: O((N+Q)logN)\mathcal{O}((N+Q)\log N)

C++

#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
class Tree {
private:

Explanation 3

We can also find the LCA of two nodes using Tarjan's Offline LCA algorithm.

By taking advantage of the DFS traversal, we can precompute the answers to the queries through forming subtrees and calculated the common parent with a similar structure as Disjoint-Set Union.

Implementation

C++

#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 1;
bool vis[MAX];
int lca[MAX], fa[MAX];
vector<array<int, 2>> adj[MAX], qry[MAX];
int find(int u) { return (fa[u] == u) ? u : fa[u] = find(fa[u]); }
void tarjan(int node) {
vis[node] = true;
Resources
cp-algo

Focus Problem – try your best to solve this problem before continuing!

Explanation

Since we have the depth of all the nodes, the distance between two nodes aa and bb is depth[a]+depth[b]2depth[lca(a,b)]\texttt{depth}[a] + \texttt{depth}[b] - 2 \cdot \texttt{depth}[\textrm{lca}(a, b)].

Here's some intuition if you're confused about how this formula works. To get from node aa to bb, one way would be to go to the root of the tree and then to bb. This gives us a distance of depth[a]+depth[b]\texttt{depth}[a] + \texttt{depth}[b]. However, notice that we're passing through all the nodes above the LCA twice. Thus, we have to subtract off twice the depth of the LCA, giving us our final expression.

Implementation

Time Complexity: O((N+Q)logN)\mathcal{O}((N+Q)\log N)

C++

#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
Code Snippet: LCA Tree (Click to expand)

Problems

USACO

StatusSourceProblem NameDifficultyTags
PlatinumEasy
Show TagsLCA
PlatinumNormal
Show TagsLCA
Old GoldNormal
Show TagsBinary Jumping, Euler Tour, Small to Large
PlatinumHard
Show TagsLCA
PlatinumHard
Show TagsDiameter
PlatinumHard
Show TagsLCA
PlatinumVery Hard
Show TagsLCA

General

StatusSourceProblem NameDifficultyTags
CFEasy
Show TagsBinary Jumping
CFNormal
Show TagsLCA
Baltic OINormal
CFNormal
Show TagsLCA
CFNormal
Show TagsLCA
CSANormal
Show TagsLCA
CFNormal
Show TagsLCA
Back to SchoolNormal
Show TagsLCA
Google KickstartHard
Show TagsBinary Jumping, DFS, LCA
CFHard
Show TagsBinary Jumping, LCA
TLXHard
Show TagsLCA
TLXHard
Show TagsLCA

Module Progress:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

PrevNext