Skip to content

Instantly share code, notes, and snippets.

@mikezink

mikezink/BFS.py Secret

Created October 17, 2019 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikezink/35de6e166b58c35b1ff5891c2c4bf126 to your computer and use it in GitHub Desktop.
Save mikezink/35de6e166b58c35b1ff5891c2c4bf126 to your computer and use it in GitHub Desktop.
from pythonds.graphs import Graph, Vertex
from pythonds.basic import Queue
from buckets import buildGraph
def bfs(g,start):
start.setDistance(0)
start.setPred(None)
vertQueue = Queue()
vertQueue.enqueue(start)
while (vertQueue.size() > 0):
currentVert = vertQueue.dequeue()
for nbr in currentVert.getConnections():
if (nbr.getColor() == 'white'):
nbr.setColor('gray')
nbr.setDistance(currentVert.getDistance() + 1)
nbr.setPred(currentVert)
vertQueue.enqueue(nbr)
currentVert.setColor('black')
print(currentVert) # prints the status of the current visited vertex
def traverse(y):
x = y
while (x.getPred()):
print(x.getId())
x = x.getPred()
print(x.getId())
g = Graph()
g = buildGraph('wordFile.txt')
bfs(g,g.getVertex('fool'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment