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

Find all paths in a Tree represented as Graph, From any node to any node. ( Only in downward direction )

Given edges Edges: { {1,2}, {1,3}, {3,4}, {3,5}, {5,6}}; Find all possible paths from any node to any node ( Note moves only downwards from root node ). Expected…
0
votes
2 answers

Looking for correct recursive algorithm for Leetcode 112

I am working on Leet Code problem 112. Path Sum: Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no…
0
votes
0 answers

breadth first search for characters instead of integers c++

I am trying to create a menu driven program to produce a depth first search and breadth first search for a graph. I was able to get it to work for integers but I need it to do the same for characters or strings instead of integers. I keep getting a…
0
votes
1 answer

JAVA rat in a maze DFS using stack and no node

I am a student and studying JAVA. I did write my code for DFS (Rat in a maze) and I need to use stack. I don't want Node class and my maze is just final. [0][0] is start and [5][5] is exit which has 'e'. So, here's my code but if I run this code,…
nojob
  • 17
  • 2
0
votes
1 answer

My code exceeds the time limit. How can I make my code more optimized?

My code exceeds the time limit at test case 23. I have used dfs to traverse the connected houses. I did everything I could to optimize from my side. Kattis problem: where's my internet? import java.util.Scanner; import java.util.HashMap; import…
0
votes
2 answers

Calling a custom function on each node during DFS traversal

I am wondering what would be the most elegant way to code a DFS traversal that can be adapted to solve different problems (in C++). I was thinking to pass a function pointer and a void * to my function and let the user pass a callback that would be…
0
votes
3 answers

How to properly backtrack in Depth First Search Algorithm?

I am trying to solve a problem which is: Find all ancestors of a particular node in a binary tree. Input: root, targetNode Output: An array/list containing the ancestors Suppose, we have the above binary tree as an example. We want to find the…
h_a
  • 119
  • 2
  • 8
0
votes
0 answers

Use deep-first-search. Find out all the articulation points and biconnected components. Show the change process of Low[v] for each node

Given the following undirected graph G = (V, E). Using vertex s as the source vertex. If multiple neighbors are available, visit them using alphabetical order.
Jane_IDK
  • 65
  • 7
0
votes
1 answer

Python recursion updates an array

Might I ask a question a bout Python recursion? I would like to check the logic behind this, and why this is able to keep updating the results? The question: A DFS recursion is constructed to append all its children nodes with a specified condition…
Qiang Super
  • 323
  • 1
  • 11
0
votes
1 answer

Generating all possible DAGs using R

I'm using R to generate DAGs with 4 nodes. I need to generate all the possible DAGs without using any packages. Because there are 6 pairs and each pair has 3 options: no egde, or two directions, the total possibilities are 3**6 = 729. I think it's a…
0
votes
0 answers

How to change from Yield to return with loop?

I'm doing a code project about resolving labyrinth, I'm using DFS algorithm to find a path from a starting point to a target point. I've two sections of code, one with 'Yield' which I'm not authorize to use, and other but it sends a bool, I don't…
0
votes
0 answers

True/False: If $v$ is a leaf in every spanning tree resulting from DFS(s), then $v$ is a leaf in every spanning tree resulting from BFS(s)

Let G = (V,E) be a connected undirected graph. Let s in V be a vertex in the graph. True/False: If v is a leaf in every spanning tree resulting from DFS(s), then v is a leaf in every spanning tree resulting from BFS(s) (v != s). I assume that this…
0
votes
1 answer

JavaScript array DFS but always back to root after searching

I'd like to get output from input. It should be passed all the ways that is possible but shouldn't be passed the way which was already visited. It looks similar with Depth-First Search but this should be returned to parent node and then search…
0
votes
0 answers

DFS n-queens problem. Why my code is so slow?

Recently got an assignment on solving the n-queens problem using BFS/DFS. My BFS stops at N=7 and I think it's understandable but DFS has a problem with N=8 and goes on for a few minutes. Do you have any suggestions on how to speed it up? Also, can…
0
votes
0 answers

DFS implementation using class in c++

Please let me know the reason for the error that is coming in the program. I was trying to create a graph class and implement printing of elements using dfs. The link to the following code is: #include using namespace std; class…