Skip to main content
The 2024 Developer Survey results are live! See the results

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
1 vote
1 answer
55 views

Java DepthFirstSearch Algorithm not working

I have tried to code a depthfirstsearch algorithm to solve a maze. This is what I have so far. I have tried to add as much detail so the logic is understandable: //Upgraded dfs algorithm that creates ...
LonelyBoi404's user avatar
0 votes
1 answer
69 views

How does Prolog "redo" search when backtracking

I think that I understand how Prolog uses unification and backtracking to find the first match. However I'm having a hard time trying to understand what does it do when it is asked to "redo" ...
Tim's user avatar
  • 7,352
-2 votes
2 answers
73 views

Leetcode 417: Pacific Atlantic Waterflow [closed]

There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's ...
bluets's user avatar
  • 1
-12 votes
1 answer
172 views

Knight's journey solution gives incorrect output?

The knights journey where we are given the start/end points and the size of board(n*n): Find the minimum steps to reach the end from the starting point. If no path is there return -1: I've tried ...
new's user avatar
  • 1
0 votes
0 answers
41 views

Why use two arrays of insertion_time and lowest_insert_time in Tarjans algorithm for Bridges in Graph? [duplicate]

I've been working on the LeetCode problem 1192. Critical Connections in a Network. From various sources, I have the following code: class Solution { int timer = 1; void dfs(int node, int ...
Nikhil Garg's user avatar
1 vote
1 answer
94 views

Single Source - Single Destination Shortest Path (DFS + DP) - Time complexity?

Context: I want to write an algorithm for finding the shortest(least cost) path from a fixed source to a fixed destination node in a weighted directed graph with non negative weights (can have cycles)....
Lupin's user avatar
  • 59
2 votes
1 answer
56 views

Topological Sort as Reverse Post-DFS | Course Schedule II LeetCode

Currently solving Course Schedule II on LeetCode and this is the code that DOES NOT pass all test cases because of the following line: Collections.reverse(postOrder);. Removing this line solves all ...
Lola's user avatar
  • 21
1 vote
1 answer
61 views

Why is BFS is much quicker than DFS when I implement both of them?

I am trying to use DFS and BFS to find all simple paths that have lengths up to a given k, starting at a given vertex in a directed graph. No cycles are allowed. My code is as follows, and I have ...
ivygrowing's user avatar
0 votes
1 answer
68 views

Word Hunt DFS Algorithm Not Finding Optimal Solution

I am trying to code an algorithm for the Word Hunt game on Iphone Game Pigeon. How word hunt works: There is a 4x4 grid of letters given to each player. Each player must form words that are three ...
Siddd's user avatar
  • 31
2 votes
0 answers
99 views

Are BFS and DFS the same and just the Monad is different?

I wrote a tail-recursive BFS in Scala: import scala.collection.mutable /** Do BFS from start and return the smallest distance to end (None if not connected) */ def bfs[A](start: A, end: A, neighbors: ...
pathikrit's user avatar
  • 33.5k
0 votes
1 answer
74 views

Tarjan Algorithms for SCC

I am stumbled at this part of SCC. I know that 5,6,7 is a strongly connected component. Performing the tarjan Algorithm for SCC starting at no de 5, I get unsatisfied values of low-link at 7. Graph ...
Swayam Swostik Behera's user avatar
1 vote
0 answers
55 views

Shortest path in Directed Acyclic Graph. Is topological Sort Really needed?

Here's a famous problem: Given a Directed Acyclic Graph of N vertices from 0 to N-1 and a 2D Integer array(or vector) edges[ ][ ] of length M, where there is a directed edge from edge[i][0] to edge[i][...
ABGR's user avatar
  • 5,003
1 vote
1 answer
21 views

How do i add memoization when calculating longest subsequence for an integer array?

I'm doing https://leetcode.com/problems/longest-increasing-subsequence/ I know in theory that each index needs to be assigned a length but i'm having trouble coding it up using my iterative approach /*...
xqcccccccccc's user avatar
1 vote
0 answers
51 views

Dfs in assembly function

I tried to do this function DFS recursively in assembly and I keep recieving "Segmentation fault" and I don't know from where. neighbours_t expand(uint32_t node); void dfs(uint32_t node, ...
Alexandra Dinu's user avatar
1 vote
1 answer
40 views

133. Clone Graph: Node with value 2 doesn't exist in the original graph

I get a similar error with a different root cause compared to: Clone Graph LeetCode 133 Consider the below implementation. If I use a Node-type key for processed_node_map, the algorithm passes. If I ...
John Vandivier's user avatar

15 30 50 per page
1
2 3 4 5
175