0

I wrote up a program which counts the size of the 5 largest SCC's in a given graph. The number of nodes in the graph is 875714. Following is the basic DFS code I'm using in the problem. (Both functions are methods in a class)

def DFSloop(self):
    exp = [False] * (self.size + 1)

    for i in range(1,(self.size + 1)):
        if exp[i] == False:
            self.DFS(i, exp) 

def DFS(self, s, exp):
    exp[s] = True
    for vertex in self.g[s]:
        if exp[vertex] == False:
            self.DFS(vertex, exp)

Basically DFS has to recurse a lot as number of nodes and edges is huge. It showed the following error even after setting the recursion limit to 10,000

RuntimeError: maximum recursion depth exceeded in cmp

Then upon increasing the limit to 100,000, it showed:

Segmentation fault: 11

And the system crashed. Any help in overcoming the situation?

0

1 Answer 1

1

I overcame a similar issue by using a stack instead of using recursion.

Example:

def fib(n):
    stack = [n]
    s = 0
    while len(stack) > 0:
        n = stack.pop()
        if n < 2:
            s += n
        else:
            stack.append(n-1)
            stack.append(n-2)

    return s
Sign up to request clarification or add additional context in comments.

1 Comment

who said imperative programs cannot be beautiful?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.