-
-
Save mikezink/e4d868786a48848a64c7877c994dcff5 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 | |
class DFSGraph(Graph): | |
def __init__(self): | |
super().__init__() | |
self.time = 0 | |
def dfs(self): | |
for aVertex in self: | |
aVertex.setColor('white') | |
aVertex.setPred(-1) | |
for aVertex in self: | |
if aVertex.getColor() == 'white': | |
self.dfsvisit(aVertex) | |
def dfsvisit(self,startVertex): | |
startVertex.setColor('gray') | |
self.time += 1 | |
startVertex.setDiscovery(self.time) | |
for nextVertex in startVertex.getConnections(): | |
if nextVertex.getColor() == 'white': | |
nextVertex.setPred(startVertex) | |
self.dfsvisit(nextVertex) | |
startVertex.setColor('black') | |
self.time += 1 | |
startVertex.setFinish(self.time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment