-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesignGraph.py
73 lines (55 loc) · 1.88 KB
/
designGraph.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import networkx as nx
import matplotlib.pyplot as plt
from ingester import prepareDataStructure
from welsh_powell import welshPowell, countColors
from greedy import greedy
from generateSchedule import generateSchedule
def createNxGraph(graph):
'''
Creates a networkx graph based off the given Graph object.
'''
G = nx.Graph()
for node in graph.nodes:
# edges = []
if node.eventConflicts == []:
G.add_node(node.name)
else:
for edge in node.eventConflicts:
# edges.append(edge.name)
G.add_edge(node.name, edge.name)
# G.add_edge(node.name, edges)
return G
def buildColorDict(graph):
'''
Builds a dictionary of node name:color number based off the given Graph object.
'''
res = {}
for node in graph.nodes:
res[node.name] = node.color
return res
def drawGraph(graph, colors):
'''
Takes a nx.Graph obj and a node name:color dictionary and draws the graph and colors it.
'''
pos = nx.spring_layout(graph)
values = [colors.get(node, 'blue') for node in graph.nodes()]
nx.draw(graph, pos, with_labels = False, node_color=values, edge_color='black', width=1, alpha=0.7)
if __name__ == "__main__":
graph = prepareDataStructure('Data/allConflicts.csv')
# graph = prepareDataStructure('Data/noConflicts.csv')
# graph = prepareDataStructure('Data/dataset.csv')
# Run coloring algorithm
# greedy(graph)
# welshPowell(graph)
# Count number of colors assigned
print("Colors required: ", countColors(graph))
# Build node name: color dictionary
col_val = buildColorDict(graph)
# print(col_val)
# Build network
G = createNxGraph(graph)
# Draw graph and color it
drawGraph(G, col_val)
plt.show()
# Optionally also generate a print out of the schedule
generateSchedule(graph)