Skip to content

Commit

Permalink
完成第19章图的深度优先搜索。
Browse files Browse the repository at this point in the history
  • Loading branch information
zwdnet committed Jun 29, 2020
1 parent 166d626 commit 0d977a5
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions 44/19/weekend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 《programming for the puzzled》实操
# 19.周末聚会问题


# 判断图是否为二分图
def bipartiteGraphColor(graph, start, coloring, color):
if start not in graph:
return False, {}

if start not in coloring:
coloring[start] = color
elif coloring[start] != color:
return False, {}
else:
return True, coloring

if color == 'Sha':
newcolor = 'Hat'
else:
newcolor = 'Sha'

for vertex in graph[start]:
val, coloring = bipartiteGraphColor(graph, vertex, coloring, newcolor)
if val == False:
return False, {}

return True, coloring


if __name__ == "__main__":
dangling = {"A":["B", "E"],
"B":["A", "E", "C"],
"C":["B", "D"],
"D":["C", "E"],
"E":["A", "B", "D"]}
res, col = bipartiteGraphColor(dangling, "A", {}, "Sha")
print(res, col)

0 comments on commit 0d977a5

Please sign in to comment.