0 votes
What is the code level difference between graph search and tree search?
in Informed Search by AlgoMeister (1.6k points)

2 Answers

0 votes

Tree does not contain cycles and where as graph can. So when we do search, we should avoid cycles in graphs so that we don't get into infinite loops. 

In Graph we will have a list of explored nodes, while in tree search we don't. When we have undirected cycled graph, these two searches produce different outputs because in Graph Search we know which node has been explored(visited) and then we don't expand it, but in Tree Search we don't know and expand it again. 

Another aspect is tree will typically have some kind of topological sorting or a property like binary search tree which makes search so fast and easy compared to graphs.

by Active (296 points)
0 votes
In tree search, the algorithm does not keep track of visited states. It explores nodes without checking whether a similar state has been encountered before.
This can lead to revisiting the same state multiple times, which might result in inefficiency and redundant computations.
Tree search is more straightforward to implement but can be less efficient in terms of time and space.

In graph search, the algorithm keeps track of visited states to avoid revisiting them. This is typically done using a set or some data structure to store the explored states.
This ensures that the algorithm does not waste time revisiting the same states, improving both time and space efficiency.
The additional bookkeeping for visited states makes the implementation slightly more complex than tree search.
by (212 points)
The Book: Analysis and Design of Algorithms | Presentations on Slideshare | Lecture Notes, etc
...