Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DFS vs BFS search in prolog

Status
Not open for further replies.

E.saadallah

Technical User
Dec 2, 2017
1
SD
hello everybody i need your help i need a source code for dfs and bfs by prolog i tried many code but i didnt get how the querie should be done so hope if you can help as soon as possible.

DFS:-
dfs_search_node(Tree,Node, N) :-
dfs_search_node_(Tree,Node, 1, _, N).


dfs_search_node_(tree(Node, _, _), Node, FN, _, FN) :- !.

dfs_search_node_(tree(_, L, R), Node, CN, _TN, FN) :-
CN1 is CN + 1,
dfs_search_node_(L, Node, CN1, TN1, FN),
( CN1 \== FN
-> dfs_search_node_(R, Node, TN1, _TN, FN)
; true).

dfs_search_node_(nil, _Node, CN, TN, _FN) :-
TN is CN + 1.

BFS:-
bfs_search_node(Tree,Node, N) :-
bfs_search_node_([Tree],Node, 1, N).

bfs_search_node_([tree(Node, _, _) | _T], Node, FN, FN) :- !.

bfs_search_node_([tree(_, L, R) | T], Node, CN, FN) :-
CN1 is CN + 1,
append(T, [L, R], T1),
bfs_search_node_(T1, Node, CN1, FN).

actualy i found above code in a reply of post in this site and i dont know how query should be done .. thanx alot

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top