What is the expected space used by the skip list after inserting n elements?
I expect that in the worst case the space consumption may grow indefinitely.
Wikipedia says “Space O(n)”.
How can this be proven one way or another?
According to this thesis, which I find more reliable then wikipedia, wikipedia is wrong. Probabilistic Skip List is Theta(nlogn)
worst case space complexity.
Despite the fact that on the average the PSL performs reasonably well, in the worst case its Theta(n lg n) space and Theta(n) time complexity are unacceptably high
The worst case is not infinite because you can limit yourself to f(n)
number of lists, where f(n) = O(logn)
, and stop flipping coins when you reached this height. So, if you have f(n)
complete rows, you get O(nlogn)
total number of nodes, thus the space complexity in this case is O(nlogn)
, and not O(n)
.
EDIT:
If you are looking for expected space consumption, and not worst as initially was stated in the question then:
Let's denote "column" as a bottom node and all the nodes "up" from it.
E(#nodes) = Sigma(E(#nodes_for_column_i)) (i in [1,n])
The above equation is true because linearity of expected value.
E(#nodes_for_column_i) = 1 + 1/2 + ... + 1/n < 2
(for each i). This is because with probability 1 it has 1 node, with p=1/2, each of these has an extra node. with p'=1/2, each of these has an extra node (total p*p'=1/4) ,.... Thus we can derive:
E(#nodes) = n*E(#nodes_for_each_column) = n*(1+1/2+...+1/n) < 2n = O(n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With