carbon225 commited on
Commit
a7905a1
1 Parent(s): 4fa25c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -7
app.py CHANGED
@@ -102,6 +102,29 @@ def app_hamilton(V, E):
102
  st.graphviz_chart(G)
103
 
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  def main():
106
  V = st.number_input('Number of vertices', min_value=1, value=10)
107
  density = st.slider('Density', min_value=0.0, max_value=1.0, value=0.5)
@@ -113,13 +136,21 @@ def main():
113
  st.warning('Too many edges to display')
114
  return
115
 
116
- t1, t2 = st.tabs(['Matching', 'Hamilton'])
117
-
118
- with t1:
119
- app_matching(V, E)
120
-
121
- with t2:
122
- app_hamilton(V, E)
 
 
 
 
 
 
 
 
123
 
124
  if __name__ == '__main__':
125
  main()
 
102
  st.graphviz_chart(G)
103
 
104
 
105
+ def solve_vertex_cover(V, E):
106
+ m = gp.Model()
107
+ x = m.addVars(range(V), vtype=GRB.BINARY)
108
+ m.setObjective(x.sum(), GRB.MINIMIZE)
109
+ m.addConstrs(x[u] + x[v] >= 1 for u, v in E)
110
+ m.optimize()
111
+ return [u for u in range(V) if x[u].x > 0.5]
112
+
113
+
114
+ def app_vertex_cover(V, E):
115
+ cover = solve_vertex_cover(V, E)
116
+ st.metric('Vertex cover size', len(cover))
117
+ G = graphviz.Graph()
118
+ for u in range(V):
119
+ if u in cover:
120
+ G.node(str(u), style='filled', fillcolor='lightblue')
121
+ else:
122
+ G.node(str(u), color='gray')
123
+ for u, v in E:
124
+ G.edge(str(u), str(v))
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)
 
136
  st.warning('Too many edges to display')
137
  return
138
 
139
+ apps = [
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):
152
+ with t:
153
+ a(V, E)
154
 
155
  if __name__ == '__main__':
156
  main()