Understanding and Applying Lower Bounds
Suppose Dr. WhyLie comes up to you and claims that he has invented a super-fast comparison based priority queue. The speed of the priority queue operations are as follows (n is the number of items currently in the priority queue):
a. insert a new item in O(sqrt(log n)) time
b. extract (remove and return) the smallest item from the priority queue in O(sqrt(log n)) time.
Explain why Dr. WhyLie must be lying.
For a given problem, it is always wise to determine how much time it will take to perform the duty. For that we have time complexity of a problem i.e. determining the time it takes to complete the job in terms of an expression.
For a priority queue is a type of queue which has following properties:
Let us assume we are dealing with a comparison based priority queue named Q.
According to Dr. WhyLie an item can be inserted in the queue in O(sqrt(log n)) time. Now there are several implementation of a priority queue in Data structure. We can use Linked list, Binary heap etc.
We are declaring the following operations:
Algorithm:
Insert(head, item, p) // head is the header node of list, item is desired item, p is the priority of item
S1: create a new node with item and p //
S2: If priority(head)<priority(item) follow S3-S4 and go to S10. Else go to S5. O(1)
S3: new->next=head // O(1)
S4: head=new // O(1)
S5: set temp as the head of the list. // O(1)
S6: while temp->next!=NULL and temp->next->priority>p // the loop will continue unless and until it gets the proper place for item.
S7: temp=temp->next //end of loop it will run unless and until the criteria met i.e. at most n times
S8: new->next=new // O(1)
S9: temp->next=new // O(1)
S10: End
Delete(head)
S1: head=head->next // set the head of the list to the next node in list.
S2: Free the node at the head of the list
S3: End
Return(head)
S1: return head->item
S2: End
Complexity:
|
Operation |
Steps |
Linked list |
Binary heap |
|
Insert |
S2 |
O(1) |
O(1) |
|
S3 |
O(1) |
O(1) |
|
|
S4 |
O(1) |
O(1) |
|
|
S5 |
O(1) |
O(1) |
|
|
S6 |
O(n) |
O(log n) |
|
|
S8 |
O(1) |
O(1) |
|
|
S9 |
O(1) |
O(1) |
|
|
Delete |
S1 and S2 |
O(1) |
O(log n) |
|
Return |
S1 |
O(1) |
O(1) |
Now if we calculate the complexity of Insert function for a linked list, we can see that the loop will take at most O(n) times and at least take O(1) times ( if only S3 and S4 runs) because a linked list only goes linearly. If we use a binary heap then according to principal of binary heap either we search left sub-tree or right sub-tree for the right place of the item. Now for a binary tree it takes O(log n) time (we are using max heap where value of child is less or equal to that of a parent. Between two same values, first inserted value is parent and the next is left child) because every time we insert or delete any value from binary heap, we have to rearrange the tree.
Now for a priority queue we cannot lower our complexity to O(sqrt(log n)) as the data structures can not be made to do that. As per the idea of priority queue it will be impossible to do any operation in O(sqrt(log(n)) as low priority item will be placed at the last point of the queue (in linked list, last node, in binary heap, last node). As for return only takes O(1) times.
Hence, Dr. is lying.
Understanding and Applying Lower Bounds Suppose Dr. WhyLie comes up to you and claims that he...