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.

2567 questions
18
votes
3 answers

Finding the number of paths of given length in a undirected unweighted graph

'Length' of a path is the number of edges in the path. Given a source and a destination vertex, I want to find the number of paths form the source vertex to the destination vertex of given length k. We can visit each vertex as many times as we…
2147483647
  • 1,177
  • 3
  • 13
  • 33
17
votes
3 answers

Javascript-ONLY DOM Tree Traversal - DFS and BFS?

Can anyone provide either code, pseudocode, or even provide good links to implementing DFS (Depth-first search) and BFS (breadth-first search) in plain JavaScript (No JQuery, or any helper libraries)? I've been trying to understand how to implement…
17
votes
4 answers

How do I learn Tarjan's algorithm?

I have been trying to learn Tarjan's algorithm from Wikipedia for 3 hours now, but I just can't make head or tail of it. :( http://en.wikipedia.org/wiki/Tarjan's_strongly_connected_components_algorithm#cite_note-1 Why is it a subtree of the DFS…
Nihal
  • 245
  • 1
  • 3
  • 12
15
votes
4 answers

Finding the longest cycle in a directed graph using DFS

I need to find the longest cycle in a directed graph using DFS. I once saw this Wikipedia article describing the way of doing this, and I think it approached the problem something like marking the node with one of three states: Node not yet visited,…
John Graveston
  • 151
  • 1
  • 1
  • 3
15
votes
2 answers

Find all *vertices* on all simple paths between two vertices in an undirected graph

Enumerating all simple paths between two vertices in an arbitrary graph takes exponential time in general, because there may be an exponential number of simple paths between the vertices. But what about if we're only interested in the vertices that…
15
votes
7 answers

How to implement a breadth first search to a certain depth?

I understand and can easily implement BFS. My question is, how can we make this BFS limited to a certain depth? Suppose, I just need to go 10 level deep.
user1220022
  • 11,167
  • 19
  • 41
  • 57
14
votes
3 answers

Why is Depth-First Search said to suffer from infinite loops?

I have read about DFS and BFS many times but I have this doubt lingering my mind since long. In a lot of articles it is mentioned that DFS can get stuck in infinite loops. As far as I know, this limitation can easily be removed by keeping track of…
vjain27
  • 3,514
  • 9
  • 41
  • 60
13
votes
3 answers

Implementation of BFS, DFS and Dijkstra

Is it true that the implementation of BFS, DFS and Dijkstra are almost the same, except that BFS uses queue, DFS uses stack, while Dijkstra uses min priority queue? More precisely. Can we use the following code for all of BFS, DFS, and Dijkstra,…
13
votes
2 answers

If topological sort uses DFS, how can it succeed on disconnected graphs?

There's a gap in my knowledge but I'm not sure exactly where. Topological sorting can be done using depth first search, as wikipedia explains. However I've only seen depth first search implemented for trees, where as topological sort is for…
Celeritas
  • 14,489
  • 36
  • 113
  • 194
13
votes
1 answer

Depth First Search in MySQL

I'm trying to write a MySQL PROCEDURE which takes an edge e and an edge set eset as inputs and outputs a boolean value iscyclic to determine whether the additional edge results in a cyclic graph. Would there be any more straightforward way to do…
Mike
  • 825
  • 3
  • 12
  • 30
12
votes
5 answers

Random-first search?

The two most common ways to traverse a graph are breadth-first search and depth-first search. Both of these search algorithms follow a common template: Create a worklist W, seeded with the start node s. While the worklist isn't empty: Remove the…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
12
votes
2 answers

In what cases would BFS and DFS be more efficient than A* search algorithm?

I've tested A* search against Breadth First Searching (BFS) and Depth First Searching (DFS) and I find that fewer nodes are being expanded with A*. I understand that A* expands paths that are already less expensive by using the heuristic and edge…
12
votes
1 answer

In what sense is DFS faster than BFS?

While reading about DFS vs BFS, I came across a statement that DFS is faster than BFS, and requires less memory. My implementation is in C++ for both, making a stack for DFS and queue for BFS. Can someone please explain what, and how are the speed…
12
votes
2 answers

Spanning Tree VS. Spanning Forest

What's the difference between a Spanning Tree and a Spanning Forest in graphs, conceptually. Also, is it possible to construct a Spanning Forest through DFS or BFS traversals? Why? How? I understand the Spanning Tree, but I couldn't find any clear…
12
votes
1 answer

Topological sort to find the number of paths to t

I have to develop an O(|V|+|E|) algorithm related to topological sort which, in a directed acyclic graph (DAG), determines the number of paths from each vertex of the graph to t (t is a node with out-degree 0). I have developed a modification of DFS…
Stratford
  • 325
  • 1
  • 2
  • 11