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

Traverse a graph represented in an adjacency matrix

I am wanting to use the depth-first and breadth-first methods for traversing a graph. I have done this on a simple list of nodes before, but I have never tried it with an adjacency matrix and I honestly don't even know where to start. Here is my…
JLott
  • 1,818
  • 3
  • 35
  • 56
4
votes
1 answer

What type of traversal should be used to find the sum of a binary tree?

The following question appeared on a test my instructor gave a couple of years ago. He provided an answer, but did not provide a satisfactory explanation. I have googled this topic, but I didn't find much. I was hoping that the community could…
Ian
  • 348
  • 1
  • 2
  • 11
4
votes
1 answer

Finding paths in undirected graph with specific cost

Suppose we have undirected, weighted graph. Our task is to find all paths beetween two vertices (source and destination) which total cost equal = N. I think it can be done with modified Dijkstra's algorithm combined with BFS or DFS, but I have no…
4
votes
3 answers

Dividing a graph in three parts such the maximum of the sum of weights of the three parts is minimized

I want to divide a graph with N weighted-vertices and N-1 edges into three parts such that the maximum of the sum of weights of all the vertices in each of the parts is minimized. This is the actual problem i am trying to solve,…
2147483647
  • 1,177
  • 3
  • 13
  • 33
4
votes
1 answer

Depth-first nested routing in an ASP.NET website

I've been exploring the System.Web.Routing namespace, playing with constraints and such, but I can't see a way to implement this. I'm working on an ASP.NET website (non-WAP, non-MVC) using the WebPages/Razor framework. I'm trying to implement a form…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
4
votes
4 answers

error in the code given in skiena's book for the application of dfs to find a cycle in a graph

This is the code for dfs. bool processed[MAXV+1]; /* which vertices have been processed */ bool discovered[MAXV+1]; /* which vertices have been found */ int parent[MAXV+1]; /* discovery relation */ #define MAXV 1000 /* maximum number of vertices…
jairaj
  • 1,789
  • 5
  • 19
  • 32
4
votes
3 answers

Java depth first search infinite loop

I'm trying to implement a depth first search algorithm in Java. Any idea why this method is going into an infinite loop? Thanks. public Node search(Graph graph, String nodeName, int ID) { //Get the root node Node root = graph.getRoot(); …
James
  • 111
  • 2
  • 4
  • 15
4
votes
3 answers

Avoiding Python's Stack

I am trying a number of search algorithms for an generalized AI problem, one of which is depth-first-search. I have converted breadth-first-search, greedy, and A* searches from their natural recursive form into an iterative one, but am having a bit…
4
votes
4 answers

Depth-first flattened collection of an object hierarchy using LINQ

I have an object hierarchy (MasterNode -> ChildNodes) where master and child nodes are of the same type, and there are only two levels (top level and children) like this ('A' is parent of D,E and F, 'B' is parent of G, etc) A--+ | D | E | …
Thanasis Ioannidis
  • 2,981
  • 1
  • 30
  • 50
4
votes
1 answer

BFS or DFS for a web crawler?

I was tasked with creating a simple web crawler for a search engine. Now, how should the crawler exactly map the net? Follow the first link he finds and never go back, or some more advanced search methods like BFS or DFS?
corazza
  • 31,222
  • 37
  • 115
  • 186
4
votes
4 answers

Find first null in binary tree with limited memory

I have a binary tree where each node can have a value. I want to find the node in the tree that has value null and is closest to the root. If there are two nodes with the same distance from the root, either will do. I need to minimize the number…
Eyal
  • 5,728
  • 7
  • 43
  • 70
3
votes
2 answers

DFS and a stack

I am learning CS algorithms in my spare time and have been getting on quite well but I'm having trouble understanding adjacency matrix and DFS. 010100 101100 010110 111000 001001 000010 If the above is a undirected graph, with 6 vertices (a, f)…
3
votes
2 answers

Solving 8-Puzzle using DFS optimizations

I'm using Java to solve the 8-Puzzle problem using DFS. this is what I came up with: public static boolean found = false; public void solveDepthFirst(EightPuzzle currentState, int lastMove){ if(currentState.goal()){ …
Jonny
  • 2,787
  • 10
  • 40
  • 62
3
votes
1 answer

Finding cycles with a stack-based depth first search

I know that you use a recursive implementation of DFS where all nodes start as white, are colored gray when they are first encountered, and are colored black after all of their children are explored, you know that there is a cycle if you ever…
user1090859
  • 31
  • 1
  • 1
  • 3
3
votes
1 answer

dfs algorithms uses queue?

i have seen in internet following DFS algorithm #include #include #define MAX 100 using namespace std; queue myQueue; int G[MAX][MAX]; int visit[MAX]; int V, E; void dfs(int s) { int i, j, node; memset(visit, 0,…
user466534