carbon225 commited on
Commit
884c797
1 Parent(s): a7905a1
Files changed (1) hide show
  1. app.py +43 -19
app.py CHANGED
@@ -3,6 +3,21 @@ import graphviz
3
  import random
4
  import gurobipy as gp
5
  from gurobipy import GRB
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
 
8
  def generate_random_graph(V, density):
@@ -15,42 +30,30 @@ def generate_random_graph(V, density):
15
  def solve_matching(V, E):
16
  m = gp.Model()
17
  x = m.addVars(E, vtype=GRB.BINARY)
18
-
19
  m.setObjective(x.sum(), GRB.MAXIMIZE)
20
  m.addConstrs(x.sum(u, '*') + x.sum('*', u) <= 1 for u in range(V))
21
-
22
  m.optimize()
23
-
24
- matching = []
25
- for e in E:
26
- if x[e].x > 0.5:
27
- matching.append(e)
28
-
29
- return matching
30
 
31
 
32
  def app_matching(V, E):
33
  M = set(solve_matching(V, E))
34
-
35
  if len(M) == V // 2:
36
  st.success('Perfect matching found')
37
  else:
38
  st.metric('Matching size', len(M))
39
-
40
  G = graphviz.Graph()
41
  for u, v in E:
42
  if (u, v) in M:
43
  G.edge(str(u), str(v), color='red')
44
  else:
45
  G.edge(str(u), str(v), color='gray', style='dashed')
46
-
47
  st.graphviz_chart(G)
48
 
49
 
50
  def solve_hamilton(V, E):
51
  m = gp.Model()
52
  x = m.addVars(range(V), range(V), vtype=GRB.BINARY)
53
-
54
  m.addConstrs(x.sum(u, '*') == 1 for u in range(V))
55
  m.addConstrs(x.sum('*', i) == 1 for i in range(V))
56
  for u in range(V):
@@ -59,12 +62,9 @@ def solve_hamilton(V, E):
59
  for i in range(V - 1):
60
  m.addConstr(x[u, i] + x[v, i + 1] <= 1)
61
  m.addConstr(x[u, V - 1] + x[v, 0] <= 1)
62
-
63
  m.optimize()
64
-
65
  if m.status != GRB.OPTIMAL:
66
  return None
67
-
68
  cycle = []
69
  for i in range(V):
70
  for u in range(V):
@@ -76,12 +76,10 @@ def solve_hamilton(V, E):
76
 
77
  def app_hamilton(V, E):
78
  cycle = solve_hamilton(V, E)
79
-
80
  if cycle is None:
81
  st.error('Hamilton cycle not found')
82
  else:
83
  st.success('Hamilton cycle found')
84
-
85
  G = graphviz.Graph()
86
  if cycle is not None:
87
  for u in range(V):
@@ -98,7 +96,6 @@ def app_hamilton(V, E):
98
  G.node(str(u))
99
  for u, v in E:
100
  G.edge(str(u), str(v))
101
-
102
  st.graphviz_chart(G)
103
 
104
 
@@ -125,6 +122,31 @@ def app_vertex_cover(V, E):
125
  st.graphviz_chart(G)
126
 
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  def main():
129
  V = st.number_input('Number of vertices', min_value=1, value=10)
130
  density = st.slider('Density', min_value=0.0, max_value=1.0, value=0.5)
@@ -140,12 +162,14 @@ def main():
140
  app_matching,
141
  app_hamilton,
142
  app_vertex_cover,
 
143
  ]
144
 
145
  tabs = st.tabs([
146
  'Matching',
147
  'Hamilton',
148
  'Vertex cover',
 
149
  ])
150
 
151
  for t, a in zip(tabs, apps):
 
3
  import random
4
  import gurobipy as gp
5
  from gurobipy import GRB
6
+ import colorsys
7
+
8
+
9
+ def hsv2rgb(h, s, v):
10
+ r, g, b = colorsys.hsv_to_rgb(h, s, v)
11
+ return int(255 * r), int(255 * g), int(255 * b)
12
+
13
+
14
+ def format_color(color):
15
+ return '#%02x%02x%02x' % color
16
+
17
+
18
+ def generate_colors(n):
19
+ colors = [hsv2rgb(i / n, 0.5, 1.0) for i in range(n)]
20
+ return [format_color(color) for color in colors]
21
 
22
 
23
  def generate_random_graph(V, density):
 
30
  def solve_matching(V, E):
31
  m = gp.Model()
32
  x = m.addVars(E, vtype=GRB.BINARY)
 
33
  m.setObjective(x.sum(), GRB.MAXIMIZE)
34
  m.addConstrs(x.sum(u, '*') + x.sum('*', u) <= 1 for u in range(V))
 
35
  m.optimize()
36
+ return [e for e in E if x[e].x > 0.5]
 
 
 
 
 
 
37
 
38
 
39
  def app_matching(V, E):
40
  M = set(solve_matching(V, E))
 
41
  if len(M) == V // 2:
42
  st.success('Perfect matching found')
43
  else:
44
  st.metric('Matching size', len(M))
 
45
  G = graphviz.Graph()
46
  for u, v in E:
47
  if (u, v) in M:
48
  G.edge(str(u), str(v), color='red')
49
  else:
50
  G.edge(str(u), str(v), color='gray', style='dashed')
 
51
  st.graphviz_chart(G)
52
 
53
 
54
  def solve_hamilton(V, E):
55
  m = gp.Model()
56
  x = m.addVars(range(V), range(V), vtype=GRB.BINARY)
 
57
  m.addConstrs(x.sum(u, '*') == 1 for u in range(V))
58
  m.addConstrs(x.sum('*', i) == 1 for i in range(V))
59
  for u in range(V):
 
62
  for i in range(V - 1):
63
  m.addConstr(x[u, i] + x[v, i + 1] <= 1)
64
  m.addConstr(x[u, V - 1] + x[v, 0] <= 1)
 
65
  m.optimize()
 
66
  if m.status != GRB.OPTIMAL:
67
  return None
 
68
  cycle = []
69
  for i in range(V):
70
  for u in range(V):
 
76
 
77
  def app_hamilton(V, E):
78
  cycle = solve_hamilton(V, E)
 
79
  if cycle is None:
80
  st.error('Hamilton cycle not found')
81
  else:
82
  st.success('Hamilton cycle found')
 
83
  G = graphviz.Graph()
84
  if cycle is not None:
85
  for u in range(V):
 
96
  G.node(str(u))
97
  for u, v in E:
98
  G.edge(str(u), str(v))
 
99
  st.graphviz_chart(G)
100
 
101
 
 
122
  st.graphviz_chart(G)
123
 
124
 
125
+ def solve_coloring(V, E):
126
+ m = gp.Model()
127
+ vertex_color = m.addVars(range(V), vtype=GRB.INTEGER, lb=0)
128
+ or_helper = m.addVars(E, vtype=GRB.BINARY)
129
+ chi = m.addVar(vtype=GRB.INTEGER)
130
+ m.setObjective(chi, GRB.MINIMIZE)
131
+ m.addConstrs(vertex_color[u] <= chi for u in range(V))
132
+ m.addConstrs((or_helper[u, v] == 0) >> (vertex_color[u] - vertex_color[v] >= 1) for u, v in E)
133
+ m.addConstrs((or_helper[u, v] == 1) >> (vertex_color[v] - vertex_color[u] >= 1) for u, v in E)
134
+ m.optimize()
135
+ return [round(vertex_color[u].x) for u in range(V)]
136
+
137
+
138
+ def app_coloring(V, E):
139
+ coloring = solve_coloring(V, E)
140
+ st.metric('Chromatic number', max(coloring) + 1)
141
+ colors = generate_colors(max(coloring) + 1)
142
+ G = graphviz.Graph()
143
+ for u in range(V):
144
+ G.node(str(u), style='filled', fillcolor=colors[coloring[u]])
145
+ for u, v in E:
146
+ G.edge(str(u), str(v))
147
+ st.graphviz_chart(G)
148
+
149
+
150
  def main():
151
  V = st.number_input('Number of vertices', min_value=1, value=10)
152
  density = st.slider('Density', min_value=0.0, max_value=1.0, value=0.5)
 
162
  app_matching,
163
  app_hamilton,
164
  app_vertex_cover,
165
+ app_coloring,
166
  ]
167
 
168
  tabs = st.tabs([
169
  'Matching',
170
  'Hamilton',
171
  'Vertex cover',
172
+ 'Coloring',
173
  ])
174
 
175
  for t, a in zip(tabs, apps):