Algorithms DFS depth of any DFS (Depth First Search) tree - algorithm

The depth of any DFS (Depth First Search) tree rooted at a vertex is atleast as depth of any BFS tree rooted at the same vertex. True or False?
I don't understand what this statement means depth of what? Is it asking for stack depth ?
Please explain with the example

Depth of the tree. Meaning, the maximal length of the shortest path from the root to another node in the tree.

The depth of a (search) tree is the length of the longest path (expressed in number of edges) from root to leaf that such a tree has.
The question is asking you to compare two search algorithms, DFS and BFS, and more specifically their search trees, which start at the same vertex.
Here is an example graph (it is undirected):
Let's say the chosen root vertex is "a". We can imagine a DFS that visits vertices in the following order: a-b-c-d-e. At e there is no more neighbor that has not yet been visited, so the DFS algorithm backtracks to d and then extends to f. It then backtracks again all the way to the root, only to find there is no other vertex to visit.
So the search tree of DFS is:
The longest paths have 4 edges, so we say that the depth of this search tree is 4:
a-b-c-d-e
a-b-c-d-f
Let's do the search with BFS. In a first round all the neighbors of a are visited:
a-b
a-d
a-e
Then each of these paths are extended to neighboring vertices that are not visited yet:
a-b-c
a-d-f
At e there is no more extension possible. All vertices have been visited. The BFS search tree is as follows:
BFS has thus found these paths, of which the longest have 2 edges, so the depth of this search tree is 2:
a-b-c
a-d-f
a-e
So in this case the depth of the DFS search tree is greater than the depth of the BFS search tree.
It is up to you to prove that you can never have the situation where the BFS search tree is deeper than the one of the DFS search tree (for the same graph and same starting vertex).

Related

how to take tree roots in a DFS forest?

A DFS starting at some vertex v explores the graph by building up a
tree that contains all vertices that are reachable from v and all
edges that are used to reach these vertices. We call this tree a DFS
tree. A complete DFS exploring the full graph (and not only the part
reachable from a given vertex v) builds up a collection of trees, or
forest, called a DFS forest.
If I have a directed graph and I need to find the roots of these trees that are part of the dfs forest, do I have to modify the dfs algorithm to do this?
To find the roots of the trees in a directed graph, loop over the nodes and list any that have out edges but no in edges
Like this:

How to describe an algorithm to decide whether T is a depth-first spanning tree rooted at s?

Let's Suppose we have a connected graph G, a start vertex s, and a spanning tree T of G and G is undirected. How can I describe an algorithm to decide if T is a depth-first spanning tree rooted at s or not?
All DFS trees T for an undirected graph G have the following property:
{u, v} is an edge in G if and only if u is an ancestor of v in T or v is an ancestor of u in T.
To see why, assume without loss of generality that u is visited before v in the DFS. When building the DFS tree node for u, we will either (1) choose to visit node v as a neighbor of u, making node u a parent of node v, or (2) starting at node u we will visit some other neighbor z, and in recursively exploring z we will visit v, in which case u is a parent of z and z is an ancestor of v.
Moreover, we can make a stronger claim: any tree meeting the above criterion is a DFS tree for some DFS tree of G. Here’s how to see this. Start with the root node of T and look at its children. Given any two subtrees of the root, none of the nodes in those subtrees can be adjacent to one another in G, since otherwise by the above property one of those nodes would have to be an ancestor of the other. Therefore, each subtree consists of a set of nodes that are all reachable from one another via paths that only involve the nodes within that subtree. We can then recursively assemble one possible DFS ordering by starting at the root, recursively building DFS trees for the subgraphs represented by the subtrees in any order we’d like, and gluing those DFS orders together.
With this observation in mind, we can check very quickly with a second DFS whether T can be a DFS tree rooted at s, tracking which nodes have been visited as the DFS runs. After all children of a node v have been processed, check whether all the neighbors of v in graph G have been visited. If so, great! If not, it means that some neighbor of v is neither an ancestor nor a descendant, and the tree isn’t a DFS tree. If this process terminated without finding any violations, the process itself traces out a DFS of G using the edges of T, so T is definitely a valid DFS tree.
This algorithm runs in time O(m + n), which is as fast as possible here. After all, if you don’t look at all the nodes or edges of G, you can’t be sure whether the tree is a valid DFS tree because you can’t check the core property listed above.

Does a DFS produce a MST for an unweighted directed graph?

I'm getting confused from reading online posts. I know that a BFS traversal on an unweighted directed graph will produce a minimum spanning tree and shortest path. Can a DFS traversal on an unweighted directed graph do the same?
Yes, Breadth-First and Depth-First both yield spanning trees. It doesn't make much sense to discuss "minimum spanning tree" for an unweighted graph, because all spanning trees on a given graph with n vertices have the same number of vertices (n) and the same number of edges (n-1).
No, Depth-First does not guarantee shortest path. You really need Breadth-First for that. Consider a cyclic graph:
a - h - g - f
| |
b - c - d - e
Starting from vertex a, there are two possible results to depth-first search: a->b->c->d->e->f->g->h, and a->h->g->f->e->d->c->b. The former returns a very long path from a to h, and the latter returns a very long path from a to b.
Note that in this example, the graph is undirected. But undirected graphs are a special case of directed graphs; you can replace every undirected edge by two directed edges in opposing directions.

Which graph has same Breadth-First traversal and Depth-First Traversal?

I was just wondering if it is possible for two graphs to have the same breadth-first and depth-first traversal. The graph may be directed or undirected.
A connected, undirected graph where there are N nodes and (N-1) edges, and where there is only two nodes with the degree 1 (i.e. the other (N-2) nodes would have the degree 2) could have the same visiting order for BFS and DFS provided that you start from one of the nodes with degree 1. If you start from one of the nodes with rank 2, the traversal order would differ.
Thinking of linearly traversing through a singly linked list may help you visualize it better.

Does for a given graph G, both DSF and BFS will be same only when the graph is not cyclic in nature?

Does for a given graph G, both depth first search and breadth first search will be same only when the graph is not cyclic in nature ??
It is easier to explain and visualize in terms of a rooted tree.
In the case of BFS, we will visit the tree 'level-by-level'. All nodes of a level will be printed before the next level is visited.
In the case of DFS, we will go through all levels of a node to the leaf before we visit the next. So it's more of a 'strand-by-strand' traversal.
If you visualize the two, it is clear that BFS and DFS traversal are identical when the graph is such that each node has one incoming and outgoing edge, except for the end nodes which have one edge. Hence, the graph should be of the form A_4->A_3->A_4..->A_N. Basically, there is exactly one strand of DFS here.
The other possibility is a star-configuration, i.e., one root whose children are all leaves. There the strand-by-strand and level-by-level approaches are identical. Basically there is one level of BFS here.
I doubt there will be a case where there are both multiple levels of BFS and multiple strands of DFS where the identical traversal will hold.
And in either of these cases, the graphic is not cyclic. So we can conclude (not a rigorous proof) that if the BFS and DFS traversals are identical, the graph is acyclic.
Note that there are a number of graphs (any tree which does not fit into the above patterns) different from the above cases where the DFS and BFS traversals are different. Hence the converse of the statement is not true.

Resources