Skip to main content

Questions tagged [depth-first-search]

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. 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.

Filter by
Sorted by
Tagged with
1 vote
0 answers
64 views

Intro Still working on PathFinding.java. This time I concentrate on three private methods of GridModel that are responsible for generating random perfect mazes. A maze is perfect if for each two cells ...
coderodde's user avatar
  • 32.3k
1 vote
0 answers
71 views

Intro I have this GitHub repository for doing pathfinding in directed unweighted graphs. This post is about BIDDFS proposed by Richard Korf in his paper. Code ...
coderodde's user avatar
  • 32.3k
1 vote
0 answers
40 views

Intro I have this GitHub repository containing some iterative deepening pathfinding algorithms. Code ...
coderodde's user avatar
  • 32.3k
5 votes
1 answer
363 views

Background I can't seem to find an existing answer solving this doubt of mine. In academic books, it seems that DFS is usually presented in both recursive and iterative forms. The implementations ...
Michel H's user avatar
  • 151
1 vote
1 answer
74 views

I solved this problem: A. Ice Skating, Codeforces My approach was to create grid from given snow drifts coordinates, then convert this grid into graph and then count how many disconnected components ...
Szyszka947's user avatar
6 votes
3 answers
746 views

This program solves the minimum dominating set but it takes an insanely long time to run. In case you do not know, in graph theory, a dominating set for a graph G is a subset D of its vertices, such ...
BeginnerProgrammer6's user avatar
2 votes
1 answer
299 views

I have a rectangle of . (platforms) and # (obstacles). I can start at any platform in the top row and go to the left, right or ...
Aliaksei Badnarchuk's user avatar
1 vote
1 answer
149 views

Let's consider a matrix of integers m: [[1,2,3] [4,5,6] [7,8,9]] Which represents a graph: ...
Gameplay's user avatar
  • 171
2 votes
0 answers
209 views

Motivation Recently, Buzzfeed released a browser-based puzzle game called "Pyramid Scheme." It is available at the following link. The puzzle in an initial state looks like this: To solve ...
WilliamMorris's user avatar
3 votes
1 answer
81 views

I am doing some assignments on my own and one of them is about a Island Counting program, I have come up with a working program but the problem is its very slow. When running test on for example maps ...
phuck's user avatar
  • 33
2 votes
1 answer
150 views

I'm struggling a bit with a USACO silver question using Python: http://usaco.org/index.php?page=viewproblem2&cpid=992. The question provides an unsorted list of numbers (...
Luke's user avatar
  • 21
3 votes
1 answer
158 views

I have been trying to solve Trie related problems over at leet code and came across this interesting problem 211. Design Add and Search Words Data Structure Here is the question for convenience: 211. ...
Shawn Frank's user avatar
1 vote
1 answer
99 views

Part 1/3 Part 3/3 I have this library for performing shortest path queries on dags (directed acyclic graphs). This post presents the shortest path algorithms and the topological sorters. Two ...
coderodde's user avatar
  • 32.3k
1 vote
3 answers
136 views

Assuming I've the following code: ...
Regyn's user avatar
  • 143
0 votes
1 answer
253 views

The following code is applying the depth-first search and breadth-first search algorithm to find the shortest path from bottom left to top right in a 10 x 10 grid. ...
Mohamed Magdy's user avatar
0 votes
1 answer
213 views

I have referred to this for what constitutes as an Euler Cycle: https://xlinux.nist.gov/dads/HTML/eulercycle.html This assignment required me to use Depth First Search and have the methods I have ...
William Golovlev's user avatar
1 vote
1 answer
1k views

So a little context to understand the code: This is a helper function to solve a bigger problem here Quick summary: A Knight is in a dungeon/2D grid of Ints, starts at 0,0 and must reach the last ...
Sergio Bost's user avatar
1 vote
0 answers
395 views

Task You are at position [0, 0] in maze NxN and you can only move in one of the four cardinal directions (i.e. North, East, South, West). Return true if you can reach position [N-1, N-1] or false ...
Михаил Спирин's user avatar
1 vote
1 answer
96 views

The class in this post returns a directed cycle in a financial loan graph. A financial loan graph consists of nodes and directed arcs. If there is an arc \$(u, v)\$ with weight \$w = w(u, v)\$, then ...
coderodde's user avatar
  • 32.3k
0 votes
1 answer
1k views

I have implemented the following question and looking forward for the reviews. Question Explanation : We have a two-dimensional board game involving snakes. The board has two types of squares on it: +...
Neslihan Bozer's user avatar
0 votes
2 answers
214 views

I am looking for a simpler implementation of the following question. Could it get better? I am expecting the new solution via DFS again. Thanks. OA: Given a 2d matrix with chars and a target string. ...
Neslihan Bozer's user avatar
1 vote
1 answer
1k views

I have implemented a correct version of DFS (with comments explaining the implementation): ...
Charlie Parker's user avatar
3 votes
2 answers
444 views

Inspired by this question, here's some C code to check if a point is "inside" of a shape on a simple black and white bitmap. For this exercise: Set bits form the "outlines" of a ...
user673679's user avatar
  • 12.2k
7 votes
6 answers
5k views

I got the C++ implementation of depth-first search (DFS) from here and made slight modifications to it. The modified code is as follows: ...
a_sid's user avatar
  • 435
3 votes
1 answer
455 views

