Đây là chương trình được mình viết trong bài niên luận Đại học lập trình mô phỏng lại việc tìm đường đi ngắn nhất trên đồ thị có hướng và vô hướng bằng thuật toán Dijkstra.
Trang 1CODE DIJKSTRA
restar :
# Khai báo các thư viện
with(Maplets[Elements]):
with(LinearAlgebra):
with(GraphTheory):
with(Maplets[Tools]):
with(plots):
# Hàm nhập Ma Trận Trọng Số cho đồ thị (Số chiều nhập tối đa cho
ma tran là 9 )
Nhap := proc ()
global G, L, t, n;
t := 1;
SetPL1();
L := Matrix([[0, 1, 0, 0, 0, 20], [0, 0, 2, 0, 0, 0], [7, 0, 0,
20, 5, 6], [0, 0, 0, 0, 0, 6], [1, 0, 0, 5, 0, 0], [0, 9, 0, 0,
4, 0]]);
G := Student[LinearAlgebra][MatrixBuilder](L, 2, 9, square = true);
n := RowDimension(G):
DT()
end:
# Hàm dùng để ràng buộc hiển thị Kết Quả hoặc là hiển thị thông báo chưa có đủ đầu vào.
QuyenTuyChon := proc (XP, KT)
global t;
if t = 0 then Thongbao1() else
HienThiKQ(XP, KT)
fi:
end:
#Khởi tạo giá trị các mảng cho việc tính toán trong các hàm Tìm đường đi.
khoitao := proc (XP, KT)
local i, j;
global Truoc, DanhDau, True, False, TU, n, G:
True := 1:
False := 0:
Truoc := Matrix(1, n):
DanhDau := Matrix(1, n):
TU := Matrix(1, n):
for i to n do
for j to n do
if G[i, j] = 0 and i <> j then
G[i, j] := infinity
Trang 2od:
od:
for i to n do Truoc[1, i] := XP:
DanhDau[1, i] := True:
TU[1, i] := G[XP, i]
od:
DanhDau[1, XP] := False;
Truoc[1, XP] := XP
end:
# Hàm tiến hành lựa chọn đường đi ngắn nhất bằng thuật toán Dijktra
TimDijkstra := proc (XP, KT)
local u, v, Min, i;
global TU, G, DanhDau, Truoc;
while 1 = 1 do u := 0;
Min := infinity;
for i to n do
if DanhDau[1, i] = True and TU[1, i] < Min then
Min := TU[1, i];
u := i end if end do;
if u = 0 or u = KT then
break
end
if:
DanhDau[1, u] := False;
for v to n do
if DanhDau[1, v] = True and TU[1, u]+G[u][v] < TU[1, v] then Truoc[1, v] := u;
TU[1, v] := TU[1, u]+G[u][v]
fi:
od:
od:
end:
KetQua:=proc(XP,KT)
local i,m:=0,t,k,j,T,H,l;
global KQ,Truoc,G,FN,n;
FN:=Matrix(n);
if TU[1, KT]=infinity then t:=1 ;
else
t:=KT;
while t<>XP do
m:=m+1;
t:=Truoc[1, t];
od:
KQ:=Matrix(1, m+1);
Trang 3t:=KT;
k:=0;
while t<>XP do
KQ[1, m+1-k]:=t;
t:=Truoc[1, t];
k:=k+1;
od:
KQ[1,1]:=XP;
fi: #print(KQ);
for i from 1 to n do
for j from 1 to n do
if G[i,j]=infinity then
G[i,j]:=0;
fi:
od:
od:
for i from 1 to m do
FN[KQ[1, i],KQ[1, i+1]]:=G[KQ[1,i],KQ[1,i+1]];
od:
end:
SetPL1 := proc ()
local p;
p := plot(0, x = 0 0, axes = none, thickness = 0);
Set('PL1' = p)
end:
SetPL2 := proc ()
local p;
p := plot(0, x = 0 0, axes = none, thickness = 0);
Set('PL2' = p)
end:
Thongbao1:=proc()
Maplets[Display]( Maplet(AlertDialog(title = "Maplet-Chuyen dang toan phuong ve dang chinh tac:","Ham so nay khong phai dang toan phuong, ban vui long nhap lai! ",'onapprove' = Shutdown(),
'oncancel' = Shutdown())));
end:
Thongbao2 := proc ()
Maplets[Display](Maplet(AlertDialog(title = "Maplet-Tim duong di ngan nhat tren do thi thông báo:", "Gia tri dau vao khong thich hop ban vui long nhap lai !",
'onapprove' = Shutdown(),
'oncancel' = Shutdown())))
end:
DTKQ := proc ()
global p1, G, T, FN, H;
T := Graph(G);
Trang 4H := Graph(FN);
HighlightEdges(T, H, red);
p1 := DrawGraph(T, style = circle);
Set('PL2' = p1)
end :
DT := proc ()
global p1, G, T;
T := Graph(G);
p1 := DrawGraph(T, style = circle);
Set('PL1' = p1)
end:
HienThiKQ := proc (XP, KT)
global KQ, TU, n;
if n < XP or n < KT or XP < 1 or KT < 1 then Thongbao2()
else khoitao(XP, KT):
TimDijkstra(XP, KT):
KetQua(XP, KT):
DTKQ(XP, KT):
Maplets[Tools][Set]('TP'(value) = XP):
Maplets[Tools][Set]('TT'(value) = KT):
Maplets[Tools][Set]('CP'(value) = TU[1, KT]):
if TU[1, KT] <> infinity then
Maplets[Tools][Set]('HT'(value) = KQ) ;
else
Maplets[Tools][Set]('HT'(value) = KQ);
fi:
fi:
end:
CapNhat := proc ()
global t;
t := 0
end:
MyMaplet:=Maplet(
'onstartup' = 'A1',
Window['W1']("Maplet-Dijkstra",'menubar'='MNB',
[[BoxColumn(border=true, background ="#57E9CD",Label("PHẦN MỀM TÌM ĐƯỜNG ĐI NGẮN NHẤT TRÊN ĐỒ THỊ",'foreground' = blue,'font' = Font(bold, 16)),Label("BẰNG THUẬT TOÁN DIJKSTRA",'foreground' = blue,'font' = Font(bold, 16)))],[BoxColumn(border=true,
background ="#57E9CD",Label("ĐỒ THỊ GIẢ THIẾT BÀI
TOÁN",'foreground' = blue,'font' = Font(bold, 16)),Plotter['PL1'] (height = 300))],[Button("Nhập Ma Trận Kề",foreground =
"#294509", background = "#0A9EB0", Action(Evaluate('function' = 'Nhap()'))),Button("Tùy Chọn Tìm Kiếm",foreground = "#294509", background = "#0A9EB0", Action(RunWindow('W4'))), Button("Xóa Dữ
Trang 5Liệu",foreground = "#294509", background =
"#0A9EB0",Action(Evaluate('function' = 'CapNhat()'),
Evaluate('function' = 'SetPL1()'))),Button("Thoát",foreground =
"#294509", background = "#0A9EB0", Action(Evaluate('function' = 'CapNhat()'), Action(RunWindow('W2')))) ]]
),
Window['W4'](
"Tùy chọn tìm kiếm-hiển thị kết quả",
[BoxColumn(border = true, background = "#57E9CD",
[Label("TÙY CHỌN TÌM KIẾM – HIỂN THỊ KẾT QUẢ",'foreground' = blue, 'font' = Font(bold, 13))],
[Label("ĐỈNH XUẤT PHÁT",'foreground' = blue, 'font' =
Font(Italic, 12)),TextField['XP'](width = 3, "1"),Label(" ĐỈNH KẾT THÚC",'foreground' = blue, 'font' = Font(Italic, 12)),
TextField['KT'](width = 3, "2")]),
[Button("Khởi động tìm kiếm",Action(SetOption(target = 'TP', value = ""),SetOption(target = 'TT', value = ""),SetOption(target
= 'CP', value = ""),SetOption(target = 'HT', value =
""),Evaluate('function' = 'SetPL2()'), Evaluate('function' = 'QuyenTuyChon(XP, KT)')))],
[BoxColumn(border = true, background = "#57E9CD",
Label("ĐỒ THỊ KẾT QUẢ TÌM KIẾM",'foreground' = blue, 'font' = Font(Italic, 12)),Plotter['PL2'](height = 300))],
BoxColumn(border = true, background = "#57E9CD",
[Label("KẾT QUẢ TÌM KIẾM",'foreground' = blue, 'font' =
Font(Italic, 12))],
[Label("CHI PHÍ TỪ ĐỈNH",'foreground' = blue, 'font' =
Font(Italic, 12)), TextField['TP'](width = 3, editable = false,
""),
Label("ĐẾN ĐỈNH",'foreground' = blue, 'font' = Font(Italic, 12)), TextField['TT'](width = 3, editable = false, ""),
Label("LÀ",'foreground' = blue, 'font' = Font(Italic, 12)),
TextField['CP'](width = 3, editable = false, "")],
[Label("HÀNH TRÌNH ĐƯỜNG ĐI",'foreground' = blue, 'font' =
Font(Italic, 12)),MathMLViewer['HT'](height = 38, width = 180, background = "#E8EEEE", foreground = "#253BA3")]),
[Button("Trở lại",Action(SetOption(target = 'TP', value =
""),SetOption(target = 'TT', value = ""),SetOption(target = 'CP', value = ""),SetOption(target = 'HT', value =
""),Evaluate('function' = 'SetPL2()'),CloseWindow('W4'))),
Trang 6Button("Thoát",Action(Evaluate('function' = 'CapNhat()'),
Action(RunWindow('W2'))))]])
,
Window['W2']("Maplet-Tìm đường đi ngắn nhất bằng thuật toán
Dijkstra thông báo",[["Bạn thực sự muốn thoát khỏi hệ thống?"], [Button("Ok", Action(Evaluate('function' = 'Capnhat()'),
Shutdown())),Button("Cancel", CloseWindow('W2'))]]),
MenuBar['MNB'](Menu("File", MenuItem("Close", Shutdown())),
Menu("Run",Menu("Tìm Dijkstra",MenuItem("Nhập ma trận
kề",Action(Evaluate('function' = 'Nhap(XP, KT)'))),
MenuSeparator(),MenuItem("Tùy chọn tìm
kiếm",Action(RunWindow('W3'))),MenuSeparator()),MenuSeparator(),M enuItem("Quit", Shutdown())),Menu("Help", MenuItem("quick Help ", Action(RunWindow('W3'))))),
Window['W3']("Giới thiệu-Trợ giúp",[["GIỚI THIỆU-TRỢ GIÚP"],
[TextBox['IB1'](background = "#BFE9E6" ,foreground =
"#1015E9" ,10 40, editable = false,"\n*Giới thiệu.
Họ và tên: Phan Văn Cương
Trường: Đại học Khoa Học Huế
Ngành: Toán tin Ứng Dụng
Lớp: Toán Tin K32\n*Hướng dẫn.
Đầu tiên hãy nhập vào ma trận liền kề, sau đó tiến hành điển các tùy chọn tìm kiếm bao gồm: Đỉnh Xuất phát, đỉnh Kết thúc Sau đó nhấn vào nút Khởi động tìm kiếm để xem kết quả ")],
[Button("Thoát", CloseWindow('W3'))]]),Action['A1']
(RunWindow('W1'))
):
Maplets[Display](MyMaplet):