You are on page 1of 16

REVIEW 3 ECE2003 J COMPONENT

CSE2003 - DATA STRUCTURES &


ALGORITHMS

J COMPONENT REPORT

SHORTEST POSSIBLE PATH TRAVELLED


BY AN AIRCRAFT USING THREE
DIFFERENT ALGORITHMS

MEMBER DETAILS:
1. AKSHAY JAITHALIA – 19BEC0704
2. KASHYAP BASTOLA – 19BCE2529

1|Pa g e
REVIEW 3 ECE2003 J COMPONENT

ABSTRACT
Finding the shortest distance for any object to travel on is an achievement in itself and this is
realised using many types of algorithms. These algorithms are the building blocks to better and
more efficient algorithms as technology progresses. One such important field where shortest
distance is the key to overall success is the aviation industry. This industry is responsible for
short to long distance transportation of essential services around the world with maximum
efficiency.

This industry in turn relies on Avionics industry which produce hi-tech navigation devices
using concepts from the existing algorithms like Dijkstra, Bellman-Ford, A* search, Floyd
Warshall, Johnson, Viterbi algorithm and many more, to come up with one of the most
advanced and efficient navigation algorithms yet.

This project compares a few of these shortest path algorithms to find out which algorithm is
the most applicable with respect to number of nodes and runtime and will also be discussing
the algorithms of all three methods and how the number of nodes affect the time.

This project also discusses the runtime of these algorithms. By comparing these algorithms,
conclusion can be drawn as to which algorithm is best for a required number of nodes and the
efficiency of that algorithm with regards to time and memory. Our goal in this project is to
demonstrate three different algorithms namely, Bellman-Ford, Floyd Warshall and Johnson
which are used for calculating shortest distance and will also proposing a better algorithm based
on the number of results acquired in the minimum time frame and memory space used.

By the end of this report, we would be able to fully understand the underlying working principle
behind these algorithms. We are going to use PYTHON 3 coding software to simulate the
algorithms and, in the end, compare the time complexities of all three and will discuss as to
which on is better than the other in many aspects the most important of which is the time
complexity of all three of them.

2|Pa g e
REVIEW 3 ECE2003 J COMPONENT

TABLE OF CONTENTS

INTRODUCTION………………………………………….………4

LITERATURE REVIEW………………………….………………5

ALGORITHMS USED………………………...………………….6-8

1. BELLMAN FORD

2. FLOYD WARSHALL

3. JOHNSON

CODE AND OUTPUT…………………………………………..9-14

CONCLUSION AND COMPARSION………………….……...15

REFERENCES…………………………………………………. 16

3|Pa g e
REVIEW 3 ECE2003 J COMPONENT

INTRODUCTION:
The shortest path problem in graph theory is the task of finding a path between two vertices (or
nodes) in a graph that minimises the sum of the weights of its constituent edges. The problem
of finding the shortest path between two intersections on a road map can be modelled as a
special case of the shortest path problem in graphs, where the vertices correspond to
intersections and the edges to road segments, each of which is weighted by the segment's
length.

On web mapping websites like MapQuest or Google Maps, shortest path algorithms are used
to automatically find directions between physical locations, such as driving direct ions. Fast
specialised algorithms are available for this application. A similar concept can be applied to air
navigation, which is made up of a haphazard collection of zones and waypoints.

Traveling by plane from one place to another is becoming a cost-effective mode of


transportation in today's generation because the greater the distance travelled, the more the user
has to pay. In some cases, we will first have to go to another place, from where we will have
to take a flight to the desired location, which will cost a lot of money.

Most of the time, users miss out on the shortest route to fly and end up spending more than
they should. The concept of shortest distance mainly comes into play in emergencies where
time is of the essence, such as floods, tsunamis, earthquakes. In such cases helicopters and
planes have to move from place to place to help survivors and also transport them to safety
depending on the disaster. In such cases, taking the shortest route is the most important thing
that saves a lot of time.

The same technology at work at Google Maps uses the same algorithms to provide alternative
routes with the lowest eta. The main objective of this project is based on a comparison of the
performance of different shortest path algorithms and their effects on users of modern transport,
especially air transport. There are different algorithms that can be used to find the shortest path,
the best known are Dijkstra algorithm, Bellman Ford algorithm, Floyd warshall algorithm and
Johnson algorithm.

4|Pa g e
REVIEW 3 ECE2003 J COMPONENT

LITERATURE REVIEW:
Some existing papers and sources were revisited to better understand our motivation of taking
up this project and to justify why shortest distance algorithms are the way to go in the coming
future too.

1. Investigation of Bellman–Ford Algorithm, Dijkstra's Algorithm for suitability of SPP

