The Gateway to Computer Science Excellence

+52 votes

An algorithm performs $(\log N)^{\frac{1}{2}}$ find operations , $N$ insert operations, $(\log N)^{\frac{1}{2}}$ delete operations, and $(\log N)^{\frac{1}{2}}$ decrease-key operations on a set of data items with keys drawn from a linearly ordered set . For a delete operation, a pointer is provided to the record that must be deleted . For the decrease-key operation, a pointer is provided to the record that has its key decreased. Which one of the following data structures is the most suited for the algorithm to use, if the goal is to achieve the best total asymptotic complexity considering all the operations?

- Unsorted array
- Min - heap
- Sorted array
- Sorted doubly linked list

0

@Arjun , Can you please explain how you are saying insertion and deletion in an unsorted array takes only O(1) time? . Arrays are a static data structures unlike heap and linked list. Adding/removing a single element in an array requires complete deletion of the current array and creation of another new array which is definitely not an O(1) operation and should be O(n). Please correct me if I am wrong.

+81 votes

Best answer

$\small \begin{array}{|c|l|l|l|l|} \hline

&\mathbf{(\log N)^{\frac{1}{2}}} \text{ find} & \mathbf{N} \text{ insert} & \mathbf{(\log N)^{\frac{1}{2}}} \text{delete} & \mathbf{(\log N)^{\frac{1}{2}}} \text{decrease-key} \\ \hline

\text{Unsorted Array} & O(N (\log N)^{\frac{1}{2}}) &O(N) &O(\log N)^{\frac{1}{2}})&O(\log N)^{\frac{1}{2}})\\ \text{Min-heap} &O(N (\log N)^{\frac{1}{2}})&O(N \log N)&O(\log N)^{\frac{3}{2}})&O((\log N)^{\frac{3}{2}})\\ \text{Sorted Array} &O((\log N)^{\frac{3}{2}})&O(N^2)&O(N (\log N)^{\frac{1}{2}})&O(N (\log N)^{\frac{1}{2}})\\ \text{Sorted doubly linked-list} &O(N (\log N)^{\frac{1}{2}})&O(N^2)&O((\log N)^{\frac{1}{2}})&O(N (\log N)^{\frac{1}{2}})\\\hline \end{array}$

So, Unsorted array is the answer. The operations given can be performed in any order. So, for Min-heap we cannot do the usual BuildHeap method. Delete in unsorted array is $O(1)$ as we can just swap the deleted element with the last element in the array and delete the last element. For sorted-doubly linked-list we cannot do binary search as this would require another array to maintain the pointers to the nodes.

So, Unsorted array is the answer.

The operations given can be performed in any order. So, for Min-heap we cannot do the usual BuildHeap method.

Delete in unsorted array is $O(1)$ as we can just swap the deleted element with the last element in the array and delete the last element.

For sorted-doubly linked-list we cannot do binary search as this would require another array to maintain the pointers to the nodes.

Correct Answer: $A$

&\mathbf{(\log N)^{\frac{1}{2}}} \text{ find} & \mathbf{N} \text{ insert} & \mathbf{(\log N)^{\frac{1}{2}}} \text{delete} & \mathbf{(\log N)^{\frac{1}{2}}} \text{decrease-key} \\ \hline

\text{Unsorted Array} & O(N (\log N)^{\frac{1}{2}}) &O(N) &O(\log N)^{\frac{1}{2}})&O(\log N)^{\frac{1}{2}})\\ \text{Min-heap} &O(N (\log N)^{\frac{1}{2}})&O(N \log N)&O(\log N)^{\frac{3}{2}})&O((\log N)^{\frac{3}{2}})\\ \text{Sorted Array} &O((\log N)^{\frac{3}{2}})&O(N^2)&O(N (\log N)^{\frac{1}{2}})&O(N (\log N)^{\frac{1}{2}})\\ \text{Sorted doubly linked-list} &O(N (\log N)^{\frac{1}{2}})&O(N^2)&O((\log N)^{\frac{1}{2}})&O(N (\log N)^{\frac{1}{2}})\\\hline \end{array}$

So, Unsorted array is the answer. The operations given can be performed in any order. So, for Min-heap we cannot do the usual BuildHeap method. Delete in unsorted array is $O(1)$ as we can just swap the deleted element with the last element in the array and delete the last element. For sorted-doubly linked-list we cannot do binary search as this would require another array to maintain the pointers to the nodes.

So, Unsorted array is the answer.

The operations given can be performed in any order. So, for Min-heap we cannot do the usual BuildHeap method.

Delete in unsorted array is $O(1)$ as we can just swap the deleted element with the last element in the array and delete the last element.

For sorted-doubly linked-list we cannot do binary search as this would require another array to maintain the pointers to the nodes.

Correct Answer: $A$

0

I m geting following for

insert- N^2(logN)

decrease- o(1)(logN)N(logN)^1/2 please someone tell where i m wrong

for decrease- o(1) to decrease , O(logn) to find position where new decrease value to be inserted, It might require O(n) swap , and so for (logN)^1/2 element it will be o(1) O(n(logn)^3/2)

//

insert- N^2(logN)

decrease- o(1)(logN)N(logN)^1/2 please someone tell where i m wrong

for decrease- o(1) to decrease , O(logn) to find position where new decrease value to be inserted, It might require O(n) swap , and so for (logN)^1/2 element it will be o(1) O(n(logn)^3/2)

//

0

@Arjun Sir

@Bikram,

@srestha, I am not getting how you get this table. what is the no of elements you are considering in the data structure

If you are considering data structure contain n elements then (As per answer is Unsorted array):-

insert -> O(1);

delete -> O(n);

find -> O(n);

delete key -> -

Please explain?

@Bikram,

@srestha, I am not getting how you get this table. what is the no of elements you are considering in the data structure

If you are considering data structure contain n elements then (As per answer is Unsorted array):-

insert -> O(1);

delete -> O(n);

find -> O(n);

delete key -> -

Please explain?

0

insert is O(1), but there is N element, i.e why it will take O(N)

For delete here one pointer is provided , i.e. why delete taking O(1) time

For delete here one pointer is provided , i.e. why delete taking O(1) time

0

Some confusion in this question

Min heap decrease key , time complexity should be $N\left ( log N \right )^{\frac{5}{2}}$

Sorted array delete should be $\left ( log n \right )^{\frac{1}{2}}$

because we already know delete and insert time of every element and decrease key operation is nothing but delete and then insert operation

So, if we multiply delete and insert operation we get decrease key, rt?

Min heap decrease key , time complexity should be $N\left ( log N \right )^{\frac{5}{2}}$

Sorted array delete should be $\left ( log n \right )^{\frac{1}{2}}$

because we already know delete and insert time of every element and decrease key operation is nothing but delete and then insert operation

So, if we multiply delete and insert operation we get decrease key, rt?

0

@srestha

Min heap decrease key , time complexity should be N (log N)

^{3/2}

No, it is not.

the decrease key operation in MinHeap will take O(logN) for each element so in total it is (log N)^{3/2}

as in question it says , " (log N)^{1/2} decrease-key operations on a set of data items with keys drawn from a linearly ordered set " so log N * (log N) ^{1/2 }= (log N)^{1+1/2 } = (log N)^{3/2 }

+1

@srestha

Sorted array delete should be (logn)

^{1/2}

No, it is not the way .

Delete in sorted array done in O(n) for each element , now in question says " algorithm takes (log N)^{1/2} delete operations, "

so O(n) * (log N)^{1/2} = N (log N)^{1/2}

Hence Sorted array delete is N (log N)^{1/2}

+1

Why O( (log N)^{1/2}) for deletion in sorted linked list?? Because sorted linked list will take O(1) time to delete only if pointer is provided to the previous element where we want to delete??

I know it wont change the answer but still ....?

+1

An algorithm performs (log N)

^{1/2}find operations , N insert operations, (log N)^{1/2}delete operations,

means total number of delete operations are (log N)^{1/2}

now sorted linked list will take O(1) time to delete

total time = O(1) * (log N)^{1/2}

= O (log N)^{1/2}

0

Still problem lies

In unsorted array deletion

it has a pointer provided

So, O(1) time to delete operation. Now, there are log n^{1/2} operations

But if it delete from middle say i, then we need to swap back all element 1 position after ith element to fill the gap.

So, here n more operation required.

So, total complexity will be O(N log N^{1/2})

chk it

0

Deletion is linked list is nothing deleing node and modify pointer(address field) so why You do swapping.

You are assuming array.

You are assuming array.

0

@Arjun Sir,

Deletion in unsorted array will take O(n) time, as srestha mentioned that if element is deleted from middle then we need to do n swaps to fill the gap. Please clarify?

Deletion in unsorted array will take O(n) time, as srestha mentioned that if element is deleted from middle then we need to do n swaps to fill the gap. Please clarify?

0

@arjun sir

as we are not deleting min from min heap..we are deleting a node which is pointed by pointer..it can be internal node also..so how the complexity of deleting internal node in min heap is logn????

