Pairs of Vertices Unreachable from Each Other in a Directed Graph - algorithm

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

Related

Directed Graph walk - visit every node at least once

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

Finding a Minimal set of vertices that satisfy the given constraints

Note: no need for formal proof or anything, just the general idea of the algorithm and I will go deeper myself.
Given a directed graph: G(V,E), I want to find the smallest set of vertices T, such that for each vertex t in T the following edges don't exist: {(t,v) | for every v outside t} in O(V+E)
In other words, it's allowed for t to get edges from vertices outside T, but not to send.
(You can demonstrate it as phone call, where I am allowed to be called from outside and it's free but it's not allowed to call them from my side)
I saw this problem to be so close or similar to finding all strongly connected components (scc) in a directed graph which its time complexity is O(V+E) and I'm thinking of building a new graph and running this algorithm but not totally sure about that.
The main idea is to contract each strongly connected component (SCC) of G into a single vertex while keeping a score on how many vertices were contracted to create each vertex in the contracted graph (condensation of G). The resulting graph is a directed acyclic graph. The answer is the vertex with lower score among the ones with out-degree equal 0.
The answer structure is an union of strongly connected components because of the restriction over edges and you can prove that there is only a SCC involved in the answer because of the min restriction.

Is there a better algorithm to find the shortest path in a graph?

I'm facing a problem where I have to find the shortest path from two nodes in a graph. The graph has some caracteristics that I'm sure can lead to a better solution, as all the ones I've found and thought of 'till now are O(V+E).
In particular:
-The graph is a single connected component.
-The graph is not oriented and unweighted.
-The nodes which arrange a simple cycle are a complete subgraph (***).
I need to find and return the minimum distance, given two nodes of the graph.
I've looked at different algorithms, for weighted and unweighted graphs: Dijkstra, Bellman-Ford, Floyd-Warshall and Breadth First Search, but I can't find an algorithm that makes use of the (***) property, which I'm quite sure is important and useful.
Thanks in advance.
If the input to your problem is a graph and a single pair of vertices then you cannot hope for a solution faster than O(V + E) simply because you need to at least read the input data. However, if you have multiple (say, K) queries, then you can indeed do better than O(K*(V + E)).
If that is the case then one way of incorporating property (***) that I see is the following:
If the graph is a (rooted) tree then the shortest distance between two vertices (u, v) is a path (u--w--v), where w is the least common ancestor (LCA) of u and v. There exists an algorithm that takes O(V + E) time for a certain precomputation and then O(1) time for the actual LCA queries (it is described, for example, here. Once you have the vertex w, it is then straightforward to calculate the length of the path, since it is essentially (depth(w) - depth(u)) + (depth(w) - depth(v)), where depth(x) is the depth of the vertex x in our rooted tree.
In your case, the graph is not a tree, but resembles one a bit. I will give a high level idea of what seems to be possible for this case.
Property (***) tells us that each strongly connected component is a complete subgraph, and the distances between each pair of vertices inside such a component is 1. Therefore, if we contract each strongly connected component into a single vertex then we could do something similar to the previous case.
However, there would be a few subtleties to take care of. For example, when a path in the "contracted" tree passes a vertex, it could mean that we need to visit either one or two vertices in the original graph, depending on whether or not we need to switch the vertex before continuing along our contracted tree. But this is something that we can precompute once for each contracted vertex, and then each query can again be made to run in O(1) time, so overall for K queries we would then have O(V + E) for preprocessing and O(K) for queries, giving us total O(V + E + K) time.

Efficient algorithm to detect all 1-cuts in an undirected graph

Given a connected graph G = (V, E), I want to find all of the 1-cuts. A 1-cut is just a single edge whose removal splits G into 2 connected components.
The algorithm I'm using right now is to remove each edge in turn and then use DFS to check whether its two endpoints are in the same connected component. The runs in O(E(V + E)) time. This is already faster than the best algorithm I can find in the min-cut literature from Nagamochi, Nishimura, and Ibaraki (1998), which takes O(VE(V + E)) time. Their algorithm is valid for all min-cuts of any size. I only need mine to be valid for 1-cuts.
Does anyone know of a better algorithm out there somewhere? Another feature of my use case that might be useful is that the graphs I work with tend to have very small feedback arc sets. If I had an algorithm whose run time depended on the size of a feedback arc set, that would also be useful.
Find all of the biconnected components in the graph (https://en.wikipedia.org/wiki/Biconnected_component). The wikipedia article outlines the classic Hopcroft/Tarjan algorithm that can do this in linear time.
Merge biconnected components that share a vertex, and then each edge that connects different components is a 1-cut.
The common name for a 1-cut is a bridge (https://en.wikipedia.org/wiki/Bridge_(graph_theory))

minimum cost to make an undirected graph disconnected

What is the minimum cost to make an undirected weighted(positive weight) graph disconnected.
i mean i have to find out those edges which removal disconnect the graph and their cost is minimized.
I have following ideas...
1.find out all the bridges of the graph . then the bridge edge of minimum weight wiil be the ans.
2.if there is no bridge that means all the nodes are in a cycle(i'm not sure about it). then i sort the edge according to their weight and the sum of the two minimum edge weight will be the ans.
The graph has no self loop.
Is this algo correct?
This question looks to be the same question answered by the study of "minimum cuts" in graphs. I would recommend the following reading here and here to learn more about why it works from a graph theoretic point of view - the link provides some pseudocode as well.
Regarding your proposed algorithm, finding the bridges in a graph may get tricky.. you would have to inspect both endpoints and their local structure to confirm the existence of a bridge.. using edge contraction perhaps would be simpler to implement.

Resources