Sei sulla pagina 1di 7

Lecture 14

DIJKSTRA’s ALGORITHM
Scribe prepared by Anshul Gupta (07CS3020)
Teacher: Prof. Partha Bhowmick
Department of Computer Science and Engineering
IIT Kharagpur
August 22, 2008

1
1 Introduction
Dijkstra’s Algorithm : Dijkstra’s algorithm solves the single source
shortest paths problem on a weighted directed graph G=(V,E) for the case
in which all edge weights are non negative.In this section ,we assume that
w(u,v) is greater than or equal to 0 for each (u,v) in E.This algorithm is quite
similar to thePrim’s algorithm of minimum spanning tree.The running time
of Dijkstra’s algorithm is lower than the other algorithm for single source
shortest psth i.e. Bellman Ford algorithm.

Dijkstra’s algorithm maintains a set S of vertices whose final shortest


path weights from a source s have already been determined. The algorithm
repeatedly selects the vertex u in V-S with the minimum shortest path esti-
mate, adds u to S, and relaxes all edges leaving u.In the implementation of
Dijkstra’s algorithm ,we use a minimum priority queue Q of vertices keyed
by their d value .

2 Pseudo Code of Dijkstra’s algorithm :


DIJKSTRA(G,w,s)
1. for each u ∈ V
2. key[u] ← ∞
3. π[u] ← NIL
4. S ← φ
5. key[s] ← 0
6. Q ← V[G]
7. while Q 6= φ
8. u ← EXTRACT-MIN(Q)
9. S←SUu
10. for each v ∈ Adj[u]
11. if v ∈ Q and key[v] > w(u,v) + key[u]
12. key[v] ← w(u,v) + key[u]
13. π[v] ←u

2
3 Explanation of the algorithm
This algorithm is quite similar to Prim’s Algorithm except for the last few
lines where instead of checking for just key[u], we check for key[u]+w(u,v) .
This is because here we are looking for the minimum path between any two
vertices which is the sum of lenghts of all the paths that we visit in between.

In the first three lines we actually do INITIALISE SINGLE SOURCE(G,s)


where for each vertex in V we keep its key as zero and its ancestor as NIL .
Now,we consider one of the vertices,say, s which is taken as the initial single
source and thus its key is initialised to zero . We use a queue Q in our pro-
gram which keeps in track of those vertices which have not yet become black
i.e. those vertices for which all its children are not yet visited . This queue
is initialised to the set of all vertices in V.

The major algorithm is comprised in the while loop . Each time through
the while loop,a vertex u is extracted from Q=V-S at the start of each iter-
ation of the while loop in line 8 . This vertex u is the lightest or closest edge
in V-S to add to set S,thus we say that this algorithm uses a greedy strategy .
Now since is a safe vertex,we can add it to the set S.In the for loop,we relax
the edge (u,v) . The key of each of the vertices which has been visited is
tried to be kept minimum by considering the extra paths formed at each step
from the source to that particular vertex . Observe that vertices are never
inserted in Q after line 6 and that each vertex is extracted from Q exactly
once.

3
Figure 1: Vertices and Edges

Figure 2: Final shortest single source path tree obtained

4
4 Analysis of Time Complexity
The performance of Dijkstra’s algorithm depends on the time complexity of
the function EXTRACT-MIN.We know from the chapter on heap that it is
O(log V). The body of the while loop is executed —V— times, and hence
the total time for all calls to EXTRACT-MIN is O(V lg V). The for loop in
lines 10-13 is executed O(E) times altogether, since the sum of the lengths
of all adjacency lists is —E—. Within the for loop, the test for membership
in Q in line 11 can be implemented in constant time by keeping a bit for
each vertex that tells whether or not it is in Q, and updating the bit when
the vertex is removed from Q. The assignment in line 13 involves an implicit
DECREASE-KEY operation on the min-heap, which can be implemented in
a binary min-heap in O(lg V) time.Thus, the total time for Dijkstra’s algo-
rithm is O(V lg V + E lg V) = O(E lg V).

5 Correctness of the Algorithm


It is enough to show that for each vertex u in V,we have key[u] as the mini-
mum distance between s and u at the time when u is added to S.Before we
add u in S , all the possible paths from s to u have been considered in the al-
gorithm and finally we find the minimum among them with the if condition.
Hence we obtain the shortest distance path between any vertex to source.

5
6 Closest Pair of Points
A brute-force closest pair algorithm simply looks at all the pair combina-
tions possible and give the shortest distance pair among them. This gives us
a time complexity of O(n*n).But we can design a divide and conquer algo-
rithm of time complexity O(n log n).This is actually got by the recurrence
T(n)=T(n/2)+O(n).

Divide : It finds a vertical line l that bisects the point set P into two
sets PL and PL such that PL = ceil(P/2), PR = floor(P/2), all points in PL
are on or to the left of line l, and all points in PR are on or to the right of l.

Conquer : Having divided P into PL and PR , it makes two recursive


calls, one to find the closest pair of points in PL and the other to find the
closest pair of points in PR .Let the closest pair distances returned for PL and
PL be dL and dR , respectively, and let d = min(dL ,dR ).

Combine : The closest pair is either the pair with distance d found by
one of the recursive calls, or it is a pair of points with one point in PL and
the other in PR . To find a pair of shortest distance in between PL and PR ,
we can say that by pigeon hole principle that there can be atmost 8 points
(4 in each) that we must consider , giving a total of 49 comparisons. The
algorithm is as given in the next page.

6
7 Algorithm :
1. It creates an array Y’, which is the array Y with all points not in the
2d-wide vertical strip removed. The array Y’ is sorted by y-coordinate, just
as Y is.

2. For each point p in the array Y’, the algorithm tries to find points in
Y’ that are within d units of p. As we shall see shortly, only the 7 points in
Y’ that follow p need to be considered. The algorithm computes the distance
from p to each of these 7 points and keeps track of the closest-pair distance
d’ found over all pairs of points in Y’.

3. If d′ < d, then the vertical strip does indeed contain a closer pair than
was found by the recursive calls. This pair and its distance d’ are returned.
Otherwise, the closest pair and its distance d found by the recursive calls are
returned.

By considering the entire algorithm in detail, we can obtain the time


complexity of the algorithm to be O(n log n).

Potrebbero piacerti anche