Solving Tree problems

栏目: IT技术 · 发布时间: 4年前

内容简介:Here in this blog, I will discuss a general problem that has similar complexity issues and share a thought to break the solution.Some Prerequisites

Solving A Tree Problem

Mar 29 ·6min read

Solving Tree problems

Balanced Binary Tree

T ree problems are quite popular among competitive programming questions. All the fame is because of the complexity of thought they deliver to the mind of a beginner and sometimes they even scare the intermediate programmers, who have been programming for a while.

Here in this blog, I will discuss a general problem that has similar complexity issues and share a thought to break the solution.

Some Prerequisites

Sites all over the internet are filled with basic level tree problems like

  • Finding the depth of a node in a tree
  • Finding the height of a tree
  • Sum of a subtree rooted at some internal node of the tree
  • Finding LCA (Lowest Common Ancestor)of two nodes of a tree (in O(N))
  • Finding LCA of any two nodes of a tree (in O(logN))

Generally speaking, all such problems are the building block for the tree-problems of intermediate level, like one discussed below.

A Generic Problem

The problem starts with a description of a tree and clearing out what all kind of trees exist in this world, and then they quote a problem, which obviously can’t be solved with easy tree traversal approaches under given constraints, as

Assume that u and v are two nodes of a balanced binary tree with some given value ‘u’ and ‘v’ and dist(u, v) represent the distance (path length) between the nodes u and v in the tree. For all possible pairs of u and v formulate the expression given below

Solving Tree problems

Constraints

Let N be the number of nodes, then N ≤10⁵ .

With this, a modulus operation with some large prime is also applied onto the function to avoid overflows in the predefined datatypes in languages like C++ and Java.

This is the section where the real problem begins, and the hope of a beginner dies. O(N² * N) is the best approach that comes to the mind and then sticks there until you decide to leave the question.

Solving the problem

Before actually approaching the solution, I would like to enlighten you how experience helps in solving such problems and make them easy to break:
  • A pool of previously solved problems exists inside your brain, from where you can fetch a few hints to approach a problem
  • You already know names of some already existing algorithms, that you must have searched while solving problems with weird complexities and use them for your benefits
  • You build an attitude that even if I am not able to solve it this time but next time I will have this problem in my pocket for tearing down some other problem of similar structure
  • It helped me learn that a tree problem directly involving all the nodes can’t be solved in less than O(N) and always keep a tree in mind while solving the problem. Actually taking the input for the tree takes O(N) complexity wise. So don’t hope for miracles, unless there’s a catch in the statement. Yes, I was dumb.

So as I said solving intermediate-level tree problems takes experience so here are some anchors from my pools of experience that helped me solve the problem

  • In tree problems involving the distance of a node from anywhere is somewhere related to its parent or ancestor, and especially, in the case of pairs of nodes, the LCA (Least Common Ancestor) is that special ancestor that can help in solving the problem
  • I knew that Google can answer my questions like ‘How to find LCA in less than O(N)?’, and this time I also knew the algorithm beforehand. Hint: search Binary Lifting.
  • Secondly, the question tempts me to directly do a direct DFS/BFS from node u to node v to find the distance, hence it's obvious that I should not do, it’s a trap. For all pairs as DFS/BFS has the time complexity of O(N) and in total O(N²) pairs exist in the tree.

With all these thoughts in mind, I started to modify the formula, as follows

Solving Tree problems

  • Taking all the possible node u with all possible node v, broke the big summation into two. Finally, we would have an answer for {u, v} and {v, u} both adding up in the resultant, so just halving the resultant would have given the final answer.
  • Now in a tree, there exists a single path from a node to another, that obviously passes through their LCA. Assuming that the LCA of a valid pair of nodes u and v is L , I split the dist(u, v) = dist(u, L) + dist(L, v) where LCA(u, v) = L .

Solving Tree problems

  • Then, I had 2 similar looking parts of the formula, F1 and F2. Obviously, F1 = F2.
  • On observing formula F1and the tree carefully, it becomes clear that for a given node u and L, some part of the formula is independent of the direct relation between u and v. Actually a relation between a subtree and u is established rather than a single node v, via L.

Solving Tree problems

Solving Tree problems

  • Here the relationship between u, L and v can be exploited. The inner summation of v is actually summing of all the node values with ‘vi’ such that LCA of u and vi’s is L.

    In other words LCA(vi,u) = L where i = 1, 2, 3….

Solving Tree problems
  • Hence the summation on v is the sum of values in the subtree of L, not containing the node u.
    This summation can be precomputed for each node of the tree using some general traversal technique in O(N). Actually, this was the first time it hinted to me that I am proceeding in some right direction and solving simple questions like that paid.
  • Now I could say that the new formula depended upon u and L rather than u and v
  • How did this dependency actually help in solving the problem?

    For answering this we need to understand, what all nodes can be ‘L’ for a given node u.

    Here, L can be any potential node that is parent or ancestor of u. So the complexity for solving for each node is now dependent upon the number of parents of a node. And here’s a catch, the tree given is a balanced binary tree so maximum ancestors a node can have is log(N). This simple catch was the final hint and the whole of the question just broke inside my head.

Since the total number of nodes present in the tree is N and at the max, a node can have log(N) parents so the time complexity for solving the expression for the tree is O(N*logN) , prefect for N ≤ 10⁵.

After this just properly formulate the summation with all the needed requisites like subtree sum etc. And it’s done!

From my side…

Refraining myself from the ideal editorial-sequence, that first discusses a solution for smaller N (ie N ≤ 10³) and then jumping onto the main constraints, I chose to directly discuss the problem’s solution because I think that sometimes it’s just not worth it. Solving for some relaxed constraints doesn’t always give you some hints for further advancements until it’s a DP or subset problem, it’s just an approach, not a solution. And I feel while understanding something new, learning the flow of thoughts is also important, the flow of how people think, observe and apply. If I waste time reading solutions for relaxed constraints I won’t be able to learn how people hit the bigger problem in one go without solving for subparts separately, and this is important for confidence.

That’s my part

Signing off


以上所述就是小编给大家介绍的《Solving Tree problems》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

黑客大曝光

黑客大曝光

Joel Scambray、Vincent Liu、Caleb Sima / 姚军 / 机械工业出版社华章公司 / 2011-10 / 65.00元

在网络技术和电子商务飞速发展的今天,Web应用安全面临着前所未有的挑战。所有安全技术人员有必要掌握当今黑客们的武器和思维过程,保护Web应用免遭恶意攻击。本书由美国公认的安全专家和精神领袖打造,对上一版做了完全的更新,覆盖新的网络渗透方法和对策,介绍如何增强验证和授权、弥补Firefox和IE中的漏洞、加强对注入攻击的防御以及加固Web 2.0安全,还介绍了如何将安全技术整合在Web开发以及更广泛......一起来看看 《黑客大曝光》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码