An undirected graph G is given as an input.
I need to tell whether it is possible to direct each edge of G so that the resulting
directed graph is strongly connected.
What algorithm should I use?
This is equivalent to determining whether the graph is connected and bridgeless (where a "bridge" is an edge such that, if you removed it, some vertices would become disconnected).
I trust you'll have no difficulty figuring out whether the graph is connected. For determining whether it's bridgeless, you can use Tarjan's bridge-finding algorithm, which will find a bridge if and only if one exists.
Related
I’m familiar with the Hamilton path for a directed graph - visit every node exactly once.
I’m looking for an algorithm to walk the graph so that I visit every node at least once. I can’t find the standard name for this problem, if any.
This graph is walkable - root-a-d-b-c
This graph is not walkable - because in my walk, if I reach c, I have no directed edge to reach a & d and conversely, if I walk to a, d; there’s no directed edge that takes me to b & c
Hope that clarifies the question? Is there a standard name for this type of graph walk and an algorithm to solve it?
Hamiltonian path
Finding at most 2 leafs in the graph
I don't know if there's a name for a directed "walkable" graph, but it's not too hard to determine of a graph is walkable or not:
Find all the strongly connected components using Tarjan's algorithn, for example
Make a new directed graph of the connections between SCCs. This will be a DAG, and your original graph is walkable if and only if this DAG is walkable.
To determine whether or not a DAG is walkable, do a topological sort. Then check that each vertex has an edge to the next.
Each of these steps takes linear time, so you get O(|V|+|E|) complexity for the whole algorithm.
Theorem: a directed graph is walkable if and only if its strongly connected components are totally ordered by reachability.
Proof sketch: (walkable implies condition) The existence of a walk implies that, for each two strongly connected components, a vertex from one or the other appears first in the walk. That component can reach the other. (condition implies walkable) Since there is full connectivity inside a strongly connected component, each is walkable on its own. We just have to concatenate the walks according to the total order, adding the necessary transitions.
The proof is constructive, so we can extract an algorithm.
Algorithm
Compute the strongly connected components.
Concatenate the strongly connected components in topological order. Actually Tarjan's algorithm will list them in reverse topological order, so this doesn't need to be a separate step.
For each adjacent pair in the previous list, use breadth-first search to find a shortest path.
In general, this algorithm does not find the shortest walk (that's NP-hard by reduction from Hamiltonian path).
How to check if a directed graph is eulerian?
1) All vertices with nonzero degree belong to a single strongly connected component.
2) In degree is equal to the out degree for every vertex. Source: geeksforgeeks
Question:
In the given two conditions, is the first one strict? I mean why is it really necessary for the graph to be "strongly" connected graph? What if the graph is just connected?
I learned that condition 1 can be replaced with weakly connected graph. Again, what if the graph is just connected instead of weakly connected?
Will be glad to see some examples.
P.S: Consider condition 2 is always fulfilled in the above discussion. And by "just connected", I mean there exists a vertex in the graph from which all other vertices are reachable.
This is an interesting question. There is, to the best of my knowledge, no standardized meaning of "connected" in the context of a directed graph. The two general notions of connectivity in a directed graph are
strong connectivity, where for any pair of nodes u and v there's a path from u to v and a path from v to u, and
weak connectivity, where the undirected graph formed by ignoring the directionality on the arrowheads is connected.
Your version of the directed graph being "just connected" is slightly different than these definitions, but it's related to strong connectivity. Any directed graph can have its nodes partitioned into strongly connected components (SCCs), groups of nodes that can all reach one another. Those strongly connected components form a DAG, where each strongly connected component is a node and there's an edge from one SCC to another if one of the nodes in the first SCC has an edge to a node in the second SCC.
Your definition of the graph being "just connected" could then be pinned down like this:
"just connected": the DAG of SCCs has a source node that can reach all other nodes.
Notice that "just connected" implies weakly connected, but not the other way around.
It turns out that, in this case, if you have a graph where each node's indegree happens to equal its outdegree, then if the graph is "just connected," then it has an Euler circuit. If your graph is "just connected," then it's weakly connected. Then, we're going to claim that any weakly connected graph with indegrees equal to outdegrees must be strongly connected as well. To see this, pick any SCC in the DAG of SCCs with no incoming edges. Any edge entering any of the nodes in this SCC must come from within that SCC. As a result, if we went through each node in the SCC and added up the number of edges leaving that node, that total would match the number of incoming edges into each node in the SCC. But then, since the sum of indegrees of nodes equals the sum of outdegrees of the nodes, there can't then be any edges starting within this SCC and ending in another, since all edges are accounted for. Therefore, this SCC has no edges leaving it.
We've just shown that any source SCC must have no edges to any other SCCs. And since there is some node in some source SCC that can reach every node, this means that there are no other SCCs in the graph, so the graph has just one SCC and is therefore strongly connected.
I know how to find Articulation points for a undirected graph using DFS variant. But it seems like it is for undirected graph and only looks for back edge. But how to find Articulation points if my graph has forward edge or cross edge.I know i can always run dfs for every node and figure that out but is there any better algorithm.
Definition for Articulation points on directed graph is not unique.
It depends on what kind of connectedness you are considering. There are 3 kind of connections in directed graph
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.
If you are using second definition of connection, then U can use DFS for finding points.
I am not sure, but I think that if you define connection as Weakly connected then you can use DFS too. But it need to be proved.
Agreed that 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
Let G(V,E), an undirected, connected graph with no bridges at all. Describe an algorithm which direct the edges, so the new graph is an SCC.
My suggested algorithm
So we run DFS from an arbitrary vertex. We notice that since it's an undirected graph there are only tree-edges and back-edges (Right?). We connect edges accordingly (a tree-edge would be directed "down" and a back-edge would be directed "up").
A start of proof
We notice that the graph has no bridges. Therefore, every edge is part of some cycle. Therefore, the last edge to be discovered in some cycle must be a back-edge.
Now, I think the rest of the proof would need to show that we can always "climb" to the root, and so the graph is an SCC.
I'd be glad if you could help me connect the dots (or vertices XD)
Thanks
What you are looking for is a proof for Robbins' Theorem.
For a more formal proof you can look at this paper (See proof for theorem 2).
The below is not a formal proof but it's a way you can think of it:
As you already mentioned, since there are no bridges, every edge is a part of some cycle. Since you want your output graph to be SCC, a DFS on this output graph (from any vertex) must only have back-edges and tree-edges. It cannot have forward-edges or cross-edges.
Lets assume we have a forward-edge from s to t. This means that in the DFS we ran in order to build the graph, t was discovered in the sub-DFS (recursive call) of s and had no other gray or white adjacents. But this is not true because whenever we discover t in our DFS we would still have a gray adjacent.
Lets assume we have a cross-edge from s to t. This means that t's sub-DFS has ended before s was discovered. Again, this cannot happen in our DFS because either when discovering t first we would discover s in its sub-DFS or the opposite direction.
Here is a simple graph to help you see those cases.
I am requested to design an algorithm to determine whether there exists any pair of vertices unreachable from each other in a directed graph. The algorithm must run in O(|V| + |E|) time
Meaning: vertex i cannot reach vertex j, and vertex j cannot reach vertex i.
I have read about the method for finding strongly connected components, I wonder whether or not I can start from there and devise an algorithm usable under the current circumstance?
If you can find all strongly connected components in the linear O(V + E) time requested then you are done. This may seem a bit overkill but it solves the problem. To find all strongly connected components, assuming your graph is represented as an adjacency list, perhaps the simplest O(V + E) linear time algorithm is Kosaraju's, see e.g. http://en.wikipedia.org/wiki/Kosaraju%27s_algorithm
Once you find all strongly connected components, then it is fairly simple to test whether there is a pair of strongly connected components that is not connected by any path, by considering the condensed graph where nodes are strongly connected components and an edge exists if there is an edge between any two nodes chosen from the two connected components.
Here are a few hints to help you get started:
Try solving this problem first when the graph you're given is a DAG. What does the structure of the DAG have to be in order for any pair of nodes to be at least weakly connected?
If you compute the strongly-connected components of a graph, those SCCs themselves form a DAG. Could you use this observation, in conjunction with your algorithm for part (1), to form an algorithm that works on arbitrary graphs?
Hope this helps!
If you are able to find the strongly connected components then you conversely know the vertices that are not connected as well.
The wiki: http://en.wikipedia.org/wiki/Kosaraju%27s_algorithm is pretty instructive. I have implemented the algorithm and it is available here. Runs in O (v + E ). We are assuming that the graph is backed as an adjacency list.
Link for Kosaraju Implementation in Java
Try it out, hopefully you would find no bugs :-)