-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneighbors.pl
30 lines (25 loc) · 1.9 KB
/
neighbors.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
%****************************************************************************************************
% *
% Find Neighbors of a Node in a Graph *
% *
% The given Prolog code defines a predicate to find all neighbors of a node in a graph. *
% The `neighbors` predicate uses the `findall` predicate to collect all neighbors by *
% checking both incoming and outgoing edges. The neighbors are then sorted and returned. *
% *
% Example usage: *
% ?- neighbors(c, Neighbors). *
% Neighbors = [a, d, e, f] *
% *
%****************************************************************************************************
% -----------------------------------------Define edges of the graph--------------------------------------------------------
edge(a, b).
edge(a, c).
edge(b, d).
edge(c, d).
edge(c, e).
edge(d, f).
edge(e, f).
% The predicate to find all neighbors of a node.
neighbors(Node, Neighbors) :-
findall(Neighbor, (edge(Node, Neighbor); edge(Neighbor, Node)), NeighborsList), % Use findall to collect all neighbors, considering both directions of edges.
sort(NeighborsList, Neighbors). % Remove duplicates and sort the list of neighbors.