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

IEEE PAPER

FINDING THE SHORTEST POSSIBLE DISTANCE TRAVELLED BY


AEROPLANE USING DIFFERENT ALGORITHMS
Sriram.Usha sree
#B.Tech – Computer Science and Engineering(SCOPE)
Vellore Institute of Technology,Vellore – 632014,India
sriramusha.sree2018@vitstudent.ac.in

ABSTRACT:
With the increase in the number of algorithms to find the shortest path for
airplanes between cities such as Dijkstra, Bellman-Ford, A* search, Floyd
Warshall, Johnson, Viterbi algorithm and many more, we are in a problem of
finding the most efficient algorithm to calculate the shortest. We can by no
means clearly rule a single best algorithm, but we can try to identify which
algorithm works best under certain conditions. Bellman-Ford algorithm
generate the minimum distance from a source vertex to all other vertices.
Floyd Warshall algorithm generates matrix which will represent the minimum
distance from any node to other nodes. Johnson’s algorithm works by using the
Bellman–Ford algorithm to compute a transformation of the input graph that
removes all negative weights, allowing Dijkstra's algorithm to be used on the
transformed graph. All of these algorithms can also handle negative weights.
Also, these algorithms can’t handle negative cycles as the loop will run
forever. This project compares a few of these shortest path algorithms such as
Bellman-Ford, Floyd-Warshall and Johnson’s algorithm to find out which
algorithm is the most applicable with respect to number of nodes and runtime.
We are going to discuss the algorithms of all three algorithms and how the
number of nodes affect the time. Further, this project also discusses the
runtime of all the three different algorithms. By comparing these algorithms,
we can find out which algorithm is best for a required number of nodes and the
efficiency of that algorithm with regards to time and memory. Thus, we will be
able to deploy the suitable algorithm with respect to the number of number and
get results in the minimum time and memory space used.

KEYWORDS - Dijkstra, Bellman-Ford, A* search, Floyd Warshall,


Johnson, Viterbi algorithms

INTRODUCTION:
In the present generation, traveling through air from one place to another is
becoming a cost effective mode of transport since more the distance you travel
the more money you have to pay. In some scenarios, we have to travel to
another place first from where we have to catch a flight, to reach our desired
destination which means spending a lot of money. Most people being unaware
about which path (flight) takes shortest distance waste their money. Also
during the times of emergencies (floods, tsunamis, earthquakes) helicopters
and aircrafts have to travel from one place to another to help people and also
carry them to a safe place. Here traveling through the shortest path is the most
important thing which saves a lot of time. Now a days, the shortest path
difficulties can also be found in Road, Railway transport due to which most
people are taking the longest paths and wasting their time and money.

OBJECTIVE:
The main objective of this paper is based on comparison of working of the
different shortest path algorithms and their influence on the modern transport
users. There are different algorithms which could be used to find the shortest
path out of them the most commonly known are Dijkstra’s algorithm, Bellman
ford algorithm, Floyd warshall algorithm and Johnson’s algorithm.

EXISTING ALGORITHMS:
Dijkstra’s algorithm:
Dijkstra's algorithm is an algorithm for finding the shortest paths between
nodes in a graph, which may represent, for example, road networks. It was
conceived by computer scientist Edsger W. Dijkstra in 1956 and published
three years later. For a given source node in the graph, the algorithm finds the
shortest path between that node and every other. It can also be used for finding
the shortest paths from a single node to a single destination node by stopping
the algorithm once the shortest path to the destination node has been
determined. The Dijkstra algorithm uses labels that are positive integer or real
numbers, which have the strict weak ordering defined. Interestingly, Dijkstra
can be generalized to use labels defined in any way, provided they have the
strict partial order defined, and provided the subsequent labels are
monotonically non-decreasing. Dijkstra's original algorithm does not use a
min- priority queue and runs in time O(|V2|). In some fields, artificial
intelligence in particular, Dijkstra's algorithm or a variant of it is known as
uniform cost search and formulated as an instance of the more general idea of
best-first search.
Bellman ford algorithm:
This algorithm was first proposed by Alfonso Shimbel (1955), but is instead
named after Richard Bellman and Lester Ford. Jr, who published it in 1958 and
1956, respectively. The Bellman–Ford algorithm is an algorithm that computes
shortest paths from a single source vertex to all of the other vertices in a
weighted digraph. Even though it is slower than Dijkstra's algorithm it is
more versatile, as it is capable of handling graphs in which some of the edge
weights are negative numbers. Going around the negative cycle an infinite
number of times would continue to decrease the cost of the path (even though
the path length is increasing). Because of this, Bellman-Ford can also detect
negative cycles which is a useful feature. Relaxation is also the most important
step in Bellman-Ford. It is what increases the accuracy of the distance to any
given vertex. Relaxation works by continuously shortening the calculated
distance between vertices comparing that distance with other known distances.
Negative edge weights are found in various applications of graphs. If a graph
contains a "negative cycle" (i.e. a cycle whose edges sum to a negative value)
that is reachable from the source, then there is no cheapest path: any path that
has a point on the negative cycle can be made cheaper by one more walk
around the negative cycle. In such a case, the Bellman–Ford algorithm can
detect negative cycles and report their existence. Bellman–Ford runs in
O(|V|.|E|) time, where |V| and |E| are the number of vertices and edges
respectively.

