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
5
votes
1 answer

Detecting cycle in an undirected graph using iterative DFS?

So, I implemented the DFS in an iterative manner by the following method: void dfsiter (graph * mygraph, int foo, bool arr[]) { stack mystack; mystack.push(foo); while (mystack.empty() == false) { int k =…
John Lui
  • 1,434
  • 3
  • 23
  • 37
5
votes
1 answer

how to change scrapy request queue size ? How to achieve strict DFO order

According to FAQ, Scrapy explores depth first but I noticed it will process 10 to 30 requests from start_urls before running any deeper requests. Can I change a parameter and force scrapy to explore depth before processing start_urls ? Reference…
Frederic Bazin
  • 1,530
  • 12
  • 27
5
votes
2 answers

How to detect if an undirected graph has a cycle and output it using BFS or DFS

The other question on this only answered how to detect a cycle, not also output it. So, I'd like to write an algorithm run BFS or DFS in O(V + E) time (V=vertices, E=edges), on an undirected graph, and output the cycle if it has one. What I know so…
5
votes
1 answer

Python maze generator explanation

Welcome. Can someone explain me what happens in this code? I would like to know how exactly does this work (it comes from http://rosettacode.org/wiki/Maze_generation#Python). from random import shuffle, randrange def make_maze(w = 16, h = 8): …
mate317
  • 51
  • 1
  • 2
5
votes
3 answers

Does depth first search create redundancy?

I'm trying to make sense of the canonical DFS pseudocode on Wikipedia (http://en.wikipedia.org/wiki/Depth-first_search) -- in particular, the non-recursive implementation that uses a stack. In BFS, you check whether a node is already explored before…
Site
  • 245
  • 3
  • 15
5
votes
1 answer

Pre and post numbers

When doing a depth first search on a Directed Graph what is meant by pre and post numbers? For example: If you were to start at node A and do an alphabetic Depth First Search how do you determine the pre and post numbers?
Deekor
  • 9,144
  • 16
  • 69
  • 121
5
votes
4 answers

Depth-First search in Python

I'm trying to do a Depth-First search in Python but it's not working. Basically we have a peg-solitaire board: [1,1,1,1,1,0,1,1,1,1] 1's represent a peg, and 0 is an open spot. You must move a peg one at a time TWO SLOTS backwards or forward ONLY…
y2k
  • 65,388
  • 27
  • 61
  • 86
5
votes
4 answers

extra space for recursive depth-first search to store paths

I am using depth-first search to identify paths in a directed weighted graph, while revisiting nodes that belong to a cycle, and setting cutoff conditions based on total distance traveled, or stops from the source node. As I understand, with…
denchr
  • 4,142
  • 13
  • 48
  • 51
5
votes
3 answers

Forward Edge in an Undirected Graph

CLRS - Chapter 22 Theorem 22.10 In a depth-first search of an undirected graph G, every edge of G is either a tree edge or a back edge. Proof Let (u,v) be an arbitrary edge of G, and suppose without loss of generality that u.d < v.d. Then the…
p0lAris
  • 4,750
  • 8
  • 45
  • 80
5
votes
1 answer

How to find if a graph has a cycle?

I know this question has been asked many times in this forum and everywhere else in the internet. But before you attack me with your claws outstretched please bear with me. I am a newbie learning graph. As part of my excercise I am given to add a…
as3rdaccount
  • 3,711
  • 12
  • 42
  • 62
5
votes
1 answer

JavaScript Depth-first search

I am trying to implement DFS in JavaScript but I am having a little problem. Here is my Algorithm class: "use strict"; define([], function () { return function () { var that = this; this.search = function (searchFor, node) { …
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
5
votes
2 answers

Maze generation using DFS fails and I don't know why

I just wanted to generate some mazes using the easiest algorithm, but all my mazes look like the following one: Here is a piece of Java code (a whatVisit function works correct, don't look at it): private void dfs(Point start, boolean[][] visited)…
vortexxx192
  • 929
  • 1
  • 9
  • 24
5
votes
1 answer

BGL dfs from a set of source nodes

Problem Having an adjacency list graph, I would like to traverse it with a DFS algorithm from a specific set of source nodes. The main problem is that the color map is passed by value. I tried To encapsulate the color map by reference into a…
Pierre T.
  • 380
  • 1
  • 13
5
votes
3 answers

Depth first search using Queue

How do I do a depth first search using a Queue in c#? The following is my datastructure: public class Node { public string Name{get;set} public IEnumerable Children{get;set;} } Now I have a collection of Node object each with children,…
Mike
  • 3,204
  • 8
  • 47
  • 74
5
votes
7 answers

Non-Recursive DFS Implementation

Recently I needed to implement non-recursive DFS as part of a more complicated algorithm, Tarjan's algorithm to be precise. The recursive implementation is very elegant, but not suitable for large graphs. When I implemented the iterative version, I…
Nir Friedman
  • 17,108
  • 2
  • 44
  • 72