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

Python Recursion Issue (Leetcode 542)

I think I misunderstand some important concepts in Python and it is not specific to the Leetcode problem. I greatly appreciate for any help from who knows Python deeply. The Leetcode 542 is that given a 2D array consisting of 0 and 1 only, and for…
TripleH
  • 447
  • 7
  • 16
3
votes
2 answers

Finding all maze solutions with Python

I am trying to find (using Python) all possible solutions to a maze. I have a DFS script that returns one solution. I am trying to adapt it but I'm really having a hard time wrapping my head around the whole recursion thing. Here's the code I have,…
3
votes
1 answer

How to backtrack when using a iterative DFS implemented via a stack

I was completing this Leetcode problem: https://leetcode.com/problems/word-search/ and I randomly chose to implement the DFS iteratively with a while loop and a stack but I encountered some inconveniences when backtracking that I wouldn't normally…
alee18
  • 45
  • 1
  • 5
3
votes
2 answers

Infinity loop error using the depth-first search algorithm

I'm trying to create a Boggle Solver program and I am having errors with my depth-first-search function. After I iterate through the 'visited' array using a for-loop, my function should return and start going through my trie again. Instead, it…
TonyGrey
  • 37
  • 4
3
votes
3 answers

Decode String - Javascript

I'm trying to implement the Decode String algorithm in javascript. Problem: Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k…
myTest532 myTest532
  • 2,091
  • 3
  • 35
  • 78
3
votes
1 answer

Bipartite Matching with a constraint

I am trying to get a matching between two sets of vertices, one represents meets between two teams and the other time slots when the meets could happen. The adjacency map represents both teams' availability to meet at any given time slot. This would…
3
votes
3 answers

Is there a good way to exit a DFS procedure?

I've learned that a recursive depth-first search procedure searches a whole tree by its depth, tracing through all possible choices. However, I want to modify the function such that I can call a "total exit" in the middle, which will completely…
Telescope
  • 2,068
  • 1
  • 5
  • 22
3
votes
3 answers

Optimizing BVH traversal in ray tracer

In my CPU ray tracer (well, path tracer), the majority of CPU time spent is in the BVH traversal function. According to my profiler, 75% of time spent raytracing is spent in this function and functions that it calls, while 35% of time is spent in…
Leo Adberg
  • 342
  • 2
  • 18
3
votes
1 answer

Mutating a tree while performing a modified DFS without recursion

I am trying to implement a modified version of a tree DFS without recursion and am struggling with the borrow checker. My requirement is that I want to ensure that child nodes are treated before their parent, and I would like all this to be mutable…
3
votes
1 answer

Efficient code for finding route between nodes of a data tree

I have a file with the following format: Y1DP480P T FDVII005 ID=000 Y1DPMS7M T Y1DP480P ID=000 Y1DPMS7M T Y1DP4860 ID=000 Y1DPMS7M T Y1ENDCYP ID=000 Y1DPMS6M T Y1DPMS7M ID=000 Y1DPMS5M T VPY1CM28 ID=000 Y1DPMS5M T Y1DPMS6M ID=000 Y1DPAS21 T Y1DPMS5M…
Steve Ives
  • 7,894
  • 3
  • 24
  • 55
3
votes
0 answers

Can a directed graph have two DFS traversals?

Here is my DFS traversal algorithm (Recursive approach): def dfs(v,visited) : for i in Graph[v] : if i not in visited : visited.append(i) print(i) dfs(i,visited) n = int(input()) Graph = {} for…
3
votes
0 answers

How do I get highest degree of by using Deep-first-search algorithm

First I have a txt file that contains thousands pairs of nodes and it looks like this: 1 3 1 5 1 8 1 7 2 3 2 4 2 5 2 6 3 2 3 4.... Every pair of node can be one edge, so I was using scanner to read them and and put every pair of nodes into a…
TIGUZI
  • 231
  • 1
  • 3
  • 12
3
votes
1 answer

Finding maximum number of nodes in set of undirected graphs

I have a set of nodes (N=7) {a, b, c, d, e, f, g} These nodes form one or more distinct undirected graphs, I want to find the graph with the maximum number of nodes. However, I have a constraint that the complexity can not be more than (N*M) where…
3
votes
1 answer

Recursive Searching in Java

So I've been writing a program for the game boggle. I create a little board for the user to use, but the problem is I don't know how to check if that word is on the board recursively. I want to be able to check if the word the entered is indeed on…
ekm
  • 31
  • 1
  • 3
3
votes
1 answer

Implementing depth-first graph traversal

I have conflicting information about depth first traversal and could use some help in understanding how to build a program. Given a certain graph, I want to print a sequence of vertices. The user will input a particular node to start the traversal…
Raging Java
  • 49
  • 1
  • 7