Beruflich Dokumente
Kultur Dokumente
Overview
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)
Step1: Initialisation
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
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) =
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
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
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
6
d(6) = 2
C56 = 5
5
d(5) = 5
d(2) = 3
2 1
C16 = 2
3
C34 = 4
4
C45 = 4
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
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:
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
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