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
2 answers

How to detect cycles in directed graph in iterative DFS?

My current project features a set of nodes with inputs and outputs. Each node can take its input values and generate some output values. Those outputs can be used as inputs for other nodes. To minimize the amount of computation needed, node…
RenWal
  • 181
  • 2
  • 7
4
votes
1 answer

Clojure and ClojureScript REPL produce different output

Using the following recursive definition of a depth first search a Clojure (JVM) and ClojureScript (tested with both browser connected repl and lumo) REPL produce two different outputs, i.e. order in which nodes are printed is different and Clojure…
mac
  • 9,885
  • 4
  • 36
  • 51
4
votes
1 answer

How to find the shortest path in a puzzle with keys and doors

I tried the below code, but it didn't give me the right answer. Here is the problem statement. Suppose you have a 2-D grid. Each point is either land or water. There is also a start point and a goal. There are now keys that open up doors. Each…
yilia
  • 125
  • 1
  • 6
4
votes
1 answer

Running DFS on large graph

I am trying to find Strongly Connected Components in a large graph implementing Kosaraju's algorithm. It requires running a DFS on a graph in reverse, and then forward. If you're interested the list of edges for this graph are here:…
edd91
  • 185
  • 8
4
votes
1 answer

How to stop depth-first search when the target vertex of the graph is found?

Here is the graph: g = { 0: [2, 5, 7], 1: [7], 2: [0, 6], 3: [5, 4], 4: [3, 6, 7], 5: [3, 4, 0], 6: [2, 4], 7: [0, 1, 4] } I have the following function in Python: def dfs(graph, start, target, visited=None): if…
Patterson
  • 326
  • 1
  • 7
  • 19
4
votes
2 answers

Equivalence of a graph and a BFS and DFS tree

I have this problem which I am not able to prove. Can someone offer some insight into this problem We have a connected graph G = (V,E), and as pecific vertex u ∈ V.Suppose we compute a depth-first search tree rooted at u, and obtain a tree T that…
4
votes
1 answer

Graph pre-order/post-order traversal?

This is a DFS pre-order vertex numbering which corresponds to a pre-order traversal of the DFS tree and the second one is A post-order numbering which corresponds to a post-order traversal of the DFS tree. Can someone please explain how did we get…
Square-root
  • 853
  • 1
  • 12
  • 25
4
votes
2 answers

Adjacency matrix neighbors

I have a matrix with 0s and 1s. I can start from any cell. I want to know what is the minimum number of steps(up,down,left,right) required to cover all possible 1s. I can start from 0 or 1. Example: 0 1 0 1 1 1 0 1 0 Start from (2,2) within 1 step…
4
votes
3 answers

What are the practical uses of DFS and BFS?

I had this question asked in the interview today. I told them its traversal and DFS can be used to see if the graph is connected. They said it was too simple. What are some more important practical uses of DFS and BFS?
4
votes
1 answer

Recursive Depth-first search algorithm

I am trying to write a recursive depth-first search algorithm that takes an adjacency list representing a graph and prints the visit order of the vertices. My input is a graph stored as an adjacency list: graphAL2 = {0 : [1,2,3], 1 :…
superfluousAM
  • 111
  • 2
  • 13
4
votes
1 answer

Ruby recursive DFS method

I have some troubles with recursive method for depth first search algorithm implementation. Here's the binary tree photo: The method works well with the right side of the tree (55, 89, 144), but it returns nil when it comes to the left side, even…
poctek
  • 256
  • 4
  • 12
4
votes
1 answer

Doing a DFS on a neo4j graph

I have a database with the following structure. The property node is of the type create (A:Property {value:"abc"}) How to do a dfs so that it will be able to print all the values in the graph.In the order A->B->E->F->C->G->H->D->I->J The…
lost Coder
  • 577
  • 2
  • 8
  • 34
4
votes
1 answer

Determining if a graph has a cycle without using DFS

I came around one of those questions in my exams: Topologocial sorting using Kahn's Algorithm requires the graph to be DAG (Directed Acyclic Graph). How can we determine if a graph contains no cycles without using DFS/BFS first? I am trying…
Theocharis K.
  • 1,281
  • 1
  • 16
  • 43
4
votes
2 answers

Directed graph property/ depth first search

To my knowledge, it is possible that if a path from vertex "a" to vertex "b" exists on some arbitrary directed graph, that there can be some circumstance where, using a depth first search on the graph, that vertex "b" can be discovered in the search…
4
votes
2 answers

C++: Fast algorithm for finding minimum sum of cycle edges

I have an undirected weighted graph and want to find the minimum sum of all cycles edges. That means if I have no cycle, the answer is 0. If I have one cycle the answer is the minimum edge weight of that cycle. And if I have more than one cycle it's…
Timotheus
  • 111
  • 7