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

DFS Adjacency matrix wrong traversal (when backtracking)

I am trying for hours to print DFS trace (including when you get stuck and you have to backtrack) but my output is always missing a 0 and having double numbers My input matrix is like this 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 1 0 0…
LimetaPeta
  • 67
  • 5
0
votes
1 answer

Can we find all paths from source to destination in an undirect graph without DFS recursion?

I am trying to retrieve all possible paths based on some conditions from source to destination using iterative depth-first search (not recursion). I already solved the problem using recursion. Now, I am wondering if we can retrieve all paths in an…
Liu Bei
  • 565
  • 3
  • 9
  • 19
0
votes
0 answers

How to change (return) a pointer in a recursive function which is a function parameter in the function

I am currently studying graphs. I have a recursive program which finds all possible paths between two nodes. My function for finding paths works fine. However, I want to save these paths and sort them based on how many nodes do they have. And I…
Batost
  • 11
  • 3
0
votes
1 answer

Graph Traversal (is there a connection between source and destination nodes in the graph) - visited DFS vs BFS

I don't understand for potential cyclic graph traversals, why for is there a connection between source and destination node in the graph: i) DFS, if a node is visited, we return false ii) BFS, if a node is visited, we continue (in the loop) example…
0
votes
4 answers

BGL DFS Visitor with Priority Queue

I have a tree (in the graph sense) representation of a tree (in the physical sense). The tree is represented as a BGL adjacency list where each vertex contains radius and position properties, i.e., my graph is defined in the form struct…
John
  • 783
  • 6
  • 12
0
votes
2 answers

How does time complexity for depth first search on a graph come out to be O(V+E) in the following code?

How does time complexity for depth first search on a graph come out to be O(V+E) in the following code? Using a Python dictionary to act as an adjacency list graph = { 'A' : ['B','C'], 'B' : ['D', 'E'], 'C' : ['F'], 'D' : [], …
prin
  • 35
  • 3
0
votes
3 answers

How can i find the max depth

How can i make a FindMaxDepth method / This function will find the longest depth of the tree (the distance between the root node and the last leaf node), the input that the function will take is: (graph, rootNode) and the output will be a numeric…
0
votes
2 answers

Leetcode 261 Graph Valid Tree: second test fails

I am trying to write a solution for Leet Code problem 261. Graph Valid Tree: Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid…
0
votes
0 answers

Is is possible to implement a depth first search on a binary tree without using recursion?

I wrote the small class below for playing with a binary tree. Is it possible to do depth first search with out recursion? It looks like you might need a queue to remember where you are in the traversal, but I'm not sure. Is there a way to do it…
0
votes
1 answer

How can I back up to an earlier value in Depth-First Search c++?

Here, I'm implementing Depth-First Search (DFS) in c++. However, I can't seem to find a way to back up to the previous place (i.e. 1->2->3 if 3 is end of line go back to 2) Can I use vectors or stacks to track my movements? int searcher(int line[3],…
0
votes
3 answers

Tree recursion - how to include conditions in depth-first search?

I have a tree (non-binary, unbalanced, no cycles), all nodes have flags (green=active, red=inactive). I'm starting from the root node and I have to find a complete path (from root to leaf) where all nodes are active. (To find at least one path is…
Misa
  • 23
  • 4
0
votes
0 answers

#here i did't pass the visited list so it should start with default False list but its not?

I have Created a graph but it has the above problem as issued in the title Basically what i am trying to do is dfs traversal in the graph using adjancency matrix class Graph: def _init_(self,vertices): self.vertices = vertices …
0
votes
1 answer

why list.append cannot work in DFS algorithm

not sure about the problem, so might misleading in the title. this question from LC 77. below is my code, but it returns TypeError: object of type 'NoneType' has no len() however, if I change path.append(lis[i]) to path + [[lis[i]], the code works…
fyxc
  • 1
  • 2
0
votes
2 answers

Algorithm using DFS

im trying to solve this, its stuck in a loop but i cant understand why. I think i might need to add some more conditions and i have looked others people's code but they seem too complicated. function solve(m, s, x, y) { if (x ==…
Bryar
  • 131
  • 2
  • 11
0
votes
1 answer

Find max sum path and trace its path in 2d matrix with obstacles

For example I have 2d matrix like this: .X..X.. 2...2.. ..X.1.. 2.....X Starting at (0,0),I can move down 1 cell or right 1 cell at a time, cell 'X' is an obstacle. Find the path with maximum sum. The answer for the above input is: DRRRRRD (D for…
nanh
  • 13
  • 3