AlgoDesign

Deadlock detection

1class Vertex:
2    WHITE, GRAY, BLACK = range(3)
3    def __init__(self):
4        self.colour = Vertex.WHITE
5        self.edges = []
6
7class Solution:
8    def deadlockDetection(self, graph):
9        for vertex in graph:
10            if vertex.colour == Vertex.WHITE and self.dfs(graph, vertex): return True
11        return False
12    
13    def dfs(self, graph, vertex):
14        if vertex.colour == Vertex.GRAY: return True
15        vertex.colour = Vertex.GRAY
16
17        for neighbour in vertex.edges:
18            if neighbour.colour != Vertex.BLACK and self.dfs(graph, neighbour): return True
19        vertex.colour = Vertex.BLACK
20        return False