This paper discusses the concept of shortest path in detail explaining how node to node one can
travers and how the system is able to find the shortest path efficiently. Two algorithms are used
in this paper whose results were also compared namely Bellman Ford algorithm and Dijkstra’s
algorithm. The results were compared based on a small no of nodes and also for large no of
nodes. It was observed that running time for both algorithms Dijkstra’s and Bellman Ford is in
mere micro seconds thus concluding that bellman ford is more efficient for small no of nodes
whereas Dijkstra is more suited involving large no of nodes.

2. A Review And Evaluations Of Shortest Path Algorithms

The importance of shortest path is well highlighted in this paper, and four algorithms will be
compared in solving the shortest path problem, namely Dijkstra's Algorithm, Floyd-Warshall
Algorithm, Bellman-Ford Algorithm, and Genetic Algorithm (GA). The overall goal of the
paper is to present the results of evaluating the Dijkstra's, Floyd-Warshall, and Bellman-Ford
algorithms, as well as their time complexity. The GA algorithm was found to be the most
optimal of the four because it generates a large number of solutions rather than just one.

3. A Survey of Shortest-Path Algorithms

The main goal of this paper was to test the different shortest path algorithms that can be used
in navigation systems, such as Dijkstra's algorithm, Symmetrical Dijkstra's algorithm, A*
algorithm, Bellman-Ford algorithm, Floyd-Warshall algorithm, and Genetic algorithm. In most
situations, the Bellman Ford algorithm outperformed other algorithms, accordi ng to the
findings. As a result, the Bellman-Ford algorithm was found to be the most efficient shortest
path algorithm when compared to others. The genetic algorithm, on the other hand, was shown
to have the longest running time but to be capable of producing the best solution in most
situations, including multiple solutions.

5|Pa g e
REVIEW 3 ECE2003 J COMPONENT

ALGORITHMS USED:
In our project we have decided to run and compare three of the more popular shortest distance
algorithms which are as under:

1. Bellman Ford algorithm

A single source shortest path algorithm is the Bellman Ford algorithm. The shortest
distance between a single vertex and all other vertices in the weighted graph is found using
this approach. Other techniques, such as Dijkstra's algorithm, are used to find the shortest
path. If the weighted network has negative weight values, then
Dijkstra's algorithm cannot determine whether it produces the correct solution.
The ford bellman algorithm, unlike Dijkstra's algorithm, guarantees the correct answer
even if the weighted network has a negative weight value.

HOW IT WORKS

6|Pa g e
REVIEW 3 ECE2003 J COMPONENT

2. Floyd Warshall algorithm

The Floyd – Warshall algorithm (also known as the Floyd algorithm, the Roy -
Warshall algorithm, the Roy - Floyd algorithm, or the WFI algorithm) is a directed
weighted graph algorithm for finding the weighted shortest path number of positive or
negative edges (but no negative period). The length (sum weight) of the shortest paths
between all pairs of vertices will be found in one iteration of the process. Paths can be
recreated with simple adjustments to the method, although it does not return the details of
the paths themselves. Versions of this method can also be used to determine the transitive
closure of a relation (RR display type) or the widest paths between all pairs of vertices in a
weighted graph (results). compatible with the Schulze voting system).

HOW IT WORKS

7|Pa g e
REVIEW 3 ECE2003 J COMPONENT

3. Johnson’s algorithm

The goal is to discover the shortest path between any two vertices in a weighted directed
lattice, where the weights can be negative. We can find all pairs of shortest paths in O(V2
log V + VE) time using Johnson's algorithm. Both Dijkstra's algorithm and BellmanFord's
algorithm are used in Johnson's algorithm. The "weighted" technique is used by Johnson's
algorithm. If all edge weights w in the graph G = (V, E) are non-negative, we can use
Dijkstra's algorithm to determine the shortest path between all pairs of vertices. If G has
negatively weighted edges, we compute a new set of non-negative edge weights allowing
us to continue using the same approach. The new set of w-weighted edges must satisfy two
requirements: the shortest path from u to v using the weight function w is also the shortest
path from u to v using the weight function w for every pair of vertices u, v, V. The new
weight w(u, v) is non-negative for all edges (u, v). Let h: vR be any function that translates
vertices into a real integer, for a directed graph with weight G = (V, E) with weight function
w: ER.

HOW IT WORKS

8|Pa g e
REVIEW 3 ECE2003 J COMPONENT

CODE & OUTPUT:

Bellman ford
# Bellman Ford Algorithm in Python 3
class Graph:

def __init__(self, vertices):


self.V = vertices # Total number of vertices in the graph
self.graph = [] # Array of edges

# Add edges
def add_edge(self, s, d, w):
self.graph.append([s, d, w])

# Print the solution


def print_solution(self, dist):
print("Vertex Distance from Source")
for i in range(self.V):
print("{0}\t\t{1}".format(i, dist[i]))

def bellman_ford(self, src):

# Step 1: fill the distance array and predecessor array


dist = [float("Inf")] * self.V
# Mark the source vertex
dist[src] = 0

# Step 2: relax edges |V| - 1 times


for _ in range(self.V - 1):
for s, d, w in self.graph:
if dist[s] != float("Inf") and dist[s] + w < dist[d]:
dist[d] = dist[s] + w

# Step 3: detect negative cycle


# if value changes then we have a negative cycle in the graph
# and we cannot find the shortest distances
for s, d, w in self.graph:
if dist[s] != float("Inf") and dist[s] + w < dist[d]:
print("Graph contains negative weight cycle")
return

# No negative weight cycle found!


# Print the distance and predecessor array
self.print_solution(dist)

g = Graph(5)
g.add_edge(0, 1, 5) # (SOURCE, DESTINATION, WEIGHT)
g.add_edge(0, 2, 4)

9|Pa g e
REVIEW 3 ECE2003 J COMPONENT

g.add_edge(1, 3, 3)
g.add_edge(2, 1, 6)
g.add_edge(3, 2, 2)
g.add_edge(2, 4, 3)

g.bellman_ford(0)

Floyd warshall
# Floyd Warshall Algorithm in python
# The number of vertices
nV = 4

INF = 999

# Algorithm implementation
def floyd_warshall(G):
distance = list(map(lambda i: list(map(lambda j: j, i)), G))

# Adding vertices individually


for k in range(nV):
for i in range(nV):
for j in range(nV):
distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]) **********
print_solution(distance)

# Printing the solution


def print_solution(distance):
for i in range(nV):
for j in range(nV):
if(distance[i][j] == INF):
print("INF", end=" ")
else:
print(distance[i][j], end=" ")

10 | P a g e
REVIEW 3 ECE2003 J COMPONENT

print(" ")

G = [[0, 3, INF, 5],


[2, 0, INF, 4],
[INF, 1, 0, INF],
[INF, INF, 2, 0]]
floyd_warshall(G)

NOTE:
• The 0s have the index a[i][i]. They are basically the distance fro
m the same vertex to the same vertex hence 0.

• for example if the distance is 5 from node 2 to 3, then 5 will be


placed in a matrix index of a[2][3]
• If for some combination the distance cannot be measured so it is
labelled as infinity.

• But after applying the Floyd warshall algorithm it can certainly b


e updated again and again resulting in multiple matrices.
• The output we see is just the best possible matrix for the input c
ombination

Johnson’s algorithm
# Implementation of Johnson's algorithm in Python3

# Import function to initialize the dictionary


from collections import defaultdict
INT_MAX = float('Inf')

# Function that returns the vertex


# with minimum distance
# from the source
def Min_Distance(dist, visit):

(minimum, Minimum_Vertex) = (INT_MAX, 0)


for vertex in range(len(dist)):
if minimum > dist[vertex] and visit[vertex] == False:
(minimum, minVertex) = (dist[vertex], vertex)

11 | P a g e
REVIEW 3 ECE2003 J COMPONENT

return Minimum_Vertex

# Dijkstra Algorithm for Modified


# Graph (After removing the negative weights)
def Dijkstra_Algorithm(graph, Altered_Graph, source):

# Number of vertices in the graph


tot_vertices = len(graph)

# Dictionary to check if given vertex is


# already included in the shortest path tree
sptSet = defaultdict(lambda : False)

# Shortest distance of all vertices from the source


dist = [INT_MAX] * tot_vertices

dist[source] = 0

for count in range(tot_vertices):

# The current vertex which is at min Distance


# from the source and not yet included in the
# shortest path tree
curVertex = Min_Distance(dist, sptSet)
sptSet[curVertex] = True

for vertex in range(tot_vertices):


if ((sptSet[vertex] == False) and
(dist[vertex] > (dist[curVertex] +
Altered_Graph[curVertex][vertex])) and
(graph[curVertex][vertex] != 0)):

dist[vertex] = (dist[curVertex] +Altered_Graph[curVertex][vertex])

# Print the Shortest distance from the source


for vertex in range(tot_vertices):
print ('Vertex ' + str(vertex) + ': ' + str(dist[vertex]))

# Function to calculate shortest distances from source


# to all other vertices using Bellman-Ford algorithm
def BellmanFord_Algorithm(edges, graph, tot_vertices):

# Add a source s and calculate its min


# distance from every other node
dist = [INT_MAX] * (tot_vertices + 1)
dist[tot_vertices] = 0

for i in range(tot_vertices):
edges.append([tot_vertices, i, 0])

12 | P a g e
REVIEW 3 ECE2003 J COMPONENT

for i in range(tot_vertices):
for (source, destn, weight) in edges:
if((dist[source] != INT_MAX) and
(dist[source] + weight < dist[destn])):
dist[destn] = dist[source] + weight

# Don't send the value for the source added


return dist[0:tot_vertices]

