Djikistra Algorithm
Djikistra Algorithm
CS-439
Class : BE EE
• One routing protocol cannot handle the task of updating routing tables of all routers on the internet. For
this reason, an internet is divided into autonomous systems.
• An autonomous system (AS) exhibits the following characteristics:
• AS is a set of routers and networks managed by a single organization.
• An AS consists of a group of routers exchanging information via a common routing protocol.
• AS is connected that is, there is a path between any pair of nodes , except in the times of failure.
• Interior gateway routing protocol is used for passing routing information between routers within an AS.
The protocol used to pass routing information between routers in different ASs are refered as an exterior
gateway routing protocol.
• AS are connected by linking a router in one AS to a router in another AS. An AS consists of a set of routers
with two different types of connectivity:
• Internal Routers - These routers in an AS connect only to other routers in the same AS. These run interior
gateway routing protocols.
• Border Routers – These routers in an AS connect both to routers within an AS and to routers in one or
more AS. These devices are responsible for passing traffic between the AS and the rest of theinternetwork.
They run both interior and exterior routing protocols.
2
Internal Routing
External Routing
5
Dijkstra Algorithm
• Dijkstra’s algorithm can be stated as: Find the shortest paths from a given source node to all
other nodes by developing the paths in order of increasing path length.
• Dijkstra Algorithm is the centralized routing algorithm that is it takes the connectivity between
all nodes and all link costs as inputs and stores them in central location.
• Dijkstra’s algorithm is iterative and has the property that after the 𝑘𝑡ℎ iteration of the
algorithm, the least cost paths are known to 𝑘 destination nodes.
• This algorithm consists of an initialization step followed by a loop. The number of times the
loop is executed is equal to the number of nodes in the network.
• Upon termination, the algorithm will have calculated the shortest paths from the source node
to every other node in the network.
• Dijkstra cannot handle negative metrics.
• It is greedy: in every situation it makes the choice that is currently the best, without regard to
future situations.
6
Dijkstra Algorithm
1. Define
𝑠 = source node
P𝑣 = predecessor node(neighbor of 𝑣)
ሖ=
𝑁 set of visited nodes by the algorithm
2. Initialization
𝑁’ = {𝑠}
for all nodes 𝑣, if 𝑣 is a neighbor of 𝑠
then 𝐷𝑠,𝑣=𝑑𝑠,𝑣
else 𝐷𝑠,𝑣 = ∞
3. Loop
find 𝑤 not in 𝑁’ such that 𝐷𝑠,𝑤 is a minimum
add 𝑤 to 𝑁’, update 𝐷𝑠,𝑣 for each neighbor 𝑣 of 𝑤 and not in 𝑁’
𝐷𝑠,𝑣 = min( 𝐷𝑠,𝑣, 𝐷𝑠,𝑤 + 𝑑𝑤,𝑣 ) /* new cost to 𝑣 is either old cost to 𝑣 or known least path cost to 𝑤 plus cost
from 𝑤 to 𝑣 */
7
Execution of Dijkstra Algorithm
Using Dijkstra’s algorithm, find the least cost path from node A to node B in the given network.
5
C D
1
1
3
2 1
2 2
8
2
2 2
A E B
1 3
3
4
7 1
6
F G
3
8
Execution of Dijkstra Algorithm
Step 1 & 2 - Define & Initialization
𝑠 = A
P 𝐴 = A
ሖ = {A}
𝑁
Step ഥ
𝑵 𝑫𝑨,𝑪, 𝐏 𝑪 𝑫𝑨,𝑭 ,𝑷 𝑭 𝑫𝑨,𝑬 , 𝐏 𝑬 𝑫𝑨,𝑫, 𝐏 𝑫 𝑫𝑨,𝑮,𝐏 𝑮 𝑫𝑨,𝑩 , 𝐏 𝑩
1 {A} 1 , A (AC) 1 , A (AF) ∞ ∞ ∞ ∞
5
C D
1
1
3
2 1
2 2
8
2
2 2
A E B
1 3
3
4
7 1
6
F G
3
10
Execution of Dijkstra Algorithm
Step 3 - Loop
ሖ=
𝑁 {A,C}
Both nodes ‘C’ and ‘F’ have cost of 1 from source node. We chose Node C
for second iteration. Now we choose Node ‘F’ for third iteration and
included in 𝑵ሖ
11
Execution of Dijkstra Algorithm
Step ഥ
𝑵 𝑫𝑨,𝑪, 𝐏 𝑪 𝑫𝑨,𝑭 ,𝑷 𝑭 𝑫𝑨,𝑬 , 𝐏 𝑬 𝑫𝑨,𝑫, 𝐏 𝑫 𝑫𝑨,𝑮,𝐏 𝑮 𝑫𝑨,𝑩 , 𝐏 𝑩
1 {A} 1 , A (AC) 1 , A (AF) ∞ ∞ ∞ ∞
2 {A,C} 1 , A (AC) 1 , A (AF) 3 , C (ACE) 6 , C (ACD) ∞ ∞
5
C D
1
1
3
2 1
2 2
8
2
2 2
A E B
1 3
3
4
7 1
6
F G
3
12
Execution of Dijkstra Algorithm
Third Iteration (Loop)
ሖ=
𝑁 {A,C,F}
13
Executinoodens hoavfe bDeeinjvkissitterdaandAinlcgluoderdiitnh𝑵m.
14
Execution of Dijkstra Algorithm
Step ഥ
𝑵 𝑫𝑨,𝑪, 𝐏 𝑪 𝑫𝑨,𝑭 ,𝑷 𝑭 𝑫𝑨,𝑬 , 𝐏 𝑬 𝑫𝑨,𝑫, 𝐏 𝑫 𝑫𝑨,𝑮,𝐏 𝑮 𝑫𝑨,𝑩 , 𝐏 𝑩
1 {A} 1 , A (AC) 1 , A (AF) ∞ ∞ ∞ ∞
2 {A,C} 1 , A (AC) 1 , A (AF) 3 , C (ACE) 6 , C (ACD) ∞ ∞
3 {A,C,F) 1 , A (AC) 1 , A (AF) 3 , C (ACE) 6 , C (ACD) 7 , F (AFG) ∞
5
C D
1
1
3
2 1
2 2
8
2
2 2
A E B
1 3
3
4
7 1
6
F G
15
Execution of Dijkstra Algorithm
Fourth Iteration (Loop)
ሖ=
𝑁 {A,C,F,E}
node, for fifth iteration and continue step no. 3 of algorithm until all
16
ExecutthieonondesohfaveDbiejenkvsistirtead aAndligncolurdeidthinmሖ𝑵.
17
Execution of Dijkstra Algorithm
Step ഥ
𝑵 𝑫𝑨,𝑪, 𝐏 𝑪 𝑫𝑨,𝑭 ,𝑷 𝑭 𝑫𝑨,𝑬 , 𝐏 𝑬 𝑫𝑨,𝑫, 𝐏 𝑫 𝑫𝑨,𝑮,𝐏 𝑮 𝑫𝑨,𝑩 , 𝐏 𝑩
1 {A} 1 , A (AC) 1 , A (AF) ∞ ∞ ∞ ∞
2 {A,C} 1 , A (AC) 1 , A (AF) 3 , C (ACE) 6 , C (ACD) ∞ ∞
3 {A,C,F) 1 , A (AC) 1 , A (AF) 3 , C (ACE) 6 , C (ACD) 7 , F (AFG) ∞
4 {A,C,F,E} 1 , A (AC) 1 , A (AF) 3 , C (ACE) 4 , E (ACED) 7 , F (AFG) ∞
5
C D
1
1
3
2 1
2 2
8
2
2 2
A E B
1 3
3
7 4
1
6
F G 17
3
Execution of Dijkstra Algorithm
Final Picture (All Nodes have been added to 𝑁ሖ)
Step ഥ
𝑵 𝑫𝑨,𝑪, 𝐏 𝑪 𝑫𝑨,𝑭 ,𝑷 𝑭 𝑫𝑨,𝑬 , 𝐏 𝑬 𝑫𝑨,𝑫, 𝐏 𝑫 𝑫𝑨,𝑮,𝐏 𝑮 𝑫𝑨,𝑩 , 𝐏 𝑩
1 {A} 1 , A (AC) 1 , A (AF) ∞ ∞ ∞ ∞
2 {A,C} 1 , A (AC) 1 , A (AF) 3 , C (ACE) 6 , C (ACD) ∞ ∞
3 {A,C,F) 1 , A (AC) 1 , A (AF) 3 , C (ACE) 6 , C (ACD) 7 , F (AFG) ∞
4 {A,C,F,E} 1 , A (AC) 1 , A (AF) 3 , C (ACE) 4 , E (ACED) 7 , F (AFG) ∞
5 {A,C,F,E,D} 1 , A (AC) 1 , A (AF) 3 , C (ACE) 4 , E (ACED) 6 , D (ACEDG) 7 , D (ACEDB)
6 {A,C,F,E,D,G} 1 , A (AC) 1 , A (AF) 3 , C (ACE) 4 , E (ACED) 6 , D (ACEDG) 7 , D (ACEDB)
7 {A,C,F,E,D,G,B} 1 , A (AC) 1 , A (AF) 3 , C (ACE) 4 , E (ACED) 6 , D (ACEDG) 7 , D (ACEDB)
18
Dijkstra’s Algorithm - Performance
• When this algorithm terminates, we have, for each node, its predecessor along the least cost
path from the source node.
• For each predecessor, we also have its predecessor, and so in this manner we can construct
the entire path from the source to all destinations.
• What is the computational complexity of this algorithm?
• If we have 𝑛 nodes (excluding the source), then in the first iteration, we need to search
ሖ) to determine the nodes that has the minimum cost.
through all 𝑛 nodes(not in 𝑁
• In the second iteration, we need to check 𝑛 – 1 nodes to determine the minimum cost; in the
third iteration 𝑛 – 2 nodes, and so on.
• Overall, the total number of nodes we need to search through over all the iterations is
𝑛(𝑛 + 1)/2.
• So this algorithm has worst case complexity of 𝑂 𝑛 2.
19
Dijkstra Algorithm - Performance
• Dijkstra’s algorithm requires that each node must have complete topological information about
the network. That is, each node must know the link costs of all links in the network. Thus, for
this algorithm, information must be exchanged with all other nodes.
• The algorithm converges under static conditions of topology, and link costs. If the link costs
change over time, the algorithm will attempt to catch up with these changes.
20
Summary
• Interior and Exterior Gateway Routing Protocols
• Shortest Path Routing Algorithm
• Dijkstra Algorithm
• Execution of Dijkstra Algorithm
• Dijkstra Algorithm – Performance
21