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
8
votes
3 answers

Count number of cycles in directed graph using DFS

I want to count total number of directed cycles available in a directed graph (Only count is required). You can assume graph is given as adjacency matrix. I know DFS but could not make a working algorithm for this problem. Please provide some…
8
votes
4 answers

Find all cycles in graph, redux

I know there are a quite some answers existing on this question. However, I found none of them really bringing it to the point. Some argue that a cycle is (almost) the same as a strongly connected components (s. Finding all cycles in a directed…
Shadow
  • 1,042
  • 2
  • 15
  • 23
8
votes
3 answers

Solving 8-Puzzle using DFS

I am looking for code in java that implement DFS and BFS for the 8-puzzle game by given initial state : 1 2 3 8 0 4 7 6 5 and Goal state 2 8 1 0 4 3 7 6 5 I need to print the solution path from initial to goal state (Not done yet) This is the code…
8
votes
3 answers

BFS, DFS searches required to mark as Visited for trees?

Looking at the BFS and DFS algorithms they seem to mark the nodes as visited. If I am navigating trees only is it still necessary for my implementation to mark nodes as visited or not? I want to perform some action on every node exactly once. It…
roverred
  • 1,841
  • 5
  • 29
  • 46
7
votes
1 answer

Parallel depth-first search in Erlang is slower than its sequential counterpart

I am trying to implement a modified parallel depth-first search algorithm in Erlang (let's call it *dfs_mod*). All I want to get is all the 'dead-end paths' which are basically the paths that are returned when *dfs_mod* visits a vertex without…
skanatek
  • 5,133
  • 3
  • 47
  • 75
7
votes
2 answers

depth-first graph search that returns path to goal

I've been trying this all week and cannot, for the life of me, figure it out. I know that I need to have a helper function that will recurse and return pathSoFar. I can't seem to get my head around the recursion. I'm so confused that I can't even…
mikey555
  • 432
  • 3
  • 8
  • 19
7
votes
2 answers

runtime error: reference binding to misaligned address 0xbebebebebebebec6 for type 'int', which requires 4 byte alignment (stl_vector.h)

i am writing code to solve this problem on leetcode my strategy to solve this is: run dfs for each cell index (x,y) on each dfs call check if cell is a destination cell accordingly set the flags if both flags are true then add this cell to "ans"…
dagwood
  • 379
  • 1
  • 2
  • 13
7
votes
1 answer

Complex Continuation in F#

All of the continuation tutorials I can find are on fixed length continuations(i.e. the datastructure has a known number of items as it is being traversed I am implementing DepthFirstSearch Negamax(http://en.wikipedia.org/wiki/Negamax) and while the…
Snark
  • 1,664
  • 14
  • 27
7
votes
2 answers

Finding cycles: DFS versus union-find?

DFS with coloring would take O(V+E) vs union find would take O(ElogV) reference: http://www.geeksforgeeks.org/detect-cycle-undirected-graph/ So union find approach is slower. If V = 100, E = 100, DFS = 200, Union find is 1,000. Is there a reason to…
7
votes
2 answers

How to find the longest path between two nodes in Lisp?

I need to program a Lisp function that finds the longest path between two nodes, without revisiting any nodes. Though, if the start and end node are the same, this node can be revisited. The function needs to be both recursive and…
Jay
  • 183
  • 6
7
votes
1 answer

How to implement dfs using recursion?

I'm trying to implement DFS with recursion using the following code, public static void dfs(int i, int[][] mat, boolean [] visited){ visited[i] = true; // Mark node as "visited" System.out.print(i + "\t"); for ( int j = 0; j <…
Arefe
  • 1,029
  • 5
  • 16
  • 30
7
votes
2 answers

Is there way to represent static data in Haskell? Or is there any other elegant algorithm for DFS traversal in Haskell?

I am trying to construct DFS tree using recursive algorithm. Pseudo code for this is : DFF(G) Mark all nodes u as unvisited while there is an unvisited node u do DFS(u) . DFS(u) Mark u as visited for each v in u's neighbor do if v is not…
MazaYong
  • 197
  • 1
  • 8
7
votes
4 answers

breadth first or depth first search

I know how this algorithm works, but cant decide when to use which algorithm ? Are there some guidelines, where one better perform than other or any considerations ? Thanks very much.
7
votes
2 answers

Implementing a depth-first tree iterator in Python

I'm trying to implement an iterator class for not-necessarily-binary trees in Python. After the iterator is constructed with a tree's root node, its next() function can be called repeatedly to traverse the tree in depth-first order (e.g., this…
norman
  • 5,128
  • 13
  • 44
  • 75
7
votes
1 answer

Implementation of an explicit stack in a depth first search

I have a particularly large graph, making it nearly impossible to traverse using recursion because of the excessive amount of memory it uses. Below is my depth-first function, using recursion: public function find_all_paths($start, $path) { …
server_kitten
  • 386
  • 3
  • 13