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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prolog DFS method implementation for a tree

Status
Not open for further replies.

VinnieCFC

Technical User
Jan 12, 2015
1
CZ
i tried to write a prolog code for Depth First Search (DFS). Program has given a certain state space on input, which is rated tree and i need to realize browsing of this state space with DFS method.

My code is:

dfs(state(NAME,TRANSITIONS)) :- write(NAME), write('\n'), expand(TRANSITIONS).
expand([]).
expand([H]) :- dfs(H).
expand([H|T]) :- dfs(H), expand(T).

And my defination for goal is:

dfs(state('A',[state('AA',[state('AAA',[])]),state('AB',[])])).

Answer of prolog is:

A
AA
AAA
AB
true.

I dont know how to define certain space. Do you have any advice ?

Thank you and ha ve a nice day Vinnie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top