as we are not deleting min from min heap..we are deleting a node which is pointed by pointer..it can be internal node also..so how the complexity of deleting internal node in min heap is logn????

0

Can anyone explain why $(log n)^\frac{1}{2}$ decrease key operations of sorted array and sorted doubly linked list is $O(n(log n)^\frac{1}{2})$?

+2

After performing the decrease key operation in a sorted array you need to perform upto n shift operation to make array again sorted. Same for sorted doubly linked list .

So (logn)^{1/2 }Each take n time i.e.^{ }O(n(logn)^{1/2})

+1

@Arjun sir,

Cost of deletion in unsorted array should be $O(n)$ for single operation and for such $\sqrt{logn}$ operation it should be $n\sqrt{logn}$

Cost of deletion in unsorted array should be $O(n)$ for single operation and for such $\sqrt{logn}$ operation it should be $n\sqrt{logn}$

0

"For a delete operation, a pointer is provided to the record that must be deleted".

This is given in the question. So no need to search for the element.

0

@Arjun sir,

@Anybody

The purpose of maintaining the min heap is to get the minimum element in O(1) time. So why have you not considered the fact that we are finding the minimum element in the min heap every time and finding root logn elements would be root logn time but not n root logn.

@Anybody

The purpose of maintaining the min heap is to get the minimum element in O(1) time. So why have you not considered the fact that we are finding the minimum element in the min heap every time and finding root logn elements would be root logn time but not n root logn.

0

Time complexity of deleting an element from unsorted array would be N*N

It's because pointer to element that has to decreased or deleted is only given which mean index of that element is only given.

So swapping the element (that has to be deleted) with last element can't be performed. It's because we don't know the last index of the array.

So better to delete the element as we know only it's index and then shift the element one by one.

So, complexity come out to be O(n)

As n insert operations.

Therefore,

O(n*n)

0

This is how it will happen.

A[ 0 1 2 3 4 5 6....] **We want to delete the 5th element.**

we don't know the last index but we know the first index so we can swap with the first one and reduce the size like this.

A[1 2 3 4 5 6....]

If you observe here we have deleted the element so It so happen that we can be asked to delete the element at index 5 again and we will do this (logn)^1/2.

Now what if we are asked to delete 0th index element after we have already deleted 18000 elements??

Our array is A[18000 18001 18002 18003...] so we will just have variable that knows the starting index of our array. Say** mFirstIndexNumber = 18000 and mDeleteIndexNumber = 0**

we'll do like this.

swap(mFirstIndexNumber and mDeleteIndexNumber);

mFirstIndexNumber += 1;

**This will take constant time to delete one element so we have to do like this (logn)^1/2 **

**so O(1)*(logn)^1/2 = O(logn)^1/2.**

+31 votes

*I got the same table as Arjun sir got.*

& Everybody must know that how it derived.

But I* think for **average** mind like me it will **time-consuming** in **exam.*

I have one observation while I* read this question.*

Imp Note : I have Considered (find,insert,delete,dec-key) operations while giving ans.** **

Before and After doing operations on data structure x it is required that it should be data structure x only.Now replace x with given data structures.

So If it is a heap then after doing specified operations(find,insert,delete,dec-key) it should be heap only.

Means Heap Constraint need to be satisfied. Same phenomena applicable for other data structure.

So max time is consuming in fulfilling that constraint.

That's why Unsorted Array (where no such constraint) won the Race compare to Heap,Sorted array and Sorted doubly linked list which are truely structured.

*So Option A. unsorted array is Ans.*

PS:- 1. Plz verify my ans. And comment if u r not agree.

2. Must realize Arjun sir's Ans.

+14 votes

0

In the worst case suppose we delete the root. then copy one of the leaf node to the root and call heapify() on the root as the left subtree is min heap as well as right subtree. heapify() will take logn time.

0

since we have been provided a pointer at which we have to do delete operation .

so we go to that element and delete it

and replace it with leaf node

and apply heapify on that subtree(where deleted element position is root)

hence O(log n) time.

so we go to that element and delete it

and replace it with leaf node

and apply heapify on that subtree(where deleted element position is root)

hence O(log n) time.

- All categories
- General Aptitude 1.8k
- Engineering Mathematics 7.4k
- Digital Logic 2.9k
- Programming and DS 4.9k
- Algorithms 4.4k
- Theory of Computation 6.2k
- Compiler Design 2.1k
- Databases 4.1k
- CO and Architecture 3.4k
- Computer Networks 4.1k
- Non GATE 1.5k
- Others 1.7k
- Admissions 595
- Exam Queries 576
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 17

50,666 questions

56,154 answers

193,758 comments

93,722 users