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

What's the purpose of BFS and DFS?

I've learned how these algorithms work, but what are they used for? Do we use them to: find a certain node in a graph or to find a shortest path or to find a cycle in a graph ? Both of them just visit all the nodes and mark them visited, and I…
Nayana
  • 1,513
  • 3
  • 24
  • 39
12
votes
8 answers

Non-recursive Depth-First Search (DFS) Using a Stack

Ok this is my first post on Stack Overflow I have been reading for a little while and really admire the site. I am hoping this is something that will be acceptable to ask. So I have been reading through Intro to Algorithms (Cormen. MIT Press) all…
Cory Gross
  • 36,833
  • 17
  • 68
  • 80
10
votes
4 answers

Is there a difference between dfs and topological sort? Can topological ordering be achieved without using dfs?

I was trying to write code for detecting a cycle in a directed graph and if there is no cycle then return a topological order of the same. While I was searching for it I came across different techniques like DFS and topological sorting to detect…
10
votes
5 answers

Back edges in a graph

I'm having a hard time understanding Tarjan's algorithm for articulation points. I'm currently following this tutorial here: https://www.hackerearth.com/practice/algorithms/graphs/articulation-points-and-bridges/tutorial/. What I really can't see,…
10
votes
9 answers

Depth-first search (DFS) code in python

Can you please let me know what is incorrect in below DFS code. It's giving correct result AFAIK, but I don't know when it will fail. graph1 = { 'A' : ['B','S'], 'B' : ['A'], 'C' : ['D','E','F','S'], 'D' : ['C'], 'E' :…
Vicky
  • 161
  • 1
  • 1
  • 6
10
votes
3 answers

algorithm to enumerate all possible paths

Consider the following graph: I'm trying to find a way to enumerate all possible paths from a source node to a target node. For example, from A to E, we have the following possible paths: A B C D E A B C E A C D E A C E Note that for A C D E,…
10
votes
2 answers

How to output all biconnected components of an undirected graph?

Given a general undirected graph, how can we print all the biconnected components of the graph in O(N+M) time? I know Tarjan's algorithm that is used to output all the articulation points of an undirected graph but I am finding it hard to extend the…
Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
10
votes
4 answers

Kernel module that iterates over the tree of children of a task using depth first search

So I know how to create a kernel and to iterate over the processes linearly by simply including linux/sched.h and using the following code: struct task_struct *task; for_each_process(task) { printk("Name: %s PID: [%d]\n", task->comm,…
Hassan Jalil
  • 1,114
  • 4
  • 14
  • 34
9
votes
2 answers

Longest chain that can be arranged

I found this problem somewhere in a contest and haven't been able to come up with a solution yet. I have the positive integers. I have to find longest subset that among each two neighbour elements one divides another. What I'm doing is: I'm…
Rasul Kerimov
  • 549
  • 4
  • 18
9
votes
2 answers

Depth First Search Algorithm Prolog

I am hoping you could help me with this. I am trying to learn about Depth First search algorithm in Prolog and I have come across the following code go(Start, Goal) :- empty_stack(Empty_been_list), stack(Start, Empty_been_list, Been_list), …
Sean Gray
  • 133
  • 2
  • 2
  • 8
9
votes
1 answer

prolog depth first iterative deepening

I am trying to implement a depth first iterative deepening search of a state space graph. I have a graph with three vertices and their are two activating edges and two inhibition edges. Each node has a binary value, collectively this is the state of…
9
votes
3 answers

Functional style early exit from depth-first recursion

I have a question about writing recursive algorithms in a functional style. I will use Scala for my example here, but the question applies to any functional language. I am doing a depth-first enumeration of an n-ary tree where each node has a label…
W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111
8
votes
1 answer

How to properly label branches of a tree in a depth first search

I have a tree with a structure like this: __2__3__4 / \__5__6 0__1___7/__8__9 \\ \\__10__11__12 \__ __ __ 13 14 15 Node 1 has four children (2,7,10,13), nodes 2 and 7 have two children each (both sharing node 5 as…
8
votes
2 answers

What's the good of using 3 states for a vertex in DFS?

In the explanation of depth-first search (DFS) in Algorithms in a Nutshell (2nd edition), the author used 3 states for a vertex, say white(not visited), gray(has unvisited neighbors), black(visited). Two states (white and black) are enough for a…
nn0p
  • 1,189
  • 12
  • 28
8
votes
2 answers

Implementing DFS and BFS for binary tree

I'm trying to traverse a binary tree using depth first traversal and breadth first traversal, but I'm running into trouble. My node and tree implementation seems to be fine, I'm just not sure how to properly traverse the tree depth-wise and…
123
  • 8,733
  • 14
  • 57
  • 99