# Find the lengths of strongly connected components in a graph

I wrote a code to calculate the lengths of strongly connected components in a graph. The results I get are correct but it scales horribly.

This is the code I have. I understand that the runtime complexity of a graph traversals should be O(V+E) and as far as I understand, I am not visiting any node twice. And yet it takes forever for bigger graphs (875714 nodes and 5105043 edges)

class Graph:
def __init__(self):
self.graph = {}
self.rev_graph = {}
self.num_vertices = 0
if tail in self.graph:
else:
else:
def dfs(self, visited, start_node, stack):
visited[start_node] = True
if start_node in self.graph:
for i in self.graph[start_node]:
if not visited[i]:
self.dfs(visited, i, stack)
# del self.rev_graph[start_node]
stack.append(start_node)
def dfs_counter(self, visited, start_node, delim='  '):
l1 = sum(visited)
visited[start_node] = True
if start_node in self.rev_graph:
# print(delim, 'rev graph neighbors: ', start_node, self.rev_graph[start_node])
for i in self.rev_graph[start_node]:
if not visited[i]:
self.dfs_counter(visited, i, 2*delim)
l2 = sum(visited)
return l2 - l1
# print(stack)
def find_scc_lens(self):
self.num_vertices = len(set(list(self.graph.keys()) + list(self.rev_graph.keys())))
visited = [False]*self.num_vertices
stack = []
self.dfs(visited, 0, stack)
# stack = stack[::-1]
# print(stack)
# del(self.graph)
visited = [False] * self.num_vertices
scc_lengths = []
for k,i in enumerate(stack[::-1]):
if not visited[i]:
# print("rev graph node count", k, i)
scc_lengths.append(self.dfs_counter(visited, i))
return scc_lengths


This exercise is from an online course I am taking. I tried a few other solutions online I found elsewhere and they are really fast compared to my runtimes.

What parts of the code can I optimize to improve the runtimes?

Example:

g = Graph()
g.find_scc_lens()

• Graph traversal is an NP class of problem. That ALL algorithms we are aware off scale horribly. While I can't claim to be intimately familiar with graph traversal, I also don't see any glaring issues. How big, exactly, is the size of the Graphs when you get MemoryErrors? Does "really big" mean 1000 nodes or 1000000 nodes? – Gloweye Oct 23 '19 at 11:12
• Also, welcome to Code Review. Please read the tour if you haven't, or How to Ask if you want to know more about what questions exactly we want. Note that if it turns out you're looking for mathematical help, this question may be migrated towards a more suitable site. – Gloweye Oct 23 '19 at 11:14
• I understand there might be a need for a little bit more information but I really don't think this post should be closed, the code is there, works to OPs knowledge and the title is fine. – IEatBagels Oct 23 '19 at 13:54
• @Gloweye, graph traversal is indeed in NP, but it's also in P, and OP is correct to say that it's in $O(V + E)$ (although this particular implementation isn't). – Peter Taylor Oct 23 '19 at 16:10

class Graph:
def __init__(self):
self.graph = {}
self.rev_graph = {}
self.num_vertices = 0


This looks reasonable. Note that the PEP8 style guide wants you to add a blank line after each method.

    def addEdge(self, tail, head):
if tail in self.graph:
else:
else:


The failure to update self.num_vertices here does not look reasonable. I suspect that the reason is that num_vertices shouldn't be a property of the class.

    def dfs(self, visited, start_node, stack):
visited[start_node] = True
if start_node in self.graph:
for i in self.graph[start_node]:
if not visited[i]:
self.dfs(visited, i, stack)
# del self.rev_graph[start_node]
stack.append(start_node)


In future, please remove commented code before submitting the code for review.

I could use a comment to explain the purpose of stack, and why it adds in post-order rather than pre-order.

    def dfs_counter(self, visited, start_node, delim='  '):
l1 = sum(visited)
visited[start_node] = True
if start_node in self.rev_graph:
# print(delim, 'rev graph neighbors: ', start_node, self.rev_graph[start_node])
for i in self.rev_graph[start_node]:
if not visited[i]:
self.dfs_counter(visited, i, 2*delim)
l2 = sum(visited)
return l2 - l1
# print(stack)


sum(visited) is expensive: too expensive to use in this method, which is called inside a loop. If you refactor to track the sum directly then I expect you'll see a notable speedup.

2*delim probably isn't really desirable: it should probably be delim + ' '.

    def find_scc_lens(self):
self.num_vertices = len(set(list(self.graph.keys()) + list(self.rev_graph.keys())))


It should be possible to do this without using list.

        visited = [False]*self.num_vertices


This is buggy. num_vertices is the number of vertices, not the largest vertex. Consider the graph

g = Graph()


The output should be either [2] (vertices with no edges don't exist) or some permutation of [1, 1, 2] (with vertices 0 to 3).

        stack = []
self.dfs(visited, 0, stack)
# stack = stack[::-1]
# print(stack)
# del(self.graph)


This is also buggy. What about vertices which aren't reachable from 0? Even if you add documentation saying that this method only works for connected graphs, that doesn't rule out

g = Graph()

        visited = [False] * self.num_vertices
scc_lengths = []
for k,i in enumerate(stack[::-1]):
if not visited[i]:
# print("rev graph node count", k, i)
scc_lengths.append(self.dfs_counter(visited, i))
return scc_lengths


In order to validate the correctness of this implementation, it would be immensely useful to have a comment which says which algorithm it implements (and ideally links to reference material).

• Thanks for the nice detailed comments, Peter. I didn't realize the sum could be a potential issue. That was sort of a revelation. I went through a couple of videos online after this post and realized the implementation is indeed buggy and works only for a few cases which I was using as examples. I am not sure if we are supposed to keep up the posts that are coming out of incorrect understanding. I threw away this code and rewrote it which resolves all the current issues. I was going through meta posts and meanwhile, you answered. It's a pickle for me now. I.... – Clock Slave Oct 23 '19 at 16:29
• If the sequence of operations had been different, you could have deleted your question temporarily, edited it with the fixed code, and undeleted it. But once you have an answer, that option is removed. What you can do now is to post a new question with the new code and link back to this one for context. – Peter Taylor Oct 23 '19 at 16:32
• Okay. But my new question wouldn't have any issues (as far as I know). I have fixed the code since I put up the post. Is it okay to put up posts with no real issues? And wouldn't I be downvoted a lot? – Clock Slave Oct 23 '19 at 16:38
• If you think the new version is fine, you don't have to ask for it to be reviewed, but the option is there if you want it. There's a sense in which the better the code, the more advanced the review can be, so you might still learn something. See codereview.stackexchange.com/search?q=Iterative+review for reassurance about downvotes. – Peter Taylor Oct 23 '19 at 22:58