-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
185 lines (157 loc) · 5.26 KB
/
app.py
File metadata and controls
185 lines (157 loc) · 5.26 KB
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
182
183
184
185
import streamlit as st
import matplotlib.pyplot as plt
import pandas as pd
from graph_model import Graph
from PIL import Image
# --- Helper to convert matplotlib fig to bytes for download ---
def fig_to_bytes(fig):
import io
buf = io.BytesIO()
fig.savefig(buf, format="png", bbox_inches='tight')
buf.seek(0)
return buf
# --- Hàm khởi tạo trạng thái ban đầu ---
def init_session():
if "graph" not in st.session_state:
st.session_state.graph = Graph()
if "input_text" not in st.session_state:
st.session_state.input_text = ""
# --- Hàm xử lý reset toàn bộ ---
def reset_all():
st.session_state.clear()
# --- Hàm xử lý đọc input người dùng ---
def handle_input():
user_input = st.session_state.input_text
try:
lines = user_input.strip().splitlines()
v, e = map(int, lines[0].split())
names = lines[1].split()
edge_list = [tuple(line.strip().split()) for line in lines[2:] if line.strip()]
g = st.session_state.graph
g.load_graph(v, e, names, edge_list)
st.success("Đã nạp đồ thị!")
except Exception as ex:
st.error(f"Lỗi khi đọc input: {ex}")
# --- Hàm thực hiện bước tô màu tiếp theo ---
def step_coloring():
g = st.session_state.graph
changed = g.step_one_vertex()
if not changed:
st.info("Đã tô màu xong!")
# --- Hàm tô toàn bộ đến khi xong ---
def full_coloring():
g = st.session_state.graph
while g.step_one_vertex():
pass
st.success("Đã tô toàn bộ xong!")
# --- Giao diện chính ---
def main():
logo = Image.open("./assets/logo-uit.png")
st.set_page_config(
page_title="UIT@CS | KhoiBui16 | Tô màu đồ thị",
page_icon=logo,
layout="centered"
)
st.title("🎨 Tô màu đồ thị theo chiến lược Heuristic")
st.markdown("""
### 📘 Đề bài
Cho một đơn đồ thị vô hướng. Hãy tô màu cho đồ thị bằng **PHƯƠNG PHÁP THAM LAM** sao cho các đỉnh lân cận nhau không trùng màu với nhau và số màu phải dùng là nhỏ nhất có thể.
Màu được sử dụng là hệ màu RGB 24 bit với tối đa hơn 16 triệu màu, các màu được đánh số từ `0` (màu đen `#000000`) đến `16777215` (màu trắng `#FFFFFF`).
#### 🔽 INPUT:
- Dòng đầu tiên chứa 2 số `v`, `e` lần lượt là số đỉnh và số cạnh.
- Dòng thứ 2 chứa `v` chuỗi (tên các đỉnh).
- `e` dòng tiếp theo, mỗi dòng chứa 2 tên đỉnh có cạnh nối với nhau.
#### 🔼 OUTPUT:
- Một dòng gồm `v` số nguyên — là màu của từng đỉnh, theo đúng thứ tự xuất hiện trong input.
#### 📌 Ví dụ:
Input:
```
7 18
J R Q F L P I
L I
L F
F R
F J
L J
J R
J P
F Q
P L
F I
R P
I Q
Q L
F P
J I
I R
P Q
P I
```
Output:
```
0 1 0 2 1 3 4
```
""")
init_session()
g = st.session_state.graph
st.subheader("📥 Nhập input theo đề bài")
st.markdown(
"""
📂 [**Xem các test case mẫu** tại đây:](https://github.com/KhoiBui16/Graph_Coloring_Heuristic/tree/main/test_case)
""",
unsafe_allow_html=True
)
st.text_area(
"Toàn bộ input",
key="input_text",
height=150,
placeholder="Nhập dữ liệu input theo đúng định dạng đề bài...",
)
st.markdown("""
<style>
textarea[data-testid="stTextArea"] {
height: auto !important;
min-height: 100px;
overflow-y: auto;
}
</style>
""", unsafe_allow_html=True)
col1, col2, col3, col4 = st.columns([1, 1, 1, 1])
with col1:
if st.button("📥 Đọc dữ liệu"):
handle_input()
with col2:
if st.button("▶ Bước tô tiếp"):
step_coloring()
with col3:
if st.button("🖌 Tô toàn bộ"):
full_coloring()
with col4:
if st.button("🔄 Thiết lập lại"):
reset_all()
st.rerun()
if g.v > 0:
fig = g.draw_graph()
st.pyplot(fig)
st.subheader("📊 Trạng thái của đồ thị")
st.write("**🧠 Ma trận thông tin:**")
degree_list = [len(g.adjList[i]) for i in range(g.v)]
df = pd.DataFrame([
g.nodeNames,
degree_list,
g.colors
], index=["Tên đỉnh", "Bậc ban đầu", "Màu"])
st.dataframe(df.style.set_properties(**{'text-align': 'center'}).set_table_styles([{
'selector': 'th',
'props': [('background-color', '#f0f0f0'), ('color', 'black')]
}, {
'selector': 'td',
'props': [('background-color', '#fafafa')]
}]), use_container_width=True)
used_colors = set(c for c in g.colors if c != -1)
st.write(f"**🌈 Số màu đã sử dụng:** {len(used_colors)}")
if st.download_button("📷 Tải ảnh đồ thị (PNG)", data=fig_to_bytes(fig), file_name="do_thi.png", mime="image/png"):
st.success("Tải ảnh thành công!")
# Gọi hàm main
if __name__ == "__main__":
main()