Dijkstras Shortest Path Algorithm Gordon College 1 A

  • Slides: 6
Download presentation
Dijkstra’s Shortest Path Algorithm Gordon College 1

Dijkstra’s Shortest Path Algorithm Gordon College 1

A Link-State Routing Algorithm Notation: • • c(i, j): link cost from node i

A Link-State Routing Algorithm Notation: • • c(i, j): link cost from node i to j. cost infinite if not direct neighbors D(v): current value of cost of path from source to dest. V p(v): predecessor node along path from source to v, that is next v N: set of nodes already in spanning tree (least cost path known) Examples: • c(B, C) = 3 • D(E) = 2 • p(B) = A • N = { A, B, D, E } 5 2 A B 2 1 D 3 C 3 1 5 F 1 E 2 2

Dijsktra’s Algorithm 1 Initialization: 2 N = {A} 3 for all nodes v 4

Dijsktra’s Algorithm 1 Initialization: 2 N = {A} 3 for all nodes v 4 if v adjacent to A 5 then D(v) = c(A, v) 6 else D(v) = infinity 7 8 Loop 9 find w not in N such that D(w) is a minimum 10 add w to N 11 update D(v) for all v adjacent to w and not in N: 12 D(v) = min( D(v), D(w) + c(w, v) ) 13 /* new cost to v is either old cost to v or known 14 shortest path cost to w plus cost from w to v */ 15 until all nodes in N 3

Dijkstra’s algorithm: example Step N 0 A 1 AD 2 ADE 3 ADEB 4

Dijkstra’s algorithm: example Step N 0 A 1 AD 2 ADE 3 ADEB 4 ADEBC 5 ADEBCF D(B), p(B) D(C), p(C) D(D), p(D) D(E), p(E) D(F), p(F) 2, A 5, A 1, A infinity, 2, A 4, D 1, A 2, D infinity, 2, A 3, E 1, A 2, D 4, E 5 A 1 2 B 2 D 3 C 3 1 5 F 1 E 2 4

Spanning tree gives routing table Step N ADEBCF D(B), p(B) D(C), p(C) D(D), p(D)

Spanning tree gives routing table Step N ADEBCF D(B), p(B) D(C), p(C) D(D), p(D) D(E), p(E) D(F), p(F) 2, A 3, E 1, A 2, D 4, E Result from Dijkstra’s algorithm destination Routing table: 5 Outgoing link to use, cost B B, 2 C D, 3 D D, 1 E D, 2 F D, 4 A 1 2 B 2 D 3 C 3 1 5 F 1 E 2 5

Dijkstra’s algorithm performance Algorithm complexity (n nodes and l links) • Computation – –

Dijkstra’s algorithm performance Algorithm complexity (n nodes and l links) • Computation – – n iterations each iteration: need to check all nodes, w, not in N n*(n+1)/2 comparisons: O(n 2) more efficient implementations possible: O(n log n) • Messages – – network topology and link cost known to all nodes each node broadcasts its direct link cost O(l) messages per broadcast announcement O(n l) 6