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
6
votes
1 answer

Dynamic Programming on Trees with Modifications

Recently I read a problem on trees But I was finding difficulty in approaching this problem. Here is problem: There is country with n cities (2 to 10^5) and (n-1) bidirectional roads, such that It is possible to travel between any pair of cities. We…
Kautsya Kanu
  • 451
  • 1
  • 7
  • 20
6
votes
3 answers

Why are you guaranteed to find your result if it is in the graph with BFS but not with DFS?

I've read somewhere that DFS is not gaurenteed to find a solution while BFS is.. why? I don't really get how this is true.. could someone demonstrate a case for me that proves this?
Skizit
  • 43,506
  • 91
  • 209
  • 269
6
votes
2 answers

DFS on disconnected Graphs

How does DFS(G,v) behaves for disconnected graphs ? Suppose a graph has 3 connected components and DFS is applied on one of these 3 Connected components, then do we visit every component or just the on whose vertex DFS is applied. Means Is it…
mcjoshi
  • 299
  • 1
  • 4
  • 11
6
votes
2 answers

Finding all possible paths in graph

I'm looking for some algorithm which will help me find all possible paths in a graph. Everything I found so far is not fully satisfying. Let's imagine we have a graph (tree) like this one: And let's use some algorithm like Breadth-First Search or…
6
votes
1 answer

Knight's tour algorithm with adjacency-lists

I'm trying to solve the Knight's tour problem in Java. My goal is to calculate all possible tours of a horse on a chessboard with any dimension. What I have tried to use is an adjadency-lists data structure. The problem now, is that I know which…
ShinyForce
  • 61
  • 2
6
votes
1 answer

How to Traverse an NLTK Tree object?

Given a bracketed parse, I could convert it into a Tree object in NLTK as such: >>> from nltk.tree import Tree >>> s = '(ROOT (S (NP (NNP Europe)) (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends)))) (. .)))' >>>…
alvas
  • 115,346
  • 109
  • 446
  • 738
6
votes
1 answer

Depth First Search on 2-D array

I am trying to learn DFS by creating a program that navigates my ogre through a maze (2d array).This is similar to a dailyprogramming challenge, but I am doing it with just a 1x1 ogre. My maze: static int[][] maze = {…
6
votes
4 answers

C++ pass by reference

I've recently (4 days) started to learn C++ coming from C / Java background. In order to learn a new language I ussualy start by re-implementing different classical algorithms, as language specific as I can. I've come to this code, its a DFS - Depth…
Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
6
votes
2 answers

DFS Recursive vs DFS Iterative

I'm trying to understand the difference between DFS Recursive and DFS iterative. Does the one with the stack use an iterative or recursive approach? For example, what would be the output of using a DFS recursive traversal of the graph and a DFS…
BostonMan
  • 161
  • 1
  • 1
  • 5
6
votes
1 answer

How to use DFS on an array

I have a 1 dimensional list of values, it looks like this "int[] values'". I beleive I have converted it to a 2d list like this : for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { board[i][j] = values[i * 4 + j]; } } The…
user2835532
  • 113
  • 1
  • 1
  • 10
6
votes
3 answers

Why do we need to run DFS on the complement of a graph in the Kosaraju's algorithm?

There's a famous algorithm to find the strongly connected components called Kosaraju's algorithm, which uses two DFS's to solve this problem, and runs in θ(|V| + |E|) time. First we use DFS on complement of the graph (GR) to compute reverse…
Atinesh
  • 1,790
  • 9
  • 36
  • 57
6
votes
1 answer

DFS vs BFS in web crawler design

I come up with an interview question that I would like to know your opinion about that. The questions are say that in designing a web crawler: 1) what kind of pages will you hit with a DFS versus BFS? 2) how would you avoid getting into infinite…
Nazgol
  • 171
  • 2
  • 8
6
votes
4 answers

Which Procedure we can use for Maze exploration BFS or DFS

I know we can use DFS for maze exploration. But I think we can also use BFS for maze exploration. I'm little bit confused here because most of the books and articles that I've read had used DFS for this problem. What I think is that the Best Case…
6
votes
1 answer

How to use the DepthFirstSearchIterator class to run a depth first search on a graph using JGraphT

I am experimenting with JGraphT and have hit a brick wall trying to implement a depth first search using the JGraphT API. I have created a simple graph with nodes and vertices's as follows: DirectedGraph graph = new …
kazuwal
  • 1,071
  • 17
  • 25
6
votes
2 answers

Implementing a tree for a maze to use in DFS, BFS

My program is taking a char array as input from a file. The array looks like this: "#########", "# # #", "# ## # #", "# # #", "### # ###", "# # # #", "# # #####", "# # #", "#########", I am implementing DFS and BFS to solve this maze…
Powerbyte
  • 159
  • 9