0

I'm using the following python code to find out any cycle in graph but it doesn't return correct results

for node in graph:
    new_path=node
    path = node
    for adjacent in graph.get(node, []):
        if not adjacent in new_path:
            new_path = list(path)
            new_path.append(adjacent)
        else:
            print (new_path)

Could you please help me find any cycles in the graph?

Input Sample:

graph = {
    '181': set(['284','248','247','350']),
    '350': set(['400', '401']),
    '400': set(['181', '654'])
}

Desired Output:

181 350 400 181

My code does not currently output anything.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Siavash R
  • 67
  • 2
  • 8

0 Answers0