Floyd-Warshall algorithm:
The Floyd–Warshall algorithm is an example of dynamic programming, and
was published in its currently recognized form by Robert Floyd in 1962.
The Floyd–Warshall algorithm is an algorithm for finding shortest paths in a
weighted graph with positive or negative edge weights (but with no negative
cycles).A single execution of the algorithm will find the lengths (summed
weights) of shortest paths between all pairs of vertices. Although it does not
return details of the paths themselves, it is possible to reconstruct the paths
with simple modifications to the algorithm. Floyd-Warshall is extremely useful
in networking, similar to solutions to the shortest path problem. However, it is
more effective at managing multiple stops on the route because it can calculate
the shortest paths between all relevant nodes. In fact, one run of Floyd-
Warshall can give you all the information you need to know about a static
network to optimize most types of paths. It is also useful in computing
matrix inversions
Johnson’s Algorithm:
Johnson's algorithm was first named after Donald B. Johnson, who published
the technique in 1977.
Johnson’s algorithm is a used to find the shortest path between all pairs of
vertices in a sparse, edge-weighted, directed graph. It allows some of the edge
weights to be negative numbers, but no negative-weight cycles may exist. The
reason that Johnson's algorithm is better for sparse graphs is that its time
complexity depends on the number of edges in the graph. Johnson's algorithm
runs in O(|V2|.log|V|+|V|.|E|) time. Johnson's algorithm is interesting since it
uses two other shortest path algorithms as subroutines. It uses ‘Bellman- Ford’
in order to reweight the input graph to eliminate negative edges and detect
negative cycles. With this new, altered graph, it then uses ‘Dijkstra's’ shortest
path algorithm to calculate the shortest path between all pairs of vertices. The
output of the algorithm is then the set of shortest paths in the original graph.

ADVANTAGES :
Time is the most important thing to everyone in present day, also time is being
treated equal to money so people are more interested in saving time and also
travel by air has become more expensive the more the distance the more the
more money we pay. This point became the main motivation for me to write
this paper, also we have seen a lot of people wasting their money and time by
traveling a lot and also during the times of emergencies where even a second
counts people get stuck up which way to travel. Since data structure had made
good progress in the fields of logic implementation and mapping, our team was
interested in applying what was taught in class to be applied in real life
situation. From this project we want to present the different algorithms which
could be used to find the shortest path which could be socially useful.

PROPOSED WORK:
Since the main aim of the paper is to find shortest path travelled by aeroplane
using different algorithms, so we were working on the best algorithms in the
present scenario to check for their efficiency and comparing them one another.
The algorithms used here are: Bellman ford algorithm, Floyd–Warshall’s
algorithm, Johnson’s algorithm and dijkstra’s algorithm (for reference).I
implemented my work by providing inputs for the algorithms i.e. initializing
the vertices and edges with of a graph with some values and checking the
outputs there by comparing which algorithm is taking the shortest path in
reaching the destination from the source and also comparing their space and
time complexities. For comparison of algorithms we have taken Dijkstra’s
algorithm as a reference.

EXPERIMENTATION:
INPUT:

Source Destination Weight


1 3 -2
3 4 2
4 2 -1
2 1 4
2 3 3

Bellman ford algorithm:

Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
struct Edge
{
int source,destination,weight;
};
struct Graph
{
int V,E;
struct Edge*edge;
};

struct Graph*createGraph(int V,int E)

struct Graph*graph=(struct Graph*) malloc(sizeof(struct Graph));

graph->V=V;

graph->E=E;

graph->edge=(struct Edge*)malloc(graph->E*sizeof(struct Edge));

return graph;

void FinalSolution(int dist[],int n)

printf("\nVertex:\t\tDistance from Source Vertex:\n");

int i;

for (i=1;i<n+1;i++){

printf("%d \t\t %d\n",i,dist[i]);

}
}

void BellmanFord(struct Graph*graph,int source)

int V=graph->V;

int E=graph->E;

int StoreDistance[V];

int i,j;

for (i=0;i<V;i++)

StoreDistance[i]=INT_MAX;

StoreDistance[source]=0;

for (i=1;i<= V-1;i++)

for (j=0;j<E;j++)

int u=graph->edge[j].source;

int v=graph->edge[j].destination;

int weight=graph->edge[j].weight;

if (StoreDistance[u]+weight<StoreDistance[v])

StoreDistance[v]=StoreDistance[u]+weight;

for (i=0;i<E;i++)

int u=graph->edge[i].source;
int v=graph->edge[i].destination;

int weight=graph->edge[i].weight;

if (StoreDistance[u]+weight<StoreDistance[v])

printf("This graph contains negative edge cycle\n");

FinalSolution(StoreDistance,V);

return;

int main()

int V,E,S;

printf("Enter number of vertices in graph:\n");

scanf("%d",&V);

printf("Enter number of edges in graph:\n");

scanf("%d",&E);

printf("Enter your source vertex number:\n");

scanf("%d",&S);

struct Graph*graph=createGraph(V,E);

int i;

for(i=0;i<E;i++){

printf("\nEnter the details of edge %d:Source, destination, weight:\n",i+1);

scanf("%d",&graph->edge[i].source);

scanf("%d",&graph->edge[i].destination);

scanf("%d",&graph->edge[i].weight);
}

BellmanFord(graph,S);

return 0;

}
Output:

Floyd-Warshall algorithm:

Code:

#include<stdio.h>
int main()
{
int a,b,d,e,f,i,j,k;
printf("Enter the number of vertices and edges: ");
scanf("%d %d",&a,&b);
int c[a][a];
for(i=0;i<a;i++){
for(j=0;j<a;j++)
{
c[i][j]=999;
}
}
printf("Enter the source vertex, destination vertex and its weight:\n");
for(i=0;i<b;i++){
scanf("%d %d %d",&d,&e,&f);
c[d-1][e-1]=f;
}
for(k=0;k<a;k++)
{
for(i=0;i<a;i++)
{
for(j=0;j<a;j++)
{
if(c[i][j]>(c[i][k]+c[k][j]))
{
c[i][j]=(c[i][k]+c[k][j]);
}
}
}
}
printf("\n");
for(i=0;i<a;i++)
{
for(j=0;j<a;j++)
{
if(i!=j)
{
printf("%d %d %d\n",i+1,j+1,c[i][j]);
}
}
printf("\n");
}
return 0;
}
Output:

Johnson’s algorithm:

Code:
#include<iostream>
#include<conio.h>

using namespace std;

int min(int a, int b);


int cost[10][10], a[10][10], i, j, k, c;

int min(int a, int b)


{
if (a < b)
return a;
else
return b;
}

int main(int argc, char **argv)


{
int n, m;
cout << "Enter no of vertices";
cin >> n;
cout << "Enter no of edges";
cin >> m;
cout << "Enter the\nEDGE
Cost\n"; for (k = 1; k <= m; k++)
{
cin >> i >> j >> c;
a[i][j] = cost[i][j] =
c;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= n;
j++)
{
if (a[i][j] == 0 && i != j)
a[i][j] = 31999;
}
for (k = 1; k <= n;
k++) for (i = 1; i <=
n; i++)
for (j = 1; j <= n; j++)
a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
cout << "Resultant adj matrix\n";
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (a[i][j] != 31999)
cout <<
i<<"\t"<<j<<"\t"<<a[i][j]<<endl; } cout <<
"\n";
}
return 0;

Output:
Conclusion:

From the above outputs received from the execution of the codes with same inputs we can
conclude that:

 Bellman ford algorithm is generating less time complexity in comparison to other


algorithms.
 Floyd-warshall algorithm is also generating time complexity less than Johnson’s but
greater than bellman ford zz
 Johnson’s algorithm is generating the most time complexity in comparison to other
algorithms.
Shortest path algorithms Time complexities
Bellman ford algorithm O(|V|.|E|)
Floyd-warshall algorithm O(|V3|)
Johnson’s algoritm O(|V2|.log|V|+|V|.|E|)
 Bellman–Ford algorithm can detect the presence of negative cycles in the graph and
report their existence.
 In johnson’s algorithm if negative cycles are present first they are converted to
positive edges before executing the program. This is the main reason it is used over
dijkstra’s algorithm.
 Floyd-warshall allows the edge weights to be negative numbers, but no negative-

ACKNOWLEDGMENT:
I wish to express my sincere gratitude towards Prof. Seetha R under
whose expert guidance we were able to accomplish our goal. A special
note of thanks to our family and my friends for their keen interest and
encouragement at various stages.

References:

 https://www.researchgate.net/figure/Chart-with-the-results-for-the-first-algorithms-
for-the-first-table-in-seconds-and_fig1_304665934
 http://web.eecs.utk.edu/courses/spring2016/cosc494/notes/Floyd/index.html
 https://www.ijedr.org/papers/IJEDR1801130.pdf
 https://brilliant.org/wiki/shortest-path-algorithms/#comparison-of-algorithms
 https://www.researchgate.net/figure/Run-times-on-the-scale-free-benchmarks-for-
graphs-of-induced-widths-38-to-58-and-varying_fig6_259804004
 https://www.sanfoundry.com/
 https://gist.github.com/
 https://gist.github.com/ashleyholman/6793360
 https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
 https://www.thecrazyprogrammer.com/2017/06/bellman-ford-algorithm-in-c-and-
c.html
 https://brilliant.org/wiki/shortest-path-algorithms/
 https://www.programiz.com/dsa/bellman-ford-algorithm

VIDEO LINK:
https://youtu.be/8o2eNq0t3wQ


You might also like