AI Search Algorithm
BFS & DFS Implementation:
Prolog Code for DFS:
% ---------- Graph Definition ----------
edge(a, b).
edge(a, c).
edge(b, d).
edge(c, e).
edge(e, f).
% ---------- DFS Implementation ----------
dfs(Start, Goal, Path) :-
dfs_helper(Start, Goal, [Start], Path).
dfs_helper(Goal, Goal, Visited, Path) :-
reverse(Visited, Path).
dfs_helper(Current, Goal, Visited, Path) :-
edge(Current, Next),
\+ member(Next, Visited),
dfs_helper(Next, Goal, [Next|Visited], Path).
Prolog Code for BFS:
% ---------- Graph Definition ----------
edge(a, b).
edge(a, c).
edge(b, d).
edge(c, e).
edge(e, f).
% ---------- BFS Implementation ----------
bfs(Start, Goal, Path) :-
bfs_helper([[Start]], Goal, RevPath),
reverse(RevPath, Path).
bfs_helper([[Goal|Rest]|_], Goal, [Goal|Rest]).
bfs_helper([[Current|Rest]|Others], Goal, Path) :-
findall([Next,Current|Rest],
(edge(Current, Next), \+ member(Next, [Current|Rest])),
NewPaths),
append(Others, NewPaths, UpdatedQueue),
bfs_helper(UpdatedQueue, Goal, Path).
Queries Execution:
% ?- dfs(a, f, Path).
% ?- bfs(a, f, Path).