Dijkstra Algorithm
-
Upload
jorgecanoramirez -
Category
Documents
-
view
17 -
download
0
description
Transcript of Dijkstra Algorithm
-
1
Dijkstra's Algorithm
Sources: S. Skiena. The Algorithm Design Manual. S. Sedgewick. Algorithms in C++ (3rd Edition)
-
2
Problem
Two algorithms, depending on what information we want Recall: All pairs shortest path: Floyd's algorithm Single source shortest path: Dijkstra's algorithm
-
3
Dijkstra's Algorithm
Dijkstra's algorithm solves related problem to APSP (Floyd's algorithm): Single source shortest path (SSSP) Idea: For a single node, what is shortest path to any
other node? More restricted question Can answer somewhat more quickly
Dijkstra's algorithm: Greedy approach
-
4
Dijkstra's Algorithm
Basic idea: Divide vertices into three groups Tree vertices: We know the shortest path to these Fringe vertices: Adjacent to a tree vertex; we don't
know the shortest path to these yet Distant vertices: Not adjacent to a tree vertex; we
don't consider them at all Iterate n times:
Find fringe vertex with lowest distance from source Move from fringe to tree Update any vertices that are now fringe vertices
-
5
Example
Assume we start at A
A
B
D
C
E
F
J
H
I
G
4
2 7
6
9
3
4
8
13
1
41
5
7
3
5 Tree Fringe Distant
Vertex Dist NeighA* 0 -B 7 AC - -D 3 AE - -F - -G - -H - -I - -J - -
Vertex Dist NeighA* 0 -B 7 AC - -D 3 AE - -F - -G - -H - -I - -J - -
-
6
Example
Add closest
A
B
D
C
E
F
J
H
I
G
4
2 7
6
9
3
4
8
13
1
41
5
7
3
5 Tree Fringe Distant
Vertex Dist NeighA* 0 -B 7 AC - -D* 3 AE 4 DF - -G 10 DH - -I - -J - -
Vertex Dist NeighA* 0 -B 7 AC - -D* 3 AE 4 DF - -G 10 DH - -I - -J - -
-
7
Example
Add closest
A
B
D
C
E
F
J
H
I
G
4
2 7
6
9
3
4
8
13
1
41
5
7
3
5 Tree Fringe Distant
Vertex Dist NeighA* 0 -B 7 AC - -D* 3 AE* 4 DF 8 EG 10 DH - -I - -J - -
Vertex Dist NeighA* 0 -B 7 AC - -D* 3 AE* 4 DF 8 EG 10 DH - -I - -J - -
-
8
Example
Add closest
A
B
D
C
E
F
J
H
I
G
4
2 7
6
9
3
4
8
13
1
41
5
7
3
5 Tree Fringe Distant
Vertex Dist NeighA* 0 -B* 7 AC - -D* 3 AE* 4 DF 8 EG 10 DH - -I - -J - -
Vertex Dist NeighA* 0 -B* 7 AC - -D* 3 AE* 4 DF 8 EG 10 DH - -I - -J - -
-
9
Example
Add closest
A
B
D
C
E
F
J
H
I
G
4
2 7
6
9
3
4
8
13
1
41
5
7
3
5 Tree Fringe Distant
Vertex Dist NeighA* 0 -B* 7 AC - -D* 3 AE* 4 DF* 8 EG 9 FH - -I - -J 14 F
Vertex Dist NeighA* 0 -B* 7 AC - -D* 3 AE* 4 DF* 8 EG 9 FH - -I - -J 14 F
-
10
Example
Add closest
A
B
D
C
E
F
J
H
I
G
4
2 7
6
9
3
4
8
13
1
41
5
7
3
5 Tree Fringe Distant
Vertex Dist NeighA* 0 -B* 7 AC - -D* 3 AE* 4 DF* 8 EG* 9 FH - -I - -J 14 F
Vertex Dist NeighA* 0 -B* 7 AC - -D* 3 AE* 4 DF* 8 EG* 9 FH - -I - -J 14 F
-
11
Example
Add closest
A
B
D
C
E
F
J
H
I
G
4
2 7
6
9
3
4
8
13
1
41
5
7
3
5 Tree Fringe Distant
Vertex Dist NeighA* 0 -B* 7 AC 19 JD* 3 AE* 4 DF* 8 EG* 9 FH 22 JI 23 JJ* 14 F
Vertex Dist NeighA* 0 -B* 7 AC 19 JD* 3 AE* 4 DF* 8 EG* 9 FH 22 JI 23 JJ* 14 F
-
12
Example
Add closest
A
B
D
C
E
F
J
H
I
G
4
2 7
6
9
3
4
8
13
1
41
5
7
3
5 Tree Fringe Distant
Vertex Dist NeighA* 0 -B* 7 AC* 19 JD* 3 AE* 4 DF* 8 EG* 9 FH 22 JI 23 JJ* 14 F
Vertex Dist NeighA* 0 -B* 7 AC* 19 JD* 3 AE* 4 DF* 8 EG* 9 FH 22 JI 23 JJ* 14 F
-
13
Example
Add closest
A
B
D
C
E
F
J
H
I
G
4
2 7
6
9
3
4
8
13
1
41
5
7
3
5 Tree Fringe Distant
Vertex Dist NeighA* 0 -B* 7 AC* 19 JD* 3 AE* 4 DF* 8 EG* 9 FH* 22 JI 23 JJ* 14 F
Vertex Dist NeighA* 0 -B* 7 AC* 19 JD* 3 AE* 4 DF* 8 EG* 9 FH* 22 JI 23 JJ* 14 F
-
14
Example
Add closest
A
B
D
C
E
F
J
H
I
G
4
2 7
6
9
3
4
8
13
1
41
5
7
3
5 Tree Fringe Distant
Vertex Dist NeighA* 0 -B* 7 AC* 19 JD* 3 AE* 4 DF* 8 EG* 9 FH* 22 JI* 23 JJ* 14 F
Vertex Dist NeighA* 0 -B* 7 AC* 19 JD* 3 AE* 4 DF* 8 EG* 9 FH* 22 JI* 23 JJ* 14 F
-
15
Implementation
Question: How to implement? Need to be able to find fringe vertices quickly Also need to be able to find least valued fringe
vertex Solution: A priority queue, implemented with a
heap Order = distance from source vertex
Need to specify graph structure...
-
16
Graph Structure
class Vertex data label or similar index integer: Index in G.vertices qindex integer: Index in priority queue neighbors list
Elements are (float,int) pairs First = weight of edge, second = index of neighbor
nearestv integer Used for SSSP
nearestw float Used for SSSP
class Graph vertices list of Vertex objects
-
17
Example
Example for this small graph: G: Graph object
vertices = [ , , , ] : Vertex object
data = A, index=0 neighbors = [(7,1),(3,3)]
: Vertex object data = B, index=1 neighbors=[ (2,0) ,(4,3) ]
: Vertex object data = C, index=2 neighbors=[ (1,3) ]
: Vertex object data = D, index=3 neighbors = [ ]
A
B
D
C
4
2 7
3
1
-
18
Dijkstra's Algorithm
def dijkstra(G,src): #G = graphQ=[ ]for v in G.vertices:
v.nearestv = Noneif v.name == src: v.nearestw=0else: v.nearestw=NoneQ.append(tmp)
heapify(Q) #None counts as infinityfor i in range(len(Q)):
Q[i].qindex = i...continued...
-
19
Dijkstra's Algorithm
while len(Q) > 0:x=heappop(Q) #updates qindexfor nn in x.neighbors:
ew = nn[0] #edge weightni = nn[1] #neighbor indexn = G.vertices[ ni ] #neighbor itselfif n.nearestw == None or n.nearestw > x.nearestw + ew:
n.nearestv = x.indexn.nearestw = x.nearestw + ewreheapify(Q,n)
-
20
Reheapify
Remember, priority queue is sorted based on distance from source vertex
If we change a vertex's nearest neighbor, we might move it up in the queue
So this is what reheapify does Parameters: The queue (array) and the index
node we have altered
-
21
Reheapify
def reheapify(Q,v):vi = v.qindex #index in queuepi = (vi-1)/2 #parent's index in queuep = Q[pi] #parent node itselfwhile vi > 0 and p.nearestw > v.nearestw:
#swap queue entries for parent & vtmp = Q[pi]Q[pi] = Q[vi]Q[vi] = tmp#update qslot dataQ[pi].qslot = piQ[vi].qslot = vivi = pipi = (vi-1)/2
-
22
heappop
def heappop(Q):x = Q[0]; y = Q.pop(); Q[0]=y; Q[0].qindex=0vi = 0; li = vi*2+1 ; ri = vi*2+2 #li,ri=left/right childrenwhile 1:
if li < len(Q): v1=Q[li].nearestwelse: v1 = infinityif ri < len(Q): v2=Q[ri].nearestwelse: v2 = infinityif v1 < Q[vi].value and v1
-
23
Analysis
How much time for Dijkstra? Does V loop iterations Each loop iteration takes time lg(V) (to pop and
reheapify) Thus, O(V lg V) for vertex processing Also, within loop, we consider all neighbors of
popped vertex Each edge gets considered exactly once
So O(V lg V + E) time total Dense graph: EV2 Thus, O(V2 + V lg V)