You are on page 1of 25

Shortest Dijkstra's algorithm

In Dijkstra’s algorithm to find the shortest path from a single source


vertex to all other vertices in the given graph.

Algorithm
Step1: Create a set sptSet (shortest path tree set) that keeps track of
vertices included in shortest path tree, i.e., whose minimum distance
from source is calculated and finalized. Initially, this set is empty.

Step2:
Assign a distance value to all vertices in the input graph. Initialize all
distance values as INFINITE. Assign distance value as 0 for the source
vertex so that it is picked first.

Step3: While sptSet doesn’t include all vertices

a) Pick a vertex u which is not there in sptSet and has minimum distance
value.
b) Include u to sptSet.
c) Update distance value of all adjacent vertices of u. To update the
distance values, iterate through all adjacent vertices. For every adjacent
vertex v, if sum of distance value of u (from source) and weight of edge
u-v, is less than the distance value of v, then update the distance value of
v.
Let's consider the following graph. We need to find the shortest distance
to all vertices from source vertex i.e. 0.
Now, we will assign the weight to these vertices. We will assign infinite
values to all except for source vertex.

Now, start constructing the shortest path tree, we will take the vertex with the
minimum weight and also update the weights of the neighbors of that vertex.
Keep updating the same till we cover all the nodes.
Note:- When two nodes have equal weights, we can give priority to anyone.
Bellman–Ford Algorithm

Dijkstra doesn’t work for Graphs with negative weight edges, Bellman-Ford
works for such graphs. Bellman-Ford is also simpler than Dijkstra Algorithm.
But time complexity of Bellman-Ford is more than Dijkstra.
Algorithm
Following are the detailed steps.
Input: Graph and a source vertex src
Output: Shortest distance to all vertices from src. If there is a negative weight
cycle, then shortest distances are not calculated, negative weight cycle is
reported.
Step1: This step initializes distances from the source to all vertices as infinite
and distance to the source itself as 0. Create an array dist[] of size |V| with all
values as infinite except dist[src] where src is source vertex.
Step 2: This step calculates shortest distances. Do following |V|-1 times where |
V| is the number of vertices in given graph.

a) Do following for each edge u-v


If dist[v] > dist[u] + weight of edge uv, then update dist[v]
dist[v] = dist[u] + weight of edge uv
Step3: This step reports if there is a negative weight cycle in graph. Do
following for each edge u-v.
If dist[v] > dist[u] + weight of edge uv, then “Graph contains negative weight
cycle”.
Note: The idea of step 3 is, step 2 guarantees the shortest distances if the graph
doesn’t contain a negative weight cycle. If we iterate through all edges one
more time and get a shorter path for any vertex, then there is a negative weight
cycle.
Example
Let us understand the algorithm with following example graph. The images are
taken from this source.
Let the given source vertex be 0. Initialize all distances as infinite, except the
distance to the source itself. Total number of vertices in the graph is 5, so all
edges must be processed 4 times.

Let all edges are processed in the following order: (B, E), (D, B), (B, D), (A, B),
(A, C), (D, C), (B, C), (E, D). We get the following distances when all edges are
processed the first time. The first row shows initial distances. The second row
shows distances when edges (B, E), (D, B), (B, D) and (A, B) are processed.
The third row shows distances when (A, C) is processed. The fourth row shows
when (D, C), (B, C) and (E, D) are processed.
The first iteration guarantees to give all shortest paths which are at most 1 edge
long. We get the following distances when all edges are processed second time
(The last row shows final values).
Floyd Warshall Algorithm

The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path
problem. The problem is to find shortest distances between every pair of
vertices in a given edge weighted directed Graph.
Example:
Input:
graph[][] = { {0, 5, INF, 10},
{INF, 0, 3, INF},
{INF, INF, 0, 1},
{INF, INF, INF, 0} }
which represents the following graph

Note that the value of graph[i][j] is 0 if i is equal to j


And graph[i][j] is INF (infinite) if there is no edge from vertex i to j.

Output:

Shortest distance matrix

0 5 8 9

INF 0 3 4

INF INF 0 1

INF INF INF 0


Floyd Warshall Algorithm

We initialize the solution matrix same as the input graph matrix as a first step.
Then we update the solution matrix by considering all vertices as an
intermediate vertex. The idea is to one by one pick all vertices and updates all
shortest paths which include the picked vertex as an intermediate vertex in the
shortest path. When we pick vertex number k as an intermediate vertex, we
already have considered vertices {0, 1, 2, .. k-1} as intermediate vertices. For
every pair (i, j) of the source and destination vertices respectively, there are two
possible cases.

Step1: k is not an intermediate vertex in shortest path from i to j. We keep the


value of dist[i][j] as it is.

Step2: k is an intermediate vertex in shortest path from i to j. We update the


value of dist[i][j] as dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j].

The following figure shows the above optimal substructure property in the all-
pairs shortest path problem.

Flooding Algorithm
Flooding
In which every incoming packet is sent out on every outgoing line. Flooding
obviously generates vast numbers of duplicate packets.
This adapts the technique in which every incoming packet is sent on every
outgoing line except from which it arrived. One problem with this is that
packets may go in loop and as a result of which a node may receive huge
duplicate packets. These problems can be overcome with the help of sequence
numbers and hop count
 Requires no network information like topology, load condition ,cost of
different paths
 Every incoming packet to a node is sent out on every .

For Example in above figure


 A incoming packet to (1) is sent out to (2),(3)
 from (2) is sent to (6),(4)
 from (3) it is sent to (4),(5)
 from (4) it is sent to (6),(5),(3)
 from (6) it is sent to (2),(4),(5)
 from (5) it is sent to (4),(3)
Characteristics –
 All possible routes between Source and Destination is tried. A packet will
always get through if path exists
 As all routes are tried, there will be atleast one route which is the shortest
 All nodes directly or indirectly connected are visited
Limitations –
 Flooding generates vast number of duplicate pakects
 Suitable damping mechanism must be used
Hop-Count –
 A hop counter may be contained in the packet header which is
decremented at each hop with the packet being discarded when the
counter becomes zero
 The sender initializes the hop counter. If no estimate is known, it is set to
the full diameter of the subnet.
 Keep track of the packets which are responsible for flooding using a
sequence number. Avoid sending them out a second time.
Selective Flooding: Routers do not send every incoming packet out on every
line, only on those lines that go in approximately in the direction of the
destination.

Advantages of Flooding :
 Highly Robust, emergency or immediate messages can be sent (eg
military applications)
 Set up route in virtual circuit
 Flooding always chooses the shortest path
 Broadcast messages to all the nodes

Hierarchical Routing
Hierarchical Routing is the method of routing in networks that is based on
hierarchical addressing. Most transmission control protocol, Internet protocol.
When hierarchical routing is used, the routers are divided into what we will call
regions. Each router knows all the details about how to route packets to
destinations within its own region.
When different networks are interconnected, it is natural to regard each one as a
separate region to free the routers in one network from having to know the
topological structure of the other ones.

For huge networks, a two-level hierarchy may be insufficient, it may be


necessary to group the regions into clusters, the clusters into zones, the zones
into groups, and so on, until we run out of names for aggregations.

Figure : Hierarchical routing.


Broadcast Routing
By default, the broadcast packets are not routed and forwarded by the routers on
any network. Routers create broadcast domains. But it can be configured to
forward broadcasts in some special cases. A broadcast message is destined to all
network devices.

Algorithm:
Step1: A router creates a data packet and then sends it to each host one by one.
In this case, the router creates multiple copies of single data packet with
different destination addresses. All packets are sent as unicast but because they
are sent to all, it simulates as if router is broadcasting. This method consumes
lots of bandwidth and router must destination address of each node.

Step2: when router receives a packet that is to be broadcasted, it simply floods


those packets out of all interfaces. All routers are configured in the same way.

