Is there an efficient algorithm that takes as input a directed cyclic graph and returns the size of each subgraph originating from each of the nodes? By "efficient" I mean something more efficient than carrying out DFS on each of the nodes.
1 Answers
DFS takes time proportional to the number of edges reachable from each node, which is potentially O(E), so doing DFS from each node is O(VE) where V is the number of vertices and E is the number of edges. Assuming the average graph has O(V^2) edges, this is O(V^3) in the average case and the worst case. In the best case, repeated DFS takes O(V) time on a graph with no edges.
One simple way to do better than this - at least in theory - is to take the adjacency matrix A, write 1s along the diagonal so that each node is reachable from itself, find the and-or matrix power A^(V-1), and then count the number of 1s in each row. The time complexity of this approach is:
- O(V^2) to build the adjacency matrix,
- O(f(V) * log V) to compute the matrix power using the square-and-multiply algorithm to do log V matrix multiplications, where f(V) is the complexity of a matrix multiplication algorithm,
- O(V^2) to count the 1s in each row of the result.
The time complexity of matrix multiplication can be as low as about O(n^2.373) depending on which algorithm you use, so the overall complexity of the above algorithm is about O(V^2.373 log V). This beats repeated DFS in the average case and the worst case, but not in the best case.
That said, this answer is purely theoretical because the matrix multiplication algorithms which achieve low time complexities generally have quite large constant factors, so that they aren't actually faster for matrices of reasonable sizes. It also probably isn't the best you can do; but it does answer the existential question of "is there something more efficient?".

- 47,440
- 4
- 68
- 97
-
Thank you for the quick answer. It answers my question, if we were to assume that the average graph pertains to O(V^2) edges, which assumption gives us the worst rather than the average case. Of course any comments on the existence of an optimal (in the average case) algorithm that solves the problem would be greatly appreciated. If no such algorithm exists, given my unfamiliarity with graph theory, any hints on other algorithms that can be potentially be adapted to solve the problem would also be useful. – panos Jan 16 '20 at 14:06
-
O(V^2) edges is the average case for a random graph, as well as the worst case; there are V(V-1)/2 possible edges, so a random graph will have half of them as edges and the other half as non-edges, giving an average of V(V-1)/4 edges which is in O(V^2). If your graph is drawn from some other distribution where the edges aren't random (with a fixed probability), then it's possible the average graph from that distribution has fewer than O(V^2) edges; for example if your graph is always a tree. – kaya3 Jan 16 '20 at 17:13