Search code examples
pythongraphmicrosoft-distributed-file-system

Repeating 'False' outputs


I am trying to implement a dfs to find cycles in a string graph. For example the graph:

walkways_info = """\
U 3
0 1
1 2
2 0
"""

Would return True. I am currently having the issue that my if statement:

if current_vertex in visited or int(graph[i].split()[1]) in visited:

It is outputting False but I don't want it to output anything at this stage.

Updated code Here

I'd appreciate some help.


Solution

  • I believe that you have an order of operation error, the in and == are not playing nicely.

    current_vertex in visited == True evaluates like this (current_vertex in (visited == True))

    Example:

    In [1]: x = [0,1,2]
    
    In [2]: y = 1
    
    In [3]: y in x == True
    Out[3]: False
    
    In [3]: y in x
    Out[3]: True
    

    So if you drop your == True it should work.

    if current_vertex in visited or int(graph[i].split()[1]) in visited: