File size: 5,078 Bytes
e9caa70
 
 
 
 
884c797
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9caa70
 
3dce864
e9caa70
 
 
 
 
 
3dce864
e9caa70
 
 
 
 
884c797
e9caa70
 
845403e
e9caa70
 
 
 
 
 
 
 
 
 
 
 
 
 
845403e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7905a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
884c797
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
845403e
 
 
 
 
 
 
 
 
 
 
a7905a1
 
 
 
884c797
a7905a1
 
 
 
 
 
884c797
a7905a1
 
 
 
 
845403e
e9caa70
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import streamlit as st
import graphviz
import random
import gurobipy as gp
from gurobipy import GRB
import colorsys 
 

def hsv2rgb(h, s, v): 
    r, g, b = colorsys.hsv_to_rgb(h, s, v) 
    return int(255 * r), int(255 * g), int(255 * b)
 

def format_color(color): 
    return '#%02x%02x%02x' % color


def generate_colors(n): 
    colors = [hsv2rgb(i / n, 0.5, 1.0) for i in range(n)]
    return [format_color(color) for color in colors]


def generate_random_graph(V, density):
    E = [(u, v) for u in range(V - 1) for v in range(u + 1, V)]
    random.shuffle(E)
    E = E[:int(density * V * (V - 1) / 2)]
    return V, E


def solve_matching(V, E):
    m = gp.Model()
    x = m.addVars(E, vtype=GRB.BINARY)
    m.setObjective(x.sum(), GRB.MAXIMIZE)
    m.addConstrs(x.sum(u, '*') + x.sum('*', u) <= 1 for u in range(V))
    m.optimize()
    return [e for e in E if x[e].x > 0.5]


def app_matching(V, E):
    M = set(solve_matching(V, E))
    if len(M) == V // 2:
        st.success('Perfect matching found')
    else:
        st.metric('Matching size', len(M))
    G = graphviz.Graph()
    for u, v in E:
        if (u, v) in M:
            G.edge(str(u), str(v), color='red')
        else:
            G.edge(str(u), str(v), color='gray', style='dashed')
    st.graphviz_chart(G)


def solve_hamilton(V, E):
    m = gp.Model()
    x = m.addVars(range(V), range(V), vtype=GRB.BINARY)
    m.addConstrs(x.sum(u, '*') == 1 for u in range(V))
    m.addConstrs(x.sum('*', i) == 1 for i in range(V))
    for u in range(V):
        for v in range(V):
            if (u, v) not in E and (v, u) not in E:
                for i in range(V - 1):
                    m.addConstr(x[u, i] + x[v, i + 1] <= 1)
                m.addConstr(x[u, V - 1] + x[v, 0] <= 1)
    m.optimize()
    if m.status != GRB.OPTIMAL:
        return None
    cycle = []
    for i in range(V):
        for u in range(V):
            if x[u, i].x > 0.5:
                cycle.append(u)
                break
    return cycle


def app_hamilton(V, E):
    cycle = solve_hamilton(V, E)
    if cycle is None:
        st.error('Hamilton cycle not found')
    else:
        st.success('Hamilton cycle found')
    G = graphviz.Graph()
    if cycle is not None:
        for u in range(V):
            G.node(str(cycle.index(u)))
        for u, v in E:
            if ((cycle.index(u) + 1) % len(cycle)) == cycle.index(v):
                G.edge(str(cycle.index(u)), str(cycle.index(v)), dir='forward')
            elif ((cycle.index(u) - 1) % len(cycle)) == cycle.index(v):
                G.edge(str(cycle.index(u)), str(cycle.index(v)), dir='back')
            else:
                G.edge(str(cycle.index(u)), str(cycle.index(v)), style='dashed', color='gray')
    else:
        for u in range(V):
            G.node(str(u))
        for u, v in E:
            G.edge(str(u), str(v))
    st.graphviz_chart(G)


def solve_vertex_cover(V, E):
    m = gp.Model()
    x = m.addVars(range(V), vtype=GRB.BINARY)
    m.setObjective(x.sum(), GRB.MINIMIZE)
    m.addConstrs(x[u] + x[v] >= 1 for u, v in E)
    m.optimize()
    return [u for u in range(V) if x[u].x > 0.5]


def app_vertex_cover(V, E):
    cover = solve_vertex_cover(V, E)
    st.metric('Vertex cover size', len(cover))
    G = graphviz.Graph()
    for u in range(V):
        if u in cover:
            G.node(str(u), style='filled', fillcolor='lightblue')
        else:
            G.node(str(u), color='gray')
    for u, v in E:
        G.edge(str(u), str(v))
    st.graphviz_chart(G)


def solve_coloring(V, E):
    m = gp.Model()
    vertex_color = m.addVars(range(V), vtype=GRB.INTEGER, lb=0)
    or_helper = m.addVars(E, vtype=GRB.BINARY)
    chi = m.addVar(vtype=GRB.INTEGER)
    m.setObjective(chi, GRB.MINIMIZE)
    m.addConstrs(vertex_color[u] <= chi for u in range(V))
    m.addConstrs((or_helper[u, v] == 0) >> (vertex_color[u] - vertex_color[v] >= 1) for u, v in E)
    m.addConstrs((or_helper[u, v] == 1) >> (vertex_color[v] - vertex_color[u] >= 1) for u, v in E)
    m.optimize()
    return [round(vertex_color[u].x) for u in range(V)]


def app_coloring(V, E):
    coloring = solve_coloring(V, E)
    st.metric('Chromatic number', max(coloring) + 1)
    colors = generate_colors(max(coloring) + 1)
    G = graphviz.Graph()
    for u in range(V):
        G.node(str(u), style='filled', fillcolor=colors[coloring[u]])
    for u, v in E:
        G.edge(str(u), str(v))
    st.graphviz_chart(G)


def main():
    V = st.number_input('Number of vertices', min_value=1, value=10)
    density = st.slider('Density', min_value=0.0, max_value=1.0, value=0.5)
    st.button('Generate')

    V, E = generate_random_graph(V, density)

    if len(E) > 40:
        st.warning('Too many edges to display')
        return

    apps = [
        app_matching,
        app_hamilton,
        app_vertex_cover,
        app_coloring,
    ]

    tabs = st.tabs([
        'Matching',
        'Hamilton',
        'Vertex cover',
        'Coloring',
    ])

    for t, a in zip(tabs, apps):
        with t:
            a(V, E)

if __name__ == '__main__':
    main()