-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreedy.py
42 lines (34 loc) · 1.12 KB
/
greedy.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
from ingester import prepareDataStructure, printGraph
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 print_nodes(graph):
"""
Prints nodes and their valence.
"""
for node in graph.nodes:
print(node.name, node.valence)
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)
if __name__ == "__main__":
# get file name
# graph = prepareDataStructure('Data/dataset.csv')
graph = prepareDataStructure('Data/allConflicts.csv')
print_nodes(graph)
greedy(graph)
print("NUMBER OF COLORS: ", countColors(graph))