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

Stack implementation in recursive function

I'm trying to implement a recursive backtracking function using depth first search and I'm stuck in a point where I need to know my previous position in a matrix. The idea is this: I have a matrix as a 2D Array and this is my function: Mark the…
Spyromancer
  • 435
  • 1
  • 3
  • 10
4
votes
1 answer

Time complexity for detecting a cycle in a graph

I am trying to understand the time complexity of some efficient methods of detecting cycles in a graph. Two approaches for doing this are explained here. I would assume that time complexity is provided in terms of worst-case. The first is…
4
votes
2 answers

How to do a BFS or DFS in directed graph from particular vertex with some vertex having outdegree 0?

What is the order of DFS or BFS traversals for graphs when there may be vertices which have outdegrees of 0 if we want to do DFS/BFS from a given vertex {0..n-1}. Below is one BFS implementation for a graph import java.util.LinkedList; import…
4
votes
4 answers

How to track the depth in this object graph depth-first search algorithm?

I have this code which iterates over a tree, doing a depth-first search. Every element is tackled exactly once. Very good. -(void)iterateOverTree:(TreeNode *)node { NSMutableArray * elements = [NSMutableArray array]; [elements…
Proud Member
  • 40,078
  • 47
  • 146
  • 231
4
votes
2 answers

Special Perfect Maze Generation Algorithm

I'm trying to create a special perfect maze generator. Instead of the standard case that has rooms and walls, I'm dealing with a grid of cells filled with blocks, where I can remove blocks from some cells: to connect two given cells (for example,…
Karim DRIDI
  • 101
  • 6
4
votes
1 answer

Get list of all sub-keys from a python dictionary

I have some dictionaries(json output). I want to get the base element which can be a list of strings or a string. Currently I am doing it like this:- folder="shared/" files=os.listdir('shared') for f in files: f=folder+f print(f) with…
Asim
  • 1,430
  • 1
  • 22
  • 43
4
votes
1 answer

Sum of the minimum elements in all connected components of an undirected graph

In a city, there is n persons and each person has some friends in the city. (if a is friend of b then b is also friend of a) We want to spread a rumor between these persons but saying the rumor to each person has a cost c_i but after that, the…
amir na
  • 217
  • 1
  • 13
4
votes
3 answers

Can't we find Shortest Path by DFS(Modified DFS) in an unweighted Graph? and if not then Why?

It is said that DFS can't be used to find the shortest path in the unweighted graph. I have read multiple post and blogs but not get satisfied as a little modification in DFS can make it possible. I think if we use Modified DFS in this way, then we…
coderAJ
  • 310
  • 5
  • 17
4
votes
2 answers

In NetworkX, how to get the DFS tree including the node data?

In a directed graph in which the nodes have not only IDs, but also data (a dictionary of attributes), I'd to obtain the DFS tree starting from a given source node, including the data of the child nodes. I've noticed, however, that the nx.dfs_tree…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
4
votes
1 answer

Understand JavaScript Recursion and Call Stack in Depth First Traversal

I have a perfect balanced binary tree: 1 / \ 2 5 / \ / \ 3 4 6 7 A simple node structure with DF traversal: function TreeNode(val){ this.val = val this.left = this.right = null this.addLeft =…
Daniel
  • 437
  • 1
  • 5
  • 14
4
votes
1 answer

Intuition behind using backtracking (and not DFS)

I am solving Word Search question on LeetCode.com: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically…
J. Doe
  • 1,291
  • 2
  • 10
  • 19
4
votes
2 answers

Python: Convert a depth first search to a breadth first search for all combinations of a list

I have a simple recursive function that provides a depth first search of each possible combination of a list of options: def twoCharacters_dfs(options, used): for i in range(len(options)): used.append(options.pop(0)) …
4
votes
1 answer

Addition and += give different results for list (depth first search)

I've been trying to understand depth first search, and using various sources online obtained the following code: graph = {'A': ['B', 'D'], 'B': ['E'], 'C': [], 'D': ['C', 'E'], 'E': ['H', 'I', 'J'], 'F': [], 'G': [], 'H': ['F'], …
HelloWorld4444
  • 125
  • 1
  • 9
4
votes
2 answers

Compared to other algorithms, what makes Depth-first Search a better solution for counting the number of connected-components in a graph?

I've looked for ways to count the no. of connected components online. I noticed that in most sites, the algorithm used is Depth-first search. I believe you can achieve the same thing Breadth-first search and union-find as well. So why do people…
Vktr
  • 89
  • 8
4
votes
1 answer

print all paths from root to leaves n-ary tree

I am trying to print all paths from root to all leaves in n-ary tree. This code prints the paths to the leaves, but it also prints subpaths too. For example, let's say one path is 1-5-7-11. It prints 1-5-7-11, but it also prints 1-5-7, 1-5, so…
eds
  • 41
  • 2