Search code examples
pythonfor-loopwhile-loopgraph-coloring

Python "If max number of steps is exceeded, break the loop"


I'm working on a (very convoluted and inelegant) Python code to 3-color a graph by brute force, and in my main block of code I'm trying to include a statement that says "if the max number of runs through the loop exceeds (some arbitrary number), break out of the first (while a in range(0,len(vertices))) loop".

a = 0
steps = 0   
while a in range(0,len(vertices)):
    for j in newdict:
        steps+=1      #count number of times it goes through the loop
        print(steps)
        if a>=len(vertices) or steps>=100000:
            break     #this is where my error is occurring

        i = vertices[a]
        if dict1[i[0]]==dict1[i[1]] and (list(dict1.values()))!=j:   #if the adjacent vertices are the same color and we have not cycled back to our original dict1,
            dict1[i[1]]=colors[dict1[i[1]]+1]   #change color of the second vertex
            a = 0    #after a vertex is changed colors, I don't want it to keep going: I want the code to stop and reexamine from the top with this new value
            newdict.append(list(dict1.values()))   #running list of all previous dictionaries (attempted colorings): if our dictionary ever cycles through to something it already was, try again
            check = all(dict1[i[0]] != dict1[i[1]] for i in vertices)  # check all adjacent vertices are different colors
            if not check:
                continue
            elif check:
                break  #at any point throughout the code, if the graph is colorable, break the loop and jump to end instead of continuing to color
        elif dict1[i[0]]==dict1[i[1]]:  #if we do eventally cycle back to our original dict1, now we'll try changing the first vertex color
            dict1[i[0]] = colors[dict1[i[0]] + 1]
            a = 0
            newdict.append(list(dict1.values()))
            check = all(dict1[i[0]] != dict1[i[1]] for i in vertices)  # check all adjacent vertices are different colors
            if not check:
                continue
            elif check:
                break  #at any point throughout the code, if the graph is colorable, break the loop and jump to end instead of continuing to color
        else:
            a+=1

However, I find that even when more than 100000 steps elapse (which I can see as I'm printing the number of steps), the loop doesn't break and becomes an infinite loop, and the number of steps continues past 100000. Should I include another loop,

while steps < 100000:

instead of just adding another condition to my first loop? Am I making a syntax mistake or is it a more in depth issue with my code?

(The full code is available here.)


Solution

  • You have two loops

    while a in range(0,len(vertices)): # Loop 1
        for j in newdict: # Loop 2
            steps+=1      #count number of times it goes through the loop
            print(steps)
            if a>=len(vertices) or steps>=100000:
                break     #this is where my error is occurring
    

    so when you break, it break the inner loop, which is for j in newdict: you have to add another condition to break loop while