Alex Rivera | Logout

What is a cyclic data structure good for?

Asked 2009-01-01T22:06:09.903
14

I was just reading through "Learning Python" by Mark Lutz and came across this code sample:


>>> L = ['grail']
>>> L.append(L)
>>> L
['grail', [...]]

It was identified as a cyclic data structure.

So I was wondering, and here is my question:

What is a 'cyclic data structure' used for in real life programming?

There seems to be a little confusion, which i think stems from the very brief code sample... here's a few more lines using the same object L


>>> L[0]
'grail'
>>> L[1][0]
'grail'
>>> L[1][1][0]
'grail'

Edit
Report

3 Answers

6

I recently created a cyclic data structure to represent the eight cardinal and ordinal directions. Its useful for each direction to know its neighbors. For instance, Direction.North knows that Direction.NorthEast and Direction.NorthWest are its neighbors.

This is cyclic because each neighor knows its neighbors until it goes full swing around (the "->" represents clockwise):

North -> NorthEast -> East -> SouthEast -> South -> SouthWest -> West -> NorthWest -> North -> ...

Notice we came back to North.

That allows me to do stuff like this (in C#):

public class Direction
{
    ...
    public IEnumerable<Direction> WithTwoNeighbors
    {
        get {
           yield return this;
           yield return this.CounterClockwise;
           yield return this.Clockwise;
        }
    }
}
...
public void TryToMove (Direction dir)
{
    dir = dir.WithTwoNeighbors.Where (d => CanMove (d)).First ()
    Move (dir);
}

This turned out to be quite handy and made a lot of things much less complicated.

answered 2009-01-01T22:21:42.600
1

L just contains a reference to itself as one of it's elements. Nothing really special about this.

There are some obvious uses of cyclical structures where the last element knows about the first element. But this functionality is already covered by regular python lists.

You can get the last element of L by using [-1]. You can use python lists as queues with append() and pop(). You can split python lists. Which are the regular uses of a cyclical data structure.

>>> L = ['foo', 'bar']
>>> L.append(L)
>>> L
['foo', 'bar', [...]]
>>> L[0]
'foo'
>>> L[1]
'bar'
>>> L[2]
['foo', 'bar', [...]]
>>> L[2].append('baz')
>>> L
['foo', 'bar', [...], 'baz']
>>> L[2]
['foo', 'bar', [...], 'baz']
>>> L[2].pop()
'baz'
>>> L
['foo', 'bar', [...]]
>>> L[2]
['foo', 'bar', [...]]
answered 2009-05-27T22:38:53.210
0

Suppose you have limited storage, and data constantly accumulates. In many real life cases, you don't mind getting rid of old data, but you don't want to move data. You can use a cyclic vector; implemented using a vector v of size N with two special indices: begin and end, which initiate on 0.

Insertion of "new" data now goes like this:

v[end] = a;
end = (end+1) % N;
if (begin == end)
  begin = (begin+1) % N;

You can insert "old" data and erase "old" or "new" data in a similar way. Scanning the vector goes like this

for (i=begin; i != end; i = (i+1) % N) {
 // do stuff
}
answered 2009-01-01T22:37:49.637

Your Answer