dijkstra’s algorithm shortest path (single source) comparison to floyd’s algorithm: solves a...
Post on 20-Dec-2015
237 views
TRANSCRIPT
Dijkstra’s AlgorithmShortest Path (Single Source)
Comparison to Floyd’s Algorithm:Solves a simpler problem
O(n2) vs. O(n3)
Dijkstra’s: A Greedy Algorithm
• What does greedy mean?• At the beginning of each step, the algorithm picks
the best option.• Q: What other problem used a greedy algorithm?• A: Knapsack problem• Recall that there were 3 greedy strategies
– Highest $$$– Lowest weight– Highest $$$/weight ratio
Dijkstra’s: A Dynamic Programming Algorithm• Recall that dynamic programming refers to
– an algorithm that chooses to store information – rather than re-compute it.
• To really be considered Dynamic Programming– Store or update information at each step– Use the collective information to compute the
next step
Recall Floyd’s Algorithm
for (int k = 0; k < N; k++) // For each vertex
// Update the matrix
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
// Allow vertex k to be an intermediate hop
if (M[i][k]+M[k][j] < M[i][j]) then
M[i][j] = M[i][k]+M[k][j]
Recall Floyd’s Algorithm
• The beauty of Floyd’s algorithm is that it progressively minimizes the distance between vertices
• Each step incorporates more and more possibilities, – i.e., we add another intermediate hop.
i jk1
k2 k2
k3 k3 k3
k3
Step 0
Step 1
Step 2
Step 3
Recall Floyd’s Algorithm
• However, at the end of each step, the algorithm can rule out inferior possibilities.
if (M[i][k]+M[k][j] < M[i][j]) then
M[i][j] = M[i][k]+M[k][j]
• If the i k j is a better option than i j then – we never consider i j in the future.
• We always know that hopping through k is better
Recall Floyd’s Algorithm
• Implicitly Floyd’s algorithm computes the minimum path between any two vertices given O(n!) possibilities.
• But, it actually does NOT compute all the possibilities
• hops that are not optimal are quickly identified and never considered in the future.
i
k
j
4 5
8
How is Dijkstra’s algorithm better?
• Its really not better.• Its more appropriate when you have a
designated starting vertex.• Since you know the source vertex, you can
limit your exploration.• If you limit your exploration properly, you
can compute the minimum distance to all vertices in O(n2) computations.
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b null
c null
d null
e null
f null
g null
h null
i null
j null
Initial Step:
Given a graph
Adjacency List
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b null
c null
d null
e null
f null
g null
h null
i null
j null
Step 1:
Pick the V with the
smallest dist
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b a 28
c null
d a 5
e null
f null
g null
h null
i null
j null
Step 1:
Mark it as visited.
Iterate over its adj. list and update the table
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b a 28
c null
d a 5
e null
f null
g null
h null
i null
j null
Step 2:Pick v with
smallest dist.
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c null
d a 5
e null
f null
g d 16
h null
i null
j null
Step 2:Mark it as
visited.Iterate over its adj. list and update the table
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c null
d a 5
e null
f null
g d 16
h null
i null
j null
Step 3:Pick v with
smallest dist.
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c null
d a 5
e null
f null
g d 16
h g 21
i null
j g 61
Step 3:Mark it as
visited.Iterate over its adj. list and update the table
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c null
d a 5
e null
f null
g d 16
h g 21
i null
j g 61
Step 4:Pick v with
smallest dist.
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e null
f null
g d 16
h g 21
i null
j g 61
Step 4:Mark it as
visited.Iterate over its adj. list and update the table
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e null
f null
g d 16
h g 21
i null
j g 61
Step 5:Pick v with
smallest dist.
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e h 42
f null
g d 16
h g 21
i h 51
j h 31
Step 5:Mark it as
visited.Iterate over its adj. list and update the table
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e h 42
f null
g d 16
h g 21
i h 51
j h 31
Step 6:Pick v with
smallest dist.
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e h 42
f c 33
g d 16
h g 21
i h 51
j h 31
Step 6:Mark it as
visited.Iterate over its adj. list and update the table
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e h 42
f c 33
g d 16
h g 21
i h 51
j h 31
Step 7:Pick v with
smallest dist.
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e h 42
f c 33
g d 16
h g 21
i j 36
j h 31
Step 7:Mark it as
visited.Iterate over its adj. list and update the table
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e h 42
f c 33
g d 16
h g 21
i j 36
j h 31
Step 8:Pick v with
smallest dist.
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 36
f c 33
g d 16
h g 21
i j 36
j h 31
Step 8:Mark it as
visited.Iterate over its adj. list and update the table
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 36
f c 33
g d 16
h g 21
i j 36
j h 31
Step 9:Pick v with
smallest dist.
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 36
f c 33
g d 16
h g 21
i j 36
j h 31
Step 9:Mark it as
visited.Iterate over its adj. list and update the table
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 39
f c 33
g d 16
h g 21
i j 36
j h 31
Step 10:Pick v with
smallest dist.
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 39
f c 33
g d 16
h g 21
i j 36
j h 31
Step 10:Mark it as
visited.Iterate over its adj. list and update the table
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 39
f c 33
g d 16
h g 21
i j 36
j h 31
TracebackThe table can
be used to recursively trace all the
shortest paths from
vertex a
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 36
f c 33
g d 16
h g 21
i j 36
j h 31
TracebackWhat is the
shortest path
to vertex e?
e
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 39
f c 33
g d 16
h g 21
i j 36
j h 31
TracebackWhat is the
shortest path
to vertex e?
e
f
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 39
f c 33
g d 16
h g 21
i j 36
j h 31
TracebackWhat is the
shortest path
to vertex e?
e
f
c
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 39
f c 33
g d 16
h g 21
i j 36
j h 31
TracebackWhat is the
shortest path
to vertex e?
e
f
c
b
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 39
f c 33
g d 16
h g 21
i j 36
j h 31
TracebackWhat is the
shortest path
to vertex e?
e
f
c
b
d
a b c
d e f
g h i
j
28
13 17
8
14 38 6 7325
20 21 4 2611
11
5 30
26
51045
3
46
V Prev Dist
a null 0
b d 19
c b 27
d a 5
e f 39
f c 33
g d 16
h g 21
i j 36
j h 31
TracebackWhat is the
shortest path
to vertex e?
e
f
c
b
d
a
How to code Dijkstra’s Algorithm
Data Structures:
• AdjacencyList (adjList)– adjList.find(label)
• Finds a vertex in constant time
• Uses the vertex label as the hash index
• Sets an iterator to point to label’s adjacency list
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
Vertex v = new Vertex(‘h’);
adjList.find(v.getLabel())
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
Vertex v = new Vertex(‘h’);
adjList.find(v.getLabel())
// Finds h
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
Vertex v = new Vertex(‘h’);
adjList.find(v.getLabel())
// Moves iterator to front of h’s adjacency list
How to code Dijkstra’s Algorithm
Data Structures:
• AdjacencyList (adjList)– adjList.getNext();
• Returns the edge
• Moves the iterator to the next edge
• Returns null if it reaches the end of label’s adjacency list
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
while (e = adjList.getNext())
print(e.getLabel());
How to code Dijkstra’s Algorithm
Data Structures:
• Edge (e)– e.getLabel()
• Returns the label of the vertex that the edge points to
– e.getDist()• Returns the edge’s distance (weight of the edge)
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
while (e = adjList.getNext())
print(e.getLabel());
j
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
while (e = adjList.getNext())
println(e.getLabel());
j
d
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
while (e = adjList.getNext())
println(e.getLabel());
j
d
e
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
while (e = adjList.getNext())
println(e.getLabel());
j
d
e
i
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
while (e = adjList.getNext())
println(e.getLabel());
j
d
e
iloop
terminatesnull
How to code Dijkstra’s Algorithm
Data Structures:
• PathTable (pTable)– pTable.getMin()
• Finds the minimum distance vertex and sets it as visited.
• Returns the vertex
• Returns null if all vertices are marked visited.
Vertex v = pTable.getMin();V Prev Dist
a null 0
b d 19
c null
d a 5
e null
f null
g d 16
h g 21
i null
j g 61
Vertex v = pTable.getMin();V Prev Dist
a null 0
b d 19
c null
d a 5
e null
f null
g d 16
h g 21
i null
j g 61
Vertex v = pTable.getMin();V Prev Dist
a null 0
b d 19
c null
d a 5
e null
f null
g d 16
h g 21
i null
j g 61
v
label = ‘b’dist = 19
How to code Dijkstra’s Algorithm
Data Structures:
• PathTable (pTable)– pTable.update(vertex, prev, dist)
• Updates previous vertex and total distance
• Label used as the hash index
• Constant time update
pTable.update(‘c’, v.getLabel(), v.getDist() + 10);
V Prev Dist
a null 0
b d 19
c null
d a 5
e null
f null
g d 16
h g 21
i null
j g 61
v
label = ‘b’dist = 19
pTable.update(‘c’, v.getLabel(), v.getDist() + 10);
V Prev Dist
a null 0
b d 19
c null
d a 5
e null
f null
g d 16
h g 21
i null
j g 61
v
label = ‘b’dist = 19
pTable.update(‘c’, v.getLabel(), v.getDist() + 10);
V Prev Dist
a null 0
b d 19
c b 29
d a 5
e null
f null
g d 16
h g 21
i null
j g 61
v
label = ‘b’dist = 19
How to code Dijkstra’s Algorithm
Weight w = 0;Edge e = null;Label v = new Label(‘a’);pTable.update(v, null, 0);while(v = pTable.getMin()) {
adjList.find(v.getLabel());while (e = adjList.getNext()) {
w = e.getDist() + v.getDist();pTable.update(e.getLabel(), v.getLabel(), w);
}}
How to code Dijkstra’s Algorithm
Weight w = 0;Edge e = null;Label v = new Label(‘a’); // variablespTable.update(v, null, 0);while(v = pTable.getMin()) {
adjList.find(v.getLabel());while (e = adjList.getNext()) {
w = e.getDist() + v.getDist();pTable.update(e.getLabel(), v.getLabel(), w);
}}
How to code Dijkstra’s Algorithm
Weight w = 0;Edge e = null;Label v = new Label(‘a’);pTable.update(v, null, 0); // Sets A as the start vertexwhile(v = pTable.getMin()) {
adjList.find(v.getLabel());while (e = adjList.getNext()) {
w = e.getDist() + v.getDist();pTable.update(e.getLabel(), v.getLabel(), w);
}}
How to code Dijkstra’s Algorithm
Label v = new Label(‘a’);
pTable.update(v, null, 0);
while(v = pTable.getMin()) {
adjList.find(v.getLabel());
while (e = adjList.getNext()) {
w = e.getDist() + v.getDist();
pTable.update(e.getLabel(), v.getLabel(), w);
}
}
V Prev Dist
a null 0
b null
c null
d null
e null
f null
g null
h null
i null
j null
How to code Dijkstra’s Algorithm
Label v = new Label(‘a’);
pTable.update(v, null, 0);
while(v = pTable.getMin()) {
adjList.find(v.getLabel());
while (e = adjList.getNext()) {
w = e.getDist() + v.getDist();
pTable.update(e.getLabel(), v.getLabel(), w);
}
}
a d b5 28
b c a8 13
c f b6 17
d g b11 14 a32
e i d4 26 b38 f46
f e c3 7
g h j5 45
h j d10 20 e21 i30
i h f11 26
j i5
How to code Dijkstra’s Algorithm
Label v = new Label(‘a’);
pTable.update(v, null, 0);
while(v = pTable.getMin()) {
adjList.find(v.getLabel());
while (e = adjList.getNext()) {
w = e.getDist() + v.getDist();
pTable.update(e.getLabel(), v.getLabel(), w);
}
}
V Prev Dist
a null 0
b a 28
c null
d a 5
e null
f null
g null
h null
i null
j null
a d b5 28