in DS edited by
5,089 views
14 votes
14 votes
Match the pairs in the following questions:$$\begin{array}{|ll|ll|} \hline  (a) & \text{A heap construction} & (p) & \ \Omega(n\log_{10}n)  \\\hline (b) & \text{Constructing Hashtable with linear probing} & (q) & \ O(n) \\\hline (c) & \text{AVL tree construction} &  (r) & \ O(n^{2})  \\\hline (d) & \text{Digital trie construction} & (s) & \ O(n\log_{2}n) \\\hline \end{array}$$
in DS edited by
5.1k views

4 Comments

@Lakshman Patel RJIT

pls put options in question..

0
0
0
0
Cool
0
0

3 Answers

22 votes
22 votes
Best answer

$(A)$ A heap Construction(Build  heap) : -

Build-Heap (A)

  1. heap-size$[A]$ $\leftarrow$ length$[A]$

  2. for $ i \leftarrow$ $\left \lfloor l\text{ength}[A] / 2  \right \rfloor$ downto $1$

  3.        do Heapify $( A, i )$

Note that $A[ ( \left \lfloor n/2 \right \rfloor + 1) \ldots n]$ are all leaves so they are length $1$ heaps.

Trivial Analysis: Each call to Heapify requires $\log(n)$ time, we make $n$ such calls $\implies O(n \log n).$

Tighter Bound: Each call to Max-Heapify requires time $O(h)$ where $h$ is the height of node $i.$ Therefore running time is $$\sum_{h=0}^{\log n}\dfrac{h}{2^{h}+1}\times O(h) = O\left(n \ \sum_{h=0}^{\log n}\dfrac{h}{2^{h}} \right)$$where $2^{h} + 1 = $ Number of nodes at height $h$ and $O(h) = $ Running time for each node.$$= O\left(n \ \sum_{h=0}^{\infty}\dfrac{h}{2^{h}} \right) = O(n)$$ Note:  $\sum_{h=0}^{\infty}\dfrac{h}{2^{h}} = 2$

Reference:

$(B)$ Constructing Hash table with linear probing for $n$ elements has time complexity $\implies O(n^{2})$ in the worst case. This happens when all the $n$ elements are hashed to the same location and this means the number of probes will go like $0, 1, 2, 3, \ldots n-1$ for the $n$ elements giving the total number of probes $\frac{n.(n-1)}{2}$ which is $O(n^2).$

$(C)$ AVL Tree construction : -

  • AVL trees are height-balanced binary search trees.
  • Balance factor of a node : height(left subtree) $-$ height(right subtree).
  • An AVL tree has balance factor calculated at every node, for every node, heights of left and right subtree can differ by no more than $1.$
  • Constructing an AVL tree, to create a tree we have to insert $n$ elements in it. To insert the element in a balanced tree we need $\log(n).$ Therefore we can do this with $O(n\log(n))$ time complexity.
  • We can traverse an AVL tree in $O(n)$ time and the inorder traversal gives the sorted list of $n$ elements. Since, sorting of $n$ elements takes minimum $\Omega(n \log n)$ time this also implies that creation of AVL tree is $\Omega (n \log n).$
  • From above two points we get the time complexity of AVL tree creation as $\Theta(n \log n)$

Reference:

$(D)$  Digital trie construction of $n$ keys with maximum length $m$ requires $O(n \times m)$ time. Among the options $m$ is not specified and assuming $m$ is constant (small length keys), the time complexity will be $O(n)$ which is also $O(n \log n).$ So, $(s)$ gets mapped to $(D)$ and $(p)$ to $(C)$ though both $(p)$ and $(s)$ are possible for $(C).$

Correct Answer: 

$A - q, B -r, C - p, D - s$

selected by

11 Comments

@Lakshman Patel RJIT how much time you have taken to write ?

 

1
1
5 to 10 minute I think, but I edit so many times because  of adding some more point.
1
1
edited by

@Lakshman Patel RJIT

 can u give the reference for D part which is digital trie construction?

See this link, it is saying different complexity

1
1

@Lakshman Patel RJIT How you can say AVL tree construction takes theta(nlogn) Since Best case for AVL tree construction should be O(n) and worst case can be O(nlogn) So It should be O(nlogn) according to me, Please correct me If I am wrong .

1
1
it will not be O(n) because AVL tree will balanced always  making its height to be logn at max every time so for finding position of 1 element it takes logn for n elements (nlogn) in best and worst case making it theta(logn)
0
0

sags.sharma if your point is true my friend then we would have gotten the fastest sort in O(n) :))

0
0

 @ Lakshman Patel RJIT

Reference:

In your  provided reference this condition is bothering me. this condition is wrong or something I am missing.Anybody help.

 

0
0
Looks like a typo!..it should be $A[r] > A[largest]$
0
0

@Lakshman Patel RJIT @Arjun Sir,

Digital trie construction of n keys with maximum length m requires O(n×m) time.

      We don’t know the value of m but also we can’t neglect the value of m also , it may happen m= n ,then the time complexity will be O($n^{2}$),  trie tree is also a kind of BST where minimum length of string will m= log n

So, we can say it will be $\Omega(n lg n)$.

https://www.geeksforgeeks.org/trie-insert-and-search/

 

For AVL tree construction for 1 key we need maximum of lg n. For n keys we need O(n lgn).

0
0

@samarpita

No, because $m$ and $n$ can be entirely different (no relationship between them). Theoretically we cannot neglect $m$ but practically we can always assume a maximum bound and consider it as a constant.

1
1

@gatecse ok sir.

0
0
1 vote
1 vote
  1. Constructing a heap will require you to use heap-ify at all elements, $n*O(1) = O(n)$
  2. If all elements of a hash table get mapped to the same slot for the $n_{th}$ element you'd have to traverse $n-1$ elements already in the slot $ n*(n-1)= O(n^2)$  
  3. AVL Tree will always take $O(n*log(n))$
  4. A Trie for string which consists of characters in $[0,9]$ will take $\Omega(n*log_{10}(n))$ and at worst $O(n^2)$ (if all strings comprise of a single character eg: 0, 00, 0000, 000000, 00000)

    Answer: a-q, c-s, b-r, d-p  
0 votes
0 votes

Heap construction -          O(n)    ( No max or min heapification only Heap construction)

Constructing hash table -  O(n)

AVL tree construction    - O(n log2 n)

Digital trie construction -  O(no of string * avg. length of string )