Segmentation fault in python

My Approach:
To find the number of connected components I have used a DFS approach.
I apply DFS for every unvisited node in the given graph and mark that node and its neighbor as visited. On applying DFS every time I increase a counter by 1 which is finally returned as the answer.
I had faced an error of maximum recursion depth exceeded error. To overcome this error I had set a greater recursion depth.
Problem: On being tested with a very large performance test case my code breaks and returns Segmentation fault. (I think because of setting a different recursion limit)

Time Complexity: O(N*(N+E))

Hey, @devmodi154 When posting in the forum it’s always a nice habit to be a little more elaborate. In an ideal case by looking at your question I should know what is your approach and what parts of code do you think is creating this issue?

Can you explain your approach, time complexity of the approach, errors you got, and what you did to resolve the errors and currently where you are stuck?

Sorry for earlier post, I have edited with the information asked.

Working when using BFS

@aritra-crio-ta Can you confirm that optimized python code would get submitted without any issues?

Hey … the optimized iterative solution should pass all test cases. The recursive solution might not since it hits pythons maximum function stack memory during recursion in the performance test cases here.

1 Like

Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.