Update app.py
Browse files
app.py
CHANGED
@@ -29,17 +29,7 @@ def solve_matching(V, E):
|
|
29 |
return matching
|
30 |
|
31 |
|
32 |
-
def
|
33 |
-
V = st.number_input('Number of vertices', min_value=1, value=10)
|
34 |
-
density = st.slider('Density', min_value=0.0, max_value=1.0, value=0.5)
|
35 |
-
st.button('Generate')
|
36 |
-
|
37 |
-
V, E = generate_random_graph(V, density)
|
38 |
-
|
39 |
-
if len(E) > 40:
|
40 |
-
st.warning('Too many edges to display')
|
41 |
-
return
|
42 |
-
|
43 |
M = set(solve_matching(V, E))
|
44 |
|
45 |
if len(M) == V // 2:
|
@@ -57,5 +47,79 @@ def main():
|
|
57 |
st.graphviz_chart(G)
|
58 |
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
if __name__ == '__main__':
|
61 |
main()
|
|
|
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:
|
|
|
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):
|
57 |
+
for v in range(V):
|
58 |
+
if (u, v) not in E and (v, u) not in 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):
|
71 |
+
if x[u, i].x > 0.5:
|
72 |
+
cycle.append(u)
|
73 |
+
break
|
74 |
+
return cycle
|
75 |
+
|
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):
|
88 |
+
G.node(str(cycle.index(u)))
|
89 |
+
for u, v in E:
|
90 |
+
if ((cycle.index(u) + 1) % len(cycle)) == cycle.index(v):
|
91 |
+
G.edge(str(cycle.index(u)), str(cycle.index(v)), dir='forward')
|
92 |
+
elif ((cycle.index(u) - 1) % len(cycle)) == cycle.index(v):
|
93 |
+
G.edge(str(cycle.index(u)), str(cycle.index(v)), dir='back')
|
94 |
+
else:
|
95 |
+
G.edge(str(cycle.index(u)), str(cycle.index(v)), style='dashed', color='gray')
|
96 |
+
else:
|
97 |
+
for u in range(V):
|
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 |
+
|
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)
|
108 |
+
st.button('Generate')
|
109 |
+
|
110 |
+
V, E = generate_random_graph(V, density)
|
111 |
+
|
112 |
+
if len(E) > 40:
|
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()
|