Bidirectional Graphs
In a bidirectional graph, also called a graph, all of the edges flow in both directions. Social networks like LinkedIn and Facebook are graphs. So are highway maps where roads go both ways. Let’s express one in code. Open up a new database called edges and key this much in.
| edge(a, b). |
| edge(a, c). |
| edge(b, c). |
| edge(c, f). |
| edge(d, e). |
| |
| connected(A, B) :- |
| edge(A, B); edge(B, A). |
Except for the final rule, this graph should look familiar to you. It has the same edges as the previous graph. Since we have the connected rule letting edges flow in both directions, our graph shows no arrows:
We can use the same techniques to get the neighbors for a node, but we’d use the connected rule ...
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.