Thought process to solve tree based Dynamic Programming problems

I am having a very hard time understanding tree based DP problems. I am fairly comfortable with array based DP problems but I cannot come up with the correct thought process for tree based problems and I was hoping somebody could please explain their thought process.

I will talk about my thought process behind array based problems and then explain my troubles with tree based DP problems.

My thought process for array problems

The way I think about DP in array based problems is as follows. Let us consider a problem like Minimum Path Sum. Here the objective is to get from the top left to bottom right positions in a matrix such that we minimize the cost of the path. We can only move left and right.

The way I would approach problems like this is as follows:

  • First I would construct a recurrence. In this case the recurrence is as follows

The recurrence is:

f(i, j) = a[i][j] // if i == m and j == n f(i, j) = a[i][j] + f(i, j+1) // if i == m f(i, j) = a[i][j] + f(i+1, j) // if j == n f(i, j) = a[i][j] + Math.min( f(i, j+1), f(i+1, j) ) // Otherwise 
  • Next I look at the last equation f(i, j) = a[i][j] + Math.min( f(i, j+1), f(i+1, j) ) which tells me the problem can be solved using DP as there are overlapping subproblems in f(i+1, j) and f(i, j+1). There is also an optimal substructure.

  • I can also tell the time/space complexity just by looking at the recurrence.

    • Because we must compute all states which is all (i,j) pairs and because time per state is O(1) (adding a[i][j] to result) the time complexity is O(n^2).
    • Looking at the recurrence, i depends only on i+1 and not on i+2, i+3 … similarly j depends only on j+1 and not on j+2, j+3… so we can get away with using only 1 extra row (either i+1 or j+1) instead of the entire matrix so space complexity is O(n).

Hence I would come up with a n^2 time and n space solution. I can do this without any problems.

My thought process for tree problems

However I am having a hard time applying the same thought process to tree based DP problems. As an example let us consider the problem Diameter of Binary Tree where the objective is to find the longest path between any 2 nodes in the tree.

I can come up with a recurrence for this problem which is as follows:

f(n) = 0 // if n == null f(n) = max( 1+height(n.left) + height(n.right),         // longest path passing through root             f(n.left),                                  // longest path in left subtree             f(n.right)                                  // longest path in right subtree 

Because f(n.left) for example is computed by doing 1+height(n.left.left) + height(n.left.right) I can tell that DP must be used.

So my approach would be to create a cache of size ‘n’ that stores all the heights of the nodes. So the space complexity would be O(n).

However the optimal solution of this problem has a space complexity of O(1) and I am having a hard time figuring that out just by looking at the recurrence. How does the recurrence tell you that space complexity can be reduced and that O(1) space is enough and O(n) is not needed? How do you know what value(s) to store in this case? In array based problems I can get the answers to both these questions just by looking at the recurrence but for tree based dp it is not so obvious to me.

My questions:

  1. What can you tell about this problem just by looking at the recurrence for the tree problem? Putting aside my own thought process, if I gave you this recurrence and nothing else what conclusions would you reach and how would you write the program? I am curious about your thought process.

  2. For array based problems I can tell just by looking at the recurrence both how much space I needed to solve the problem AND what exactly I needed to store (I need to store values of row i+1 in min path sum and nothing else). How can I do the same for the tree problem?


Accessing individual dynamic tags in a for loop [closed]

JavaScript beginner here. I have created dynamic tags in JavaScript using a for loop but have trouble accessing individual tags. I want to be able to style these tags as well as checking if a tag is clicked or not, like in html, but in this case, JavaScript.


JavaScript: function createImg() { img = document.createElement(“img”); img.src = “bugImage.jpg”; for (i = 0; i < 4; i++) { img[i] = document.createElement(“img”); img[i].src = img.src; document.getElementById(“div1”).appendChild(img[i]); } }

Any help is greatly appreciated!

Undo dynamic input

Here is a simplified example of the problem.