# Function to implement Johnson Algorithm


def JohnsonAlgorithm(graph):

edges = []

# Create a list of edges for Bellman-Ford Algorithm


for i in range(len(graph)):
for j in range(len(graph[i])):

if graph[i][j] != 0:
edges.append([i, j, graph[i][j]])

# Weights used to modify the original weights


Alter_weigts = BellmanFord_Algorithm(edges, graph, len(graph))

Altered_Graph = [[0 for p in range(len(graph))] for q in


range(len(graph))]

# Modify the weights to get rid of negative weights


for i in range(len(graph)):
for j in range(len(graph[i])):

if graph[i][j] != 0:
Altered_Graph[i][j] = (graph[i][j] +
Alter_weigts[i] - Alter_weigts[j]);

print ('Modified Graph: ' + str(Altered_Graph))

# Run Dijkstra for every vertex as source one by one


for source in range(len(graph)):
print ('\nShortest Distance with vertex ' +
str(source) + ' as the source:\n')
Dijkstra_Algorithm(graph, Altered_Graph, source)

# Driver Code
graph = [[0, -5, 2, 3],
[0, 0, 4, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]]

JohnsonAlgorithm(graph)

13 | P a g e
REVIEW 3 ECE2003 J COMPONENT

14 | P a g e
REVIEW 3 ECE2003 J COMPONENT

OUR CONCLUSION & COMPARISION:


We can conclude from the above outputs obtained from the execution of the codes with the
same inputs that the Bellman ford algorithm generates less time complexity than other
algorithms and is the most economical of the three where as Johnson’s algorithm is more
complex and has a better and a more detailed result. It is just like an upgraded version of
Bellman Ford algorithm in a lot of ways with some concepts improved from the existing
Dijkstra algorithm

• Floyd Warshall algorithm generates time complexity lower than Johnson but higher than

Bellman Ford.

• Compared with other algorithms, Johnson's algorithm generates the most time complexity.

• The Bellman–Ford algorithm can easily flag the presence of negative cycles in the graph.

• If negative cycles are present, they are converted to positive edges prior to program

execution in Johnson's algorithm. This is why it is preferred over Dijkstra's algorithm.

• Negative edge weights are allowed in Floyd Warshall, but negative edge weights are not

allowed in Floyd Warshall.

15 | P a g e
REVIEW 3 ECE2003 J COMPONENT

REFERENCES:
[1] https://www.programiz.com/dsa/floyd-warshall-algorithm
[2] https://www.sanfoundry.com/python-program-implement-johnsons-algorithm/
[3] https://www.thecrazyprogrammer.com/2017/06/bellman-ford-algorithm-in-c-and-c.html
[4] https://www.researchgate.net/figure/Chart-with-the-results-for-the-first-algorithmsfor-the-
first-table-in-seconds-and_fig1_304665934
[5] http://web.eecs.utk.edu/courses/spring2016/cosc494/notes/Floyd/index.html
[6] https://www.ijedr.org/papers/IJEDR1801130.pdf
[7] https://brilliant.org/wiki/shortest-path-algorithms/#comparison-of-algorithms
[8] https://www.researchgate.net/figure/Run-times-on-the-scale-free-benchmarks-for-
graphs-of-induced-widths-38-to-58-and-varying_fig6_259804004
[9] https://www.sanfoundry.com/
[10] https://gist.github.com/
[11] https://gist.github.com/ashleyholman/6793360
[12] https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
[13] https://www.thecrazyprogrammer.com/2017/06/bellman-ford-algorithm- in-c-and
c.html.
[14] https://brilliant.org/wiki/shortest-path-algorithms/
[15] https://www.programiz.com/dsa/bellman-ford-algorithm
[16] Investigation of Bellman–Ford Algorithm, Dijkstra's Algorithm for suitability of SPP
Jitendra Bahadur Singh1 , R.C.Tripathi2 Electronics Engineering Dept.,NGBU,
Allahabad (India) 1 Dean Research, NGBU, Allahabad (India) 2
[17] https://www.researchgate.net/publication/310594546_A_Review_and_Evaluations_of
_Shortest_Path_Algorithms
[18] International Journal of Applied Engineering Research ISSN 0973-4562 Volume 13,
Number 9 (2018) pp. 6817-6820 © Research India Publications.
http://www.ripublication.com 6817 A Survey of Shortest-Path Algorithms

[19] First International Conference on Advanced Algorithms and Control Engineering IOP
Publishing IOP Conf. Series: Journal of Physics: Conf. Series 1087 (2018) 022011 doi
:10.1088/1742-6596/1087/2/022011 The Comparison of Three Algorithms in Shortest Path
Issue Xiao Zhu WANG Northeastern University at Qinhuangdao, Computer science and
technology, Hebei 066004, China 2656398251@qq.com

16 | P a g e

You might also like