I have been solving graph problems in Python and using a set() to store already visited nodes to prevent getting stuck in a loop. However, I have seen people who code in C++/Java use vectors or arrays to store visited counts. I am wondering which approach is more efficient in Python or in general for time and space complexity.
For instance, considering we already know the index of the node:
visited = [0] * V # space = O(V)
if not visited[node] # takes Time = O(1)
visited = set() # space = O(V) worst case
if node not in visited: # takes O(1)
But according to this answer Time complexity of python set operations? sometimes set() complexity can be O(N). So, is it better to use an array?