-
-
Save mikezink/35de6e166b58c35b1ff5891c2c4bf126 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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