9549dbb266
I've subtly been implementing breadth-first traversals in graphs incorrectly. The change is subtle, but updating `seen` needs to happen immediately after queuing an item. The results will remain the same, but the runtimes will differ dramatically. I didn't notice this until I attempted to complete LeetCode's "count islands" challenge, and LeetCode rejected my solution because it could not finish before timing out. After looking at other candidates' solutions and comparing them to mine, I couldn't see any difference... except for this subtle difference. This SO answer provides a helpful explanation: https://stackoverflow.com/questions/45623722/marking-node-as-visited-on-bfs-when-dequeuing The take-away lesson here is to always call `seen.add(..)` immediately after enqueuing.
60 lines
1.4 KiB
Python
60 lines
1.4 KiB
Python
from collections import deque
|
|
|
|
class Palette(object):
|
|
def __init__(self, n):
|
|
self.i = 0
|
|
self.colors = list(range(n))
|
|
|
|
def get(self):
|
|
return self.colors[self.i]
|
|
|
|
def advance(self):
|
|
self.i += 1 % len(self.colors)
|
|
|
|
class GraphNode(object):
|
|
def __init__(self, label):
|
|
self.label = label
|
|
self.neighbors = set()
|
|
self.color = None
|
|
|
|
def __repr__(self):
|
|
result = []
|
|
xs = deque()
|
|
xs.append(self)
|
|
seen = set()
|
|
while xs:
|
|
node = xs.popleft()
|
|
result.append('{} ({})'.format(node.label, str(node.color)))
|
|
for c in node.neighbors:
|
|
if c.label not in seen:
|
|
xs.append(c)
|
|
seen.add(node.label)
|
|
return ', '.join(result)
|
|
|
|
def color_graph(graph, d):
|
|
seen = set()
|
|
start = graph
|
|
xs = deque()
|
|
palette = Palette(d + 1)
|
|
xs.append((start, palette.get()))
|
|
while xs:
|
|
x, color = xs.popleft()
|
|
x.color = color
|
|
for c in x.neighbors:
|
|
if c.label not in seen:
|
|
palette.advance()
|
|
xs.append((c, palette.get()))
|
|
seen.add(x.label)
|
|
|
|
a = GraphNode('a')
|
|
b = GraphNode('b')
|
|
c = GraphNode('c')
|
|
|
|
a.neighbors.add(b)
|
|
b.neighbors.add(a)
|
|
b.neighbors.add(c)
|
|
c.neighbors.add(b)
|
|
|
|
print(a)
|
|
color_graph(a, 3)
|
|
print(a)
|