retagged by
34,220 views
97 votes
97 votes

$N$ items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided to the record to be deleted. For a decrease-key operation, a pointer is provided to the record on which the operation is to be performed.

An algorithm performs the following operations on the list in this order: $\Theta \left(N\right)$ delete, $O (\log N)$ insert, $O(\log N)$ find, and $\Theta(N)$ decrease-key. What is the time complexity of all these operations put together?

  1. $O(\log^{2} N)$
  2. $O(N)$
  3. $O(N^{2})$
  4. $\Theta\left(N^{2}\log N\right)$
retagged by

6 Answers

Best answer
149 votes
149 votes

Answer is (C).

  • Delete $O(1)$
  • Insert $O(N)$
  • Find $O(N)$

Decrease Key $\Rightarrow O(N)$ //Because we need to search position in Linked list. (It is similar to a Delete followed by an Insert with the decreased value)

  • $O(n)$ delete $\Rightarrow O(N \times 1 ) =O(N)$
  • $O(\log N)$ find $\Rightarrow O(\log N \times N ) = O(N \log N)$
  • $O(\log N)$ insert $\Rightarrow O(N \log N)$
  • $O(N)$ decrease key $\Rightarrow O(N\times N) = O(N^2)$

Even though it says at start we got $N$ elements, then we are deleting $Q(N)$ elements, here $Q(N)$ can be anything like $N/2,N/4,N/3$ and list need not be empty, then above explanation holds !

In case it actually deleted all elements at start analysis will be something like below $\Rightarrow$

All $N$ elements are deleted, Time complexity $O(1)$ each delete, total delete $O(N)$

Now $\log N$ insert, it'll take $1 + 2  + \log N$ time, then ($\log N * \log N-1)/2 \Rightarrow O((\log N)^2)$

Now $\log N$ finds $\Rightarrow$ it'll take $\log N$ time per find (because find take $O(N)$ but here $N = \log N$)
 $\Rightarrow O((\log N)^2)$

Now $N$ decrease key, Size of list is $\log N$, each decrease key can take $O(size \ of \ list)$

So, size of list * no. of decrease key $\Rightarrow N \times \log N = O(N \log N)$

There is no option like $O(N \log N)$

So, correct answer is $O(N^2)$.

edited by
73 votes
73 votes

Delete - θ(1) time as pointer is directly given
Insert - O(N) time,we may need to insert at the end of the sorted list..
Find - θ(N) time.. list we need to search sequentially..
Decrease key - θ(N) time, pointer is directly given, delete then insert

Now using above..
θ(N) * θ(1) + O(logN) * O(N) + O(logN) * θ(N) + θ(N) * θ(N)
using property of asymptotic notation, and removing lower terms, we get O(N2)..
So Answer O(N2)     Option (C)

edited by
31 votes
31 votes
@as given in question

1. pointer is given to the node which has to be deleted so it takes O(1) time.

2. pointer to the node is given which has to be decreased so it takes O(1) time.

@things not given in the question we have to know them

3. findning a record takes O(N) time in avg case.

4. inserting a record takes O(N) time in avg case.

@now we have to perform the following  according to the question

1. O(N) delete

2. O(log N) insert

3. O(log N) find

4. O(N) decrease

so,

1. delete takes O(1)*O(N)=O(N)

2. insert takes O(log N)*O(N)=O(Nlog N)

3. find takes O(log N)*O(N)=O(NlogN)

4. decrease takes O(N)*O(1)=O(N)

so required ans will be [O(N)+O(NlogN)+O(NlogN)+O(N)] = O(NlogN)..

but no option matches so we take upper bound which happens to be O(N^2).

NOTE: if one of the option given is O(NlogN) then it should be the answer...
Answer:

Related questions

168 votes
168 votes
17 answers
4
Akash Kanase asked Feb 12, 2016
49,805 views
The number of ways in which the numbers $1, 2, 3, 4, 5, 6, 7$ can be inserted in an empty binary search tree, such that the resulting tree has height $6$, is _________.No...