Can Breadth First Search be used on Directed Acyclic Graph? - algorithm

Can Breadth First Search be used on Directed Acyclic Graph?
For example, you start with the root node (say it has 3 connected nodes, edges all pointing towards them from the root), following BFS, you visit the first connected node from the root following the directed edge, and you got to come back to the root node and visit the second connected node if it were an undirected graph, but you can't in the case of directed graph, so I assume BFS cannot be used on Directed Acyclic Graph?
Also,a line of nodes as such 1 -> 2 -> 3 -> 4 can be considered as a Directed Acyclic Graph, correct?
Thank

Short answer : Yes
Here is a video example. (This is no Adv, I don't know the guy)
Here is some written courses and examples.
To respond more precisely to your question, if there is no edge from a given node pointing to a root then you don't "have to" go back to the root, like others said in the comments.
The fact that the graph has cycles or not is not important because in BFS algorithms a node is supposed to be marked when you visit it. The mark is then used to not enqueue the node a second time (i.e to visit it only once), so the cycle is some how broken.
Just check the pseudo-code on Wikipedia.
An yes the "line of node" you mentioned is a DAG, but a very special one.
P.S : Sorry to dig up this question but I asked it to myself, searched the internet, saw it, keep searching and find some answers, so I thought it could help somebody else in the future.

1 -> 2 -> 3 -> 4
is a DAG
BFS means breath fast search. if u start bfs from a node u, every node which are reachable from u will be found but those nodes that are not reachable from u are not found.
example G (V,E ) a graph
v={1,2,3,4}
E={(1,2),(1,3),(4,1)}
if u run bfs from node 1 ,node 2 and 3 will be discovered
but 4 will be undiscovered
but if u run bfs from 4 every node will be discovered
So if u know the topological sorting of the DAG u can run bfs from the nodes in the topological order every node will be discovered and their level will be calculated correctly.

Related

I don't understand how this algorithm will tell me if a graph is biconnected

I'm doing some practice for an upcoming interview, and a practice question I found asks for an O(V+E) algorithm to tell if a graph is biconnected. This page from Princeton says that a graph is biconnected if it has no articulation vertices, where an articulation vertex is a vertex whose removal will increase the number of connected components (since a biconnected graph should have one connected component).
One common solution to this problem is to do DFS with additional tracking to see if any of the vertices are articulation vertices. This page says that a vertex is an articulation vertex if
V is a root node with at least 2 children
OR
V has a child that cannot reach an ancestor of V
However, the condition for root nodes doesn't make sense to me. Here is an example of a biconnected graph:
If we choose any of the nodes to be the root, they ALL have 2 or more children, so would be an articulation vertex thus making the graph not biconnected. This is a common algorithm for finding connected components, so assume I have misunderstood something. What do I actually need to check for the root node to see if a graph is biconnected?
You're supposed to do depth-first search, which means that the DFS tree always looks like
1 4
| |
2---3
because you can't explore the 1--4 link until you're done exploring everything that can be reached from 2 without going through 1, and you won't add 1--4 because it makes a cycle with the tree edges. No node in this tree has two children (1 is the root).

Confusions on finding a cycle in a possibly unconnected directed graph

I am confused about this answer. Why can't DFS decide if there is a cycle in a directed graph while visiting each node and each edge at most once? Using the white, gray, black method, one should be able to find a cycle if there is a backward edge.
For an unconnected directed graph, why can't one do the following: run DFS from an arbitrary node v and visit as many nodes as v is connected to, then run DFS on another unvisited arbitrary node in the graph, if any, until all nodes are visited?
It seems to me that DFS should be able to find a cycle if it exists in at most o(|V|+|E|) time. Is this claim in the above mentioned answer wrong?
"It is possible to visit a node multiple times in a DFS without a
cycle existing"
Moreover, as this other answer suggest, if a cycle exists, DFS should find it after exploring a maximum of |V| edges, so the run time is really O(|V|).
What am I missing?
Update and conclusions:
Based on Pham Trung's comments, it looks like the "simple DFS" in that answer refers to a DFS starting from one node in a strongly connected graph. As I understand it, for a general case that the graph might be unconnected, the following statements should be true:
Using DFS and starting from an arbitrary unvisited node in an unconnected graph, it is true that each node might be visited more than once, but using white-gray-black coloring, a cycle -if exists - will be correctly found.
The run time of such a DFS algorithm is O(d.|V|+|E|), where d is the max in-degree among all nodes (i.e. the max time that we can visit each node using such DFS-based algorithm)
Moreover, as this other answer suggest, if after exploring O(|V|) edges, a cycle was not found, it does not exist. So the runtime is really O(|V|).
Imagine we have this simple graph with these edges:
1 -> 3
2 -> 3
1 ----->3
^
|
2--------
So, in our first dfs, we discover node 1 and 3. And, we continue to do dfs with node 2, now, we encounter node 3 again, but is this a cycle? obviously not.
One more example:
1 -> 3
1 -> 2
2 -> 3
1----->3
| ^
| |
| |
v |
2-------
So, starting with node 1, we visit node 3, back to node 2, and now, we encounter node 3 one more time, and, this case, it is not a cycle also.
As far as I understand the simple depth-first-search from Jay Conrod's answer means, a normal, original DFS (only checking for connected component). In the same answer, he also described how to modify simple DFS to find the existence of cycle, which is exactly the algorithm OP has cited. And right below, another answer also mentioned a lemma in the famous Introduction to algorithm book
A directed graph G is acyclic if and only if a depth-first search of G yields no back edges
In short, OP's understanding to detect cycle in directed graph is correct, it is just some complexities and shortcuts have lead to misunderstanding.

How to classify an edge that is not traversed in a depth-first search?

I have an assignment that requires one to perform a depth-first search on a directed graph and to classify all edges of the graph. However, I'm confused as to how one would classify an edge that is not traversed during the course of the depth-first search, seeing as how these edges are classified during the course of a search.
Let me summarize a depth-first search on the above pictured graph.
First we go from 1 to 2. Then we pop 2 off the stack, because there is nowhere to go, so we're back at 1. Then we go from 1 to 3. Next we go from 3 to 4.
Ok, so assuming I got that part right, then all the edges traversed are Tree edges correct? So, how would one classify the edges between 3 to 2 and the edge from 4 to 3?
Your DFS should still traverse the edges from 3->2 and 4->3. They would be a cross edge and a back edge, respectively, IIRC.
You'll only actually push a node onto the stack the first time you see it, but when you do visit it, you look at all of its outgoing edges, whether their destinations have already been visited or not.

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.

What are the articulation points of this directed graph?

5 nodes in this directed graph.
Edges:
1 -> 2
2 -> 3
2 -> 4
4 -> 5
(Graphical image : http://i.imgur.com/hafBv.jpg )
Am I correct in thinking that the articulation points are node 2 and 4 ?
(If you remove node 2 or node 4, the graph becomes disconnected)
But the definition I've seen everywhere says something similar to:
a node u is an articulation point, if for every child v of u, there is no back edge from v to a node higher in the DFS tree than u.
How does this work for a directed graph? For example, Node 3 does not have a back edge to a node higher in the DFS tree than 2. Does this classify Node 3 as an articulation point? But its removal does not cause the graph to be broken into 2 or more pieces (That is my layman definition of an articulation node).
Disclaimer: My memory is vague.
Directed graphs have three kinds of connectedness.
"Strongly connected" if there is a path from every vertex to every other vertex,
"Connected" if there is a path between any two nodes, but not in both directions.
"Weakly connected" if the graph is only connected if the arcs are replaced with undirected arcs.
eg
1->2 , 2->3 , 3->1
Strongly connected, you can get from every node to every other node
1->2 , 2->3
You can't get from 3 to 1 but you can from 1 to 3 so it's connected
1->2 , 3->2
There is no way to get from 1 to 3 or from 3 to 1, so it's only weakly connected.
What nodes are articulation points depends on what kind of connectedness you are considering.
Your graph is only weakly connected since there's no way to get from 3 to 4 or from 4 to 3.
Which would mean that the only way it makes sense to talk about articulation points is if you treat the arcs as undirected. In which case 2 and 4 would be the articulation points, as you said.
Articulation point should always have children and parent. This is something that is missing from your definition and makes nodes 1 and 3 articulation points whilst they are not.
Also note that in your reasoning for node 3 you consider the node itself not its children. If you apply the definition carefully you will see that you should consider the children instead. In your case - empty set and, with the extended by me definition, 3 is no longer articulation point.
Node 3 does not have a back edge to a node higher in the DFS tree than 2
Node 3 doesnt have children, so it cannot be an articulation point (from def).
If we put this definition in the context of a directed tree, then the articular points are all the points except the root and the leaf node
but when we follow the method we used to solve the undirected graph we get the respected (num,low) values for nodes are node-1(1,1)
2 (2,2) ,node 3 (3,3), node 4(4,4).node 5(5,5).
so following the def of articulation point and finding the node with 2 children
and satisfying the rule (low(child)>=num(node))
we get only the node 2 so that is the articulation point .
but the theory can be applied because it is still a connected graph i.e where every node is reachable but when we are finding we had to take care of tree and back edges and the rest is same as that of undirected graph.
The definition of articulation is not as clear as in case of undirected graphs. However if we choose two particular nodes: source and sink and call any point that must occur on the path from source to sink articulation point, the definition is clear.
I call them unavoidable points and have got simple solution for finding them on my GitHub

Resources