This method is easy on router's CPU but may cause the problem of duplicate
packets received from peer routers.

Reverse path forwarding is a technique, in which router knows in advance about


its predecessor from where it should receive broadcast. This technique is used to
detect and discard duplicates.

Multicast Routings
Multicast routing is special case of broadcast routing with significance
difference and challenges. In broadcast routing, packets are sent to all nodes
even if they do not want it. But in Multicast routing, the data is sent to only
nodes which wants to receive the packets.

The router must know that there are nodes, which wish to receive multicast
packets (or stream) then only it should forward. Multicast routing works
spanning tree protocol to avoid looping.
Multicast routing also uses reverse path Forwarding technique, to detect and
discard duplicates and loops.

Distance Vector Routing Algorithm-


A distance vector routing algorithm operates by having each router maintain
a table (i.e., a vector) giving the best known distance to each destination and
which link to use to get there. These tables are updated by exchanging
information with the neighbors. Eventually, every router knows the best link to
reach each destination.

The distance vector routing algorithm is sometimes called by other names, most
commonly the distributed Bellman-Ford routing algorithm, after the
researchers who developed it (Bellman, 1957; and Ford and Fulkerson, 1962). It
was the original ARPANET routing algorithm and was also used in the Internet
under the name RIP.

In distance vector routing, each router maintains a routing table indexed by,
and containing one entry for each router in the network. This entry has two
parts: the preferred outgoing line to use for that destination and an estimate of
the distance to that destination.

Distance Vector Routing is a dynamic routing algorithm. It works in the


following steps-

Step-01:

Each router prepares its routing table. By their local knowledge. each router
knows about-
 All the routers present in the network
 Distance to its neighboring routers

Step-02:
 ach router exchanges its distance vector with its neighboring routers.
 Each router prepares a new routing table using the distance vectors it has
obtained from its neighbors.
 This step is repeated for (n-2) times if there are n routers in the network.
 After this, routing tables converge / become stable

Distance Vector Routing Example-


Consider-
 There is a network consisting of 4 routers.
 The weights are mentioned on the edges.
 Weights could be distances or costs or delays.

Step-01:
Each router prepares its routing table using its local knowledge. Routing table
prepared by each router is shown below-
At Router A-
Destination Distance Next Hop

A 0 A

B 2 B

C ∞ –

D 1 D

At Router B-
Destination Distance Next Hop

A 2 A
B 0 B

C 3 C

D 7 D

At Router C-
Destination Distance Next Hop

A ∞ –

B 3 B

C 0 C

D 11 D

At Router D-
Destination Distance Next Hop

A 1 A

B 7 B

C 11 C

D 0 D

Step-02:
 Each router exchanges its distance vector obtained in Step-01 with its
neighbors.
 After exchanging the distance vectors, each router prepares a new routing
table.

At Router A-
 Router A receives distance vectors from its neighbors B and D.
 Router A prepares a new routing table as-

 Cost of reaching destination B from router A = min { 2+0 , 1+7 } = 2 via B.


 Cost of reaching destination C from router A = min { 2+3 , 1+11 } = 5 via B.
 Cost of reaching destination D from router A = min { 2+7 , 1+0 } = 1 via D.

Explanation For Destination B


 Router A can reach the destination router B via its neighbor B or
neighbor D.
 It chooses the path which gives the minimum cost.
 Cost of reaching router B from router A via neighbor B = Cost (A→B) +
Cost (B→B)= 2 + 0 = 2
 Cost of reaching router B from router A via neighbor D = Cost (A→D) +
Cost (D→B) = 1 + 7 = 8
 Since the cost is minimum via neighbor B, so router A chooses the path
via B.
 It creates an entry (2, B) for destination B in its new routing table.
 Similarly, we calculate the shortest path distance to each destination
router at every router.
Thus, the new routing table at router A is-

Destination Distance Next Hop

A 0 A

B 2 B

C 5 B

D 1 D

