1

having an issue working out how to cycle back to the start node in my graph. Currently, from the graph I create I can cycle from a start node and follow the edges till there are no connected nodes. However I can't work out how to make it cycle through and finish on the start node, if possible.

This is an example of the graph with its connections.

Node & Connection(S) [(0, 4), (1, 5), (1, 8), (3, 1), (4, 0), (4, 3), (5, 0), 
(5, 3), (5, 7), (6, 0), (6, 4), (7, 0), (8, 5), (8, 6), (8, 7)]

This is my code to cycle through the graph and follow its edges.

def pathSearch(graph, start, path=[]):
    path=path+[start]
    for node in graph[start]:
        if not node in path:
            path=pathSearch(graph, node, path)
    return path
print ('Path ', pathSearch(g, 0))

This is what I get as an output starting from node 0:

pathSearch [0, 4, 3, 1, 5, 7, 8, 6]

This is right, but why isn't it doing a full cycle back to the start node?

c0der
  • 18,467
  • 6
  • 33
  • 65
CodeMuch
  • 81
  • 1
  • 5

0 Answers0