Graph traversal help

So I have a graph that I want to traverse, but I need prolog to tell me how to traverse it. For example, see the following graph:

Some nodes are letters, some are colors. The graph is highly traversible. I don’t have an issue of whether I can do stuff (although that may be a niche consideration given possible dead ends, like the bottom left D), but rather I want to know by what rules I can traverse something.

For a more concrete example, say I start at the top left H. If I go straight on red and straight on blue, I can go H-I-D. Hid. If I start at the bottom left D, go right on red, left on blue, and straight on green, I can go D-A-R-T, dart.

I can input the nodes and edges, I can use a data structure like node(X, Y, Color, Letter) to keep track of that stuff. But how do I set it so that Red, Blue, and Green can be left or right or straight, and have Prolog return paths with a consistent behavior based on where I want to go?

Your data structure can be e.g.:

edge(letter('H'), right, color(green, right_of('H'))).

Nodes need to be unique ultimately, so I’m using right_of('H') to identify a uniquely-identifiable node of color green.

1 Like