Questions tagged [depth-first-search]

Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

Formally, DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hasn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a stack for exploration.

Source.

2070 questions
385
votes
15 answers

When is it practical to use Depth-First Search (DFS) vs Breadth-First Search (BFS)?

I understand the differences between DFS and BFS, but I'm interested to know when it's more practical to use one over the other? Could anyone give any examples of how DFS would trump BFS and vice versa?
187
votes
4 answers

Breadth First Vs Depth First

When Traversing a Tree/Graph what is the difference between Breadth First and Depth first? Any coding or pseudocode examples would be great.
87
votes
9 answers

Why DFS and not BFS for finding cycle in graphs

Predominantly DFS is used to find a cycle in graphs and not BFS. Any reasons? Both can find if a node has already been visited while traversing the tree/graph.
54
votes
4 answers

Iterative DFS vs Recursive DFS and different elements order

I have written a recursive DFS algorithm to traverse a graph: void Graph::DFS(Node n) { std::cout << ReadNode(n) << " "; MarkVisited(n); NodeList adjnodes = Adjacent(n); NodeList::position pos = adjnodes.FirstPosition(); …
JohnQ
  • 953
  • 2
  • 9
  • 16
44
votes
3 answers

Iterative deepening vs depth-first search

I keep reading about iterative deepening, but I don't understand how it differs from depth-first search. I understood that depth-first search keeps going deeper and deeper. In iterative deepening you establish a value of a level, if there is no…
44
votes
4 answers

Detecting cycles in a graph using DFS: 2 different approaches and what's the difference

Note that a graph is represented as an adjacency list. I've heard of 2 approaches to find a cycle in a graph: Keep an array of boolean values to keep track of whether you visited a node before. If you run out of new nodes to go to (without hitting…
Ivan Voroshilin
  • 4,493
  • 3
  • 29
  • 56
41
votes
5 answers

Explanation of runtimes of BFS and DFS

Why are the running times of BFS and DFS O(V+E), especially when there is a node that has a directed edge to a node that can be reached from the vertex, like in this example in the following…
40
votes
15 answers

How can I remember which data structures are used by DFS and BFS?

I always mix up whether I use a stack or a queue for DFS or BFS. Can someone please provide some intuition about how to remember which algorithm uses which data structure?
captcadaver
  • 903
  • 2
  • 8
  • 7
38
votes
4 answers

Is A* the best pathfinding algorithm?

It is generally said that A* is the best algorithm to solve pathfinding problems. Is there any situation when A* is not the best algorithm to find solution? How good is A* compared to BFS, DFS, UCS, etc?
Zombie
  • 473
  • 1
  • 8
  • 11
35
votes
13 answers

How to implement depth first search for graph with a non-recursive approach

I have spent lots of time on this issue. However, I can only find solutions with non-recursive methods for a tree: Non recursive for tree, or a recursive method for the graph, Recursive for graph. And lots of tutorials (I don't provide those links…
Alston
  • 1,889
  • 5
  • 26
  • 46
33
votes
4 answers

Is Pre-Order traversal on a binary tree same as Depth First Search?

It seems to me like Pre-order traversal and DFS are same as in both the cases we traverse till the leaf node in a depth wise fashion. Could anyone please correct me if I am wrong? Thanks in advance!
26
votes
2 answers

Depth First Search and Breadth First Search Understanding

I'm making Tetris as a fun side project (not homework) and would like to implement AI so the computer can play itself. The way I've heard to do it is use BFS to search through for available places, then create an aggregate score of the most sensible…
Growler
  • 10,676
  • 21
  • 100
  • 224
26
votes
10 answers

What is breadth-first search useful for?

Usually when I've had to walk a graph, I've always used depth-first search because of the lower space complexity. I've honestly never seen a situation that calls for a breadth-first search, although my experience is pretty limited. When does it…
Jason Baker
  • 171,942
  • 122
  • 354
  • 501
25
votes
4 answers

Difference between Breadth First Search, and Iterative deepening

I understand BFS, and DFS, but for the life of me cannot figure out the difference between iterative deepening and BFS. Apparently Iterative deepening has the same memory usage as DFS, but I am unable to see how this is possible, as it just keeps…
24
votes
4 answers

deceptively simple implementation of topological sorting in python

Extracted from here we got a minimal iterative dfs routine, i call it minimal because you can hardly simplify the code further: def iterative_dfs(graph, start, path=[]): q = [start] while q: v = q.pop(0) if v not in path: …
BPL
  • 9,807
  • 7
  • 37
  • 90
1
2 3
99 100