| | |
Minimal element in a linked list?
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
![]() |
•
•
Join Date: Dec 2005
Posts: 2
Reputation:
Solved Threads: 0
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
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
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.
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.)
(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.
>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.
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.
•
•
Join Date: Dec 2007
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- Quicksorting linked list - simple algorithm (C)
- linked list..quick question (Java)
- remove method linked list (C)
- doubly linked list implementation (Java)
Other Threads in the Computer Science Forum
- Previous Thread: 2D parity error detection scheme
- Next Thread: Syntax Highlighting
| Thread Tools | Search this Thread |
ai algorithm algorithms amazon assignment assignmenthelp assignments automata battery bigbrother binary bittorrent bizarre bletchleypark blogging bomb business cern codebreaker compiler computer computers computerscience computertrackingsoftware connect conversion csc dataanalysis dataintepretation development dfa dissertation dissertationthesis dissertationtopic ebook employment energy floatingpoint foreclosure foreclosuresoftware fuel gadgets geeks givemetehcodez government graphics hardware history homeowners homeworkassignment homeworkhelp humor ibm idea ideas internet iphone ipod itcontracts jobs kindle laser laws linkbait lsmeans mainframes marketing mobileapplication msaccess nano netbeans networking news os p2p piracy piratebay principles programming rasterizer research sam-being-cute sas science sex simulation software spying stephenfry study supercomputer supercomputing sweden technology textfield turing turingtest two'scompliment virus ww2






