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

Difference between BFS and DFS

I am reading about DFS in Introduction to Algorithms by Cormen. Following is text snippet. Unlike BFS, whose predecessor subgraph forms a tree, the predecessor subgrpah produced by DFS may be composed of several trees, because search may be…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
5
votes
0 answers

Problem with DFS graph algorithm, wrong loops found

I want to create an algorithm to understand how many closed areas there are in a graph with the relative points, at the moment the problem is that it finds almost all the loops, using a DFS algorithm. However, a problem arises This is my actual…
Klayser
  • 334
  • 4
  • 12
5
votes
1 answer

Topological Sort Kahn's algorithm BFS or DFS

Is Topological sort's method BFS or DFS, Which is right? (I think BFS is right but some site said DFS and some said BFS. I'm confused...) Is Kahn's algorithm same to the BFS(or DFS)? or BFS(or DFS) is just tool for Kahn's algorithm?
5
votes
3 answers

Fingerprint tree generation

There is group of people [let's say 1874 of them], all representing different companies [lets say 236 of them] in the world. My task is to best identify what company each person works for. The trick is that I cannot simply ask a person "Where do you…
5
votes
0 answers

What is the most efficient way to write a recursive method that needs access to a variable in Python?

I'm fairly new to Python and wanted to implement some graph algorithms just for practice. So I implemented Depth First Search both iterative and recursively and they work fine. But in the examples I found in the web I saw different approaches for…
optimum
  • 51
  • 3
5
votes
1 answer

Why is the time complexity of DFS to detect a cycle in an undirected graph O(|V|) and not O(|V| + |E|)?

Can anyone explain to me in detail why and how the upper bound of DFS to detect a cycle in an undirected graph be O(|V|) ?
Sazz
  • 57
  • 7
5
votes
1 answer

Find maximal subgraph containing only nodes of degree 2 and 3

I'm trying to implement a (Unweighted) Feedback Vertex Set approximation algorithm from the following paper: FVS-Approximation-Paper. One of the steps of the algorithm (described on page 4) is to compute a maximal 2-3 subgraph of the input graph. To…
5
votes
1 answer

Articulation Point Algorithm - Back Edge Identification

I am going through Tarjan's algorithm for finding articulation point in a graph using DFS. https://www.geeksforgeeks.org/articulation-points-or-cut-vertices-in-a-graph/ Some notations: low[] : It is an array of N elements which stores the discovery…
5
votes
1 answer

Different output from dfs iterative and dfs recursive

This program is for dfs traversal of graph one function is a iterative method and another function is a recursive method but both are giving different answer from iterative i am getting 01234 from recursive i am getting 02341 can any one explain me…
user10628441
5
votes
3 answers

Performing depth-first algorithm from a specific vertex

I am trying to find a way to perform the depth-first algorithm from a specific vertex by using the boost graph library. The depth-first algorithm provided by Boost library evaluates the graph beginning from the start vertex to the last vertex. But…
sa11
  • 123
  • 1
  • 6
5
votes
3 answers

Find number of unique routes to specific node using Depth First Search

I have a directed graph with vertexes 123456. Using a depth first search, if I wanted to be able to find the number of unique routes from 1-4 (for example), how would I go about doing it? Here is my current DFS. private final Map
spogebob92
  • 1,474
  • 4
  • 23
  • 32
5
votes
1 answer

Depth-First Search on graph

I've been doing a tutorial in OCaml. The tutorial taught me how to do a breadth-first search on a graph, which I can implement fine. However, I've been struggling with an algorithm for a depth-first search, which is one of those things where the…
jpabene
  • 129
  • 2
  • 13
5
votes
3 answers

Finding paths in (DAG) directed acyclic graph given destination

Let's say I have this array: let reportStructure = [|(2, 1); (3, 2); (4, 2); (5, 3); (6, 4); (7, 3)|] where the first int in a tuple reports to the second int. I can map that really easily with let orgMap = Map.ofArray reporting From there, I…
Steven
  • 3,238
  • 21
  • 50
5
votes
1 answer

Topological Sort using Iteration instead of Recursion

I initially had this recursive solution for a topological sort: void dfs(int v, bool visited[], node* adjList[], std::stack &myStack) { visited[v] = true; node* curr = adjList[v]; while (curr != NULL) { …
Jon
  • 359
  • 1
  • 2
  • 10
5
votes
2 answers

Most efficient algorithm to generate a random seating chart for benches?

I'm writing an app for a family member who is a teacher. She asked for an app to allow her to enter a bunch of kids, set their handedness, set who they can't sit next to, specify how many seats per bench, and then generate a random layout for the…