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:
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'
If you imagine the members of the data structure laid out as a graph, a cyclic data structure is where a member refers back to another one or the structure itself. For example: var obj = new Object(); obj.
Data structures are used for efficient data persistence, such as specifying the collection of attributes and corresponding structures used to store records in a database management system.
Data consisting of directions or times in which the measurement scale is cyclic (after 23.59 comes 00.00, after 359° comes 0°, after 31 December comes 1 January).
Lots of things. Circular buffer, for example: you have some collection of data with a front and a back, but an arbitrary number of nodes, and the "next" item from the last should take you back to the first.
Graph structures are often cyclic; acyclicity is a special case. Consider, for example, a graph containing all the cities and roads in a traveling salesman problem.
Okay, here's a particular example for you. I set up a collection of towns here in Colorado:
V=["Boulder", "Denver", "Colorado Springs", "Pueblo", "Limon"]
I then set up pairs of cities where there is a road connecting them.
E=[["Boulder", "Denver"],
["Denver", "Colorado Springs"],
["Colorado Springs", "Pueblo"],
["Denver", "Limon"],
["Colorado Springs", "Limon"]]
This has a bunch of cycles. For example, you can drive from Colorado Springs, to Limon, to Denver, and back to Colorado Springs.
If you create a data structure that contains all the cities in V and all the roads in E, that's a graph data structure. This graph would have cycles.
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.
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