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

Need Help in improving recursive DFS implementation

I am trying to implement DFS using recursion in Python I am not so good in python but trying to learn it by exploration and experience. The Following is the code I have designed based on my visualization rowNums = [0, -1, 0, 1] colNums = [-1, 0,…
Nick OZ
  • 47
  • 7
0
votes
1 answer

why does append not append the parameter

I was doing a bit of Leetcode and do not understand why res.append(subset) does not append the subset to res. Could somebody explain the reason behind this? def solve(arr): res = [] def dfs(arr,subset): if len(arr) == 0: …
0
votes
0 answers

During recursion in depth first search once the program prints the leaf node, why does it returns back to its parent node?

Context-: Artificial Intelligence. Uninformed search. Depth first search. The goal of course seems to understand the properties, but I am trying to learn first the algorithm itself to understand thsoe stuffs. This is the tree I am working…
0
votes
0 answers

Why cant we use algorithm to find all cycles to find an hamiltonian cycle?

I recently came across algorithms to find all cycles in an undirected graph, and was confused when i saw that they are running linear time (example), It seem easy to use one of these to find hamilton cycle (for each cycle we find just check if it is…
0
votes
1 answer

Depth First Search returns only root value

I am trying to get a list of values of the tree using DFS but I only get root's value. :( def depthFirstSearch(root): output = [] if root: output.append(root.value) depthFirstSearch(root.left) …
Moon
  • 1
0
votes
3 answers

Checking to see if cousins of a given node and adding to an arrayList

How to use BFS or DFS to get all cousins of a given node and add into an ArrayList. If anyone can help point out where my logic is wrong static boolean findLevel(Node root, Node find, int level) { boolean status = false; if (root == null ||…
0
votes
1 answer

Graph DFS with Integer object data type returns incorrect results

I was solving isPathExits between nodes from LeetCode. And for a data set, mentioned in below code the logic failed. On investigating, I noticed when using stack.pop(); with Integer datatype for the return ed node value it results false, If I change…
Tim
  • 1,321
  • 1
  • 22
  • 47
0
votes
1 answer

discovery time & finishing time of Depth first search

I am performing a depth-first traversal in a graph, where for a vertex v, d[v] is the discovery time, and f[v] is the finishing time. Now I want to know which of the following is wrong: i) d[u] < d[v] < f[u] < f[v] ii) d[u] < f[u] < d[v] < f[v] iii)…
Christopher Marlowe
  • 2,098
  • 6
  • 38
  • 68
0
votes
0 answers

Longest series that can be built from a given graph edges

I'm trying to build a program that finds the longest series that can be built from a given edges represented by list of tuples with pair of values, i.e: [(1,2), (1,2), (2,3), (2,17), (2,17)] Edges can be rotated when connected! The program should…
0
votes
0 answers

Why does this code work when I remove the return statement in the void function called dfs but returns an inaccurate answer otherwise?

Leetcode q: https://leetcode.com/contest/biweekly-contest-69/problems/longest-palindrome-by-concatenating-two-letter-words/ My Solution: #include using namespace std; class Solution { public: bool isPalindrome(string str){ …
jeremy
  • 59
  • 7
0
votes
0 answers

Given a directed graph G=(V,E) with pair of vertices s,t∈V, Find algorithm that find all the unique paths from s to t

Given a directed graph G=(V, E) with pair of vertices s,t∈V, Find an algorithm that counts all the unique paths from s to t in the best time complexity. (A unique path from s to t is defined to be a path from s to t that doesn't contain any common…
0
votes
0 answers

Find the longest path down a mountain with different stations

I a got a list of tuples, each tuple representing a station on a mountain. Each station has a certain distance to another station and we can only visit stations which are below us (so we basically cannot travel the mountain upwards). The tuples look…
kklaw
  • 452
  • 2
  • 4
  • 14
0
votes
1 answer

postordering depth-first-search (DFS) of HTML, using python, lxml, etree

This is not an DFS algorithm question, or library-suggestion question. It is specifically about lxml.etree (v 4). I use python 3.9. This library, lxml.etree, provides a way to iterate over the ElmentTree into which an HTML code is parsed. The…
Michael
  • 1,851
  • 4
  • 19
  • 26
0
votes
2 answers

DFS | BFS : Check if path exists between node A and node B

In a bidirectional graph check if a path exists between node A and node B. My code does not work for certain input (samples provided below). Is this implementation correct or I missed something? bool[] visited = null; public bool ValidPath(int n,…
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
0
votes
0 answers

Count cycles in undirected graph that does not share a vertex

I am trying to understand an algorithm to count the number of isolated cycles in a graph. By isolated, I mean the cycles that do not share any vertex. Here is an example: Here, 1-2-4-1 and 3-6-5-3 are the two cycles that do not share any vertex.…