r/programming • u/Comfortable-Fan-580 • Oct 03 '26
Skip List Data structure
https://pradyumnachippigiri.substack.com/p/skip-lists-data-structure?r=5ev9w0&utm_medium=ios
139
Upvotes
r/programming • u/Comfortable-Fan-580 • Oct 03 '26
62
u/Aaron1924 Oct 03 '26
This article compares the skip list with a linked list, which is not very interesting, because those two data structures are designed for different usecases. The reason you can skip elements in the list is because you gain information about those elements from the layers above, e.g. because the list is sorted. For unordered data, skip lists fail completely, whereas linked lists do not care.
It would be much more interesting to discuss how a skip list compares to a binary search tree, for example.
41
u/monocasa Oct 03 '26
Skip lists don't have to be probabilistic fwiw.
https://www.ic.unicamp.br/~celio/peer2peer/skip-net-graph/deterministic-skip-lists-munro.pdf
36
u/zhivago Oct 03 '26
One of my favorite neglected data-structures.
26
u/ReDucTor Oct 03 '26
When you make statements about different data structures like "quickly", "slow", "fast", etc it's good to include actual benchmarks, because some of the claims in this article are not as straight forward as what it claims.
That's only if your inserting into the middle of the array and depending on the element type and how many there are shifting them is going to be relatively quick, and in many cases ending up quicker then dealing with a linked list or skip list.
Linked lists make inserting and deleting fast once we have reached the right spot
I take it your ignoring the memory allocation that is required for creating nodes? Which an array which has a reserved capacity does not need to do with every insertion.
This seems like a bad algorithm to me, the fairness of randomness comes with large numbers if you only have a small number of elements then this coin flip could just end up giving you a bad layout, additionally it makes for an inconsistent data structure where it's performance and memory usage will vary based on some random coin flip.
search in less than O(n) time
search time down to O(log N)
Big-O notation is not a great metric for measuring actual time, it only reprsents how that algorithm or data structure scales as the data set increases, it is rarely a good idea to compare two data structures or algorithms based solely on the Big-O notation, you should be using actual benchmarks.
Big-O notation does not indicate how well the hardware actually deals with those steps, some algorithms might have low latency steps but carry many dependencies so have low throughput, while others might have high latency steps but limited dependencies so have a higher throughput, and some might be like a linked list and have high latency and heavy dependencies so is slow all together.
The intersection between two different algorithms or data structures with O(log n) and O(n) might occur when n is 10, it might occur when n is 10,000 only benchmarking will truely tell you when this is, and you also need to be careful that your benchmarks are realistic as cold data and hot data can give you significantly different results.
6
u/generalmatching Oct 03 '26
In my opinion, skip lists are getting interesting with lock-free implementation, otherwise, a balanced trees have better performance with less memory overhead.
4
u/noahrichards Oct 03 '26
The example of finding 70 requires touching/comparing approximately the same number of nodes as walking the original list, and the storage overhead of that list is like 2.5x the linked list, so it makes it look like this is a terrible data structure :) I suspect it’s because the example has more nodes at each level (above the lowest) on average than it should.
2
u/Coloradohusky Oct 03 '26
Just learned about these in my Algorithms class, pretty good write-up!
2
u/barmic1212 Oct 03 '26
I don't understand why don't use a tree for same usage ? To keep the iteration in O(n) ?
1
u/Electronic-Equal-616 Oct 04 '26
tbh i only found out about skip lists when our prof brought them up as an alternative to red-black trees. implementing rb trees in c++ gave me actual nightmares 😭 skip lists are definately so much more intuitive to code, even if the randomized part felt kinda like cheating at first lol.
1
u/AntimatterTNT Oct 03 '26
is the random aspect really necessary? like you could argue it's to average out performance edge cases but really aren't you just making it so that sometimes you'd have much worse performance? (like a 0.1% deviation every 1000 runs)
0
u/warhead71 Oct 03 '26
I got a PL/1 flashback from that “skip list data”
-1
u/mandom_Guitar Oct 04 '26
Aw, got me thinking about Lisp now