Cyclic graphs are graphs that contain at least one cycle. This means that as you traverse the graph, somewhere in the graph, it is possible to leave a node and return to that same node through the cycle.
Consider the following sequence of events for learning how to ride a bike:
- You get on the bike
- You begin pedaling the bike
- You fall down
- You get back on the bike
The cycle of getting on the bike, pedaling, and falling off will happen until you have successfully mastered riding a bicycle. There is no restraint on how many times this could take, and happens a lot when you’re learning!
Let’s create the cyclic graph above using Swift.
Instructions
Create a node for the following events: “Get on Bike”, “Pedal Bike”, “Fall off Bike”,
Name these nodes getOn
, pedal
, and fall
respectively.
Create a constant graph
of type Graph
with the nodes above.
Lastly, we’ll combine the nodes using addEdge(from:to:)
for each pair of nodes:
"Get on Bike"
->"Pedal"
"Pedal"
->"Fall"
"Fall"
->"Get on Bike"
Print the graph using the printGraph()
function.