0
$\begingroup$

So there is this question :

we are given a tree with n nodes. For each node, $i$ we are assigned two values : a $r_i$, that is the right boundary, and a $l_i$, which is the left boundary. We should give each node, $v_i$, a value , $a_i$, that $ a_i \in \lbrack l_i, r_i \rbrack$ . in other words $ l_i \le a_i \le r_i $. if edges are weighted and each weight of edges between every node $ (a_u, a_v)$ is equal to $ |a_v - a_u|$, then calculate the maximum possible sum of edges in this tree.

My idea to solve this question was to use a Dynamic Programming approach. I intuitively guessed that if we want to maximize the result, we should choose the boundary values for each $a_i$. Then I assumed that we need to create an $n \times 2 $ DP array. $dp[i][0]$ is the maximum subtree contribution if we assign $ a_i = l_i $ and $dp[i][1]$ is the maximum subtree contribution if we assign $ a_i = r_i $. Then I assumed that we start from the leaves of the tree to fill the table and this would be recurrence relations: $$ dp[leaf][0] = dp[leaf][1] = 0\\ \\ dp[i][0] = \sum_{c∈children(i)}{​max(∣l_i​−l_c​∣+dp[c][0],∣l_i​−r_c​∣+dp[c][1])} \\ \\ dp[i][1] = \sum_{c∈children(i)}{​max(∣r_i​−l_c​∣+dp[c][0],∣r_i​−r_c​∣+dp[c][1])} \\ \\ answer = max(dp[root][0], dp[root][1]) \\ $$ However, I can't think of a proof for this algorithm. I know that in Dynamic Programming algorithms we check for overlapping subproblems and optimal substructures, but cannot provide a reasonable proof for why we should choose the boundary values. I'd appreciate it if anyone could help me prove its correctness, or even if it's wrong, help me find the correct answer.

My closest attempt at proving : if we take a node $a_i$ and all the neighboring nodes of i, $\mathcal{N}(i) = \{a_{j_1}, a_{j_2}, ..., a_{j_k}\}$, then the sum of edges that this node is contributing to the total sum would be $ \sum_{j_t \in \mathcal{N}(i)}{|a_i - a_{j_t}|}$ which can be maximized only when $a_i$ is equal to $r_i$ or $l_i$. Because then $a_{j_t}$ can be in two positions with respects to the range $ \lbrack l_i, r_i \rbrack $ : A it can be outside the range, so then the farthest from it would be one of the boundaries; B it can be inside the range, so then because of the nature of the absolute value function, it should be one of the boundary points to maximize the sum.

$\endgroup$
2
  • $\begingroup$ If you are given the $a_i$'s, and edge weights are non negative, what prevents you to compute said sum? $\endgroup$ Commented Dec 13, 2024 at 13:51
  • $\begingroup$ I think I described the question wrongly. The point you made is correct. $a_i$ is not given by the question, but only its range is defined as it should be in $ \lbrack l_i, r_i \rbrack $. What the question wants is that we should assign $a_i$'s so that the sum of edges' weights be maximized. I edited the question. Hope it's clear now. $\endgroup$ Commented Dec 13, 2024 at 14:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.