Minimal element in a linked list?

Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Reply

Join Date: Dec 2005
Posts: 2
Reputation: vanhelsing is an unknown quantity at this point 
Solved Threads: 0
vanhelsing vanhelsing is offline Offline
Newbie Poster

Minimal element in a linked list?

 
0
  #1
Dec 20th, 2005
Hi,

A friend of mine was asked a question during a CS exam about finding and maintaining the minimal element in a linked list in constant time, as elements are randomly added to and deleted from the list. He could not answer it, and after he related this to me, neither could I.

Sure, when elements are being added, you can figure out which one is the minimal by keeping it in an outside variable, but if they are deleted, how do you find the next smallest if the minimal one is gone? Is there a particular weird data structure to be used?

Thanks!

(No, it's not a homework question. Just a pointer would be sufficient to satisfy our curiousity.

Kevin
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Minimal element in a linked list?

 
0
  #2
Dec 21st, 2005
Keep the list sorted at all times, then the smallest node is always the first. Finding the smallest is a constant operation for obvious reasons, and maintaining it is also a constant operation because insertion of a smaller node is constant as is deletion of the current smallest node. Otherwise insertion and deletion are linear operations, but the requirements say nothing about their time complexity. QED
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 2
Reputation: vanhelsing is an unknown quantity at this point 
Solved Threads: 0
vanhelsing vanhelsing is offline Offline
Newbie Poster

Re: Minimal element in a linked list?

 
0
  #3
Dec 21st, 2005
Well, the inserts and deletes in the general case are not really done in constant time - I don't think the answer is so easy...


Thanks anyway!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Minimal element in a linked list?

 
0
  #4
Dec 21st, 2005
Maybe they were speaking of average case efficiency, not worst case. You have 'random' insertions and deletions. On any deletion the probability that the minimal element is deleted is 1/n, assuming elements are distinct, where n is the size of the list. Then it only takes O(n) operations to scan through the list and recompute the minimal element. On average, this will be an O(1) cost per deletion.

(It would help to have more details about the question. How are elements 'added' or 'deleted' from the list? If the list is used like a stack or queue, with insertions and deletions on the ends, it is certainly possible to keep track of the minimal element in constant time.)
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Minimal element in a linked list?

 
0
  #5
Dec 22nd, 2005
>Well, the inserts and deletes in the general case are not really done in constant time
And you didn't require that they needed to be done in constant time. If you want an accurate answer then stop paraphrasing and give us the exact problem that was given on the exam. Chances are good that you're missing an important element that gives us a loophole for a trick, much like how I was assuming that maintaining the smallest node didn't include constant time insertions and deletions.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1
Reputation: s3ts is an unknown quantity at this point 
Solved Threads: 0
s3ts s3ts is offline Offline
Newbie Poster

Re: Minimal element in a linked list?

 
0
  #6
Dec 12th, 2007
So... the precondition for this linkedlist in add() should be that the first element in the list is always the minimum element in the list.

add(3)

3

add(10)

3, 10

add(1)

1, 3, 10

add(5)

1, 5, 3, 10


you see, when you add, you compare the element you're adding with the one at the beginning. if bigger, add after the first element. if smaller, add this element as the first in the list.

constant time, because you will always make one comparison and you will always add. linkedlist always has an addFirst() method of constant time.

*thumbsup*?

*EDIT*

NVM, i didn't read the question in it's entirety. it looks like it's not as easy.
Last edited by s3ts; Dec 12th, 2007 at 6:35 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Minimal element in a linked list?

 
0
  #7
Dec 12th, 2007
It's a pity you didn't read the post dates either
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC