1

In a circular linked list, if an elements needs to be inserted at front [just before the node pointed by head], can be done in O(1) (see the answer here)

But in a book currently, I have, it is mentioned that it is done in O(n) (the usual method). I also saw few lecture ppts, they all mention the usual method of traversing the list & adding an element.

My question is :

  1. In practical scenarios which method is used ?

  2. I am about to attend an exam, which consists of MCQs, if above question is asked shall I mark O(n), since that is the standard answer ?

avi
  • 1,463
  • 4
  • 24
  • 39
  • 1
    Did you read the accepted answer in the link you gave? – Pål GD Mar 23 '13 at 10:14
  • I am about to attend an exam, which consists of MCQs, if above question is asked shall I mark O(n), since that is the standard answer ? Ask the lecturer, or choose O(1) and send an email to him after exam. – nhahtdh Mar 23 '13 at 10:26
  • @Pål GD - Yes, I did. Why ? – avi Mar 23 '13 at 10:37
  • @nhahtdh - Okay. Thank you for replying. – avi Mar 23 '13 at 10:39
  • If you have a pointer to the tail and to the head, you can insert in front of head by letting the tail point to the new element and the new element point to head. Then you let the new element be the new head. If you do not keep a pointer to the tail element, then you are not able to do this, but I cannot see why you would not sacrifice a one bit memory allocation for a linear time improvement. What does the book say on this? – Pål GD Mar 23 '13 at 12:57
  • @PålGD - in my book it does not uses two pointers, only one head pointer. Actually I use CLRS for Data Structures, but it does not very detailed regarding this. So I read another book [it's actually kind of non-standard]. Then I read ppts of other lectures.

    But even with only one pointer it can be achieved. You insert a new node next to head, copy the value of earlier node which is pointed by head to this new node created & then you insert a value to the node pointed by head.

    – avi Mar 23 '13 at 14:03
  • @PålGD - I may not be clear with my explanation, please refer to this video [sometime after 32 mins]

    http://www.youtube.com/watch?feature=player_detailpage&v=PGWZUgzDMYI#t=1922s

    – avi Mar 23 '13 at 14:04
  • 1
    Since $O(1) \subseteq O(n)$, just check the largest $O$-class they give. (This ill-posed question illustrates why MCQs are meaningless. Beware if they refer to a particular implementation, though.) – Raphael Mar 23 '13 at 15:59
  • @Raphael - Thank you for replying. Yup, since Big-Oh gives the upper bound, even $O(n)$ is correct.

    And also agree about MCQs. Have you heard of GATE exam ? It is a post graduate entrance exam in India. The questions usually ask for complexity of a algorithm & they use a Big-Oh in questions. Let's say for example, if they ask question, what is the worst-case complexity of Quick Sort. The options will be having $O(n^2lgn)$, $O(n^2)$ and $O(n^3)$. They use Big-Oh notation only & if you chose any option other than $O(n^2)$, it will be considered as wrong & $.30$ marks will be deducted.

    – avi Mar 23 '13 at 16:37
  • So I cannot actually chose the largest $O-$class they give :( – avi Mar 23 '13 at 16:40
  • 1
    @avi: That's a pity. Can you write to them? Mind, though, that you can phrase such a question properly, e.g. "Which is the tightest bound on the runtime of the given algorithm?". – Raphael Mar 24 '13 at 15:04
  • @Raphael - I have written several mails & no one replies ever. Yes, questions can be phrased correctly, but they never ask like that. Currently I have previous question papers from last 22 years & most of the questions are like, what is the complexity of so & so algorithm. They don't mention tight bound & moreover all the options will be having Big-Oh only. But most recently, in 2013 they have asked tighter bond. I know this professor who has PhD in Algorithms & he coaches for students for this exam. When I asked him he said its correct & I shouldn't argue otherwise. I had to keep quiet :( – avi Mar 24 '13 at 16:59

1 Answers1

0

The method used in practical scenarios depends on the scenario (and on the programmer). There are several possible issues influencing the choice of implementation:

  1. Whether the algorithm is known to the programmer.
  2. Ease of coding (it's easiest if it's already implemented in some library you can use).
  3. Speed - that depends on how the data structure is used.
  4. Space overhead taken by the data structure.

An intelligent programmer should take all of these into account, and try to make themselves more knowledgeable of various algorithms and data structures.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503