Edges in a DFS forest - depth-first-search

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

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:

Having trouble figuring out this DFS riddle

one of the df trees consists of only one vertex v though v has both
incoming and outgoing edges without a self-loop.
Write G and its df forest.
How can a vertex possibly have outgoing edge, but be the only vertex in the tree? Do cross edges not count, or is there a more clever solution?
A DFS tree usually is defined to just contain the nodes that were visited by forward edges, not cross edges. As a hint: if there's an edge (v, u) and DFS is started on u, then u won't appear in v's DFS tree.
Hope this helps!

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.

Give an order for deleting vertices from a graph such that it doesn't disconnect the graph

This is a question from Algorithm Design by Steven Skiena (for interview prep):
An articulation vertex of a graph G is a vertex whose deletion disconnects G. Let G be a graph with n vertices and m edges. Give a simple O(n + m) that finds a deletion order for the n vertices such that no deletion disconnects the graph.
This is what I thought:
Run DFS on the graph and keep updating each node's oldest reachable ancestor (based on which we decide if it's a bridge cut node, parent cute node or root cut node)
If we find a leaf node(vertex) or a node which is not an articulation vertex delete it.
At the end of DFS, we'd be left with all those nodes in graph which were found to be articulation vertices
The graph will remain connected as the articulation vertices are intact. I've tried it on a couple of graphs and it seems to work but it feels too simple for the book.
in 2 steps:
make the graph DAG using any traversal algorithm
do topology sort
each step finishes without going beyond O(m+n)
Assuming the graph is connected, then any random node reaches a subgraph whose spanning tree may be deleted in post-order without breaking the connectedness of the graph. Repeat in this manner until the graph is all gone.
Utilize DFS to track the exit time of each vertex;
Delete vertices in the order of recorded exit time;
If we always delete leaves of a tree one by one, rest of the tree remain connected. One particular way of doing this is to assign a pre-order number to each vertex as the graph is traversed using DFS or BFS. Sort the vertices in descending order (based on pre-order numbers). Remove vertices in that order from graph. Note that the leaves are always deleted first.

Subgraph of a graph given a start vertex

I want to get a subgraph of a graph given the vertex to start at. All vertices connected to the starting vertex are considered part of the subgraph that should be returned.
I've already solved this requirement but am curious if there is a more efficient solution. The solution I came up with was to do a DFS of the graph and record every vertex that was encountered in a set, S. Then, I simply took all of the edges from the original graph that were connected to a vertex in S and I built a subgraph from it. The edges in the original graph are stored in a C# Dictionary which I believe is basically a hash.
DFS and BFS do not work because if you have two vertices that both have the same child, BFS or DFS will not traverse one of those edges. Hence the subgraph in that case would contain all of the correct vertices, but be missed some edge pairs.
Is there a better solution than the one I've come up with?
I think a BFS traversal is the most efficient algorithm for this.
If you do a BFS and enqueue all neighbors for each node (i.e. traverse all the edges attached to the current node) and only abort traversal when the current node has already been visited, you avoid the problem you described with "same child" / "missed edges".
a "fast" algorithm that enumerates all induced subgraphs of
given size can be found here:
http://theinf1.informatik.uni-jena.de/~wernicke/motifs-wabi2005.pdf
does this help?

Resources