Professional Documents
Culture Documents
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.
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.
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
Output:
0 5 8 9
INF 0 3 4
INF INF 0 1
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.
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 .
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.
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.
This method is easy on router's CPU but may cause the problem of duplicate
packets received from peer routers.
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.
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.
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
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-
A 0 A
B 2 B
C 5 B
D 1 D
At Router B-
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-
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.
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-
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-
A 2 A
B 0 B
C 3 C
D 3 A
At Router C-
A 5 B
B 3 B
C 0 C
D 6 B
At Router D-
A 1 A
B 3 A
C 6 A
D 0 D