Questions about graph traversal algorithms such as BFS and DFS.
Questions tagged [graph-traversal]
510 questions
87
votes
8 answers
Graph searching: Breadth-first vs. depth-first
When searching graphs, there are two easy algorithms: breadth-first and depth-first (Usually done by adding all adjactent graph nodes to a queue (breadth-first) or stack (depth-first)).
Now, are there any advantages of one over another?
The ones I…
malexmave
- 995
- 1
- 7
- 9
43
votes
9 answers
Algorithm to find diameter of a tree using BFS/DFS. Why does it work?
This link provides an algorithm for finding the diameter of an undirected tree using BFS/DFS. Summarizing:
Run BFS on any node s in the graph, remembering the node u discovered last. Run BFS from u remembering the node v discovered last. d(u,v) is…
curryage
- 541
- 2
- 7
- 8
24
votes
5 answers
The purpose of grey node in graph depth-first search
In many implementations of depth-first search that I saw (for example: here), the code distinguish between a grey vertex (discovered, but not all of its neighbours was visited) and a black vertex (discovered and all its neighbours was visited). What…
user6805
- 343
- 1
- 3
- 7
20
votes
3 answers
Difference between cross edges and forward edges in a DFT
In a depth first tree, there are the edges define the tree (i.e the edges that were used in the traversal).
There are some leftover edges connecting some of the other nodes. What is the difference between a cross edge and a forward edge?
From…
soandos
- 1,143
- 2
- 10
- 23
18
votes
3 answers
Steps that guarantee exiting a maze
Given a 2-dimensional maze where you can give 4 commands "move up/down/right/left". Knowing the maze but not where the person is, how to find the minimum sequence of commands that guarantees exiting the maze? I'm looking for a single sequence of…
seilgu
- 281
- 1
- 3
15
votes
2 answers
Shortest non intersecting path for a graph embedded in a euclidean plane (2D)
What algorithm would you use to find the shortest path of a graph, which is embedded in an euclidean plane, such that the path should not contain any self-intersections (in the embedding)?
For example, in the graph below, you want to go from $(0,0)…
Realz Slaw
- 6,251
- 33
- 71
14
votes
2 answers
Number of possible search paths when searching in BST
I have the following question, but don't have answer for this. I would appreciate if my method is correct :
Q. When searching for the key value 60 in a binary search tree, nodes containing the key values 10, 20, 40, 50, 70, 80, 90 are traversed, not…
avi
- 1,473
- 4
- 24
- 39
12
votes
3 answers
Why is DFS considered to have $O(bm)$ space complexity?
According to these notes, DFS is considered to have $O(bm)$ space complexity, where $b$ is the branching factor of the tree and $m$ is the maximum length of any path in the state space.
The same is said in this Wikibook page on Uninformed…
user20691
12
votes
3 answers
Correctness of Strongly Connected Components algorithm for a directed graph
I have been reading up on algorithm for finding the strongly connected components in a directed graph $G=(V,E)$. It considers two DFS search and the second step is transposing the original graph $G^T$.
The algorithm is the following :
Execute DFS…
Geek
- 1,191
- 1
- 10
- 12
12
votes
3 answers
What is the meaning of 'breadth' in breadth first search?
I was learning about breadth first search and a question came in my mind that why BFS is called so. In the book Introduction to Algorithms by CLRS, I read the following reason for this:
Breadth-first search is so named because it expands the…
SherlockHolmesKePapa
- 243
- 2
- 8
11
votes
1 answer
Graphs that cause DFS and BFS to process nodes in the exact same order
For some graphs, DFS and BFS search algorithms process nodes in the exact same order provided that they both start at the same node. Two examples are graphs that are paths and graphs that are star-shaped (trees of depth $1$ with an arbitrary number…
mrk
- 3,748
- 23
- 35
10
votes
2 answers
Linear-time algorithm to find an odd-length cycle in a directed graph
Problem: Give a linear-time algorithm to find an odd-length (directed) cycle in a directed graph. (Exercise 3.21 of Algorithms by S. Dasgupta, C. Papadimitriou, and U. Vazirani.)
The related post@cs.stackexchange asks for the existence of an…
hengxin
- 9,671
- 3
- 37
- 75
10
votes
2 answers
What does pre-, post- and in-order walk mean for a n-ary tree?
The tree traversal methods explained in this Wikipedia article are pre-order, post-order and in-order. Are these methods limited to binary trees? The algorithm seems to be defined in terms of left and right child. If it can be used for n-ary trees,…
Renae Lider
- 215
- 2
- 8
10
votes
6 answers
Why does DFS only yield tree and back edges on undirected, connected graphs?
Prove that if G is an undirected connected graph, then each of its edges is either in the depth-first search tree or is a back edge.
Now, from intuition and in class lectures by Steven Skiena, I know that the above holds true, since it dives all the…
Algorist
- 191
- 1
- 2
- 5
10
votes
5 answers
Can the pre-order traversal of two different trees be the same even though they are different?
This question pretty much explains that they can, but does not show any examples of there being two different trees with the same pre-order traversal.
It is also mentioned that the in-order traversal of two different trees can be the same though…
SDG
- 403
- 1
- 5
- 13