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
Related
I try to explain my problem better.
in input I have the number of nodes N and the number of edges P.
N represent the cities while P the water pipes.
So for example in input I have 4 and 2 {0,1,2,3,4} would be my graph
and the 2 represents me the already existing water pipes
since in the following lines of the file I will have the already
existing connections so since in this case I have the 2 for example in
the file I'll have 3-1 and 2-1. so at the beginning I will have a
graph with 5 vertices and an edge that goes from node 3 to node 1 and
another arc that goes from 2 to 1.
now I have to determine how many and which connections to make to
bring water to all the cities. so for example in this case a solution
could be 0-4,0-3,0-2.
NOTE 0 represents the dam basin.
I was thinking that this problem seems to me very similar to MST but the graph I have is oriented and not weighted so I cannot apply the Prim or Kruskal algorithm.
So since it doesn't make sense to apply MST maybe I could do this via a DFS or a BFS but I don't quite understand how.
I believe that if I do a dfs from the source then from node 0 and I see all the nodes that I have not been able to reach and I connect these nodes to the source I have the solution but I have not understood how to do it.
or would it be better to try to make l mst work on this type of graph then cloning it and adding weights = 1 to the arcs?
" I know I can't apply it for the type of graph I have"
How do you know this?
Unweighted. Make your graph weighted by applying a weight of 1 to every edge.
Undirected. Make your graph directed by replacing every edge with two directed edges, one going each way.
I have been stuck on this problem for two days now and still no progress. Basically the problem is as follows:
Given an undirected simple weighted and connected graph, we have to find the shortest walk from a given source to a given destination while visiting at least one vertex from a given set, A and at least one vertex from the set B with the added constraint that the vertex from set B should always come after visiting the vertex from set A.
Set A and B are disjoint and there can be vertices in the graph which neither belong to A nor B. There is a single source and destination vertex.
I tried decomposing the shortest path into one which visits a vertex, v in A from source, then from v to another w in B and then from w to destination. This can be done using 3 passes of Dijkstra with different starting vertices respectively. But, I would have to choose the minimum such v resulting in O(VElog(V)) complexity where V = number of vertices and E = number of edges.
I am terribly stuck on how to do this in O(E*log(V)), since the question asks so, i.e. using only O(1) Dijkstra runs. Can anybody please help me?
Edit: We can't construct a new graph or modify it as some people are suggesting to construct a level graph.
I have to modify the Dijkstra routine somehow to solve this.
Graph. Blue vertices are the set A, purple ones set B. Home is 0 and Destination is 8
In this graph(see link) for example, the shortest walk should be: 0 -> 1 -> 0 -> 3 -> 6 -> 7 -> 8 with total cost = 6
The easiest (to me) way to solve such problems is to separate graph vertices into "levels" (like stories in a building) and possibly replicate some vertices between more than one level.
In your case, there will be 5 levels, levels 1, 3 and 5 containing all vertices while level 2 only has A vertices and level 4 only has B. The start vertex is at level 1 and the end is at level 5. The original edges are replicated within each level and between adjacent levels.
Any path in the modified graph passes through an A vertex and after that through a B vertex, because any path must pass through all 5 levels in order.
This arrangement does not care if there are additional A and B vertices in any order, in addition to the mandatory pair in the required order (so x-x-x-A-B-A-B-x-x-x is allowed). If you need to exclude those, remove all B vertices from levels 1 and 3, and all A vertices from levels 3 and 5.
As #n.'pronouns' m. pointed out, we can solve this problem by graph layering.
Specifically for my case, just add a new source vertex and add edges from this source vertex to all the edges of vertices belonging to A of original graph. The weight of these edges would be the same as the shortest path found from original source to this vertex in original graph.
Similarly, you add a new destination vertex and add edges from all B vertices to this new vertex and the weight of the edge is again the shortest path from the B vertex to original destination vertex in original graph.
Now if you run Dijkstra from new source to new destination, you see that atleast one A vertex would be visited before B vertex finally ending at new destination. This path is indeed the shortest path.
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).
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.
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.