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

What is the runtime of my recursive memoized solution

I am giving a binary search tree with n node with unique value 1 to n and need to compute how many structurally different binary search tree I can make from it. I use DFS with memoization to solve the problem. It is basically like if we have n node,…
0
votes
1 answer

Correct Implementation to find Bridges in graph

I have following implementation to find Cut Edge/ Bridges in a graph : import java.util.Arrays; import java.util.LinkedList; import java.util.List; public class TarjanCutEdge { public static void main(String[] args) { Graph g = new…
Ankit Chauhan
  • 646
  • 6
  • 20
0
votes
1 answer

Creating a recursive path finding algorithm to find all paths between two nodes in a graph

I've implemented a DFS recursive algorithm to find ALL paths between two nodes on a directed graph. Currently, my algorithm is finding approximately half of the total paths to find (I checked on an online tool how many possible paths there should…
evie
  • 73
  • 1
  • 8
0
votes
0 answers

I need to animate depth-first-search algorithm in python for my assignment but being a beginner I have no idea where to start

I just need a basic animation showing how the nodes are visited in the algorithm. I tried searching the internet but nothing seems to be that helpful. Any references or similar projects will be highly appreciated. If there's a way to do it in C++,…
0
votes
0 answers

Graph theory, topological sorting, how to modify DFS to check "corequisites"

Say each node in my graph represents a college course, how can I use DFS to check what's a corequisite of course X and what's a prerequisite?
Ali
  • 17
  • 4
0
votes
0 answers

reconstruct itinerary depth first search recursion bug

I am working on Leetcode #332. It is a graph problem. Someone already copied the question here: Is there any chance to get rid of that error?(Memory Limit Exceeded) Here is the problem page on Leetcode:…
GNG
  • 1,341
  • 2
  • 23
  • 50
0
votes
1 answer

Maximum path in a grid with obstacles, going right, left or down

Given a grid of m*n where each cell contains either 0 or 1. 0 means obstacle, you cannot pass through a cell with 0. We have to find a path from (0,0) to (m,n) that has the maximum number of ones. Allowed moves are right, left, bottom(down). If…
0
votes
1 answer

DFS Snake solving algorithm (NoneType object has no len)

I am making a snake solving algorithm using DFS (Depth-first search) on a grid. The snake works most of the time but I keep getting this error. My friend has also posted a similar question about BFS (breadth-first search) we are in the same group…
LeonDevy
  • 53
  • 8
0
votes
1 answer

How to resolve hierarchy function within php

I have a situation in which there is an ID which I receive in a function. This ID when I pass to an API it provides me a relation list, for example if I pass as an ID A, I…
rvardhan
  • 1
  • 2
0
votes
1 answer

Why is graph DFS faster with LRU Cache than Dictionary lookup. LeetCode 128 solution difference

I'm doing leetcode 128 (longest consecutive sequence). This is the problem: problem picture I wrote 2 working solutions, both using DFS. The 1st solution uses lrucache to cache the DFS function The 2nd solution uses a dictionary to do the same thing…
0
votes
1 answer

Is there a way to use DFS to find the number of nodes in a subtree rooted at u, using a stack data structure and not recursion?

I heard that DFS problems can be done using a stack, but I can't seem to think of a way to use a stack to find this. The recurrence relation is obviously that the number of nodes in a subtree rooted at a node is 1 plus the number of nodes in each of…
Awepossum
  • 1
  • 1
0
votes
1 answer

Word Search II java.lang.ArrayIndexOutOfBoundsException

I tried to implement the Trie and dfs with backtracking to solve the LeetCode problem Word Search II, but it keeps telling me my dfs method is out of bounds: class Solution { class TrieNode{ TrieNode[] children = new TrieNode[26]; …
dean7
  • 1
  • 1
0
votes
0 answers

BFS and DFS by using adjacency matrix C#

How to implement BFS (Breadth First Search) and DFS (Depth First Search) using an adjacency matrix if the vertices in the graph are specified in string format (not integer)? Available code: https://github.com/surbacim/graph-c-/blob/main/graph
Adam
  • 1
  • 1
0
votes
1 answer

Why do I get this as a return when finding paths in a graph with DFS in python?

Good Morning! I'm working on an algorithm to find paths between two given nodes (start and end), a graph (represented as it's adjacency list) and a list containing the current visited nodes. This is the code I've done in Python: def find_path(graph,…
Julen
  • 11
  • 6
0
votes
1 answer

Time complexity of DFS on DAG without using a visited array

What would be the worst-case time complexity for Depth first search on a Directed Acyclic graph with V nodes and E edges if I do not use something like a visited array and traverse same nodes multiple times?
1 2 3
99
100