in DS retagged by
737 views
7 votes
7 votes
You are given a complete binary tree (each level must be full except the last) on $n$ vertices. Each vertex $v$ is labeled by an integer value $x_v$. Say that a vertex is a local minimum if its label is less than the labels of each of its neighbors (neighbors include the parent and the children). Assuming that all the labels are distinct, what is the worst-case time complexity of the most efficient algorithm to find a local minimum in the tree?
  1. $\theta(n)$
  2. $\theta(\sqrt{n})$
  3. $\theta(\log n)$
  4. $\theta(n \log n)$
in DS retagged by
737 views

1 comment

$ \large{\colorbox{yellow}{Detailed video solution of this question with direct time stamp}}$

All India Mock Test 4 - Solutions Part 1

0
0

3 Answers

4 votes
4 votes

The algorithm is as follows:

  1. Let $v$ be the root.
  2. If $v$ is a local minimum, output $v$.
  3. Otherwise, update $v$ to be a child that has a smaller value, and go to step (b).

Since, the algorithm moves to a child in each step, the running time is at most the depth of the tree, which is $O(\log n)$.

edited by

2 Comments

what if there is no local minimu then I have to check for each node right?
0
0

@shadymeee They have given that node values are distinct and its a tree so obviously there will always exist atleast one node which will be the local minimum. The worst case would be when it is the leaf node, that's why the time complexity is O(Logn).

0
0
1 vote
1 vote
$Answer : O(logn)$

Hint : A leaf node is considered as local minimum if its value is less than its parent node.
0 votes
0 votes

Please find the explanation below.

Hope, this helps.

3 Comments

what if 1 is in the right subtree in place of 8, then how to find the local min in log n time? 

1
1
See we don't need to find all the possible local minimum the above logic will find atleast one local minimum.

Since all the labels are distinct it is pretty sure that in one of paths from root to leaf their exist a local minimum. In short we don't care what other minimums are.

You may have a question also that what if leaf itself not local minimum so we need to check other paths also ?

See the above case will never happen because we are going down only when it is lesser than parent so in worst case leaf node will definitely be local minimum.
0
0
thanks./
0
0
Answer:

Related questions