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

DFS Path has stackoverflow Error on larger entries

This algorithm successfully gives me a path through a maze. The maze is a 2D binary matrix, and the target location is '9'. The algorithm works by going through the '0' path that reaches '9' (base case) and recursively adds the previous locations to…
beatmaister
  • 375
  • 1
  • 14
3
votes
0 answers

Design Reddit Comment API using SQL

We are building the comment model for Reddit's new backend that supports their app. We have come up with the following Comment structure. The numbers on the right side are the Like counts for each comment. - Comment uuid 1: (Root level comment) …
3
votes
1 answer

Finding connected components in a DYNAMIC Graph

When it comes to finding connected components in a graph we can use DFS or DSU. Does there exist an algorithm which is stable to a changing graph, where node can be added, edge can be removed, node can be removed. Eg. If we add a node to a graph and…
3
votes
1 answer

Trying to fix 3D mesh normals

I have triangle collection that define mesh surface of my 3D shape, I would like to fix normal of each triangle to point outshape. I was trying the following (pseudo): 1. define that first triangle normal direction is right direction 2. go over…
Sergey Kucher
  • 4,140
  • 5
  • 29
  • 47
3
votes
1 answer

Maze solver with DFS

I am trying to solve the maze using DFS algorithm.ORDER: West- North-South-East My output works in this logic Labyrinth picture1. It should not go up after the point (3,3),because the order of priority is west. What should I do? #include…
OceanWaves
  • 53
  • 9
3
votes
1 answer

How to count all occurrences of a word in a character matrix?

Problem Given a m x n 2D board of characters and a word, find how many times the word is present in the board. The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically…
3
votes
1 answer

How to optimise the solution to not get memory limit exceeded error or what might be getting me the error?

I came across the following problem. You are given the root of a binary tree with n nodes. Each node is uniquely assigned a value from 1 to n. You are also given an integer startValue representing the value of the start node s, and a different…
isnvi23h4
  • 1,910
  • 1
  • 27
  • 45
3
votes
1 answer

Processing successors in dfs with Haskell

Consider the following Python function, which, given the successors of a node, visits them and collects the results. (In practice this logic would form a part of the recursive visit function.) from typing import Any, Callable, Tuple, List,…
Eric Auld
  • 1,156
  • 2
  • 14
  • 23
3
votes
1 answer

Why for backtracking sometimes we need to explicitly pop after recursion, and sometimes we don't?

For example let consider a task where we need to find all permutations for given string preserving the character sequence but changing case. Here is backtracking solution without .pop(): def letterCasePermutation(S): """ :type S: str …
3
votes
1 answer

Can we algorithmically find expected hitting time for 2D random walk without simulating/approximating?

Suppose we're given some absorbing boundary that encloses the origin/starting position, and we take a simple random walk (up/down/left/right with equal probability). For simplicity's sake say we have access to a function which tells us for (x,y)…
3
votes
2 answers

Recursive Inner Functions Golang

Recursive inner function declaration golang Is it supposed to be ugly? I'm currently trying to write a recurisve DFS for a leetcode problem (new to Golang) Doesn't Run: when I try to create and declare my inner function like this: outerFunction…
3
votes
1 answer

F# - Traverse a tree defined not as a structure, but as a function: ls: 'a -> 'a seq

tldr; go to My Question I believe that the problem presented here must not be at all new, but I have failed to find any directly corresponding discussion. Let's say that I have the following function (in order to provide a deterministic substitute…
3
votes
1 answer

Detect cycle in an undirected graph using DFS

I am trying to detect cycle in an undirected graph. I am using DFS to detect the same. For any node, I will visit through all connected nodes. If the child node is already visited and its not parent of current node, We have a cycle in graph. I wrote…
3
votes
1 answer

Using a seen set for a directed graph vs. undirected graph

I've been deepening my understanding of algorithms for an undirected graph vs. undirected graph problems on LeetCode. The key difference I realized is for a problem like 841 Keys and Rooms because this is directed I need to add the "0" node to the…
3
votes
1 answer

What's the worst-case space complexity for the All Paths Sum problem for an unbalanced tree?

Here's the problem statement as stated on educative.io. Given a binary tree and a number ‘S’, find all paths from root-to-leaf such that the sum of all the node values of each path equals ‘S’. I understand the solution given to the problem and the…