Document

advertisement
SFO
CHAPTER 13
GRAPH ALGORITHMS
LAX
ORD
DFW
ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH
DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND
MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO
2704
BOS
867
849
PVD
ORD
740
621
1846
MINIMUM SPANNING TREES
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
BWI
1090
DFW
946
1121
MIA
2342
REMINDER
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.
• Example:
• In a flight route graph, the weight of an edge represents the distance in miles between the endpoint
airports
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
MINIMUM SPANNING TREE
ORD
• Spanning subgraph
• Subgraph of a graph 𝐺 containing all the
vertices of 𝐺
1
DEN
• Spanning tree
• Spanning subgraph that is itself a (free) tree
• Minimum spanning tree (MST)
10
PIT
9
STL
4
8
• Spanning tree of a weighted graph with
6
7
3
DCA
5
2
minimum total edge weight
• Applications
• Communications networks
• Transportation networks
DFW
ATL
EXERCISE
MST
• Show an MST of the following graph.
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
CYCLE PROPERTY
8
f
• Cycle Property:
2
6
• Let 𝑇 be a minimum spanning tree of a
C
3
e
7
7
• Let 𝑒 be an edge of 𝐺 that is not in 𝑇 and
𝐢 let be the cycle formed by 𝑒 with 𝑇
Replacing f with e yields
a better spanning tree
• For every edge 𝑓 of 𝐢, π‘€π‘’π‘–π‘”β„Žπ‘‘ 𝑓 ≤
π‘€π‘’π‘–π‘”β„Žπ‘‘ 𝑒
f
• Proof:
spanning tree of smaller weight by replacing
𝑒 with 𝑓
9
8
weighted graph 𝐺
• By contradiction
• If π‘€π‘’π‘–π‘”β„Žπ‘‘ 𝑓 > π‘€π‘’π‘–π‘”β„Žπ‘‘(𝑒) we can get a
4
2
6
8
4
C
9
3
8
7
e
7
PARTITION PROPERTY
•
•
Partition Property:
•
Consider a partition of the vertices of 𝐺 into subsets π‘ˆ
and 𝑉
•
•
Let 𝑒 be an edge of minimum weight across the partition
U
f
9
8
8
Replacing f with e yields
another MST
U
Let 𝑇 be an MST of 𝐺
•
By the cycle property,
π‘€π‘’π‘–π‘”β„Žπ‘‘ 𝑓 ≤ π‘€π‘’π‘–π‘”β„Žπ‘‘(𝑒)
•
•
Thus, π‘€π‘’π‘–π‘”β„Žπ‘‘ 𝑓 = π‘€π‘’π‘–π‘”β„Žπ‘‘ 𝑒
We obtain another MST by replacing 𝑓 with 𝑒
3
e
7
There is a minimum spanning tree of G containing edge
𝑒
If 𝑇 does not contain 𝑒, consider the cycle 𝐢 formed by
𝑒 with 𝑇 and let 𝑓 be an edge of 𝐢 across the partition
4
5
2
Proof:
•
•
V
7
f
2
V
7
4
9
5
8
8
e
7
3
PRIM-JARNIK’S ALGORITHM
•
We pick an arbitrary vertex 𝑠 and we grow the MST as a cloud of vertices, starting
from 𝑠
•
We store with each vertex 𝑣 a label 𝑑 𝑣 = the smallest weight of an edge
connecting 𝑣 to a vertex in the cloud
•
At each step:
•
•
We add to the cloud the vertex 𝑒 outside the
cloud with the smallest distance label
We update the labels of the vertices adjacent
to 𝑒
PRIM-JARNIK’S ALGORITHM
• An adaptable priority queue stores the vertices
outside the cloud
• Key: distance, 𝐷[𝑣]
• Element: vertex 𝑣
• 𝑄. π‘Ÿπ‘’π‘π‘™π‘Žπ‘π‘’πΎπ‘’π‘¦ 𝑖, π‘˜ changes the key of an item
• We store three labels with each vertex 𝑣:
• Distance 𝐷[𝑣]
• Parent edge in MST 𝑃[𝑣]
• Locator in priority queue
Algorithm PrimJarnikMST(𝐺)
Input: A weighted connected graph 𝐺
Output: A minimum spanning tree 𝑇 of 𝐺
1. Pick any vertex 𝑣 of 𝐺
2. 𝐷 𝑣 ← 0; 𝑃 𝑣 ← ∅
3. for each vertex 𝑒 ≠ 𝑣 do
4. 𝐷 𝑒 ← ∞; 𝑃 𝑒 ← ∅
5. 𝑇 ← ∅
6. Priority queue 𝑄 of vertices with 𝐷[𝑒] as the key
7. while ¬π‘„. empty( ) do
8. 𝑒 ← 𝑄. removeMin( )
9. Add vertex 𝑒 and edge 𝑃[𝑒] to 𝑇
10. for each 𝑒 ∈ 𝑒. incidentEdges do
11. 𝑧 ← 𝑒. opposite 𝑒
12. if 𝑒. weight( ) < 𝐷[𝑧]
13.
𝐷 𝑧 ← 𝑒. weight( ); 𝑃 𝑧 ← 𝑒
14.
𝑄. replaceKey 𝑧, 𝐷[𝑧]
15. return 𝑇
EXAMPLE
2
7
B
0
2
B
5
C
0
2
0
A
4
9
5
C
5
F
8
8
7
E
7
7
2
4
F
8
7
ο‚₯
B
7
D
7
3
9
8
A
D
7
5
F
E
7
2
2
4
8
8
A
ο‚₯
9
8
C
5
2
D
E
ο‚₯
2
3
7
7
B
0
3
7
7
4
9
5
C
5
F
8
8
A
D
7
ο‚₯
E
3
7
4
EXAMPLE
2
2
B
0
4
9
5
C
5
F
8
8
A
D
7
7
7
E
4
3
3
2
2
B
0
4
9
5
C
5
F
8
8
A
D
7
7
7
E
3
3
4
EXERCISE
PRIM’S MST ALGORITHM
• Show how Prim’s MST algorithm works on the following graph, assuming you
start with SFO
• Show how the MST evolves in each iteration (a separate figure for each iteration).
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
ANALYSIS
• Graph operations
• Method incidentEdges is called once for each vertex
• Label operations
• We set/get the distance, parent and locator labels of vertex 𝑧 𝑂 deg 𝑧 times
• Setting/getting a label takes 𝑂 1 time
• Priority queue operations
• Each vertex is inserted once into and removed once from the priority queue, where each insertion or
•
removal takes 𝑂 log 𝑛 time
The key of a vertex 𝑀 in the priority queue is modified at most deg 𝑀 times, where each key change
takes 𝑂 log 𝑛 time
• Prim-Jarnik’s algorithm runs in 𝑂 𝑛 + π‘š log 𝑛 time provided the graph is represented by
the adjacency list structure
• Recall that Σ𝑣 deg 𝑣 = 2π‘š
• The running time is 𝑂 π‘š log 𝑛 since the graph is connected
KRUSKAL’S ALGORITHMS
• A priority queue stores the edges outside the cloud
• Key: weight
• Element: edge
• At the end of the algorithm
• We are left with one cloud that encompasses the MST
• A tree T which is our MST
Algorithm KruskalMST(𝐺)
1. for each vertex 𝑣 ∈ 𝐺. vertices do
2. Define a cluster 𝐢 𝑣 ← 𝑣
3. Initialize a priority queue 𝑄 of edges using the
weights as keys
4. 𝑇 ← ∅
5. while 𝑇 has fewer than 𝑛 − 1 edges do
6.
𝑒, 𝑣 ← 𝑄. removeMin( )
7. if 𝐢 𝑣 ≠ 𝐢(𝑒) then
8.
Add 𝑒, 𝑣 to 𝑇
9.
Merge 𝐢 𝑣 and 𝐢 𝑒
10. return 𝑇
DATA STRUCTURE FOR KRUSKAL’S ALGORITHM
• The algorithm maintains a forest of trees
• An edge is accepted it if connects distinct
trees
• We need a data structure that maintains a
partition, i.e., a collection of disjoint sets,
with the operations:
• find 𝑒 : return the set storing u
• union 𝑒, 𝑣 : replace the sets storing u and v
with their union
REPRESENTATION OF A PARTITION
• Each set is stored in a sequence
• Each element has a reference back to the set
• Operation 𝑓𝑖𝑛𝑑 𝑒 takes 𝑂 1 time, and returns the set of which 𝑒 is a member.
• In operation π‘’π‘›π‘–π‘œπ‘› 𝑒, 𝑣 , we move the elements of the smaller set to the sequence of the larger set and
•
update their references
The time for operation π‘’π‘›π‘–π‘œπ‘›(𝑒, 𝑣) is 𝑂 min 𝑛𝑒 , 𝑛𝑣 , where 𝑛𝑒 and 𝑛𝑣 are the sizes of the sets storing
𝑒 and 𝑣
• Whenever an element is processed, it goes into
a set of size at least double, hence each element
is processed at most log 𝑛 times
ANALYSIS
• A partition-based version of Kruskal’s Algorithm performs cluster merges as
unions and tests as finds.
• Running time
• There will be at most π‘š removals from the priority queue - 𝑂 π‘š log 𝑛
• Each vertex can be merged at most log 𝑛 times, as the clouds tend to “double” in size 𝑂 𝑛 log 𝑛
• Total: 𝑂 𝑛 + π‘š log 𝑛
2704
EXAMPLE
BOS
867
849
ORD
740
621
1846
337
LAX
1391
1464
1235
187
144
JFK
1258
184
802
SFO
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXAMPLE
2704
BOS
867
849
ORD
LAX
1391
1464
1235
144
JFK
1258
184
802
SFO
337
187
740
621
1846
PVD
BWI
1090
DFW
946
1121
MIA
2342
EXERCISE
KRUSKAL’S MST ALGORITHM
• Show how Kruskal’s MST algorithm works on the following graph.
• Show how the MST evolves in each iteration (a separate figure for each iteration).
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
A
8
SHORTEST PATHS
0
4
2
B
2
8
7
5
E
C
3
2
1
9
D
8
F
5
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.
Example:
•
In a flight route graph, the weight of an edge represents the distance in miles between the
endpoint airports
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
SHORTEST PATH PROBLEM
•
Given a weighted graph and two vertices 𝑒 and 𝑣, we want to find a path of
minimum total weight between 𝑒 and 𝑣.
•
•
Example:
•
•
Length of a path is the sum of the weights of its edges.
Shortest path between Providence and Honolulu
Applications
•
•
•
Internet packet routing
Flight reservations
Driving directions
HNL
SFO
PVD
ORD
LGA
LAX
DFW
MIA
SHORTEST PATH PROBLEM
• If there is no path from 𝑣 to 𝑒, we denote the distance between them by
𝑑 𝑣, 𝑒 = ∞
• What if there is a negative-weight cycle in the graph?
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
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
• Example:
• Tree of shortest paths
SFO
LGA
from Providence
HNL
PVD
ORD
LAX
DFW
MIA
DIJKSTRA’S ALGORITHM
• The distance of a vertex 𝑣 from a vertex 𝑠 is the
length of a shortest path between 𝑠 and 𝑣
• Dijkstra’s algorithm computes the distances of all
the vertices from a given start vertex 𝑠
(single-source shortest paths)
• Assumptions:
• The graph is connected
• The edges are undirected
• The edge weights are nonnegative
• Extremely similar to Prim-Jarnik’s MST Algorithm
• We grow a “cloud” of vertices, beginning with 𝑠
and eventually covering all the vertices
• We store with each vertex 𝑣 a label 𝐷 𝑣
representing the distance of 𝑣 from 𝑠 in the
subgraph consisting of the cloud and its adjacent
vertices
• The label 𝐷 𝑣 is initialized to positive infinity
• At each step
• We add to the cloud the vertex 𝑒 outside the cloud with
the smallest distance label, 𝐷 𝑣
• We update the labels of the vertices adjacent to 𝑒, in a
process called edge relaxation
EDGE RELAXATION
•
Consider an edge 𝑒 = 𝑒, 𝑧 such that
•
•
•
•
D[u] = 50
𝑒 is the vertex most recently added to the
cloud
s
u
e
D[z] = 75
z
D[y] = 20
y
𝑧 is not in the cloud
The relaxation of edge 𝑒 updates
distance 𝐷 𝑧 as follows:
D[u] = 50
𝐷 𝑧 ← min 𝐷 𝑧 , 𝐷 𝑒 + 𝑒. π‘€π‘’π‘–π‘”β„Žπ‘‘
u
s
D[y] = 20
y
D[z] = 60
e
z
EXAMPLE
A
8
B
2
8
7
Pull in one of the vertices with red labels
2.
The relaxation of edges updates the labels of LARGER
font size
0
4
2
3
ο‚₯
2
C
1.
1
9
E
D
ο‚₯
F
A
8
B
2
7
5
E
C
3
4
5
B
8
7
5
E
2
4
2
9
1
C
3
11
F
5
7
B
2
2
1
D
9
8
A
3
5
F
8
3
D
4
2
0
2
8
A
8
0
0
4
2
7
5
E
C
3
2
1
9
D
8
F
5
3
EXAMPLE
A
8
0
4
2
B
2
7
7
C
3
5
E
2
1
9
D
8
F
3
5
A
8
0
4
2
B
2
7
7
C
3
5
E
2
1
9
D
8
F
5
3
EXERCISE
DIJKSTRA’S ALGORITHM
• Show how Dijkstra’s algorithm works on the following graph, assuming you
start with SFO, i.e., 𝑠 =SFO.
• Show how the labels are updated in each iteration (a separate figure for each iteration).
SFO
PVD
ORD
LGA
HNL
LAX
DFW
MIA
DIJKSTRA’S ALGORITHM
• A locator-based priority queue stores the
vertices outside the cloud
• Key: distance
• Element: vertex
• We store with each vertex:
•
•
distance 𝐷 𝑣 label
locator in priority queue
Algorithm Dijkstras_sssp 𝐺, 𝑠
Input: A simple undirected weighted graph 𝐺 with nonnegative edge weights
and a source vertex 𝑠
Output: A label 𝐷 𝑣 for each vertex 𝑣 of 𝐺, such that 𝐷 𝑒 is the length
of the shorted path from 𝑠 to 𝑣
1. 𝐷 𝑠 ← 0; 𝐷 𝑣 ← ∞ for each vertex 𝑣 ≠ 𝑠
2. Let priority queue 𝑄 contain all the vertices of 𝐺 using 𝐷 𝑣 as the key
3. while ¬π‘„. empty do //𝑂 𝑛 iterations
4.
//pull a new vertex 𝑒 in the cloud
5.
𝑒 ← 𝑄. removeMin //𝑂 log 𝑛
6.
for each edge 𝑒 ∈ 𝑒. incidentEdges( ) do //𝑂 𝑑𝑒𝑔 𝑒 iterations
7.
//relax edge 𝑒
8.
𝑧 ← 𝑒. opposite 𝑒
9.
if 𝐷 𝑒 + 𝑒. weight < 𝐷[𝑧] then
10.
𝐷 𝑧 ← 𝐷 𝑒 + 𝑒. weight
11.
𝑄. updateKey 𝑧 //𝑂 log 𝑛
ANALYSIS
• Graph operations
• Method incidentEdges is called once for each vertex
• Label operations
• We set/get the distance and locator labels of vertex 𝑧 𝑂 deg 𝑧 times
• Setting/getting a label takes 𝑂 1 time
• Priority queue operations
• Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes 𝑂 log 𝑛
time
• The key of a vertex in the priority queue is modified at most deg 𝑀 times, where each key change takes 𝑂 log 𝑛 time
• Dijkstra’s algorithm runs in 𝑂 𝑛 + π‘š log 𝑛 time provided the graph is represented by the adjacency list
structure
• Recall that Σ𝑣 deg 𝑣 = 2π‘š
• The running time can also be expressed as 𝑂 π‘š log 𝑛 since the graph is connected
• The running time can be expressed as a function of 𝑛, 𝑂 𝑛2 log 𝑛
EXTENSION
• Using the template method pattern, we can extend Dijkstra’s algorithm to
return a tree of shortest paths from the start vertex to all other vertices
• We store with each vertex a third label:
• parent edge in the shortest path tree
• Parents are all initialized to null
• In the edge relaxation step, we update the parent label as well
WHY DIJKSTRA’S ALGORITHM WORKS
•
•
Dijkstra’s algorithm is based on the greedy
method. It adds vertices by increasing distance.
Proof by contradiction
•
•
•
•
Suppose it didn’t find all shortest distances. Let 𝐹
be the first wrong vertex the algorithm processed.
When the previous node, 𝐷, on the true shortest
path was considered, its distance was correct.
But the edge 𝐷, 𝐹 was relaxed at that time!
Thus, so long as 𝐷 𝐹 > 𝐷 𝐷 , 𝐹’s distance
cannot be wrong. That is, there is no wrong
vertex.
s
8
0
4
2
B
2
7
7
C
3
5
E
2
9
1
D
8
F
5
3
WHY IT DOESN’T WORK FOR
NEGATIVE-WEIGHT EDGES
A
8
• Dijkstra’s algorithm is based on the
greedy method. It adds vertices by
increasing distance.
• If a node with a negative incident edge
B
8
7
C
4
2
3
ο‚₯
2
were to be added late to the cloud, it
could mess up distances for vertices
already in the cloud.
2
0
-3
9
E
D
ο‚₯
F
A
8
5
0
4
2
B
2
8
7
5
E
C
3
2
9
-3
11
F
D
5
4
C’s true
distance is 1,
but it is already
in the cloud
with 𝐷 𝐢 = 2!
0
BELLMAN-FORD ALGORITHM
• Works even with negative-weight edges
• Must assume directed edges (for otherwise
we would have negative-weight cycles)
• Iteration 𝑖 finds all shortest paths that use 𝑖
edges.
• Running time: 𝑂 π‘›π‘š
• Can be extended to detect a negativeweight cycle if it exists
• How?
Algorithm BellmanFord 𝐺, 𝑠
1. Initialize 𝐷 𝑠 ← 0 and 𝐷 𝑣 ← ∞ for all
vertices 𝑣 ≠ 𝑠
2. for 𝑖 ← 1 … 𝑛 − 1 do
3. for each 𝑒 ∈ 𝐺. edges do
4.
//relax edge 𝑒
5.
𝑒 ← 𝑒. source ; 𝑧 ← 𝑒. target
6.
if 𝐷 𝑒 + 𝑒. weight < 𝐷 𝑧 then
7.
𝐷 𝑧 ← 𝐷 𝑒 + 𝑒. weight
• Nodes are labeled with their 𝐷[𝑣] values
BELLMAN-FORD EXAMPLE
0
8
First
round 8
4
0
-2
ο‚₯
7
-2
1
ο‚₯
3
-2
ο‚₯
8
7
9
ο‚₯
5
0
-2
ο‚₯
7
0
-2
1
4
-2
1
-2
3
5
ο‚₯
-2
5
4
9
Third
round 8
4
1
-2
3
ο‚₯
Second
round 8
4
-1
5
7
9
3
9
5
-2
1
1
-2
-1
9
4
5
EXERCISE
BELLMAN-FORD’S ALGORITHM
• Show how Bellman-Ford’s algorithm works on the following graph, assuming
you start with the top node
• Show how the labels are updated in each iteration (a separate figure for each iteration).
0
8
4
-2
ο‚₯
7
ο‚₯
3
-5
ο‚₯
1
ο‚₯
ο‚₯
5
9
ALL-PAIRS SHORTEST PATHS
• Find the distance between every pair of vertices in a weighted
directed graph 𝐺
• We can make 𝑛 calls to Dijkstra’s algorithm (if no negative
edges), which takes 𝑂 π‘›π‘š log 𝑛 time.
• Likewise, 𝑛 calls to Bellman-Ford would take 𝑂 𝑛2 π‘š time.
• We can achieve 𝑂 𝑛3 time using dynamic programming
(similar to the Floyd-Warshall algorithm).
i
Uses only vertices numbered 𝑖, … , 𝑗
(compute weight of this edge)
Uses only vertices
numbered 𝑖, … , π‘˜
j
k
Uses only vertices
numbered π‘˜, … , 𝑗
Algorithm AllPairsShortestPath 𝐺
Input: Graph 𝐺 with vertices labeled 1, … , 𝑛
Output: Distances 𝐷 𝑖, 𝑗 of shortest path lengths between
each pair of vertices
1. for each vertex pair 𝑖, 𝑗 do
2. if 𝑖 = 𝑗 then
3.
𝐷0 𝑖, 𝑖 ← 0
4. else if 𝑒 = 𝑖, 𝑗 is an edge in 𝐺 then
5.
𝐷0 𝑖, 𝑗 ← 𝑒. weight
6. else
7.
𝐷0 𝑖, 𝑗 ← ∞
8. for π‘˜ ← 1 … 𝑛 do
9. for 𝑖 ← 1 … 𝑛 do
10. for 𝑗 ← 1 … 𝑛 do
11.
π·π‘˜ 𝑖, 𝑗 ← min π·π‘˜−1 𝑖, 𝑗 , π·π‘˜−1 𝑖, π‘˜ + π·π‘˜−1 π‘˜, 𝑗
12. return 𝐷𝑛
Download