Skip to content

Instantly share code, notes, and snippets.

@pscollins
Last active August 29, 2015 14:13
Show Gist options
  • Save pscollins/e236222ac83ac06129f1 to your computer and use it in GitHub Desktop.
Save pscollins/e236222ac83ac06129f1 to your computer and use it in GitHub Desktop.
This is wrong(?)
class Vertex:
def __init__(self, idx, parent, children):
self.idx = idx
self.parent = parent
self.children = children
def rb_dist(source, graph):
def init_list():
return [float('inf') for _ in graph]
dists = {
"red": init_list(),
"blue": init_list()
}
for l in dists.values():
l[source] = 0
bfs_q = queue([Vertex(v, source, v.adjacency) for v in source.adjacency])
while not bfs_q.empty():
this = bfs_q.pop()
for c1, c2 in (("red", "blue"), ("blue", "red")):
dists[c1][this] = dists[c2][this.parent] + 1
for v in (v for v in this.children if dists["red"][v] == float('inf')):
bfs_q.push(Vertex(v, this, v.adjacency))
return dists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment