Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

ODD SEMESTER-2021

Shortest Path Algorithms for Graphs


MTE PROJECT REPORT

Course Name: Discrete Structure


Course Code: CO-205

BACHELOR OF TECHNOLOGY
in

COMPUTER ENGINEERING

Submi ed by

Garvit Batra - 2K20/CO/159


Gaurav Gula - 2K20/CO/163
Under the supervision of

Mrs. Anuma Garg

DEPARTMENT OF COMPUTER ENGINEERING


DELHI TECHNOLOGICAL UNIVERSITY
(Formerly Delhi College of Engineering)
Bawana road, Delhi-110042

tt

ti

ACKNOWLEDGEMENT

In performing our major project, we had to take the help and guideline of
some respected persons, who deserve our greatest gra tude. The
comple on of this assignment gives us much pleasure. We would like to
show our gra tude to Mrs. Anuma Garg , Mentor for the this project.
Giving us a good guideline for report throughout numerous consulta ons.
We would also like to extend our deepest gra tude to all those who have
directly and indirectly guided us in wri ng this assignment.

Many people, our classmates and team members itself, have made
valuable comment sugges ons on this proposal which gave us inspira on
to improve our assignment.

We thank all the people for their help directly and indirectly to complete
our assignment.
ti
ti

ti

ti
ti

ti
ti
ti
CERTIFICATE

I hereby cer fy that the project Disserta on tled “Shortest Path


Algorithms for Graphs” which is submi ed by Gaurav Gula
(2K20/CO/163), Garvit Batra (2K20/CO/159) [Discrete
Mathema cs], Delhi Technological University, Delhi in complete
ful lment of the requirement for Evalua on of Midterm
Evalua on, is a record of the project work carried out by the
students under my supervision. To the best of my knowledge this
work has not been submi ed in part or full for any Degree or
Diploma to this University or elsewhere.

Place: Delhi Mrs. Anuma Garg





fi
ti
ti
ti

tt

ti
tt
ti
ti
ti

Introduc on
What are Graphs
A graph can be de ned as group of ver ces and edges that are used to connect
these ver ces. A graph can be seen as a cyclic tree, where the ver ces (Nodes)
maintain any complex rela onship among them instead of having parent child
rela onship.

A graph G can be de ned as an ordered set G(V, E) where V(G) represents the set of
ver ces and E(G) represents the set of edges which are used to connect these
ver ces.

A Graph G(V, E) with 5 ver ces (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E), (E,D),
(D,B), (D,A)) is shown in the following gure.

Directed and Undirected Graph


A graph can be directed or undirected. However, in an undirected graph, edges are
not associated with the direc ons with them. An undirected graph is shown in the
above gure since its edges are not a ached with any of the direc ons. If an edge

ti
ti
ti
fi

ti

ti
fi
fi

ti
ti
ti
fi
tt

ti

ti
ti
exists between vertex A and B then the ver ces can be traversed from B to A as well
as A to B.

In a directed graph, edges form an ordered pair. Edges represent a speci c path from
some vertex A to another vertex B. Node A is called ini al node while node B is
called terminal node.

A directed graph is shown in the following gure.

Graph Terminology

Path

A path can be de ned as the sequence of nodes that are followed in order to reach
some terminal node V from the ini al node U.

Closed Path

A path will be called as closed path if the ini al node is same as terminal node. A
path will be closed path if V0=VN.

Simple Path

If all the nodes of the graph are dis nct with an excep on V0=VN, then such path P is
called as closed simple path.

fi

ti
ti
fi
ti

ti

ti
ti
fi
Cycle

A cycle can be de ned as the path which has no repeated edges or ver ces except
the rst and last ver ces.

Connected Graph

A connected graph is the one in which some path exists between every two ver ces
(u, v) in V. There are no isolated nodes in connected graph.

Complete Graph

A complete graph is the one in which every node is connected with all other nodes.
A complete graph contain n(n-1)/2 edges where n is the number of nodes in the
graph.

Weighted Graph

In a weighted graph, each edge is assigned with some data such as length or weight.
The weight of an edge e can be given as w(e) which must be a posi ve (+) value
indica ng the cost of traversing the edge.

Digraph

A digraph is a directed graph in which each edge of the graph is associated with
some direc on and the traversing can be done only in the speci ed direc on.

Loop

An edge that is associated with the similar end points can be called as Loop.

Adjacent Nodes

If two nodes u and v are connected via an edge e, then the nodes u and v are called
as neighbours or adjacent nodes.

Degree of the Node

A degree of a node is the number of edges that are connected with that node. A
node with degree 0 is called as isolated node.
fi

ti

ti
fi

ti

fi
ti
ti
ti

ti
Graph Representa ons
In graph theory, a graph representa on is a technique to store graph into the
memory of computer.

To represent a graph, we just need the set of ver ces, and for each vertex the
neighbours of the vertex (ver ces which is directly connected to it by an edge). If it is
a weighted graph, then the weight will be associated with each edge.

There are di erent ways to op mally represent a graph, depending on the density of
its edges, type of opera ons to be performed and ease of use.

1. Adjacency Matrix

• Adjacency matrix is a sequen al representa on.

• It is used to represent which nodes are adjacent to each other. i.e. is there any
edge connec ng nodes to a graph.

• In this representa on, we have to construct a nXn matrix A. If there is any


edge from a vertex i to vertex j, then the corresponding element of A, ai,j = 1,
otherwise ai,j= 0.

Note, even if the graph on 100 ver ces contains only 1 edge, we s ll have to have a
100x100 matrix with lots of zeroes.

• If there is any weighted graph then instead of 1s and 0s, we can store the
weight of the edge.

Example:-

Consider the following undirected graph representa on:

Undirected graph representa on

ff
ti

ti

ti

ti
ti
ti
ti
ti

ti

ti

ti

ti
ti

ti

Directed graph Representa on

See the directed graph representa on:

In the above examples, 1 represents an edge from row vertex to column vertex, and
0 represents no edge from row vertex to column vertex.

Undirected weighted graph Representa on


ti

ti

ti

Pros: Representa on is easier to implement and follow.

Cons: It takes a lot of space and me to visit all the neighbours of a vertex, we have
to traverse all the ver ces in the graph, which takes quite some me.

2. Incidence Matrix
In Incidence matrix representa on, graph can be represented using a matrix of size:

Total number of ver ces by total number of edges.

It means if a graph has 4 ver ces and 6 edges, then it can be represented using a
matrix of 4X6 class. In this matrix, columns represent edges and rows represent
ver ces.

This matrix is lled with either 0 or 1 or -1. Where,

• 0 is used to represent row edge which is not connected to column vertex.

• 1 is used to represent row edge which is connected as outgoing edge to


column vertex.

• -1 is used to represent row edge which is connected as incoming edge to


column vertex.

Example

Consider the following directed graph representa on.


ti

fi

ti
ti
ti

ti
ti
ti
ti

ti

3. Adjacency List

• Adjacency list is a linked representa on.

• In this representa on, for each vertex in the graph, we maintain the list of its
neighbors. It means, every vertex of the graph contains list of its adjacent
ver ces.

• We have an array of ver ces which is indexed by the vertex number and for
each vertex v, the corresponding array element points to a singly linked list of
neighbors of v.

Example

Let's see the following directed graph representa on implemented using linked list:

We can also implement this representa on using array as follows:


ti

ti

ti
ti
ti

ti

Pros:

• Adjacency list saves lot of space.

• We can easily insert or delete as we use linked list.

• Such kind of representa on is easy to follow and clearly shows the adjacent
nodes of node.

Cons:

• The adjacency list allows tes ng whether two ver ces are adjacent to each
other but it is slower to support this opera on.

Need of Shortest Approach

In graph theory, the shortest path problem is the problem of nding a path between
two ver ces (or nodes) in a graph such that the sum of the weights of its cons tuent
edges is minimised.

The problem of nding the shortest path between two intersec ons on a road map
may be modelled as a special case of the shortest path problem in graphs, where the
ver ces correspond to intersec ons and the edges correspond to road segments,
each weighted by the length of the segment.
ti

ti

fi

ti
ti
ti

ti

ti

fi
ti
ti

The shortest path problem can be de ned for graphs whether undirected, directed,
or mixed. It is de ned here for undirected graphs; for directed graphs the de ni on
of path requires that consecu ve ver ces be connected by an appropriate directed
edge.

Two ver ces are adjacent when they are both incident to a common edge. A path in
an undirected graph is a sequence of ver ces such that is adjacent too for . Such a
path is called a path of length from too . (The are variables; their numbering here
relates to their posi on in the sequence and needs not to relate to any canonical
labelling of the ver ces).

Let be the edge incident to both and . Given a real-valued weight func on , and an
undirected (simple) graph , the shortest path from to is the path (where and ) that
over all possible minimises the sum When each edge in the graph has unit weight
or , this is equivalent to nding the path with fewest edges.

The problem is also some mes called the single-pair shortest path problem, to
dis nguish it from the following varia ons:

• The single-source shortest path problem, in which we have to nd shortest


paths from a source vertex v to all other ver ces in the graph.

• The single-des na on shortest path problem, in which we have to nd


shortest paths from all ver ces in the directed graph to a single des na on
vertex v. This can be reduced to the single-source shortest path problem by
reversing the arcs in the directed graph.

• The all-pairs shortest path problem, in which we have to nd shortest paths


between every pair of ver ces v, v' in the graph.

These generalisa ons have signi cantly more e cient algorithms than the simplis c
approach of running a single-pair shortest path algorithm on all relevant pairs of
ver ces. We are going to make a project that provide a complete analysis over
shortest path algorithms on graph, mainly the 3 algorithms djikstra, Floyd Warshall,
Bellman ford , these all aims towards providing shortest path in graph but in a
di erent way.
ff
ti
ti

ti

ti
ti
fi
ti
ti
ti
fi

ti
ti
ti
ti
fi
ti
ti
fi

ti

ti
ffi

fi

fi
ti
ti
ti
fi
fi
ti
ti
Dijkstra’s algorithm
What if you are provided with a graph of nodes where every node is linked to several
other nodes with varying distance. Now, if you begin from one of the nodes in the
graph, what is the shortest path to every other node in the graph?

Well simply explained, an algorithm that is used for nding the shortest distance,
or path, from star ng node to target node in a weighted graph is known as
Dijkstra’s Algorithm.

This algorithm makes a tree of the shortest path from the star ng node, the source,
to all other nodes (points) in the graph. Dijkstra's algorithm makes use of weights of
the edges for nding the path that minimizes the total distance (weight) among the
source node and all other nodes. This algorithm is also known as the single-source
shortest path algorithm.

Dijkstra’s algorithm is the itera ve algorithmic process to provide us with the


shortest path from one speci c star ng node to all other nodes of a graph. It is
di erent from the minimum spanning tree as the shortest distance among two
ver ces might not involve all the ver ces of the graph.

It is important to note that Dijkstra’s algorithm is only applicable when all weights
are posi ve because, during the execu on, the weights of the edges are added to
nd the shortest path.

And therefore if any of the weights are introduced to be nega ve on the edges of
the graph, the algorithm would never work properly. However, some algorithms like
the Bellman-Ford Algorithm can be used in such cases. It is also a known fact that
breadth- rst search(BFS) could be used for calcula ng the shortest path for an
unweighted graph, or for a weighted graph that has the same cost at all its edges.

But if the weighted graph has unequal costs at all its edges, then BFS infers uniform-
cost search. Now what? Instead of extending nodes in order of their depth from the
root, uniform-cost search develops the nodes in order of their costs from the root.
And a variant of this algorithm is accepted as Dijkstra’s Algorithm.
fi

ff
ti
ti
fi
fi
ti

fi
ti
ti
ti
ti

ti

fi
ti
ti

Generally, Dijkstra’s algorithm works on the principle of relaxa on where an


approxima on of the accurate distance is steadily displaced by more suitable values
un l the shortest distance is achieved.

Also, the es mated distance to every node is always an overvalue of the true
distance and is generally subs tuted by the least of its previous value with the
distance of a recently determined path.

It uses a priority queue to greedily choose the nearest node that has not been
visited yet and executes the relaxa on process on all of its edges.
How to Implement the Dijkstra Algorithm?

Before proceeding the step by step process for implemen ng the algorithm, let us
consider some essen al characteris cs of Dijkstra’s algorithm;

• Basically, the Dijkstra’s algorithm begins from the node to be selected, the
source node, and it examines the en re graph to determine the shortest path
among that node and all the other nodes in the graph.
• The algorithm maintains the track of the currently recognized shortest
distance from each node to the source code and updates these values if it
iden es another shortest path.
• Once the algorithm has determined the shortest path amid the source code to
another node, the node is marked as “visited” and can be added to the path.
• This process is being con nued ll all the nodes in the graph have been added
to the path, as this way, a path gets created that connects the source node to
all the other nodes following the plausible shortest path to reach each node.

Now explaining the step by step process of algorithm implementa on;

1. The very rst step is to mark all nodes as unvisited,


2. Mark the picked star ng node with a current distance of 0 and the rest nodes
with in nity,
3. Now, x the star ng node as the current node,
4. For the current node, analyse all of its unvisited neighbours and measure their
distances by adding the current distance of the current node to the weight of
the edge that connects the neighbour node and current node,

ti
ti
fi
fi
fi
fi
ti
ti

ti
ti
ti
ti
ti
ti

ti
ti
ti

ti

ti
ti

5. Compare the recently measured distance with the current distance assigned
to the neighbouring node and make it as the new current distance of the
neighbouring node,
6. A er that, consider all of the unvisited neighbours of the current node, mark
the current node as visited,
7. If the des na on node has been marked visited then stop, an algorithm has
ended, and
8. Else, choose the unvisited node that is marked with the least distance, x it as
the new current node, and repeat the process again from step 4.

Working Example of Dijkstra's Algorithm

In the above sec on, you have gained the step by step process of Dijkstra’s
algorithm, now let’s study the algorithm with an explained example. We will
calculate the shortest path between node C and the other nodes in the graph.

Example of Dijkstra's Algorithm

1. During the execu on of the algorithm, each node will be marked with its
minimum distance to node C as we have selected node C.

ft
ti

ti
ti
ti

fi

In this case, the minimum distance is 0 for node C. Also, for the rest of the nodes, as
we don’t know this distance, they will be marked as in nity (∞), except node
C (currently marked as red dot).

Graphical Representa on of Node C as Current Node

2. Now the neighbours of node C will be checked, i.e, node A, B, and D. We start
with B, here we will add the minimum distance of current node (0) with the
weight of the edge (7) that linked the node C to node B and get 0+ 7= 7.

Now, this value will be compared with the minimum distance of B (in nity), the least
value is the one that remains the minimum distance of B, like in this case, 7 is
less than in nity, and marks the least value to node B.

fi
ti

fi
fi

Assign Node B a minimum distance value

3. Now, the same process is checked with neighbour A. We add 0 with 1 (weight
of edge that connects node C to A), and get 1. Again, 1 is compared with the
minimum distance of A (in nity), and marks the lowest value.

Assign Node A a minimum distance value

The same is repeated with node D, and marked 2 as lowest value at D.



fi

Assign Node D a minimum distance value

Since, all the neighbours of node C have checked, so node C is marked as visited with
a green check mark.

Marked Node C as visited

4. Now, we will select the new current node such that the node must be
unvisited with the lowest minimum distance, or the node with the least
number and no check mark. Here, node A is the unvisited with minimum
distance 1, marked as current node with red dot.

Graphical Representa on of Node A as Current Node

We repeat the algorithm, checking the neighbour of the current node while ignoring
the visited node, so only node B will be checked.

For node B, we add 1 with 3 (weight of the edge connec ng node A to B) and obtain
4. This value, 4, will be compared with the minimum distance of B, 7, and
mark the lowest value at B as 4.

ti

ti
Assign Node B a minimum distance value

5. A er this, node A marked as visited with a green check mark. The current
node is selected as node D, it is unvisited and has a smallest recent distance.
We repeat the algorithm and check for node B and E.

Graphical Representa on of Node D as Current Node

For node B, we add 2 to 5, get 7 and compare it with the minimum distance value of
B, since 7>4, so leave the smallest distance value at node B as 4.

For node E, we obtain 2+ 7= 9, and compare it with the minimum distance of E which
is in nity, and mark the smallest value as node E as 9. The node D is marked
as visited with a green check mark.

ft
fi
ti

Marked Node D as visited

6. The current node is set as node B, here we need to check only node E as it is
unvisited and the node D is visited. We obtain 4+ 1=5, compare it with the
minimum distance of the node.

As 9 > 5, leave the smallest value at node node E as 5.

We mark D as visited node with a green check mark, and node E is set as the current
node.

Marked Node B as visited

7. Since it doesn’t have any unvisited neighbours, so there is not any


requirement to check anything. Node E is marked as a visited node with a
green mark.

Marked Node E as visited

So, we are done as no unvisited node is le . The minimum distance of each node is
now represen ng the minimum distance of that node from node C.

Pseudo Code:-
func on dijkstra(G, S)

for each vertex V in G

distance[V] <- in nite

previous[V] <- NULL

If V != S, add V to Priority Queue Q

distance[S] <- 0

while Q IS NOT EMPTY

U <- Extract MIN from Q

for each unvisited neighbour V of U

tempDistance <- distance[U] + edge_weight(U, V)



ti

ti

fi

ft

if tempDistance < distance[V]

distance[V] <- tempDistance

previous[V] <- U

return distance[], previous[]

Time Complexity:-
The given graph G=(V, E) is represented as an adjacency matrix. Here w[u, v] stores
the weight of edge (u, v).

The priority queue Q is represented as an unordered list.

Let |E| and |V| be the number of edges and ver ces in the graph, respec vely. Then
the me complexity is calculated:

Adding all |V| ver ces to Q takes O(|Vlog(V)|) me.

Removing the node with minimal dist takes O(|V|) me, and we only need O(1) to
recalculate dist[u] and update Q. Since we use an adjacency matrix here, we’ll need
to loop for |V| ver ces to update the dist array.

The me taken for each itera on of the loop is O(|V|), as one vertex is deleted from
Q per loop.

Thus, total me complexity becomes O(|Vlog(V)|) + O(|V|) \ mes O(|V|) = O(|V|


^2).

Floyd Warshall Algorithm

Just like Dijkstra’s algorithm, the Floyd Warshall algorithm is used to nd the shortest
path between all ver ces in the weighted graph. This algorithm works with both
directed and undirected graphs but it does not work along with the graph with
nega ve cycles. Therefore, if the distance from the vertex v from itself is nega ve
then we can assume that the graph has the presence of a nega ve cycle. This

ti
ti
ti

ti
ti
ti
ti

ti

ti

ti

ti

ti
fi
ti
ti
ti
algorithm follows the dynamic programming approach as its working pa ern. Here
the algorithm doesn’t construct the path itself but it can reconstruct the path with a
simple modi ca on. Floyd Warshall algorithm is also known as Roy Warshall
algorithm or Roy-Floyd algorithm. Let us study the working of the Floyd Warshall
algorithm.

Algorithm

We construct a matrix D that gives the length of the shortest path between each pair
of nodes.

The algorithm ini alizes D to L, that is, to the direct distances between nodes. It then
does n itera ons, a er itera on k, D gives the length of the shortest paths that only
use nodes in {1,2….k} as intermediate nodes.

A er n itera ons, D, therefore, gives the length of shortest paths using any of the
nodes in N as an intermediate node. If Dk represents the matrix D a er kth itera on
it can be implemented by

Dk [i , j] = min (Dk-1 [i , j], Dk-1 [i, k] + Dk-1 [k , j])

We use the principle of op mality to compute length from i to j passing through k.

How Floyd Warshall Algorithm Works (Example)


ft

ti
ti
fi
ti
ti
ft

ti
ti

ft
tt
ti

Crea ng matrix D0 contains the distance between each node with ‘0’ as an
intermediate node.

Upda ng matrix D1 which contains the distance between each node with ‘1’ as an
intermediate node. Update distance if minimum distance value smaller than exis ng
distance value found.

Dk [i , j] = min (Dk-1 [i , j], Dk-1 [i, k] + Dk-1 [k , j])

D1[3, 2] = min(D1-1[3, 2], D1-1 [3, 1] + D1-1 [1 , 2])

D1[4, 2] = min(In nity, 35)

Dk [i , j] = min (Dk-1 [i , j], Dk-1 [i, k] + Dk-1 [k , j])

D1[4, 2] = min(D1-1[4, 2], D1-1 [4, 1] + D1-1 [1 , 2])

D1[4, 2] = min(In nity, 20)

Here distance (3, 2) is updated from in nity to 35, and distance (4, 2) is updated
from in nity to 20 as shown below.

ti
ti
fi
fi
fi

fi

ti

Upda ng matrix D2 contains the distance between two nodes with ‘2’ as an
intermediate node.

D2 [i , j] = min (Dk-1 [i , j], Dk-1 [i, k] + Dk-1 [k , j])

D2[1, 3] = min(D2-1[1, 3], D2-1 [1, 2] + D2-1 [2 , 3])

D2[1, 3] = min(In nity, 20)

Dk [i , j] = min (Dk-1 [i , j], Dk-1 [i, k] + Dk-1 [k , j])

D2[1, 4] = min(D2-1[1, 4], D2-1 [1, 2] + D2-1 [2 , 4])

D2[1, 4] = min(In nity, 10)

Update distance if minimum distance value smaller than exis ng distance value
found. Here distance (1, 3) is updated from in nity to 20, and distance (1, 4) is
updated from in nity to 10.

ti
fi
fi
fi

fi

ti

Upda ng matrix D3 contains the distance between two nodes with ‘3’ as an
intermediate node.

Dk [i , j] = min (Dk-1 [i , j], Dk-1 [i, k] + Dk-1 [k , j])

D3[2, 1] = min(D3-1[2, 1], D3-1 [2, 3] + D3-1 [3 , 1])

D3[2, 1] = min(50, 45)

Update distance if minimum distance value smaller than exis ng distance value
found. Here distance (2, 1) is updated from 50 to 45

Upda ng matrix D4 contains the distance between two nodes with ‘4’ as an
intermediate node. Update distance if minimum distance value smaller than exis ng
distance value found.

Dk [i , j] = min (Dk-1 [i , j], Dk-1 [i, k] + Dk-1 [k , j])

D4[1, 3] = min(D4-1[1, 3], D4-1 [1, 4] + D4-1 [4 , 3])

D4[1, 3] = min(20, 15)

Dk [i , j] = min (Dk-1 [i , j], Dk-1 [i, k] + Dk-1 [k , j])

D4[2, 1] = min(D4-1[2, 1], D4-1 [2, 4] + D4-1 [4 , 1])

D4[1, 3] = min(45, 20)

ti
ti

ti
ti

Dk [i , j] = min (Dk-1 [i , j], Dk-1 [i, k] + Dk-1 [k , j])

D4[2, 3] = min(D4-1[2, 3], D4-1 [2, 4] + D4-1 [4 , 3])

D4[2, 3] = min(15, 10)

Here distance (1, 3) is updated from 20 to 15; distance (2, 1) is updated from 45 to
20, and distance (2, 3) is updated from 15 to 10

Pseudo Code:-

Time Complexity
There are three loops for compu ng the shortest path in the graph and each of
these loops has constant complexi es. Therefore, due to this, the me complexity of
the Floyd Warshall algorithm is O(n3). Also, the space complexity of the Floyd
Warshall algorithm is O(n2).

Bellman–Ford Algorithm
Bellman Ford algorithm helps us nd the shortest path from a vertex to all other
ver ces of a weighted graph.

It is similar to Dijkstra's algorithm but it can work with graphs in which edges can
have nega ve weights.

Why would one ever have edges with nega ve weights in real life?

Nega ve weight edges might seem useless at rst but they can explain a lot of
phenomena like cash ow, the heat released/absorbed in a chemical reac on, etc.

For instance, if there are di erent ways to reach from one chemical A to another
chemical B, each method will have sub-reac ons involving both heat dissipa on and
absorp on.

If we want to nd the set of reac ons where minimum energy is required, then we
will need to be able to factor in the heat absorp on as nega ve weights and heat
dissipa on as posi ve weights.

Why do we need to be careful with nega ve weights?

Nega ve weight edges can create nega ve weight cycles i.e. a cycle that will reduce
the total path distance by coming back to the same point.
ti
ti
ti
ti
ti
ti

fi
ti

fl

ff

ti
fi
ti
ti
ti
ti
fi
ti
ti
ti

ti

ti
ti
ti

Nega ve weight cycles can give an incorrect result when trying to nd out the
shortest path
Shortest path algorithms like Dijkstra's Algorithm that aren't able to detect such a
cycle can give an incorrect result because they can go through a nega ve weight
cycle and reduce the path length.

How Bellman Ford's algorithm works

Bellman Ford algorithm works by overes ma ng the length of the path from the
star ng vertex to all other ver ces. Then it itera vely relaxes those es mates by
nding new paths that are shorter than the previously overes mated paths.

By doing this repeatedly for all ver ces, we can guarantee that the result is
op mized.

Step-1 for Bellman Ford's algorithm


fi
ti
ti
ti

ti

ti

ti

ti
ti
ti
fi
ti
ti

Step-2 for Bellman Ford's algorithm

Step-3 for Bellman Ford's algorithm


Step-4 for Bellman Ford's algorithm

Step-5 for Bellman Ford's algorithm


Step-6 for Bellman Ford's algorithm

Bellman Ford Pseudocode

We need to maintain the path distance of every vertex. We can store that in an array
of size v, where v is the number of ver ces.

We also want to be able to get the shortest path, not only know the length of the
shortest path. For this, we map each vertex to the vertex that last updated its path length.
Once the algorithm is over, we can backtrack from the des na on vertex to the source vertex to
nd the path.

func on bellmanFord(G, S)
for each vertex V in G
distance[V] <- in nite
previous[V] <- NULL
distance[S] <- 0

for each vertex V in G


for each edge (U,V) in G
tempDistance <- distance[U] + edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
fi
ti

fi

ti

ti
ti

for each edge (U,V) in G


If distance[U] + edge_weight(U, V) < distance[V}
Error: Nega ve Cycle Exists

return distance[], previous[]

Time Complexity

Best Case Complexity O(E)

Average Case Complexity O(VE)

Worst Case Complexity O(VE)

Space Complexity

And, the space complexity is O(V).

Applica ons
Dijkstra’s Algorithm Applica ons
• For map applica ons, it is hugely deployed in measuring the least possible distance and
check direc on amidst two geographical regions like Google Maps, discovering map
loca ons poin ng to the ver ces of a graph, calcula ng tra c and delay- ming, etc.

• For telephone networks, this is also extensively implemented in the conduc ng of data in
networking and telecommunica on domains for decreasing the obstacle taken place for
transmission.

• Wherever addressing the need for shortest path explica ons either in the domain of
robo cs, transport, embedded systems, laboratory or produc on plants, etc, this algorithm
is applied.

• Besides that, other applica ons are road condi ons, road closures and construc on, and IP
rou ng to detect Open Shortest Path First.

Floyd Warshall Algorithm Applica ons


ti
ti
ti

ti

ti
ti
ti
ti

ti
ti

ti

ti

ti

ti
ti

ffi
ti
ti
ti
ti
ti

• Floyd Warshall Algorithm helps to nd the inversion of real matrices


• It helps in tes ng whether the undirected graph is bipar te
• It helps to nd the shortest path in a directed graph
• Di erent versions of the Floyd Warshall algorithm help to nd the transi ve closure of a
directed graph
• This algorithm helps to nd the regular expression the are accepted by nite automata.
• It helps in nding the similarity between the graphs
• Floyd Warshall algorithm helps in nding the op mal rou ng i.e the maximum ow
between two ver ces

Bellman Ford's Algorithm Applica ons


1. For calcula ng shortest paths in rou ng algorithms
2. For nding the shortest path

A distributed variant of the Bellman–Ford algorithm is used in distance-vector rou ng protocols,


for example the Rou ng Informa on Protocol (RIP). The algorithm is distributed because it involves
a number of nodes (routers) within an Autonomous system (AS), a collec on of IP networks
typically owned by an ISP. It consists of the following steps:

1. Each node calculates the distances between itself and all other nodes within the AS and
stores this informa on as a table.
2. Each node sends its table to all neighboring nodes.
3. When a node receives distance tables from its neighbors, it calculates the shortest routes
to all other nodes and updates its own table to re ect any changes.
ff
fi
fi
fi
ti
ti

ti
ti
ti

fi

ti

fi
ti
fi
fl

ti

ti
ti

fi
ti

fi
ti
ti
ti

fl
Conclusion

The current study focused on two areas: single-source shortest path and the shortest
path among all ver ces [6]. The advantages and disadvantages of an algorithm are
mainly measured from two aspects: the execu on me of the algorithm and the
storage space. By summarizing the papers from others [2] [3] [7], obtaining Table 1.

Table 3 Comparison Between Numerical and Experimental Results

The Dijkstra algorithm and the Bellman-Ford algorithm are similar in some place.
Both of them use the method of relaxa on calcula on, which is to nd the shortest
path by modifying the values of D[i] during the process of traversing the ver ces and
edges of the graph. The Dijkstra algorithm is mainly aimed at the graph with non-
nega ve weight nodes, while the bellman-ford algorithm can deal with the shortest
path problem with nega ve weights. They are used to draw the op mal solu on of
the shortest path, but the Bellman-Ford algorithm has huge redundancy and lower
e ciency, which is shown in Table 1.
The Dijkstra algorithm can only be used in single-source shortest path problem. But
the Floyd-Warshall algorithm is available to nd a shortest path between any two
points [7]. It is suitable for nding the shortest path among all ver ces or in a small
data scope. In conclusion, the Dijkstra algorithm applies spares graph. In actual
pplica on, the algorithm is always op mized, like heap op miza on. The me
ffi
ti
ti
ti

ti
fi

ti
ti
fi
ti
ti
ti
ti

ti
fi
ti
ti
ti
ti
ti

complexity cuts to n*logn [8]. The Bellman-Ford algorithm is ine cient but it is easy
to implement. When dealing with the problem with a large number

References

1. Rosen Discrete Mathema cs And its Applica ons 7th edi on.
2. h p s : / / i o p s c i e n c e . i o p . o r g / a r c l e /
10.1088/1742-6596/1087/2/022011/pdf
3. h ps://brilliant.org/wiki/shortest-path-algorithms/
tt
tt

ti

ti

ffi

ti
ti

You might also like