Docsity

Dijkstra’s Algorithm - Lecture Notes | CECS 328, Study notes of Discrete Structures and Graph Theory

Toggle sidebar
bg1
Lecture 15: Dijkstra’s Algorithm
In the last lecture it was mentioned that graphs represent a type of space, in which discrete
places are connected to one another via edges. One basic question that can be asked about
this space is “How to find the shortest path from one place to another?”. For Cartesian
space we know the answer involves taking a linear path from point Ato point B. Moreover,
the distance between these points can be obtained using the Pythagorean Theorem. For
graphs a different method must be used since in general vertices on a graph are not directly
connected by a line.
Let G= (V, E , c) be a network where c:E→R+represents the cost of traversing an edge of
the graph. Then the cost of the path e1, e2, . . . , emis defined as cost =Pm
i=1 c(ei). Moreover,
the distance dbetween uand vis defined as the cost of the path which minimizes the
above sum.
Example 1. Let G= (V , E, c), where
V={a, b, c, d, e, f }
and the edges-costs are given by
E={(a, b, 1),(a, c, 3),(b, c, 1),(b, d, 1),(b, e, 4),(c, d, 1),(c, f, 2),
(d, e, 1),(d, f, 3),(e, f , 2)}.
Draw the graph and determine the distance of each vertex from vertex a.
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Dijkstra’s Algorithm - Lecture Notes | CECS 328 and more Study notes Discrete Structures and Graph Theory in PDF only on Docsity!

Lecture 15: Dijkstra’s Algorithm

In the last lecture it was mentioned that graphs represent a type of space, in which discrete places are connected to one another via edges. One basic question that can be asked about this space is “How to find the shortest path from one place to another?”. For Cartesian space we know the answer involves taking a linear path from point A to point B. Moreover, the distance between these points can be obtained using the Pythagorean Theorem. For graphs a different method must be used since in general vertices on a graph are not directly connected by a line.

Let G = (V, E, c) be a network where c : E → R+^ represents the cost of traversing an edge of the graph. Then the cost of the path e 1 , e 2 ,... , em is defined as cost =

∑m i=1 c(ei). Moreover, the distance d between u and v is defined as the cost of the path which minimizes the above sum.

Example 1. Let G = (V, E, c), where

V = {a, b, c, d, e, f }

and the edges-costs are given by

E = {(a, b, 1), (a, c, 3), (b, c, 1), (b, d, 1), (b, e, 4), (c, d, 1), (c, f, 2),

(d, e, 1), (d, f, 3), (e, f, 2)}.

Draw the graph and determine the distance of each vertex from vertex a.

A Special Case: Unweighted Graphs

Let G = (V, E) be a unweighted graph. In other words c(e) = 1 for every edge e ∈ E. In this case the distance from vertex u to vertex v is the length of the shortest simple path that connects u and v. Thus we can define the distance d(a, v) between vertex a and any other vertex v using the following recursive definition.

  1. Basis step : d(a, a) = 0. Moreover, let V 0 = {a}
  2. Inductive step: suppose Vk ⊂ V has been defined for some k ≥ 0. If v 6 ∈ Vk, but v is adjacent to some vertex in Vk, then d(a, v) = k + 1. Moreover, Vk+1 is defined as the union of Vk with all such vertices.

Theorem 1. The recursive definition above yields the correct distances from a to any other vertex in G.

Proof.

Implementation of shortest- path algorithm for the unweigted case:

  1. Initialize queue Q and add a to Q.
  2. Repeat until Q is empty: dequeue the next vertex u in Q. For all vertices v that are adjacent to u and have yet to be added to Q, add v to Q, and note that - d(a, v) = d(a, u) + 1 - the parent of v is u.
  3. For all vertices v which were never added to Q, d(a, v) = ∞.

The above algorithm is an example of performing a breadth-first search on a graph. In other words, the vertices of the graph are examined in the order in which they are reached (FIFO).

Example 3. Show the contents of the queue Q during the execution of the algorithm on the graph of Example 2.

Dijkstra’s Algorithm

For the unweighted case, once a vertex v is reached, d(a, v) is immediately known. In the general case, however, this is not true. For example edge (a, b) may have a cost of 5, while edges (a, c) and (c, b) may each have costs of 1, thus making the distance from a to b ≤ 2 < 5. Hence, during each stage we only have estimated distances which may decrease during later rounds. However Dijkstra’s algorithm, which was discovered in the 1950’s, makes use of the following key observation: at any stage, the least estimated distance from a vertex v to a is the exact distance. This observation leads to Dijkstra’s Algorithm.

Dijkstra’s Algorithm

  1. Stage 0: d(a, a) = 0. Let Know 0 = {a} be the set of known distances after Stage 0. Update the estimated distances for all vertices adjacent to a. Proceed to Stage 1.
  2. Stage k ≥ 1 (repeat until either all distances are known or all estimated distances are infinite): Let v have the least estimated finite distance of all vertices whoses distances are not yet known. Let Knowk+1 = Knowk ∪ {v}. Update all estimated distances for vertices adjacent to v. Proceed to the next stage.

Theorem 1. Dijkstra’s Algorithm yields the correct distance from a for every vertex.

Proof.

Analysis of Dijkstra’s Algorithm: In the worst case, each edge needs to be examined at most twice. On the other hand, the vertices must be kept in a heap so that vertex with least estimated distance can be efficiently deleted. However, the fact that the priority values (i.e. estimated distances) are being dynamically updated makes the heaps we studied earlier this semester inadequate. However, there do exist heaps (namely Fibonacci heaps) which allow for the dynamic updating of priorities, in such a way that the worst-case complexity of building, updating, and deleting from the heap is O(|v| log |v|). Therefore, the worst-case complexity is O(|E| + |v| log |v|).

Example 5. For the graph G shown below. Use Dijkstra’s Algorithm to find the distance of each vertex from vertex a.

vertex minimum parent minimum parent minimum parent distance distance distance a b c d e f

Stage 1 Stage 2 Stage 3