Vous êtes sur la page 1sur 12

NET 4001 - Network Simulation

Dijkstra: Algorithm & Example

Marc St-Hilaire 2006-2009

Overview

- Dijkstra algorithm - Example of shortest paths using Dijkstra - Exercise - Conclusion

Dijkstra Algorithm (1/2) Algorithm used to solve the single source shortest path problem.
Discoverer: Edsger Dijkstra

Characteristics:
Greedy algorithm Makes decisions based on the most promising outcome Never reconsider previous decisions Only works with: Directed graph Non negative cost (to avoid cycling)

Dijkstra Algorithm (2/2) Lets define 4 sets:


- The set of nodes (N) - The set of links (M) - The set of permanent labels (P) - The set of temporary labels (T) - At each iteration, one node from set T is transferred to set P.

Step1: Initialisation

P = , T = N, d(i) = for all i N\{o}, d(o) = 0 and prev (o) = o.

Step2: While |P| < |N| repeat 2.1 to 2.3 2.1: i = argmin {d(j)} (j T) 2.2: P = P U {i}, T = T\{i} 2.3: For all links (i,j) M and j T If d(j) > d(i) + cij then d(j) = d(i) + cij and prev(j) = i
4

Example (1/4) Dijkstra algorithm is used to find the shortest path in a network. Each node i has a label d(i) that represents the value of the best shortest path (from origin to node i) found since the start of the algorithm. Lets find the shortest path between node 1 and node 4. Initialisation
C12 = 4 d(1) = 0 d(2) = d(3) = C23 = 2

2 1
C16 = 2

3
C34 = 4

C25 = 2 C26 = 1

4
C45 = 4

d(4) =

6
d(6) = C56 = 5

5
d(5) =
5

Example (2/4) Iteration 1


d(2) = min{d(2), d(1) + 4} = 4

d(3) =

2
C12 = 4 d(1) = 0

C23 = 2 C25 = 2

3
C34 = 4

1
C16 = 2

C26 = 1

4
C45 = 4

d(4) =

C56 = 5

5
d(5) =

d(6) = min{d(6), d(1) + 2} = 2

Iteration 2
d(2) = min{d(2), d(6) + 1} = 3 d(3) =

2
C12 = 4 d(1) = 0

C23 = 2 C25 = 2

3
C34 = 4

1
C16 = 2

C26 = 1

4
C45 = 4

d(4) =

6
d(6) = 2

C56 = 5

5
6

d(5) = min{d(5), d(6) + 5} = 7

Example (3/4) Iteration 3


d(2) = 3 d(3) = min{d(3), d(2) + 2} = 5 C23 = 2 C25 = 2 C26 = 1

2
C12 = 4 d(1) = 0

3
C34 = 4

1
C16 = 2

4
C45 = 4

d(4) =

6
d(6) = 2

C56 = 5

5
the condition to select the next node is the lowest cost -fro node 5 you select node 3 as the next node to move to the permanent set ----look next slide

d(5) = min{d(5), d(2) + 2} = 5

Iteration 4
d(2) = 3 d(3) = min{d(3), d(2) + 2} = 5 C23 = 2 C25 = 2 C26 = 1

2
C12 = 4 d(1) = 0

3
C34 = 4

1
C16 = 2

4
C45 = 4

d(4) = min{d(4), d(5) + 4} = 9

6
d(6) = 2

C56 = 5

5
d(5) = 5

Example (4/4) Iteration 5


C12 = 4 d(1) = 0 d(3) = 5 C23 = 2 C25 = 2 C26 = 1

d(2) = 3

2 1
C16 = 2

3
C34 = 4

4
C45 = 4

d(4) = min{d(4), d(5) + 4} = 9

6
d(6) = 2

C56 = 5

5
d(5) = 5
find the shortest path using dijkstra from node 1 1to node 3? -do the same ..

Iteration 6
d(2) = 3 d(3) = 5 C23 = 2 C25 = 2 C26 = 1

2
C12 = 4 d(1) = 0

3
C34 = 4

for the project have to run dijkstra multiple times

1
C16 = 2

4
C45 = 4

d(4) = 9

6
d(6) = 2

C56 = 5

5
d(5) = 5

Network with Negative Link Cost Why Dijkstra doesnt work with negative cost? Consider the following example where node A is the source:

Other algorithms such as Bellman-Ford must be used.


9

Conclusion Dijkstra will give you the shortest path from a given source to any nodes in the network (not only to a given destination). Fairly easy to implement. Fast.

10

Exercise 1) What is the shortest path between node A and node D?

2) What would you do if I ask the following question: What is the shortest path from A to all other nodes in the network?
11

References Bertsekas, D. and Gallager, T., Data Networks, 2nd edition, Prentice Hall, 1992.

12