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
7
votes
2 answers

DFS implementation in Haskell

I've been banging my head over the wall for a few hours as I can't figure out how to write DFS in Haskell.. My graph is implemented as an adjacency list where the keys (or node name of the graph) are list indices: 0 -> 1 1 -> 0, 2 2 -> 1 As a…
Coding District
  • 11,901
  • 4
  • 26
  • 30
7
votes
7 answers

How can I calculate the level of a node in a perfect binary tree from its depth-first order index?

I have a perfect binary tree, i.e. each node in the tree is either a leaf node, or has two children, and all leaf nodes are on the same level. Each node has an index in depth-first order. (E.g. in a tree with 3 levels the root node has index 0, the…
hrehfeld
  • 470
  • 1
  • 4
  • 14
7
votes
1 answer

Is Topological Sorting trying to sort vertices or edges?

Happy easters, everyone. I am currently learning topological sort and having a question about what topological sort tries to really sort. The Algorithm Design Manual describes topological sort in this way: Topological sorting is the most…
6
votes
2 answers

Javascript Tree Traversal Algorithm

I need help traversing a tree structure in a depth first fashion. I can't come up with an algorithm to do it properly. My input is this: [ ["A", "B", "C"], ["1", "2"], ["a", "b", "c", "d"] ] The output should take the form: [ …
Adam
  • 12,236
  • 9
  • 39
  • 44
6
votes
1 answer

Peg solitaire – checking pegs vs. checking holes in a depth-first search

I am trying to solve Peg Solitaire with a depth-first search algorithm – it should be possible to solve the game since "modern computers can easily examine all game positions in a reasonable time". Even after 23 hours, the algorithm didn't find any…
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
6
votes
3 answers

Find all possible paths from one vertex in a directed cyclic graph in Erlang

I would like to implement a function which finds all possible paths to all possible vertices from a source vertex V in a directed cyclic graph G. The performance doesn't matter now, I just would like to understand the algorithm. I have read the…
skanatek
  • 5,133
  • 3
  • 47
  • 75
6
votes
1 answer

How to understand this priority queue depth-first search?

The following is pseudocode implement depth-first search(DFS) with one stack and a large table to mark visited nodes: DFS(N0): StackInit(S) S.push((N0, null)) if isGoal(N0) then do return true markVisited(N0) S.push((null, N0)) …
NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
6
votes
1 answer

Python get all paths from graph

I'm trying to find the paths that a user could take through a website. I have represented my graph using this format: graph = { 0 : [1, 2], 1 : [3, 6, 0], 2 : [4, 5, 0], 3 : [1], 4 : [6, 2], 5 : [6,…
FallingInForward
  • 285
  • 2
  • 4
  • 12
6
votes
1 answer

Depth-first-search maze generation algorithm with blocks instead of walls

I am trying to implement the depth first search algorithm into my game. I have been studying this web page: http://www.mazeworks.com/mazegen/mazetut/index.htm , only to find that I wouldn't be able to use it with blocks instead of Walls. What I mean…
Xedfire
  • 157
  • 1
  • 2
  • 8
6
votes
2 answers

how to solve algorithm problems in both dfs and dp

Many algorithm problem can be solved through both DFS and Dynamic Programming. Is there any direct or indirect connections between these two algorithms? Or if i thought up the subproblem of dp, how can i convert it into the recursive function in…
Hongli Bu
  • 143
  • 2
  • 8
6
votes
1 answer

Find the shortest path to reach a given destination cell in 2D matrix with obstacles

I am working on below interview question and I am confuse on how to use BFS search here. I am confuse on how to deal with the blockades here? Given a MxN matrix find the shortest path to reach a given destination cell. The robot can move up,…
john
  • 11,311
  • 40
  • 131
  • 251
6
votes
2 answers

What does it mean to expand a node?

I'm trying to understand the algorithm for a Depth-Limited-Search on wikipedia, and I'm trying to figure out what exactly it means to expand a node. I attempted to search for an answer but all I got was more algorithms which state that nodes must be…
Rowhawn
  • 1,409
  • 1
  • 16
  • 25
6
votes
2 answers

In pathfinding, what is the difference between DFS and Dijkstra?

I'm studying about DFS and Dijkstra. In my simple test cases, most of them shows that DFS is faster. Passing every nodes costs the same in my test cases. But most people prefer Dijkstra to DFS in pathfinding because Dijkstra is so accurate. So,…
Gengaozo
  • 51
  • 1
  • 3
6
votes
2 answers

Stop boost::depth_first_search along a particular depth if certain criteria is met

I'm using BGL to store my DAG. Vertices have states. Given a change in state in one of the vertices i want to update dependent vertices. This i'm able to do using boost::depth_first_search and a custom visitor. Now the logic is that i dont want to…
Vikas
  • 8,790
  • 4
  • 38
  • 48
6
votes
1 answer

How can I return a bool in a recursive implementation of depth first search?

I want to write a function to check if two binary trees are the same. The code looks like: bool checkSame(Node* first, Node* second) { // Check if nodes are the same // Check left nodes: checkSame(first->left, second->left) // Check…