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
3
votes
4 answers

C# graph traversal - tracking path between any two nodes

Looking for a good approach to keep track of a Breadth-First traversal between two nodes, without knowing anything about the graph. Versus Depth-First (where you can throw away the path if it doesn't pan out) you may have quite a few "open"…
3
votes
1 answer

Backtracking with Depth First Search Iteratively

I was working on a coding problem which required me to find the path to a node. Using recursion and DFS, this was quite easy. public ArrayList ancestorsList = new ArrayList(); public boolean printAncestors(TreeNode root, int…
Kevin
  • 3,209
  • 9
  • 39
  • 53
3
votes
1 answer

depth-first algorithm in python does not work

I have some project which I decide to do in Python. In brief: I have list of lists. Each of them also have lists, sometimes one-element, sometimes more. It looks like…
Mateusz Korycinski
  • 1,037
  • 3
  • 10
  • 24
3
votes
0 answers

Can this depth-first search be coded with failure-driven loop without recursion?

Could you please help me to convert the below of my code into the one using the failure-driven loop (without using recursion)? I coded a simple failure-driven loop that prints the elements in a list without recursive code. print_path(List) :-…
Dora2928
  • 31
  • 1
3
votes
1 answer

Using BFS to find number of possible paths for an object on a grid

I have a matrix that represents a grid and would like to find out all possible places an object can move to. An object can only move horizontally or vertically. Let's assume that the example below is the grid I'm looking at, which is represented as…
3
votes
1 answer

Return the path between two nodes in a minimum spanning tree

I have a minimum spanning tree created using Kruskal's algorithmstored in a map of key:string and data:set(string) mst = { "A" : ["B"] "B" : ["A", "C", "D"] "C" : ["B"] "D" : ["B", "E"] "E" : ["D", "F"] } I am trying…
3
votes
3 answers

Binary Tree - Depth-First Traversal

I want to do a depth-first traversal for this binary tree: 1 / \ 4 5 / \ \ 4 4 5 Here's the node structure: function TreeNode(data){ this.data = data this.left = this.right = [] …
Daniel
  • 437
  • 1
  • 5
  • 14
3
votes
2 answers

Find size of maximum connected region in matrix

So I have a matrix (n row by m column) and want to find the region with the most connected "1s". For example if I have the following matrix: 1 1 0 0 0 1 1 0 0 0 1 0 1 0 0 0 There are 2 regions of "1s" in the matrix. 1st region: 1 1 1 1 1 2nd…
3
votes
1 answer

Find Path to Specified Node in Binary Tree (Python)

I'm having trouble computing the path from the root to a specified node in a binary tree (this is specifically about a Python solution to this problem). Here's an example. Given the binary tree below, if I specify the node whose value is 4, I want…
hobscrk777
  • 2,347
  • 4
  • 23
  • 29
3
votes
1 answer

Show that if the depth first search tree of G equals breadth first search tree of G then G is Tree

Please tell me if my proof is correct or not We have a connected graph, and specific vertex u in V(G). Suppose we compute the dfs tree of G rooted at u and obtain tree T. Now imagine we compute the bfs tree of G rooted at u and we obtain the…
3
votes
2 answers

How to implement a recursive DFS in Clojure (without using a vector / stack)

How would I code the equivalent of the following python in clojure, but strictly using recursion for the stack management (e.g. not using a loop/recur with a vector acting as the frontier)? I realize it's fairly simple with a vector maintaining…
Solaxun
  • 2,732
  • 1
  • 22
  • 41
3
votes
2 answers

Using boost::depth_first_search with Visitor

As the title suggests, I'm using boost::depth_first_search and using a Visitor (inheriting from boost::default_dfs_visitor) to implement some algorithm. However, during the algorithm's run, I want to save some information in the visitor, to be…
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
3
votes
3 answers

.NET Performance: Deep Recursion vs Queue

I'm writing a component that needs to walk large object graphs, sometimes 20-30 levels deep. What is the most performant way of walking the graph? A. Enqueueing "steps" so as to avoid deep recursion or B. A DFS (depth first search) which may step…
Jeff
  • 35,755
  • 15
  • 108
  • 220
3
votes
1 answer

DFS with cyclic dependency

I was attempting to solve this challenge Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches…
thebenman
  • 1,621
  • 14
  • 35
3
votes
1 answer

Optimizing DFS in python

I'm learning algorithms and was doing this problem which is finding the number of regions. I tried a depth first search approach using python but I am getting a call stack exceeded error. Can anyone tell me whats the flaw in my implementation and…
Salmaan P
  • 817
  • 1
  • 12
  • 32