1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25...
-
Upload
patrick-greer -
Category
Documents
-
view
217 -
download
0
Transcript of 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25...
![Page 1: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/1.jpg)
1
Shortest Path Problem
Topic 11
ITS033 – Programming & Algorithms
CB
A
E
D
F
0
328
5 8
48
7 1
2 5
2
3 9
Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.
School of Information, Computer and Communication Technology (ICT)
Sirindhorn International Institute of Technology (SIIT)
Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit
[email protected] 5013505 X 2005
![Page 2: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/2.jpg)
2
ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force Algorithm - Brute Force AlgorithmTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08 - Transform and ConquerTopic 09Topic 09 - Graph Algorithms - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power
http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.vcharkarn.com/vlesson/7
Midterm
![Page 3: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/3.jpg)
3
Weighted Graphs
In a weighted graph, each edge has an associated numerical value, called the weight of the edge.
Edge weights may represent, distances, costs, etc.
![Page 4: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/4.jpg)
4
Weighted Graphs
Example: In a flight route graph, the weight of an edge
represents the distance in miles between the endpoint airports
ORDPVD
MIADFW
SFO
LAX
LGA
HNL
849
802
13871743
1843
10991120
1233337
2555
142
1205
![Page 5: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/5.jpg)
5
Shortest Path Problem
Given a weighted graph and two vertices u and v, we want to find a path of minimum total weight between u and v.
Length of a path is the sum of the weights of its edges.
![Page 6: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/6.jpg)
6
Shortest Path Problem
Example:Find the Shortest path between SFO and MIA
ORDPVD
MIADFW
SFO
LAX
LGA
HNL
849
802
13871743
1843
10991120
1233
337
2555
142
1205
![Page 7: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/7.jpg)
7
Shortest Path Problem Example:
Shortest path between HNL and PVD
ORDPVD
MIADFW
SFO
LAX
LGA
HNL
849
802
13871743
1843
10991120
1233
337
2555
142
1205
![Page 8: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/8.jpg)
8
Shortest Path Problem
Applications Internet packet routing Flight reservations Driving directions
![Page 9: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/9.jpg)
9
Internet as a graph
![Page 10: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/10.jpg)
10
Shortest Path Properties
Property 1:A subpath of a shortest path is itself a shortest path
Property 2:There is a tree of shortest paths from a start vertex to all the other vertices
![Page 11: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/11.jpg)
11
Shortest Path Properties
Example:Tree of shortest paths from PVD
ORDPVD
MIADFW
SFO
LAX
LGA
HNL
849
802
13871743
1843
10991120
1233
337
2555
142
1205
![Page 12: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/12.jpg)
12
Shortest Path Problem: Dijkstra Algorithm
Topic 11.1 ITS033 – Programming & Algorithms
CB
A
E
D
F
0
328
5 8
48
7 1
2 5
2
3 9
Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.
School of Information, Computer and Communication Technology (ICT)
Sirindhorn International Institute of Technology (SIIT)
Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit
[email protected] 5013505 X 2005
![Page 13: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/13.jpg)
13
Dijkstra’s Algorithm single-source shortest-paths problem: for a
given vertex called the source in a weighted
connected graph, find shortest paths to all its other vertices.
The single-source shortest-paths problem asks for a family of paths, each leading from the source to a different vertex in the graph, though some paths may, of course, have edges in common.
The best-known algorithm for the single-source shortest-paths problem,: Dijkstra’s algorithm.
However, this algorithm is applicable to graphs with nonnegative weights only.
![Page 14: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/14.jpg)
14
Shortest-Path Algorithm
Single shortest path problem:Single shortest path problem:Given as input a weighted graph, G = (V,E), and a Given as input a weighted graph, G = (V,E), and a
distinguished vertex, distinguished vertex, ss, find the shortest weighted , find the shortest weighted
path from path from ss to any vertex in G. to any vertex in G.
![Page 15: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/15.jpg)
15
Dijkstra’s Algorithm
Assumptions: the graph is connected the edges are undirected (or directed) the edge weights are nonnegative
![Page 16: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/16.jpg)
16
Dijkstra’s Algorithm
The distance of a vertex v from a
vertex s is the length of a shortest
path between s and v
Dijkstra’s algorithm computes the distances of all the vertices from a
given start vertex s
![Page 17: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/17.jpg)
17
Dijkstra’s Algorithm
Idea of Dijkstra’s algorithm.
The subtree of the shortest paths already found is shown in bold. The next nearest to the source v0
vertex, u*, is selected by comparing the lengths of the subtree’s paths increased by the distances to vertices adjacent to the subtree’s vertices.
![Page 18: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/18.jpg)
18
Dijkstra’s Algorithm First, it finds the shortest path from the
source to a vertex nearest to it,
then to a second nearest, and so on.
Before its ith iteration commences, the algorithm has already identified the shortest paths to i - 1 other vertices nearest to the source.
These vertices, the source, and the edges of the shortest paths leading to them from the source form a subtree Ti of the given graph
![Page 19: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/19.jpg)
19
Dijkstra’s Algorithm
We grow a “group” of vertices, beginning with s and eventually covering all the vertices
We store with each vertex v a label d(v) representing the distance of v from s in the subgraph consisting of the cloud and its adjacent vertices
![Page 20: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/20.jpg)
20
Dijkstra’s Algorithm To facilitate the algorithm’s operations, we label each
vertex with two labels.
(1) The numeric label d indicates the length of the shortest path from the source to this vertex found by the algorithm so far; when a vertex is added to the tree, d indicates the length of the shortest path from the source to that vertex.
(2) Label indicates the name of the next-to-last vertex on such a path, i.e., the parent of the vertex in the tree being constructed.
![Page 21: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/21.jpg)
21
Dijkstra’s Algorithm After we have identified a vertex u* to be added
to the tree, we need to perform two operations:
Move u* from the fringe to the set of tree vertices.
For each remaining fringe vertex u that is connected to u* by an edge of weight w(u*, u) such that du* + w(u*, u) < du, update the labels of u by u* and du* + w(u*, u), respectively.
![Page 22: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/22.jpg)
22
Dijkstra’s Algorithm
At each step We add to the cloud the vertex u outside the
cloud with the smallest distance label, d(u)
We update the labels of the vertices adjacent to u
![Page 23: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/23.jpg)
23
Dijkstra’s Algorithm
![Page 24: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/24.jpg)
24
Dijkstra’s Algorithm Analysis The time efficiency of Dijkstra’s algorithm
depends on the data structures used for implementing the priority queue and for representing an input graph itself.
it is in θ(|V |2) for graphs represented by their weight matrix and the priority queue implemented as an unordered array.
For graphs represented by their adjacency linked lists and the priority queue implemented as a min-heap, it is in O(|E| log |V |).
![Page 25: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/25.jpg)
25
Example 2
CB
A
E
D
F
0
442288
48
7 1
2 5
2
3 9
3Step 1Step 1
![Page 26: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/26.jpg)
26
Example
CB
A
E
D
F
0
332288
55 1111
48
7 1
2 5
2
3 9
Step 2Step 2
![Page 27: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/27.jpg)
27
Example
CB
A
E
D
F
0
332288
55 88
48
7 1
2 5
2
3 9
Step 3Step 3
![Page 28: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/28.jpg)
28
Example
CB
A
E
D
F
00
33277
55 88
48
7 1
2 5
2
3 9
Step 4Step 4
![Page 29: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/29.jpg)
29
Example (cont.)
CB
A
E
D
F
0
332277
55 88
48
7 1
2 5
2
3 9
Step 5Step 5
![Page 30: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/30.jpg)
30
Example (cont.)
CB
A
E
D
F
0
332277
55 88
48
7 1
2 5
2
3 9
Step 6Step 6
![Page 31: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/31.jpg)
31
Find shortest path from Melbourne to every town ?
![Page 32: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/32.jpg)
32
Dijkstra’s algorithm – Exercise
![Page 33: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/33.jpg)
33
An example on Directed graph
![Page 34: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/34.jpg)
34
Why Dijkstra’s Algorithm Works
Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance.
CB
A
E
D
F
0
327
5 8
48
7 1
2 5
2
3 9
![Page 35: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/35.jpg)
35
Why It Doesn’t Work for Negative-Weight Edges
If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud.
CB
A
E
D
F
0
457
5 9
48
7 1
2 5
6
0 -8-8
Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance.
C’s true distance is 1, but it is already in the cloud with d(C)=5!
![Page 36: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/36.jpg)
36
Shortest Path Problem: Bellman-Ford Algorithm
Topic 11.2 ITS033 – Programming & Algorithms
CB
A
E
D
F
0
328
5 8
48
7 1
2 5
2
3 9
Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.
School of Information, Computer and Communication Technology (ICT)
Sirindhorn International Institute of Technology (SIIT)
Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit
[email protected] 5013505 X 2005
![Page 37: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/37.jpg)
37
Bellman-Ford Algorithm
The Bellman–Ford algorithm computes single-source shortest paths in a weighted digraph (where some of the edge weights may be negative). Dijkstra's algorithm accomplishes the same problem with a lower running time, but requires edge weights to be non-negative. Thus, Bellman–Ford is usually used only when there are negative edge weights.
![Page 38: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/38.jpg)
38
Bellman-Ford Algorithm
Bellman-Ford is in its basic structure very similar to Dijkstra's algorithm, but instead of greedily selecting the minimum-weight node not yet processed to relax, it simply relaxes all the edges, and does this |V| − 1 times, where |V| is the number of vertices in the graph. The repetitions allow minimum distances to accurately propagate throughout the graph, since, in the absence of negative cycles, the shortest path can only visit each node at most once.
Unlike the greedy approach, which depends on certain structural assumptions derived from positive weights, this straightforward approach extends to the general case.
![Page 39: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/39.jpg)
39
Bellman-Ford Algorithm
Works even with negative-weight edges
Must assume directed edges (for otherwise we would have negative-weight cycles)
Running time: O(nm).
![Page 40: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/40.jpg)
40
Bellman-Ford Algorithm
Algorithm BellmanFord(G, s)for all v G.vertices()
if v ssetDistance(v, 0)
else setDistance(v, )
for i 1 to n-1 dofor each e G.edges()
{ relax edge e }u G.origin(e)z G.opposite(u,e)r getDistance(u) weight(e)if r getDistance(z)
setDistance(z,r)
![Page 41: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/41.jpg)
41
Bellman-Ford Example
0
48
7 1
-2 5
-2
3 9
Nodes are labeled with their d(v) values
Step 1Step 1
![Page 42: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/42.jpg)
42
-2
Bellman-Ford Example
0
48
7 1
-2 53 9
Nodes are labeled with their d(v) values
88 -2-2 44
Step 2Step 2
![Page 43: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/43.jpg)
43
Bellman-Ford ExampleNodes are labeled with their d(v) values
-2
-2-28
0
4
48
7 1
-2 53 9
-1-155
611
99
Step 3Step 3
![Page 44: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/44.jpg)
44
Bellman-Ford ExampleNodes are labeled with their d(v) values
-25
0
1
-1
9
48
7 1
-2 5
-2
3 94
Step 4Step 4
![Page 45: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/45.jpg)
45
Summary
• Dijkstra's algorithmDijkstra's algorithmDefinition:Definition: An algorithm to find the shortest paths from a single An algorithm to find the shortest paths from a single source vertex to all other vertices in a weighted, undirected source vertex to all other vertices in a weighted, undirected graph. All weights must be nonnegative. The time complexity O(E graph. All weights must be nonnegative. The time complexity O(E + V log V), where V is the number of vertices and E is the number + V log V), where V is the number of vertices and E is the number of edges. of edges.
• Bellman-Ford ExampleBellman-Ford ExampleWorks even with negative-weight edgesWorks even with negative-weight edgesIteration i finds all shortest paths that use i edges.Iteration i finds all shortest paths that use i edges.Running time: O(nm).Running time: O(nm).
![Page 46: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/46.jpg)
46
Maximum Profit
![Page 47: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/47.jpg)
47
ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force Algorithm - Brute Force AlgorithmTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08 - Transform and ConquerTopic 09Topic 09 - Graph Algorithms - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power
http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.vcharkarn.com/vlesson/7
Midterm
![Page 48: 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,](https://reader033.fdocuments.us/reader033/viewer/2022051401/56649e445503460f94b381c3/html5/thumbnails/48.jpg)
48
End of Chapter 11
Thank you!