forked from zwdnet/MyQuant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
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
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) | ||
|