Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Shortest Path Algorithms: Dijkstra and Floyd - Prof. Nguyen, Cheat Sheet of Mathematics

An overview of two popular algorithms for finding the shortest path in a weighted graph: dijkstra's algorithm and floyd's algorithm. Dijkstra's algorithm is used to find the shortest path from a single source vertex to all other vertices in the graph, while floyd's algorithm is used to find the shortest paths between all pairs of vertices. The step-by-step implementation of these algorithms, including the initialization, update, and termination steps. It also includes several examples to illustrate the application of these algorithms on sample graphs. Key concepts such as the distance function, predecessor function, and the construction of the shortest path tree or matrix. This comprehensive coverage of the dijkstra and floyd algorithms makes this document a valuable resource for students and professionals studying graph theory, algorithms, and optimization.

Typology: Cheat Sheet

2019/2020

Uploaded on 06/11/2023

nguyen-xuan-vi
nguyen-xuan-vi 🇻🇳

2 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ĐƯỜNG ĐI NGẮN NHẤT
GV: Lê Mậu Long
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Shortest Path Algorithms: Dijkstra and Floyd - Prof. Nguyen and more Cheat Sheet Mathematics in PDF only on Docsity!

ĐƯỜNG ĐI NGẮN NHẤT

GV: Lê Mậu Long

Thuật toán DIJKSTRA.

 Thuật toán FLOYD.

Nội dung

 Xét đồ thị có hướng có trọng số liên thông G = (V, E). Bài

toán đặt ra là tìm đường đi ngắn nhất (tổng trọng số nhỏ

nhất) giữa 2 đỉnh bất kỳ cho trước.

 Trọng số cạnh = ký hiệu là ()

 Ma trận trọng số được xác định như sau:

, =

() ∞

ế Î ế Ï

Bài toán

Bài toán đặt ra là tìm đường đi ngắn nhất từ đỉnh gốc 0 đến đỉnh

bất kỳ.

Xây dựng hai hàm:

  • Hàm độ dài đường đi

: →

↦ : độ à đườ đ ừ 0 đế

  • Hàm xác định đường đi

: →

↦ : đỉℎ ướ ê đườ đ đế

Thuật toán DIJKSTRA

Ví dụ

A

B C

D E H

F G

2 8

7

3

2

3

2

3 5 6

(^42)

Ma trận trọng số

B C D E F G H [, ] [2, A] [-, A] [-, A] [-, A] [6, A] [-, A] [-, A]

Ví dụ

A

B (^) C

D E H

F G

2 8

7

3

2

3

2

3 6 6

(^42)

B C D E F G H Chọn [, ] [2, A] [-, A] [-, A] [-, A] [6, A] [-, A] [-, A] B [9, B] [10, B] [5, B] [6, A] [-, A] [-, A]

B C D E F G H Chọn [, ] [2, A] [-, A] [-, A] [-, A] [6, A] [-, A] [-, A] B [9, B] [10, B] [5, B] [6, A] [-, A] [-, A] E [8, E] [7, E] [6, A] [9, E] [-, A]

B C D E F G H Chọn [, ] [2, A] [-, A] [-, A] [-, A] [6, A] [-, A] [-, A] B [9, B] [10, B] [5, B] [6, A] [-, A] [-, A] E [8, E] [7, E] [6, A] [9, E] [-, A] F [8, E] [7, E] [9, E] [-, A]

B C D E F G H Chọn [, ] [2, A] [-, A] [-, A] [-, A] [6, A] [-, A] [-, A] B [9, B] [10, B] [5, B] [6, A] [-, A] [-, A] E [8, E] [7, E] [6, A] [9, E] [-, A] F [8, E] [7, E] [9, E] [-, A] D [8, E] [9, E] [-, A]

B C D E F G H Chọn [, ] [2, A] [-, A] [-, A] [-, A] [6, A] [-, A] [-, A] B [9, B] [10, B] [5, B] [6, A] [-, A] [-, A] E [8, E] [7, E] [6, A] [9, E] [-, A] F [8, E] [7, E] [9, E] [-, A] D [8, E] [9, E] [-, A] C [9, E] [10, C]

