shortest-path algorithms
DESCRIPTION
Shortest-Path Algorithms. BY นาย จิรวัฒน์ กรัณย์วิทยาการ ID 474487 ITE 209 Sec 01. Shortest-Path Algorithms. Shortest-Path Algorithms ใช้ directed graph ที่มี weighted Cost ของ path คือ cost ของแต่ละ edge path รวมกัน → “weighted path length” - PowerPoint PPT PresentationTRANSCRIPT
Shortest-Path Shortest-Path AlgorithmsAlgorithms
BYBY
นาย จิ�รวัฒน�นาย จิ�รวัฒน� กรณย� กรณย�วั�ทยาการวั�ทยาการ ID 474487 ITE 209 ID 474487 ITE 209 Sec 01Sec 01
Shortest-Path AlgorithmsShortest-Path Algorithms
Shortest-Path AlgorithmsShortest-Path Algorithms• ใช้� ใช้� directed graph directed graph ที่��มี� ที่��มี� weighted weighted • Cost Cost ของ ของ path path คื อ คื อ cost cost ของแต่�ละ ของแต่�ละ edge path edge path รวมีกั�นรวมีกั�น → → ““weighted path length”weighted path length”• ถ้�า ถ้�า unweighted graph,unweighted path length unweighted graph,unweighted path length
= N-1= N-1• Shortest path problem Shortest path problem คื อกัารหา คื อกัารหา shortest shortest
weighted path weighted path จากัจ�ดเร��มีต่�นจากัจ�ดเร��มีต่�น(s)(s)ไปยั�งที่�กัๆไปยั�งที่�กัๆ node node ใน ใน graphgraph
• Path Path ที่��มี� ที่��มี� negative cost negative cost จะหา จะหา shortest path shortest path ได�ยั��งยัากัได�ยั��งยัากั
Shortest-Path AlgorithmsShortest-Path Algorithms
1) unweighted shortest paths1) unweighted shortest paths• แต่�ละ แต่�ละ path path จะมี�คื�า จะมี�คื�า = 1= 1• Breadth-first search AND dept-first Breadth-first search AND dept-first
search strategy search strategy ใช้�คื$านวณหา ใช้�คื$านวณหา shortest shortest pathpath
Shortest-Path AlgorithmsShortest-Path Algorithms
2) Weighted shortest-paths (Djikstra’s 2) Weighted shortest-paths (Djikstra’s algorithm)algorithm) Djikstra’s algorithm Djikstra’s algorithm ใช้�หล�กักัารคืล�ายักั�บ ใช้�หล�กักัารคืล�ายักั�บ breadth-breadth-
first search strategy first search strategy จ�ดเป'นจ$าพวกั จ�ดเป'นจ$าพวกั greedy greedy algorithmalgorithm
• Shortest path Shortest path อาจไมี�ใช้�เส้�นที่างที่��ส้� *นที่��ส้�ด เพราะต่�องด+ อาจไมี�ใช้�เส้�นที่างที่��ส้� *นที่��ส้�ด เพราะต่�องด+ weight weight ประกัอบด�วยัเป'นหล�กัประกัอบด�วยัเป'นหล�กั
• Shortest path Shortest path เป'นเส้�นที่างที่��มี�ผลรวมี เป'นเส้�นที่างที่��มี�ผลรวมี weight weight น�อยัที่��ส้�ดน�อยัที่��ส้�ด
Shortest-Path AlgorithmsShortest-Path Algorithms
Graph with Negative edge costsGraph with Negative edge costs• ต่�วน�*ไมี�คื�อยักัารใช้�งานมีากัน�กั เพราะคื$านวณได�ยัากัต่�วน�*ไมี�คื�อยักัารใช้�งานมีากัน�กั เพราะคื$านวณได�ยัากั
มีากัมีากั• Weighted Weighted มี�คื�าต่�ดลบได� ที่$าให�กัารหา มี�คื�าต่�ดลบได� ที่$าให�กัารหา shortest shortest
path path ยั��งยัากัมีากัข-*นยั��งยัากัมีากัข-*น
Shortest-Path AlgorithmsShortest-Path Algorithms Shortest path in acyclic graph & Critical path Shortest path in acyclic graph & Critical path
analysisanalysis กัราฟที่��เป'น กัราฟที่��เป'น Acyclic graph Acyclic graph ส้ามีารถ้น$ามีาประยั�กัต่/ใช้�หา ส้ามีารถ้น$ามีาประยั�กัต่/ใช้�หา
critical path analysis critical path analysis ได�โดยัส้ามีารถ้แส้ดงให�เห1นได�จากั ได�โดยัส้ามีารถ้แส้ดงให�เห1นได�จากั activity node graph activity node graph ด�งร+ปด�งร+ป
Start
A(3)
B(2)
D(2)
C(3)
E(1)
F(3)
G(2)
H(1)
K(4)
Finish
Shortest-Path AlgorithmsShortest-Path Algorithms แต่�ละโหนดจะแที่นถ้-ง แต่�ละโหนดจะแที่นถ้-ง activity activity และเวลาที่��ต่�องใช้� และและเวลาที่��ต่�องใช้� และedge edge ที่��ที่��
เช้ �อมีระหว�างโหนดจะแส้ดงถ้-งล$าด�บว�า เช้ �อมีระหว�างโหนดจะแส้ดงถ้-งล$าด�บว�า activity activity ไหนที่��ต่�องที่$ากั�อนไหนที่��ต่�องที่$ากั�อนหน�าหล�งหน�าหล�ง
กัราฟในล�กัษณะน�*ถ้+กัน$าไปใช้�อยั�างแพร�หลายัในกัารกั$าหนดกัราฟในล�กัษณะน�*ถ้+กัน$าไปใช้�อยั�างแพร�หลายัในกัารกั$าหนดต่ารางกัารด$าเน�นงาน ต่ารางกัารด$าเน�นงาน time schedule time schedule ของงานพวกักั�อส้ร�าง ซึ่-�งของงานพวกักั�อส้ร�าง ซึ่-�งมี�กัใช้�เป'นต่�วช้�วยัต่อบคื$าถ้ามีหล�กัๆว�า โคืรงกัารน�*จะใช้�เวลาที่$าเส้ร1จเร1วมี�กัใช้�เป'นต่�วช้�วยัต่อบคื$าถ้ามีหล�กัๆว�า โคืรงกัารน�*จะใช้�เวลาที่$าเส้ร1จเร1วส้�ดกั��ว�น และช้�วยัต่อบคื$าถ้ามีที่��ว�า ส้�ดกั��ว�น และช้�วยัต่อบคื$าถ้ามีที่��ว�า activity activity ไหนบ�างที่��ส้ามีารถ้ที่$าไหนบ�างที่��ส้ามีารถ้ที่$าล�าช้�ากัว�ากั$าหนดได�และช้�าได�กั��ว�น โดยัไมี�ส้�งผลให�ภาพรวมีของล�าช้�ากัว�ากั$าหนดได�และช้�าได�กั��ว�น โดยัไมี�ส้�งผลให�ภาพรวมีของโคืรงกัารช้�าไปโคืรงกัารช้�าไป
Shortest-Path AlgorithmsShortest-Path Algorithms
โหนดไหนมี� โหนดไหนมี� input input มีากักัว�า มีากักัว�า 11 จะต่�องใช้� จะต่�องใช้� dummy dummy node node มีาช้�วยั ซึ่-�งจะเห1นได�จากัโหนด มีาช้�วยั ซึ่-�งจะเห1นได�จากัโหนด 6,8,11 6,8,11 เป'นต่�นเป'นต่�น
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
ขั้�นท�� ขั้�นท�� 11
J/2
Shortest-Path AlgorithmsShortest-Path Algorithms กัารหาเวลาที่��เร1วที่��ส้�ดที่��โคืรงกัารจะที่$าเส้ร1จกัารหาเวลาที่��เร1วที่��ส้�ดที่��โคืรงกัารจะที่$าเส้ร1จ(earliest)(earliest) ส้ามีารถ้หาได�จากักัารส้ามีารถ้หาได�จากักัาร
หา หา path path ที่��ยัาวที่��ส้�ดของกัราฟ โดยัจะเล อกัเส้�นที่างจากัคื�าที่��มีากัที่��ส้�ดด�งร+ปที่��ยัาวที่��ส้�ดของกัราฟ โดยัจะเล อกัเส้�นที่างจากัคื�าที่��มีากัที่��ส้�ดด�งร+ป
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
ขั้�นท�� ขั้�นท�� 11
0 2
J/2
Shortest-Path AlgorithmsShortest-Path Algorithms
EarliestEarliest
ขั้�นท�� ขั้�นท�� 22
1 2
3
4
6 6
5
7
8 8
9
10
11 11
A/2B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
0 2
4
3
J/2
Shortest-Path AlgorithmsShortest-Path Algorithms
EarliestEarliest
ขั้�นท�� ขั้�นท�� 33
1 2
3
4
6 6
5
7
8 8
9
10
11 11
A/2B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
0 2
4
3
4
J/2
Shortest-Path AlgorithmsShortest-Path Algorithms
EarliestEarliest
ขั้�นท�� ขั้�นท�� 44
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/20 K/1
0
0
0
0
0
G/2
H/1
I/1
0 2
4
3
4
4
8
6
J/2
Shortest-Path AlgorithmsShortest-Path Algorithms
EarliestEarliest
ขั้�นท�� ขั้�นท�� 55
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
0 2
4
3
4
4
8
6 8
J/2
Shortest-Path AlgorithmsShortest-Path Algorithms
EarliestEarliest
ขั้�นท�� ขั้�นท�� 66
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
0 2
4
3
4
4
8
6 8 10
J/2
Shortest-Path AlgorithmsShortest-Path Algorithms
EarliestEarliest
ขั้�นท�� ขั้�นท�� 77
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
0 2
4
3
4
4
8
6 8 10
11
11 J/2
Shortest-Path AlgorithmsShortest-Path Algorithms Earliest Earliest จากัร+ปให�ส้�งเกัต่ต่�วเลขที่��อยั+�บนวงกัลมี จะพบว�าเวลาที่��เร1วที่��ส้�ดที่��จากัร+ปให�ส้�งเกัต่ต่�วเลขที่��อยั+�บนวงกัลมี จะพบว�าเวลาที่��เร1วที่��ส้�ดที่��
จะที่$าโคืรงกัารน�*ได�เส้ร1จคื อ จะที่$าโคืรงกัารน�*ได�เส้ร1จคื อ 14 14 ว�น ต่ามีเส้�นที่าง ว�น ต่ามีเส้�นที่าง 1-2-4-7-8-8-10-11-111-2-4-7-8-8-10-11-11
ขั้�นท�� ขั้�นท�� 88
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
0 2
4
3
4
4
8
6 8 10
11
11
13
J/2
Shortest-Path AlgorithmsShortest-Path Algorithms Earliest Earliest จากัร+ปให�ส้�งเกัต่ต่�วเลขที่��อยั+�บนวงกัลมี จะพบว�าเวลาที่��เร1วที่��ส้�ดที่��จากัร+ปให�ส้�งเกัต่ต่�วเลขที่��อยั+�บนวงกัลมี จะพบว�าเวลาที่��เร1วที่��ส้�ดที่��
จะที่$าโคืรงกัารน�*ได�เส้ร1จคื อ จะที่$าโคืรงกัารน�*ได�เส้ร1จคื อ 14 14 ว�น ต่ามีเส้�นที่าง ว�น ต่ามีเส้�นที่าง 1-2-4-7-8-8-10-11-111-2-4-7-8-8-10-11-11
ขั้�นท�� ขั้�นท�� 88
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
0 2
4
3
4
4
8
6 8 10
11
11
13
J/2
14
Shortest-Path AlgorithmsShortest-Path Algorithms ส้$าหร�บกัารต่อบคื$าถ้ามีว�า ส้$าหร�บกัารต่อบคื$าถ้ามีว�า activity activity ไหนพอที่��จะล�าช้�าได�บ�างโดยัไหนพอที่��จะล�าช้�าได�บ�างโดยั
ไมี�ที่$าให�ที่�*งโคืรงกัารล�าช้�า เราจะต่�องเร��มีจากักัารหา ไมี�ที่$าให�ที่�*งโคืรงกัารล�าช้�า เราจะต่�องเร��มีจากักัารหา latest latest completion times completion times ให�ได�กั�อนโดยักัารที่$ายั�อนกัล�บจากัจ�ด ให�ได�กั�อนโดยักัารที่$ายั�อนกัล�บจากัจ�ด finish finish โดยัจะเล อกัเส้�นที่างจากัคื�าที่��น�อยัที่��ส้�ดแที่นด�งร+ปโดยัจะเล อกัเส้�นที่างจากัคื�าที่��น�อยัที่��ส้�ดแที่นด�งร+ป
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/113
14J/2
Shortest-Path AlgorithmsShortest-Path Algorithms LatestLatest
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
13
11
13
14J/2
ขั้�นท�� ขั้�นท�� 11
Shortest-Path AlgorithmsShortest-Path Algorithms LatestLatest
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/110
13
11
13
14J/2
ขั้�นท�� ขั้�นท�� 22
Shortest-Path AlgorithmsShortest-Path Algorithms LatestLatest
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/18 10
13
11
13
14J/2
ขั้�นท�� ขั้�นท�� 33
Shortest-Path AlgorithmsShortest-Path Algorithms LatestLatest
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
8
8
8 8 10
13
11
13
14J/2
ขั้�นท�� ขั้�นท�� 44
Shortest-Path AlgorithmsShortest-Path Algorithms LatestLatest
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
4
7
6
8
8
8 8 10
13
11
13
14J/2
ขั้�นท�� ขั้�นท�� 55
Shortest-Path AlgorithmsShortest-Path Algorithms LatestLatest
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/1
4
7
6
8
8
8 8 10
13
11
13
14J/2
ขั้�นท�� ขั้�นท�� 66
Shortest-Path AlgorithmsShortest-Path Algorithms LatestLatest
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/12
4
7
6
8
8
8 8 10
13
11
13
14J/2
ขั้�นท�� ขั้�นท�� 77
Shortest-Path AlgorithmsShortest-Path Algorithms LatestLatest
ขั้�นท�� ขั้�นท�� 88
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/12
4
7
6
8
8
8 8 10
13
11
13
14J/2
0
Shortest-Path AlgorithmsShortest-Path Algorithms
จากัน�*นให�เข�ยันต่�วเลขของที่�*งที่��หาได�จากั จากัน�*นให�เข�ยันต่�วเลขของที่�*งที่��หาได�จากั Earliest completion Earliest completion time time และ และ Latest completion times Latest completion times เข�าด�วยักั�นแล�วเมี �อเอาเข�าด�วยักั�นแล�วเมี �อเอาคื�า คื�า Latest completion Latest completion ลบด�วยัคื�า ลบด�วยัคื�า Earliest completion Earliest completion
กั1จะได�คื�า กั1จะได�คื�า slack time slack time ที่�� ที่�� activity activity น�*นจะล�าช้�าได� แต่�ถ้�าลบแล�วได� น�*นจะล�าช้�าได� แต่�ถ้�าลบแล�วได� 00 แส้ดงว�า แส้ดงว�า activity activity น�*นจะล�าช้�าไมี�ได� หากัไมี�จะส้�งผลให�ที่�*งน�*นจะล�าช้�าไมี�ได� หากัไมี�จะส้�งผลให�ที่�*งโคืรงกัารล�าช้�า และเส้�นที่างที่��มี�คื�า โคืรงกัารล�าช้�า และเส้�นที่างที่��มี�คื�า slack time=0slack time=0 ต่ลอดเส้�นที่าง ต่ลอดเส้�นที่าง กั1คื อ กั1คื อ critical path critical path น��นเอง ด�งร+ปน��นเอง ด�งร+ป
Shortest-Path AlgorithmsShortest-Path Algorithms
1 2
3
4
6 6
5
7
8 8
9
10
11 11A/2
B/1
C/2
D/1
F/4
E/2 0 K/1
0
0
0
0
0
G/2
H/1
I/10 2
4
7
6
8
8
8 8 10
13
11
13
14J/2
0 2
4
3
4
4
8
6 8 10
11
11
13
14
Critical Path
Shortest-Path AlgorithmsShortest-Path Algorithms
ต่ารางแส้ดงคื�าแต่�ละ ต่ารางแส้ดงคื�าแต่�ละ activityactivity
activactivityity
timetime latelatestst
earliearliestest
slacslackk
AA 22 22 22 00
BB 11 77 33 44
CC 22 44 44 00
DD 11 88 44 44
EE 22 88 66 22
FF 44 88 88 00
GG 22 1010 1010 00
HH 11 1313 1111 22
II 11 1111 1111 00
JJ 22 1313 1313 00
KK 11 1414 1414 00