-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwelsh_powell.py
56 lines (43 loc) · 1.42 KB
/
welsh_powell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from ingester import prepareDataStructure, printGraph
def sort_by_valence(graph):
"""
Takes a graph and sorts its nodes in order of greatest degree to least degree.
"""
graph.nodes.sort(key=lambda x: x.valence, reverse=True)
def print_nodes(graph):
"""
Prints nodes and their valence.
"""
for node in graph.nodes:
print(node.name, node.valence, node.color)
def greedy(graph):
"""
Takes a graph and colors itself using a greedy algorithmic approach.
"""
# color_list = list(range(len(graph.nodes)))
for node in graph.nodes:
available_colors = list(range(len(graph.nodes)))
for conflict in node.eventConflicts:
if conflict.color in available_colors:
available_colors.remove(conflict.color)
node.color = available_colors[0]
def countColors(graph):
'''
Takes a Graph obj and counts how many colors are used to color it.
'''
colors = []
for node in graph.nodes:
if node.color not in colors:
colors.append(node.color)
return len(colors)
def welshPowell(graph):
sort_by_valence(graph)
greedy(graph)
if __name__ == "__main__":
# get file name
# graph = prepareDataStructure('Data/allConflicts.csv')
graph = prepareDataStructure('Data/dataset.csv')
sort_by_valence(graph)
greedy(graph)
print_nodes(graph)
# print("NUMBER OF COLORS: ", countColors(graph))