DynamicModule[{customControl, eq, x, y, z},  eq[] := Quiet@Solve[x + y + z == 10];  customControl[{var_, init_, lbl_}] :=    Column[{Row[{lbl, Button["Clear", Unset[var]]}],      Control[{{var, init},        InputField[         Dynamic[var, (var = #; (eq[] /. (lhs_ -> rhs_Integer) :>                Set[lhs, rhs])) &], Number, FieldSize -> 6] &}]},     Alignment -> Center];  Framed@Row@{customControl[{x, x, "x"}], customControl[{y, y, "y"}],      customControl[{z, z, "z"}], Spacer[50],      Dynamic[Column@{x, y, z}]}] 

This piece of code solves the equation $ x+y+z=10$ with $ x$ , $ y$ and $ z$ entered using InputField. For instance, when you enter $ 1$ for $ x$ , $ 2$ for $ y$ , $ z$ is automatically set equal to $ 7$ . Now how do you undo the last operation? Something like Ctrl+Z for dynamic input to Unset the value of $ y$ AND the value of $ z$ ?

When you Unset the value of y (by clicking the button), z is still equal to 7.

S/Kademlia dynamic crypto puzzle

In S/Kademlia two crypto puzzles is used, one static and dynamic. The static is used to generate a key pair that makes an eclipse attack difficult to achieve, the dynamic to make Sybil attacks harder.

crypto puzzles

In the dynamic, I am very confused about the x which is found, since I can’t find any evidence for this ever being used again in the system, is it send along in a signature or what is it used to?

I do not think the paper mentions any use of it, I at least have not been able to find it, why I ask here for the use of x.

Optimal ordering – Dynamic programming on subsets

We have a set T of n elements and m subsets $ R_i \subset T i = 1,…,m$ . The $ S_i$ are not assumed to be different. We also define an ordering of T, a one-to-one mapping $ \pi$ of $ T$ onto the set of integers $ {1,…n}$ . That is, every $ \pi(v)$ is an integer from 1 to n and all $ \pi(v), v \in T$ are distinct. For a set $ R \subset T$ and an ordering of $ \pi$ of $ V$ , let $ l_\pi(S)$ and $ r_\pi(S)$ denote, respectively, the smallest and largest value $ \pi(v)$ among all $ v \in S$ . The problem is to find an ordering $ \pi$ that minimizes $ \sum_{i=1}^m (r_\pi(S_i) – l_\pi(S_i))$ .

This problem is NP-hard but is in FPT and can be solved with dynamic programming on subsets in $ O^*(2^n)$

The idea here is to optimize the ordering of the first $ k$ items and then go from $ k$ to $ k+1$ items and prove that the time complexity does not change when doing so.

But how do we optimize the ordering for the first $ k$ items in just $ O^*(2^n)$ time? It seems like the same problem as the initial one to me.

If a problem is in P solved via dynamic programming, is it also in NP?

So I can solve a given problem using dynamic programming in $ O(n^2k^2)$ time complexity. This means that the problem is in P. But I am asked if it is in NP.

My answer is, “Since it is also polynomial time solvable, the problem is also in $ NP$ “.

Is that a correct statement? If not, is a problem in P solved via dynamic programic also NP?

Dynamic length of union of segments (1d Klee’s measure problem)

Finding the length of union of segments (1-dimensional Klee’s measure problem) is a well-known algorithmic problem. Given a set of $ n$ intervals on the real line, the task is to find the length of their union. There is a simple $ O(n \log n)$ solution involving sorting endpoints of all intervals and then iterating over them while keeping track of some counters.

Now let’s look at a dynamic variant of this problem: we are receiving $ n$ segments one after another and the task is to find the length of the current union of segments after each new segment arrives. It seems like a natural generalization, however, I cannot find much information about this version of the problem. I wonder if it is still possible to solve this problem in $ O(n \log n)$ time in an offline setting, i.e. when we are given all segments beforehand and can preprocess them in some way before giving an answer for each segment.

I feel like a modification of a segment tree might be useful here. In every node we need to keep track of both the total sum of lengths of segments (or their parts) corresponding to this node and the length of their union. However, I cannot figure out how to implement this modification without performance of one update degrading to linear time in some cases. Maybe using a segment tree is not the right approach and a better way exists.

Is dynamic programming restricted to optimization problems?

The usual criteria used to decide if a problem can be solved using dynamic programming is (1) if it has optimal sub-problems and (2) if it has overlapping sub-problems. Does the word “optimal” mean that DP can only be used to solve optimization problems? Otherwise, it would make more sense to write (1) as “if it can be decomposed in sub-problems” (i.e. it is recursive). Obviously, this definition includes optimization problems with optimal sub-problems.