B C D E F G H Chọn [, ] [2, A] [-, A] [-, A] [-, A] [6, A] [-, A] [-, A] B [9, B] [10, B] [5, B] [6, A] [-, A] [-, A] E [8, E] [7, E] [6, A] [9, E] [-, A] F [8, E] [7, E] [9, E] [-, A] D [8, E] [9, E] [-, A] C [9, E] [10, C] G [10, C] H

Faculty of Information Technology

II. Thuật toán FLOYD

10

 Bài toán: Tìm đường đi ngắn nhất giữa 2 đỉnh bất kỳ

 Xây dựng 2 ma trận:

= (^) ; , = 1, … , : ℎể ℎệ độ à đườ đ = (^) ; , = 1, … , : ℎể ℎệ đỉℎ ế ê đườ đ

 Thuật toán Floyd:

  • B1: Khởi tạo ^ à ^ :

oVới , = 1, … :

^ = (, )

^ =

Thuật toán FLOYD

Ví dụ

A B

D E

2

7

4

1 2

C

F

4

2

3 1

 Ma trận , 0

− [, ] [−, ]

[−, ] − [, ]

[−, ] [−, ] −

[, ] [−, ] [−, ]

[−, ] [, ] [−, ]

[−, ] [−, ] [, ]

[−, ] [, ] [−, ]

[, ] [−, ] [, ]

[−, ] [, ] [−, ]

− [−, ] [−, ]

[−, ] − [−, ]

[−, ] [−, ] −

A B C D E F

Ví dụ

A B

D E

2

7

4

1 2

C

F

4

2

3 1

 Ma trận , 1

− [, ] [−, ]

[−, ] − [, ]

[−, ] [−, ] −

[, ] [−, ] [−, ]

[−, ] [, ] [−, ]

[−, ] [−, ] [, ]

[−, ] [, ] [−, ]

[, ] [, ] [, ]

[−, ] [, ] [−, ]

− [−, ] [−, ]

[, ] − [−, ]

[−, ] [−, ] −

A B C D E F

Ví dụ

A B

D E

2

7

4

1 2

C

F

4

2

3 1

 Ma trận , 3

− [, ] [, ]

[−, ] − [, ]

[−, ] [−, ] −

[, ] [, ] [, ]

[−, ] [, ] [, ]

[−, ] [−, ] [, ]

[−, ] [, ] [, ]

[, ] [, ] [, ]

[−, ] [, ] [, ]

− [, ] [, ]

[, ] − [, ]

[−, ] [, ] −

A B C D E F

Ví dụ

A B

D E

2

7

4

1 2

C

F

4

2

3 1

 Ma trận , 4

− [, ] [, ]

[−, ] − [, ]

[−, ] [−, ] −

[, ] [, ] [, ]

[−, ] [, ] [, ]

[−, ] [−, ] [, ]

[−, ] [, ] [, ]

[, ] [, ] [, ]

[−, ] [, ] [, ]

− [, ] [, ]

[, ] − [, ]

[−, ] [, ] −

A B C D E F

Ví dụ

A B

D E

2

7

4

1 2

C

F

4

2

3 1

 Ma trận , 6

− [, ] [, ]

[, ] − [, ]

[, ] [, ] −

[, ] [, ] [, ]

[, ] [, ] [, ]

[, ] [, ] [, ]

[, ] [, ] [, ]

[, ] [, ] [, ]

[, ] [, ] [, ]

− [, ] [, ]

[, ] − [, ]

[, ] [, ] −

A B C D E F

Ví dụ

A B

D E

2

7

4

1 2

C

F

4

2

3 1

 Ma trận , 6

− [, ] [, ]

[, ] − [, ]

[, ] [, ] −

[, ] [, ] [, ]

[, ] [, ] [, ]

[, ] [, ] [, ]

[, ] [, ] [, ]

[, ] [, ] [, ]

[, ] [, ] [, ]

− [, ] [, ]

[, ] − [, ]

[, ] [, ] −

A

B

C

D

E

  • Từ A đến F: 12 F A----D----B----E----C----F
  • Từ F đến A: 4 F----B----E----A