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

Finding the cheapest path with ignoring one cost

I want to find the cheapest path between 2 vertices and I can choose one path which I can go for free., e.g: Cheapest path between vertices 1 and 6 is 1-3-4-5-6 - I go on edge 1-3 (cost 30) for free and it gives me total cost 21. Is there any other…
Paulina
  • 483
  • 3
  • 13
5
votes
1 answer

Depth-first search in CUDA / OpenCL

I'm half-way through implementing parallel depth-first search algorithm in MPI and I'm thinking about trying to also do it in CUDA / OpenCL, just for fun / out of curiosity. The algorithm is simple but not trivial. The single-core version in C is…
fhucho
  • 34,062
  • 40
  • 136
  • 186
5
votes
1 answer

Is every bridge in a graph an edge in DFS search tree?

A question from Skiena's book of algorithm: Suppose G is a connected undirected graph. An edge e whose removal disconnects the graph is called a bridge. Must every bridge e be an edge in a depth-first search tree of G? My solution so far (need…
Jake
  • 16,329
  • 50
  • 126
  • 202
5
votes
2 answers

Even length path algorithm

I was asked for my homework to write an efficient algorithm that finds all the vertices in a directed graph which have even length of path to them from the given vertex. This is what I thought of: (It's very similar to "Visit" algorithm of…
Chen Saranga
  • 51
  • 1
  • 4
4
votes
1 answer

Understanding Wikipedia code for a generic depth-first tree search?

I was brushing up on different tree traversal methods and ended up reading the following Wikipedia article. As expected, there are three methods of depth first traversal for a binary tree: Preorder traversal Postorder traversal Inorder…
batbrat
  • 5,155
  • 3
  • 32
  • 38
4
votes
3 answers

Should I use breadth first or depth first for searching a filesystem for a predetermined number of errors?

I have a large filesystem that I need to traverse for errors. Each file knows whether or not it contains an error, so I simply need to travel to each node and check whether there is an error there. Also, each directory knows the total number of…
ewok
  • 20,148
  • 51
  • 149
  • 254
4
votes
2 answers

Football Guaranteed Relegation/Promotion Algorithm

I'm wondering if there is a way to speed up the calculation of guaranteed promotion in football (soccer) for a given football league table. It seems like there is a lot of structure to the problem so the exhaustive solution is perhaps slower than…
rhaskett
  • 1,864
  • 3
  • 29
  • 48
4
votes
1 answer

Directed Graph: Find special path without backedge

I need to find a path (requirements see below) in a directed graph, whose nodes can be categeorized into layers. These layers are important for the structure of the graph (properties of graph see below) Example of such a layered directed…
4
votes
2 answers

Is it possible to do a depth first search iteratively without copying visited nodes?

Background I am searching a 2D grid for a word. We can search left/right and up/down. For example, in this grid, searching for "abef" starting at (0,0) will return True Example (grid1): Where I'm at The recursive version gives expected results…
user1594322
  • 2,008
  • 3
  • 19
  • 16
4
votes
2 answers

Design a O(V+E) algorithm for determining at what point a graph becomes disconnected when deleting edges sequentially from a list of edges S

You are given a connected graph G and a series of edges S. One at a time, an edge from S is removed from G. You then check to see if G is still connected. If G is no longer connected, you return the edge. Otherwise, you remove the edge from the…
Clayton C.
  • 863
  • 1
  • 8
  • 17
4
votes
0 answers

How to overcome TLE (time limit exceeded ) for python in CSES problem Counting rooms?

Hello I am trying to solve Counting rooms problem under the graph section in cses problem set. link to the question - https://cses.fi/problemset/task/1192 Its a simple no. of connected components in a graph problem which i am trying to solve using…
4
votes
6 answers

How to convert a nested array with parent child relationship to a plain array?

I have an array with nested objects having parent-child relationship like so: [ {id: 1, title: 'hello', parent: 0, children: [ {id: 3, title: 'hello', parent: 1, children: [ {id: 4, title: 'hello', parent: 3, children: [ {id:…
4
votes
1 answer

For every node of a tree, find the nearest ancestor node such that val[node] is coprime to val[ancestor]

For a shorter ver., only read the paragraphs that immediately follow the BOLD sentences and it reduces to only 3 paragraphs. Problem Statement : Given a tree with N nodes rooted at node 1. Each node is associated with a value. Determine the closest…
4
votes
2 answers

What's the best way to perform DFS on a very large tree?

Here's the situation: The application world consists of hundreds of thousands of states. Given a state, I can work out a set of 3 or 4 other reachable states. A simple recursion can build a tree of states that gets very large very fast. I need…
Yuval
  • 7,987
  • 12
  • 40
  • 54
4
votes
1 answer

When do you add a node to visited in bfs or dfs?

Hey I've been researching BFS/DFS and I noticed that many of them have a slight modification and that is when a node is added to the visited set. In one way, the algorithm would pop the node from the stack/queue and then add it to the visited set.…