-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmMain.cs
More file actions
407 lines (370 loc) · 12.4 KB
/
frmMain.cs
File metadata and controls
407 lines (370 loc) · 12.4 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace ShortestPaths
{
public partial class frmMain : Form
{
#region InitializeProgram
public frmMain()
{
InitializeComponent();
}
private Graph graph;
private DijkstraShortestPathAlg spAlg;
private YenTopKShortestPathsAlg yenAlg;
/// <summary>
/// Kiểm tra đỉnh bắt đầu
/// </summary>
private int minVertex = 1000000;
/// <summary>
/// Max của độ dài các cạnh
/// </summary>
const int MAXINT = 1000000000;
#endregion
#region Các hàm hỗ trợ
void ReadData()
{
try
{
string path = Application.StartupPath;
OpenFileDialog Odg = new OpenFileDialog()
{
FileName = "Select a File",
Filter = "Select files (*.txt)|*.txt",
Title = "Open file"
};
Odg.InitialDirectory = path;
if (Odg.ShowDialog() == DialogResult.OK)
{
string filePath = Odg.FileName;
using (var reader = new StreamReader(filePath))
{
string data = reader.ReadLine();
n = Convert.ToInt32(data.Trim());
A = new int[n + 1, n + 1];
while ((data = reader.ReadLine()) != null)
{
data = data.Trim();
var s = data.Split('\t', ' ');
if (s.Length == 3)
{
int x = Convert.ToInt32(s[0]);
int y = Convert.ToInt32(s[1]);
int z = Convert.ToInt32(s[2]);
A[x, y] = z;
if (x < minVertex) minVertex = x;
else if (y < minVertex) minVertex = y;
}
}
}
if (graph != null)
{
graph = null;
}
graph = new VariableGraph(System.IO.Path.GetFileNameWithoutExtension(Odg.FileName) + ".txt");
MessageBox.Show("Đọc dữ liệu thành công", "Đường đi ngắn nhất", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtSource.Enabled = true;
txtDest.Enabled = true;
btnExecute.Enabled = true;
}
}
catch (Exception ex)
{
MessageBox.Show("Đã xảy ra lỗi! \n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
void Reset()
{
Cursor.Current = Cursors.WaitCursor;
txtDest.Text = "";
txtSource.Text = "";
rtbResult.Text = "";
txtSource.Enabled = false;
txtDest.Enabled = false;
btnExecute.Enabled = false;
graph = null;
Cursor.Current = Cursors.Default;
}
#endregion
#region BFS
// Mảng lưu dữ liệu đồ thị
int[,] A;
// Mảng lưu đường đi
int[] L;
// Điểm đi, điểm đến
int source, dest;
// Số đỉnh của đồ thị
int n;
// Mảng đánh dấu các đỉnh đã đi qua
int[] Tick;
// Giá trị đường đi ngắn nhất
int minLength = MAXINT;
// Các đỉnh đã đi qua
string minPath = "";
void InitializeBFS()
{
L = new int[n + 1];
Tick = new int[n + 1];
for (int i = 1; i <= n; i++)
{
Tick[i] = 0;
L[i] = 0;
}
Tick[source] = 1;
L[1] = source;
minLength = MAXINT;
}
void SaveResult(int edge)
{
string path = source.ToString() + " -> ";
int length = 0;
for (int i = 2; i <= edge; i++)
{
path = path + L[i].ToString() + " -> ";
length = length + A[L[i - 1], L[i]];
}
if (length < minLength)
{
minLength = length;
minPath = path;
}
}
// Hàm đệ quy tìm mọi đường đi ngắn nhất giữa hai đỉnh
void TryBFS(int edge)
{
if (L[edge] == dest)
{
// Mảng L là mảng lưu kết quả đường đi
SaveResult(edge);
}
else
{
for (int i = 1; i <= n; i++)
if (A[L[edge], i] > 0 && Tick[i] == 0)
{
L[edge + 1] = i;
Tick[i] = 1;
TryBFS(edge + 1);
L[edge + 1] = 0;
Tick[i] = 0;
}
}
}
#endregion
#region Ford-Bellman
int[] d;
int[] Trace;
void InitializeFord_Bellman()
{
d = new int[n + 1];
Trace = new int[n + 1];
for (int i = 1; i <= n; i++)
{
d[i] = MAXINT;
for (int j = 1; j <= n; j++)
{
if (A[i, j] == 0 && i != j)
A[i, j] = MAXINT;
}
}
d[source] = 0;
}
void Restore()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (A[i, j] == MAXINT)
A[i, j] = 0;
}
}
}
void Ford_Bellman()
{
bool isStop;
for (int i = 1; i < n; i++)
{
isStop = true;
for (int u = 1; u <= n; u++)
for (int v = 1; v <= n; v++)
{
if (A[u, v] + d[u] < d[v])
{
d[v] = d[u] + A[u, v];
Trace[v] = u;
isStop = false;
}
}
if (isStop) break;
}
}
string PrintResult()
{
string res = "";
if (d[dest] == MAXINT)
{
res = "Không có đường đi từ " + source + "đến " + dest;
}
else
{
int path = d[dest];
while (dest != source)
{
res += dest + " <-- ";
dest = Trace[dest];
}
res += source + ": " + path;
}
return res;
}
#endregion
#region Dijkstra
string Dijkstra(int[,] A, int n, int src, int des)
{
int[] DanhDau = new int[n + 1];
int[] Nhan = new int[n + 1];
int[] Truoc = new int[n + 1];
int XP, min, i;
for (i = 1; i <= n; i++)
{
Nhan[i] = MAXINT;
DanhDau[i] = 0;
Truoc[i] = src;
}
Nhan[src] = 0;
DanhDau[src] = 1;
XP = src;
while (XP != des)
{
for (int j = 1; j <= n; j++)
if (A[XP, j] > 0 && Nhan[j] > A[XP, j] + Nhan[XP] && DanhDau[j] == 0)
{
Nhan[j] = A[XP, j] + Nhan[XP];
Truoc[j] = XP;
}
min = MAXINT;
for (int j = 1; j <= n; j++)
if (min > Nhan[j] && DanhDau[j] == 0)
{
min = Nhan[j];
XP = j;
}
DanhDau[XP] = 1;
}
// Truy vết đường đi và in kết quả
string[] temp = new string[n + 1];
temp[0] = des.ToString();
temp[1] = Truoc[des].ToString();
i = Truoc[des];
int count = 2;
while (i != src)
{
i = Truoc[i];
temp[count] = i.ToString();
count++;
}
string res = "";
for (i = count - 1; i >= 1; i--)
res += temp[i] + " --> ";
res += temp[0] + ": " + Nhan[des];
return res;
}
#endregion
#region Các hàm xử lý sự kiện
private void btnExecute_Click(object sender, EventArgs e)
{
try
{
source = Convert.ToInt16(txtSource.Text);
dest = Convert.ToInt16(txtDest.Text);
if (source >= minVertex && dest <= n)
{
var watch = new Stopwatch();
watch.Start();
if (rdoBFS.Checked)
{
InitializeBFS();
TryBFS(source);
minPath = minPath.Substring(0, minPath.Length - 3);
rtbResult.Text = minPath + ": " + minLength;
}
else if (rdoFordBellman.Checked)
{
InitializeFord_Bellman();
Ford_Bellman();
rtbResult.Text = PrintResult();
Restore();
}
else if (rdoDijkstra.Checked)
{
rtbResult.Text = Dijkstra(A, n, source, dest);
}
else if (rdoYen.Checked)
{
int numberYen = Convert.ToInt32(txtNumberYen.Text);
spAlg = new DijkstraShortestPathAlg(graph);
yenAlg = new YenTopKShortestPathsAlg(graph);
Path sp = spAlg.get_shortest_path(graph.get_vertex(source), graph.get_vertex(dest));
rtbResult.Text = "Top " + numberYen + " đường đi ngắn nhất: \n";
List<Path> shortest_paths_list = yenAlg.get_shortest_paths(
graph.get_vertex(source), graph.get_vertex(dest), numberYen);
foreach (Path path in shortest_paths_list)
{
rtbResult.Text += path.ToString() + "\n";
}
}
watch.Stop();
lbTime.Text = "Thời gian thực hiện chương trình: " + watch.Elapsed.TotalMilliseconds + " ms";
}
else
{
MessageBox.Show("Điểm bắt đầu phải >= " + minVertex + " và Điểm đến phải <= " + n,
"Đường đi ngắn nhất", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void mniExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void mniOpenFile_Click(object sender, EventArgs e)
{
try
{
Reset();
Cursor.Current = Cursors.WaitCursor;
ReadData();
Cursor = Cursors.Default;
}
catch (Exception ex)
{
Cursor.Current = Cursors.Default;
MessageBox.Show(ex.Message, "Đường đi ngắn nhất", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void rdoYen_CheckedChanged(object sender, EventArgs e)
{
if (rdoYen.Checked)
{
txtNumberYen.Enabled = true;
}
else
{
txtNumberYen.Enabled = false;
}
}
private void mniReset_Click(object sender, EventArgs e)
{
Reset();
}
#endregion
}
}