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

non recursive dfs (when to mark it visited?)

So I recently implemented a non-recursive version of DFS. Turns out that I can mark the nodes "visited" as soon as they are pushed on the stack or when they are popped out. The problem which I was working on specifically stated to mark it "visited"…
6
votes
2 answers

DFS algorithm in Python with generators

Background: I was working on a project were I needed to write some rules for text processing. After working on this project for a couple of days and implementing some rules, I realized I needed to determine the order of the rules. No problems, we…
oz123
  • 27,559
  • 27
  • 125
  • 187
6
votes
1 answer

DFS on undirected graph complexity

Let's say I have an undirected graph with V nodes and E edges.If I represent the graph with adjacency lists,if I have a representation of an edge between x and y,I must also have a representation of the edge between y and x in the adjacency list. I…
Emil Grigore
  • 929
  • 3
  • 13
  • 28
6
votes
1 answer

Ideal algorithm for finding all paths in a graph

Let's take this graph for example: Now let's say I'm starting at vertex 3 and want to find vertex 7. Depth first search (depending on the implementation) will look at the children first. Now, in our example, for the sake of the argument, I start…
None None
  • 537
  • 2
  • 8
  • 15
6
votes
4 answers

finding all vertices that are a part of a simple cycle

Is there a way to find all vertices that are a part of a simple cycle in a graph in a linear time? I found a way to do it in O(|V|(|V|+|E|)) time but was wondering if there is a way to do it faster?
Amnon
  • 195
  • 3
  • 12
6
votes
3 answers

Why is my logic not working correctly for SPOJ TOPOSORT?

The given problem is http://www.spoj.com/problems/TOPOSORT/ The output format is particularly important as : Print "Sandro fails." if Sandro cannot complete all his duties on the list. If there is a solution print the correct ordering, the jobs to…
6
votes
1 answer

Why use DFS to find cycles in an undirected graph and topological sorting to find cycles in a directed graph?

For undirected graphs , if we need to find a cycle , we use depth-first search as described in this older question,which is a well known method and also optimal . But for directed graph, this other question suggests using topological sorting. My…
Spandan
  • 2,128
  • 5
  • 25
  • 37
6
votes
1 answer

Performing DFS and BFS on a directed graph

Suppose we have a graph such as: If you wanted a path from 0 to 5, in what order will we visit the nodes if we perform DFS and BFS on this graph (assume the lowest element is always pushed first). I'm having trouble conceptualizing how the…
Bob John
  • 3,688
  • 14
  • 43
  • 57
6
votes
2 answers

Closest pair of points algorithm variation

I know this may be a duplicate, but it seems like a variation on the 'Closest pair of Points' algorithm. Given a Set of N points (x, y) in the unit square and a distance d, find all pair of points such that the distance between them is at most…
Fernando
  • 7,785
  • 6
  • 49
  • 81
6
votes
2 answers

How to find longest path in graph?

We are given an Adjacency list of the form U -> (U,V,C) -> (U,V,C) ... U2 -> ... U3 -> ... . . etc (U,V,C) means there's an edge from U to V with cost C. The given Adjacency list is for a single connected tree with N nodes thus containing…
6
votes
2 answers

DFS Algorithm Traversal

Given the above graph, the DFS traversal with a stack gives me the folowing path... 1-2-3-4-5-6 Is the above path valid? Does there exists another path? (my textbook gives me 1-2-3-6-4-5) I don't have enough rep to post images over at computer…
user1234440
  • 22,521
  • 18
  • 61
  • 103
6
votes
1 answer

Maintaining iterator in Boost::graph while performing DFS

Most of the examples for the Boost:graph library perform depth-first-search by calling boost's depth first search utilities. After creation of the vertices and edges, calling DFS on the graph traverses the entire graph in a depth-first manner and if…
sc_ray
  • 7,803
  • 11
  • 63
  • 100
6
votes
2 answers

Why is BFS better suited to parallelization than DFS?

I've read that the BFS algorithm is better suited to parallel implementation than DFS. I'm having trouble grasping the intuition of why this should be true. Can anyone explain? Thanks
6
votes
1 answer

graph - How to avoid reprocessing same edge twice in Depth First Search?

The Algorithm Design Manual describes BFS and DFS quite well. The code for dfs in the book has a problem when deciding whether or not to avoid double processing edges. I found the Errata and applied the errata to the code, but still I think the…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
5
votes
4 answers

DFS: How to indicate the nodes of the connected components in C++

I am making a problem of ACM competitions to determine the number of connected components that have an undirected graph G and vertices belonging to each component. 've Already done with a DFS algorithm, counting the number of connected components of…
franvergara66
  • 10,524
  • 20
  • 59
  • 101