how to take tree roots in a DFS forest? - algorithm

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:

Related

Two BFS produce the same set of edges

Under what kind of constraints, will two BFS (could start from different vertex) on a simple undirected graph produce the same set of edges?
If the graph is a minimum spanning tree, then it will produce all the edges in the breadth-first search which can start from any vertex.
Reason:
In BFS, all nodes are going to be visited.
The minimum spanning tree contains a minimal number of edges to connect all the nodes. So, the BFS traversal will traverse all the edges in order to visit all the nodes.
So, all the edges of the graph are in the set. That means if you start BFS from any vertex, all the edges are going to be included in the set. Thus, the same set of edges for any node.
Say you run BFS on graph G with starting vertex v (BFS(G, v)) and there is some edge e = (u,w) that isn't traversed. Running BFS(G, u) guarantees that (u,w) is traversed. Thus BFS will only produce a unique set of edges when it produces all edges. I.e. in acyclic graphs.

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

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).

Spanning Tree VS. Spanning Forest

What's the difference between a Spanning Tree and a Spanning Forest in graphs, conceptually.
Also, is it possible to construct a Spanning Forest through DFS or BFS traversals? Why? How?
I understand the Spanning Tree, but I couldn't find any clear explanations about spanning forests. Even Wikipedia (https://en.wikipedia.org/wiki/Spanning_tree), doesn't give a clear definition about it.
My book (Data Structures & Algorithms, Wiley - sixth edition) also has no definition for spanning forests.
I was wondering, if we have a graph with for example three connected components in it, is it possible to construct a spanning forest by DFS/BFS traversals?
When there is only one connected component in your graph, the spanning tree = spanning forest.
But when there are multiple connected components in your graph. For example in following picture we have 3 connected components.:
So for each component, we will have a spanning tree, and all 3 spanning trees will constitute spanning forest
I was wondering, if we have a graph with for example three connected
components in it, is it possible to construct a spanning forest by
DFS/BFS traversals?
Yes it is possible. When there is only 1 connected component, your BFS or DFS will terminate visiting all the vertices and you will have a spanning tree (which in this case is equal to spanning forest).
But when you have more than 1 connected component, like in the picture, the only thing you have to do is start another BFS or DFS from an unvisited vertex. Your algorithm terminates when there is no unvisited vertex left and each BFS or DFS traversal will yield a spanning tree.
Non-trivial spanning forests can be constructed even for complete graphs via the following algorithm:
preconditions
all vertices are unmarked
the edges are enumerated from 1 to m
edge processing
if both of its adjacent vertices are marked, skip it because then that edge would either merge trees of the forest or creates a cycle in one of its trees
else mark its unmarked adjacent vertices
algorithm
process the edges in the order of their enumeration
explanaton:
while it is feasible in the construction of spanning trees to add edges that "bridge" connected components, those edges are not added in the above algorithm.
interpretation:
if the edges are enumerated according to ascending length, the edges of the resulting spanning forest will be a subsets of the MST and the trees of the forest will resemble "basins" i.e. the length of edges is smallest for the one that created the connected component and increases with every edge that is attached in later steps.
In that case the properties of spanning forest may provide insight into the structural properties of the original graph and/or be utilized in algorithms.

Edges in a DFS forest

I came across the following question.
The DFS forest generated by DFS on a DAG will have the following edges
A)BACK EDGES B)CROSS EDGES C)FORWARD EDGES D)NONE OF THESE
The answer was options B and C.Please explain how C is the answer.
In a DAG, multiple nodes can point to the same node, thus resulting in a cross edge or a forward edge in the DFS forest. Back edges in a DFS forest would indicate loops in a graph, so a DFS forest of a DAG never has any back edges.
It's just the basic functionality of DFS, really.
http://en.wikipedia.org/wiki/Depth-first_search

Visiting edges, vertices in an undirected graph

Problem: You have an Undirected graph G = (V, E) (V = vertices, E = edges) and you must visit each vertex and pass each edge in both directions.
The only algorithms I know for graphs are DFS, BFS, and a few MST's (Kruskal, etc.) My friend and I were discussing this problem, if it were directed I would simply DFS, and then DFS the transpose but the graph is unfortunately undirected. My friend proposed we perform an MST and DFS the MST and then find the remaining edges by iterating through those that arent in MST. I sort of see what he means but I am not sure this is a good approach? opinions? Also, how would I be able to pass by an edge in both directions if it is undirected?
It doesn't matter if the graph is directed or undirected. You could just replace every undirected edge with two directed edges and perform whatever algorithm you have for a directed graph. Both DFS and BFS will traverse the whole vertices and edges.
I think what you are looking for is called Graph Traversal. BFS and DFS are two graph traversal algorithms and they do not require the graph to be directed. MST on the other hand, is not a graph traversal algorithm.

Resources