Jump to content

  • Log In with Google      Sign In   
  • Create Account

priority_queue big o notation with vector and deque


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 19 November 2012 - 04:21 PM

What's the big o notation for push() and pop() in priority_queue with vector or deque as its underlying container.
I can't find this anywhere.
I'm guessing linear complexity at worst for push() because it has to search for right place to insert.

Edited by lride, 19 November 2012 - 04:30 PM.

An invisible text.

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13308

Like
0Likes
Like

Posted 19 November 2012 - 04:39 PM

The complexity should be logarithmic in the size of the queue, regardless of which of those underlying containers you are using (but expect a larger constant in front of it for deque). I can try to justify my answer but I don't have the time right now.

#3 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 19 November 2012 - 04:58 PM

The complexity should be logarithmic in the size of the queue, regardless of which of those underlying containers you are using

What?!!
Can someone please tell me the pro/cons and big o notation of having which particular underlying containers in a priority_queue
  • set
  • vector
  • list
  • deque

An invisible text.

#4 frob   Moderators   -  Reputation: 21150

Like
1Likes
Like

Posted 19 November 2012 - 05:31 PM

Talked in IM, it probably isn't homework.

Consider the underlying container.

A set is a tree. What is the complexity of adding and removing from the beginning and end of a tree? At worst you need to shuffle your way back up the tree to balance it.


A vector is an array. What is the complexity of adding and removing from the beginning and end of an array? At worst you need to move every single element in the array over by one space.

A list is a linked list. What is the complexity of adding and removing from the beginning and ending of a list? At worst you need to move some pointers.

A deque is specially designed for this purpose, it is a linked list of arrays. What is the complexity of adding or removing from the endpoints of a list-of-arrays? Endpoint manipulation at worst requires adding a page.
Check out my personal indie blog at bryanwagstaff.com.

#5 Cornstalks   Crossbones+   -  Reputation: 6985

Like
0Likes
Like

Posted 19 November 2012 - 07:28 PM


The complexity should be logarithmic in the size of the queue, regardless of which of those underlying containers you are using

What?!!
Can someone please tell me the pro/cons and big o notation of having which particular underlying containers in a priority_queue
  • set
  • vector
  • list
  • deque

I'm pretty sure set and list can't be used in a priority_queue, as one of the basic requirements for the underlying storage container of a priority_queue is that it must have random access iterators. On that list, only deque and vector meet that requirement. list and set have bidirectional iterators.

vector and deque allow for constant-time insertion/deletion to/from the end of the container, as well as constant-time random element access. You might be surprised about this for deque, but behind the scenes, it's not using a naive double linked list (as some might believe); rather, it's using a paging technique.

frob obviously wants you to think about it all, so I won't give answers away, but here's some simple answers (but seriously try to think about it and answer frob's questions before peaking):
Spoiler

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#6 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 19 November 2012 - 10:02 PM

Why would pop() be log n? It's just removing an element from the end. And how does priority_queue have all of its element sorted?
An invisible text.

#7 Álvaro   Crossbones+   -  Reputation: 13308

Like
0Likes
Like

Posted 19 November 2012 - 11:07 PM

Why would pop() be log n? It's just removing an element from the end. And how does priority_queue have all of its element sorted?


priority_queue implements basically a heap. After popping the element, you need to move O(log(n)) elements to fill the tree while keeping the feature that each node is less than its successors.

#8 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 20 November 2012 - 08:16 AM

priority_queue implements basically a heap


How is priority_queue implemented as a heap when I'm using a vector or a deque as its internal container?
An invisible text.

#9 Cornstalks   Crossbones+   -  Reputation: 6985

Like
0Likes
Like

Posted 20 November 2012 - 08:44 AM


priority_queue implements basically a heap


How is priority_queue implemented as a heap when I'm using a vector or a deque as its internal container?

A heap is just a way of organizing/ordering the data. vector and deque are just ways of storing (not organizing) the data.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#10 Álvaro   Crossbones+   -  Reputation: 13308

Like
2Likes
Like

Posted 20 November 2012 - 01:22 PM

In more detail, nodes in a heap can be assigned indices that are small consecutive integers, which makes it possible to use a vector or a deque for storage. Node 0 is the root, nodes 2*i+1 and 2*i+2 are the children of node i, and node (i-1)/2 is its parent. The resulting implementation of a heap is quite efficient, and that's probably how priority_queue is implemented by your library (although there are other possibilities).

You can read more about it here.

#11 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 20 November 2012 - 02:50 PM

In more detail, nodes in a heap can be assigned indices that are small consecutive integers, which makes it possible to use a vector or a deque for storage. Node 0 is the root, nodes 2*i+1 and 2*i+2 are the children of node i, and node (i-1)/2 is its parent. The resulting implementation of a heap is quite efficient, and that's probably how priority_queue is implemented by your library (although there are other possibilities).

You can read more about it here.

Thank you. That cleared up my question.
An invisible text.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS