ActionScript 3.0 Skip Lists 1: The Quickest Route Home
Living in Bloomfield, Connecticut, I’m about halfway between Boston and New York City. Lately they’ve been talking about building a high speed rail to Hartford and on to Springfield, Massachusetts. Naturally, when thinking about such a rail system, I like to think that the really fast part of the trip would be between New York City, Hartford, and Boston. Of course they’d need an express to stop off in New Haven and Springfield. Further, lots of other towns, including Bloomfield, would need some kind of local line. Left to my own devices, I would build the following lines:
Bullet
- New York
- Hartford
- Boston
Express
- New York
- New Haven
- Hartford
- Springfield
- Boston
Local
- New York
- Stamford
- New Haven
- Middletown
- Hartford
- Bloomfield
- Springfield
- Boston
Given these choices, I know that I can easily get to Bloomfield by taking the Local at Grand Central Terminal in New York City. However, I’d really like to skip stops in Stamford, New Haven and Middletown. If I took the express, I’d have a stop in New Haven and then on to Hartford where I could get on the Local to Bloomfield. That would be faster, but it would not be the fastest route.
To determine the fastest route, assuming that I have to go in the same direction and cannot backtrack, I diagramed the three lines in Figure 1.

Figure 1: Three Lines between New York and Boston
Looking at Figure 1, it is not too difficult to determine the quickest route between NYC and Bloomfield.
The route that skips the most stops is the quickest route.
Trace your finger from NYC to Bloomfield in Figure 1 and you can determine that route. To see if you found it, click below to continue and look at Figure 2 to see the fastest route highlighted in yellow.

Figure 2: The Route that Skips the Most Stops
If you traced the route highlighted in yellow in Figure 2; then you understand how skip lists work. In a similar way, the Binary Search discussed in posts here and here does not start at the beginning and iterate through an entire ordered list. Rather it splits the list determining the quickest way to the search item.
From Train Lines to Linked Lists
To go to the next step, you need to understand Linked Lists which we discussed in a recent post. What I’d like to do is to transform our train lines into a linked list. The first step is to change the different towns on the lines to nodes in a linked list. Figure 3 shows the first step in this transformation:

Figure 3: From Train Stops to Nodes in a Linked List
My hunch is that if I asked you what was the quickest route to find the value “30″ you’d know just what to do. However, the diagram still looks like a train dispatcher’s chart, and so we’ll update it a bit to look more like a linked list. Figure 4 shows this new diagram.

Figure 4: Linked list with multiple pointers
The arrows now have balls on their tails and look like pointers—and we know that pointers hold a reference to the next node. If we have multiple pointers with some of our nodes, we can have a single linked list that includes pointers that skip certain nodes. Figure 5 shows how that might look in the first node:

Figure 5: Node with multiple pointers
In code, we can imagine the first node having the following structure:
1 2 3 4 | nodeOne.contents=7; nodeOne.nextUp=nodeTwo; nodeOne.skip1=nodeFive; nodeOne.skip2=nodeThree; |
Now we’re ready to deal with coding skip lists in ActionScript 3.0. We’ll have to work out the algorithms, and in the next post on skip lists we should be able to conclude this series with a working example of skip list searches.
Related posts:

Bill Sanders
Would be interesting to see some numbers. Or classes to get those numbers ourselves depending on the projects.
It does seem that it can overthrow index based binary searches in search speed but from other side it seems to me that cost of constructing and filling it may be big so it is limited to cases when data is updated on rare occasions. I may be wrong though.
Hi Wonderwhyer Dude,
There seems to be plenty of articles with time comparisons—lots of papers at ACM meetings. However, I agree with you that you’ve got to consider the cost of constructing and updating them. (The sine qua non of design patterns!) The idea behind linked list is that you can “easily” insert and take out elements (nodes) compared with arrays.
In the next post on this topic, I found creating a perfect skip list to be relatively easy, but iterating through this simplest of all skip lists was anything but easy. Setting up a skip list is like setting up a toboggan run. You optimize it for search speed not merely for storing data like an array. Also, you can find lots of algorithms for inserting and deleting nodes, and so I think that once you have that worked out, they should have the search speed and organizational elements not found in arrays.
Kindest regards,
Bill
Hmm looked around and found this link that mention skip list theoretical performance
http://igoro.com/archive/skip-lists-are-fascinating/
and it mentioned in here with alternatives
http://en.wikipedia.org/wiki/Associative_array#Efficient_representations
So we have balancing trees, hash based stuff and linked list. Would be interesting to have some simple implementations to test. Seems to me skip list can indeed be a very interesting data structure for indexing and searching purpose. Should be more simple then trees and more predictable and versatile then hash based structures.
Thanks for sharing.
Hi,
I’m working on an implementation now. It wasn’t hard to make a perfect skip list (search on key work perfect skip list), but working up a search program is. The early attempts have been partially successful.
For the most part, I’m not too interested in the performance of a skip list, but I just like the idea. In fact, most implemented algorithms are nowhere near as interesting or difficult as design patterns. However, every now and then someone suggests an algorithm for a participant in a design pattern, and that’s what happened here. Why not have a go at creating your own skip list and search routine in ActionScript 3.0. It’s super interesting and not easy.
Kindest regards,
Bill
Well I am more on practical use side. I do such things when I need to gain performance in some bottleneck place and would like to use best structures and algorithms available. Often when need something like this(random number generators, sorting etc) just go and find/code implementations of at least 3 popular algorithms for comparison.
Hmm but there is one not so critical task in future which may put this to a good use but I am not shore. It seems to me it is possible to make a range searches with it. I mean say outputting all names that start from Al. It seems it can be used for that with some modifications.
Hi WWer Dude,
I gotta admit that I like the applied conceptual better than the practical because it’s more interesting and pays off in the long run. For me, the practical is work! However, with constant practice and play, the practical tasks get easier and easier. But they’re still work!
Probably the biggest payoff that I can see would be in game development where you have a lookup table that is in constant use and needs to be changed by adding and removing nodes. For a database, it does seem fairly awkward and since data rolls out of tables in arrays, it probably wouldn’t be worth it to have a skip list to catch the data and then find what you need.
Anyway, whatever use comes up, it’s fun to have a tool that can handle it.
Cheers,
Bill