Docsity

Dijkstra's Algorithm Example: Finding the Shortest Path in a Network, Study notes of Communication

Toggle sidebar
bg1
TCOM 509 – Fall 2003 Dr. Li-Chuan Chen
Dijkstra’s Algorithm Example
Given: A network topology graph, G, with 6 nodes, and the link cost between nodes as
shown below:
A
DE
F
CB
2
3
1
5
2
1
1
3
5
2
Find: Using Dijkstra’s Algorithm to find the least-cost paths for nodes A through F
Solution:
Let T = {A, B, C, D, E, F} = number of nodes in the network
s = source node = A
N = set of nodes that have been processed so far
c(i,j) = link cost from node i to node j if two nodes are directly
connected.
= ∞ if there is no link between i and j
D(v) = cost of the shortest path (least-cost path) from node s to node v
that is currently known so far. When the algorithm terminates,
D(v) is the cost of the shortest path from source s to destination
v in the network.
P(v): predecessor node along path from source s to v, that is next v
1) [Initialization]
•N = {A} initialize set N contains source node A
•Compute D(v) for v is in set T but not in set N.
Need to Compute D(v) for v = B, C, D, E, F
vD(v) = c(s,v) = c(A,v) P(v)
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Dijkstra's Algorithm Example: Finding the Shortest Path in a Network and more Study notes Communication in PDF only on Docsity!

Dijkstra’s Algorithm Example

Given : A network topology graph, G, with 6 nodes, and the link cost between nodes as shown below: A D E F B C 2 3 1 5 2 1 1 3 5 2 Find : Using Dijkstra’s Algorithm to find the least-cost paths for nodes A through F Solution: Let T = {A, B, C, D, E, F} = number of nodes in the network s = source node = A N = set of nodes that have been processed so far c(i,j) = link cost from node i to node j if two nodes are directly connected. = ∞ if there is no link between i and j D(v) = cost of the shortest path (least-cost path) from node s to node v that is currently known so far. When the algorithm terminates, D(v) is the cost of the shortest path from source s to destination v in the network. P(v): predecessor node along path from source s to v, that is next v

  1. [Initialization]
  • N = { A } initialize set N contains source node A
  • Compute D(v) for v is in set T but not in set N. Need to Compute D(v) for v = B, C, D, E, F v D(v) = c(s,v) = c(A,v) P(v)

B 2 A

C 5 A

D 1 A

E ∞ No path F ∞ No path A D E F B C 2 3 1 5 2 1 1 3 5 2

  1. [Get Next Node]
  • Find w not in N such that D(w) is the minimum. That is the smallest D(w) in the previous step add that node, w, to set N. If they are equal, randomly pick one of them. In this case, D(D) = 1 is the smallest, so we add node w = D to set N. N = { A, D }
  1. [Get Next Node]
  • Find w not in N such that D(w) is the minimum. That is the smallest D(w) in the previous step add that node, w, to set N. If they are equal, randomly pick one of them. In this case, both D(E) and D(B) have the same smallest cost of 2, we randomly pick one. We pick node w = E to set N. N = { A, D, E } A D E F B C 2 3 1 5 2 1 1 3 5 2
  1. [Update Cost for the Shortest Paths] D(v) = min [ D(v), D(w) + c(w,v) ] for all nodes v not in N = min [ D(v), D(E) + c(E,v) ] for node v = B, C, F v D(v) = min [ D(v), D(E) + c(E,v) ] D(v) P(v) B D(B) = min[D(B), 2 + c(E,B) ] = min[2, 2+∞ ] 2 A C D(C) = min[D(C), 2 + c(E,C) ] = min[4, 2+1 ] 3 New E F D(F) = min[D(F), 2 + c(E,F) ] = min[∞, 2+2 ] 4 New E

A D E F B C 2 3 1 5 2 1 1 3 5 2

[Get Next Node]

  • Find the smallest D(v) in the previous step add that node to N. If they are equal, randomly pick one of them. D(B) has the lowest cost = 2. We pick node w = B to set N. N = { A, D, E, B } A D E F B C 2 3 1 5 2 1 1 3 5 2
  1. [Update Cost for the Shortest Paths] D(v) = min [ D(v), D(w) + c(w,v) ] for all nodes v not in N

A D E F B C 2 3 1 5 2 1 1 3 5 2

  1. Update Cost for the Shortest Paths] D(v) = min [ D(v), D(w) + c(w,v) ] for all node n not in N = min [ D(v), D(C) + c(C,v) ] for node n = F v D(v) = min [ D(v), D(C) + c(C,v) ] D(v) P(v) F D(F) = min[D(F), 3 + c(C,F) ] = min[4, 3+5 ] 4 E A D E F B C 2 3 1 5 2 1 1 3 5 2

[Get Next Node]

  • Find the smallest D(v) in the previous step add that node to N. If they are equal, pick any one of them. D(F) is the only one, so we add node w = F to set N. N = { A, D, E, B, C, F } A D E F B C 2 3 1 5 2 1 1 3 5 2 N = T, so the algorithm terminate.
  1. Now, we have to write down the shortest path from node A to all other nodes. Start from backwards since those are the least cost for the shortest path, and then work towards source node A. The column D(v) is the cost for the shortest path from source A to v. v D(v) Path F 4 A-D-E-F D 1 A-D C 3 A-D-E-C E 2 A-D-E B 2 A-B