Here's my attempt. I iterate through each vertex in the graph and do a DFS to see if I reach back on a vertex I already visited in this vertex's iteration. It seems to work but I am not satisfied with ...
user2495123's user avatar
2 votes
0 answers
88 views

https://leetcode.com/problems/number-of-enclaves/ Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land) A move consists of walking from one land square 4-directionally to ...
Gilad's user avatar
  • 5,443
6 votes
1 answer
300 views

I have solved problem 10608 on UVA Online Judge using Python 3.5.1. My solution works, but it takes too long to run when the online judge evaluates it. Problem There is a town with N citizens. It is ...
Kyssmark's user avatar
5 votes
1 answer
746 views

I tried solving Leetcode 01 Matrix problem. It is running too slow when solved using DFS approach. Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance ...
Akanksha's user avatar
  • 129
6 votes
2 answers
18k views

I have implemented a depth first search using a stack (in a separate class). Any suggestions please on improvements for the below code: ...
Saurabh's user avatar
  • 445
5 votes
1 answer
149 views

I was trying to solve a problem that was briefly mentioned at the beginning of the "The Algorithm Design Manual" by Steven Skiena (Ch 1, Problem 26). It took me some time to build a working ...
Mikhail Kalashnikov's user avatar
4 votes
1 answer
381 views

I'm posting my code for a LeetCode problem. If you'd like to review, please do so. Thank you for your time! Problem Two strings X and Y are similar if we can swap two letters (in different positions) ...
Emma Marcier's user avatar
  • 3,742
4 votes
1 answer
323 views

I've recently finished my first ever C program. I implemented a Sudoku solver that uses backtracking. Does my code look like a proper C-program? Are there any recommendations for improving my code? <...
Ivan Shelomentsev's user avatar
2 votes
1 answer
389 views

Problem For the given binary tree return the list which has sum of every paths in a tree. i.e Every path from root to leaf. I've written following solution. ...
Ashish Pawar's user avatar
8 votes
1 answer
2k views

Kosaraju algorithm is mainly phrased as two recursive subroutines running postorder DFS twice to mark SCCs with linear time complexity O(V+E) below, For each vertex \$u\$ of the graph, mark \$u\$ as ...
sof's user avatar
  • 181
1 vote
1 answer
119 views

So after years of teaching myself, I think I'm ready to stand on the shoulders of giants and ask you a good question! I have a tree of nodes. What I want is a function that returns an array of the ...
aName's user avatar
  • 143
1 vote
0 answers
317 views

https://leetcode.com/problems/minesweeper/ please review for performance. I really don't like the implementation I made for GetNumberOfMines() function, but within the time limits i did this. it is ...
Gilad's user avatar
  • 5,443
1 vote
1 answer
258 views

https://leetcode.com/problems/flood-fill/ An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, ...
Gilad's user avatar
  • 5,443
4 votes
0 answers
196 views

The N-Queens puzzle can be solved in many ways. One is by a depth-first-search backtracking algorithm. Below is my generalized version using mutual recursion: ...
user avatar
-1 votes
2 answers
106 views

I got asked this question in an interview and I came up with a solution which I shared in the below but apparently it was not enough to pass. I'm looking forward to hearing how this problem could be ...
Ugur Yilmaz's user avatar
6 votes
1 answer
291 views

I am an experienced self-taught professional Python programmer working my way through the book Classic Computer Science Problems in Python to try to catch myself up on some of the things I missed by ...
user5293's user avatar
  • 163
3 votes
1 answer
136 views

I am trying to create a word-finder game. There will be an n-by-n Board filled with Tiles. ...
Kerry Huang's user avatar
5 votes
0 answers
237 views

I implemented a general function for a depth-first-search (DFS) using JavaScript with default arguments and functions that need to be provided in order for it to work. This function is using ...
Evgeny Zislis's user avatar
3 votes
1 answer
100 views

I wrote a code to calculate the lengths of strongly connected components in a graph. The results I get are correct but it scales horribly. This is the code I have. I understand that the runtime ...
Clock Slave's user avatar
5 votes
2 answers
491 views

My current code implementing a depth-first search works well for puzzle 1. But my code is too slow; I think I use too many loops and I am not sure I am using the right data structure. So I have a ...
Hoang Hai's user avatar
3 votes
1 answer
2k views

I'm trying to resolve this kata from CodeWars. Kata exercise You are at position [0, 0] in maze NxN and you can only move in one of the four cardinal directions (i.e. North, East, South, West). ...
Ender Look's user avatar
3 votes
1 answer
78 views

This question is the second version of the code here. I'm writing a general tree class. Specifically, each node should have oen parent, some number of children, and hold a value. I'm looking for ...
Alex F's user avatar
  • 443
4 votes
2 answers
1k views

I got caught in the trap of implementing my own data structure. I've created a very general tree structure, where each node gets one parent, some children, and holds a value. I'm mostly looking for ...
Alex F's user avatar
  • 443
2 votes
2 answers
291 views

https://www.hackerrank.com/challenges/connected-cell-in-a-grid/problem problem statement: Consider a matrix where each cell contains either a 1 or a 0. Any cell containing a 1 is called a filled ...
user3769323's user avatar
2 votes
1 answer
408 views

I would like to get some feedback on my completed code that finds and displays the articulation vertices in an undirected graph. I included a test case within the code, it should (and does) print <...
justanothertechdude's user avatar
1 vote
0 answers
323 views

I'm trying to solve this question: Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them. The park consists of n squares ...
nz_21's user avatar
  • 1,051