Design and Analysis of Computer Algorithm
1
Design and Analysis of
Computer Algorithm
Additional Note
Pradondet Nilagupta
Department of Computer Engineering
Design and Analysis of Computer Algorithm
March 17, 2009
2
Design and Analysis of Computer Algorithm
March 17, 2009
3
Single Source Shortest Path
●
Given a (un)weighted, directed graph G = {V, E}, in
which each edge has a non-negative cost (weight)
●
Problem: Determine the cost of the shortest path from
the source vertex to every other vertex V
■
The
length
of a path is the sum of the costs of the
edges on the path
■
Why not find path to just one particular destination?
Design and Analysis of Computer Algorithm
March 17, 2009
4
Dijkstra’s Algorithm
●
Maintain a set S of vertices whose shortest distance from
the source is already known
●
Initially S contains only source vertex
●
At each step, we add to S a remaining vertex w whose
distance from the source is as short as possible
●
Assuming all edges have non-negative costs, we can
always find a shortest path from the source to v that
passes only through vertices in S (
special path
)
●
At every step we use an array to record the length of the
shortest special path to each vertex
Design and Analysis of Computer Algorithm
March 17, 2009
8
An Example
inf
2
7
5
4
1
4
3
4
1
2
inf
a
d
5
inf
b
f
s
inf
0
7
e
c
Design and Analysis of Computer Algorithm
March 17, 2009
9
2
7
5
4
1
4
3
4
1
2
2
inf
a
d
5
5
b
f
s
inf
0
7
e
c
Design and Analysis of Computer Algorithm
March 17, 2009
10
2
7
5
4
1
4
3
4
1
2
2
9
a
d
5
4
b
f
s
inf
0
7
e
c
Design and Analysis of Computer Algorithm
March 17, 2009
11
2
7
5
4
1
4
3
4
1
2
2
8
a
d
5
4
b
f
s
inf
0
7
e
c
Design and Analysis of Computer Algorithm
March 17, 2009
12
2
7
5
4
1
4
3
4
1
2
2
8
a
d
5
4
b
f
s
inf
0
7
e
c
Design and Analysis of Computer Algorithm
March 17, 2009
13
2
7
5
4
1
4
3
4
1
2
2
8
a
d
5
4
b
f
s
14
0
7
e
c
Design and Analysis of Computer Algorithm
March 17, 2009
14
2
7
5
4
1
4
3
4
1
2
2
8
a
d
5
4
b
f
s
7
13
0
e
c
Design and Analysis of Computer Algorithm
March 17, 2009
15
2
7
5
4
1
4
3
4
1
2
2
8
a
d
5
4
b
f
s
7
13
0
e
c
Design and Analysis of Computer Algorithm
March 17, 2009
16
Shortest Path Tree
The unique simple path from
s
to
v
in the tree is a shortest
path from
s
to
v
.
2
8
a
d
f
e
c
b
s
3
5
2
2
4
4
13
0
4
Design and Analysis of Computer Algorithm
March 17, 2009
17
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
23
∞
Design and Analysis of Computer Algorithm
March 17, 2009
18
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
23
∞
Design and Analysis of Computer Algorithm
March 17, 2009
19
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
23
Design and Analysis of Computer Algorithm
March 17, 2009
20
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
23
Design and Analysis of Computer Algorithm
March 17, 2009
21
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
23
Design and Analysis of Computer Algorithm
March 17, 2009
22
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
23
Design and Analysis of Computer Algorithm
March 17, 2009
23
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
23
Design and Analysis of Computer Algorithm
March 17, 2009
24
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
Design and Analysis of Computer Algorithm
March 17, 2009
25
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
Design and Analysis of Computer Algorithm
March 17, 2009
26
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
Design and Analysis of Computer Algorithm
March 17, 2009
27
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
Design and Analysis of Computer Algorithm
March 17, 2009
28
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).
end of for-loop Until | B | == | V |.
4
Design and Analysis of Computer Algorithm
March 17, 2009
29
for each v ∈V,
k [v ] ← infinity; n[v] ← (v,v); k[ source ] ← 0;
Build a heap for all values in the array k. S← {}, T ← {}.
Repeat
Find the vertex v with minimum value in heap using extract_min. Insert v into S.
Insert n[v] into T
For each u such that (v,u)∈ E,
if k[v] plus the weight of (v,u) is less than k[u], then reduce k[u] to k[v] + weight_of ( (v,u) ), and n[u] ← (v , u).