Directed Graphs with Edges as Facts

In a directed graph, each edge has a direction. In Prolog, edges also imply directions because fact(a, b) is not the same as fact(b, a). Consider the same image as the previous one but with a tiny tweak—we’ve put an arrow on each of the lines:

images/graphs/directed.png

That means a is connected to b, but b is not connected to a. We can describe the graph like this. a is connected to b, c, and d, and b is connected to c. Directed graphs are ideal for representing roads (which may be one way), follows-style relationships in a social media network, or dependencies.

We can label the edges in the graphs with Prolog facts. Create a new directory ...

Get Programmer Passport: Prolog now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.