At Router B-

 Router B receives distance vectors from its neighbors A, C and D.


 Router B prepares a new routing table as-

 Cost of reaching destination A from router B = min { 2+0 , 3+∞ , 7+1 } =


2 via A.
 Cost of reaching destination C from router B = min { 2+∞ , 3+0 , 7+11 }
= 3 via C.
 Cost of reaching destination D from router B = min { 2+1 , 3+11 , 7+0 }
= 3 via A.

Thus, the new routing table at router B is-

Destination Distance Next Hop

A 2 A

B 0 B

C 3 C
D 3 A

At Router C-
 Router C receives distance vectors from its neighbors B and D.
 Router C prepares a new routing table as-

 Cost of reaching destination A from router C = min { 3+2 , 11+1 } = 5 via B.


 Cost of reaching destination B from router C = min { 3+0 , 11+7 } = 3 via B.
 Cost of reaching destination D from router C = min { 3+7 , 11+0 } = 10 via B.

Thus, the new routing table at router C is-

Destination Distance Next Hop

A 5 B

B 3 B

C 0 C

D 10 B
At Router D-
 Router D receives distance vectors from its neighbors A, B and C.

 Router D prepares a new routing table as-

 Cost of reaching destination A from router D = min { 1+0 , 7+2 , 11+∞ } = 1


via A.
 Cost of reaching destination B from router D = min { 1+2 , 7+0 , 11+3 } = 3
via A.
 Cost of reaching destination C from router D = min { 1+∞ , 7+3 , 11+0 } =
10 via B.

Thus, the new routing table at router D is-

Destination Distance Next Hop

A 1 A

B 3 A

C 10 B

D 0 D
Step-03:
 Each router exchanges its distance vector obtained in Step-02 with its
neighboring routers.
 After exchanging the distance vectors, each router prepares a new routing
table.

At Router A-

 Router A receives distance vectors from its neighbors B and D.


 Router A prepares a new routing table as-

 Cost of reaching destination B from router A = min { 2+0 , 1+3 } = 2 via B.


 Cost of reaching destination C from router A = min { 2+3 , 1+10 } = 5 via B.
 Cost of reaching destination D from router A = min { 2+3 , 1+0 } = 1 via D.

Thus, the new routing table at router A is-


Destination Distance Next Hop

A 0 A

B 2 B

C 5 B

D 1 D

At Router B-
 Router B receives distance vectors from its neighbors A, C and D.
 Router B prepares a new routing table as-

 Cost of reaching destination A from router B = min { 2+0 , 3+5 , 3+1 } = 2


via A.
 Cost of reaching destination C from router B = min { 2+5 , 3+0 , 3+10 } = 3
via C.
 Cost of reaching destination D from router B = min { 2+1 , 3+10 , 3+0 } = 3
via A.

Thus, the new routing table at router B is-

Destination Distance Next Hop

A 2 A
B 0 B

C 3 C

D 3 A

At Router C-

 Router C receives distance vectors from its neighbors B and D.


 Router C prepares a new routing table as-

 Cost of reaching destination A from router C = min { 3+2 , 10+1 } = 5 via B.


 Cost of reaching destination B from router C = min { 3+0 , 10+3 } = 3 via B.
 Cost of reaching destination D from router C = min { 3+3 , 10+0 } = 6 via B.

Thus, the new routing table at router C is-

Destination Distance Next Hop

A 5 B

B 3 B

C 0 C

D 6 B

At Router D-

 Router D receives distance vectors from its neighbors A, B and C.


 Router D prepares a new routing table as-

 Cost of reaching destination A from router D = min { 1+0 , 3+2 , 10+5 } = 1


via A.
 Cost of reaching destination B from router D = min { 1+2 , 3+0 , 10+3 } = 3
via A.
 Cost of reaching destination C from router D = min { 1+5 , 3+3 , 10+0 } = 6
via A.

Thus, the new routing table at router D is-

Destination Distance Next Hop

A 1 A

B 3 A

C 6 A

D 0 D

These will be the final routing tables at each router.

You might also like