Shortest Path Algorithms
Shortest Path Algorithms
SALAHADDIN
UNIVERSITY
College of Engineering
Software & Informatics
department
Supervisor:
Salar Jamal Abdulhameed
Report by:
Zryan Muhammed Jaafer
Table of Contents
Introduction................................................................................................................................1
Conclusion................................................................................................................................20
References................................................................................................................................21
TABLE OF FIGURES
In this report I will discuss one of the important Algorithms in Graph theory, Shortest Path
Algorithms, And the algorithms are very useful in real life for instance maps ...etc. So in this
report we know what are shortest path algorithms, and How to use them in different cases,
also discussing some popular problems in Shortest path algorithms.
Introduction
Shortest Path Algorithm is finding the shortest way, the minimum cost, the minimum weights
from a source vertex to another vertex, or from a place to another place especially in
Networking it is very useful, There are two main categories Single Source Shortest
Path(SSSP) and All Pairs Shortest Path(APSP), in Single Source Shortest Path we have one
Single Source vertex, in another words start point, and finding shortest path to all the others
vertices, we have three main algorithms in SSSP (BFS, Dijkstra, Bellman Ford) Algorithm.
In All Pairs Shortest Path (APSP) we have one Special algorithm that is known as Floyd
Warshall, in this algorithm we find shortest path for a vertex to all other vertices, and also all
other vertices shortest path to other vertices.[CITATION Ros11 \p 707 \n \l 1033 ]
1. BFS.
2. Dijkstra Algorithm.
3. Bellman Ford Algorithm.
4. SSSP in Directed Acyclic Graph (DAG).
1
1-Single Source Shortest Path
In single source shortest path, we have one source vertex and finding shortest path to all other
vertices, lets discuss algorithms
Algorithm:
2
Example 1:
Find the Shortest Path by Dijkstra Algorithm from vertex 0 to all the others
Figure 1 Figure (SSSP) Dijkstra Ex
3
1.2 Bellman Ford Algorithm
Invented in 1958 by Bellman and Ford independently , Given a graph and a source
vertex source in graph, find shortest paths from source to all vertices in the graph, The
graph may contain negative weight edges.
Bellman-Ford works for such graphs. Bellman-Ford is also simpler than Dijkstra and suites
well for distributed systems. But time complexity of Bellman-Ford is O(VE), which is more
than Dijkstra.
The idea of this algorithm is to go through all the edges of this graph one-by-one in some
random order. It can be any random order. But you must ensure, if u-v (where u and v are
two vertices in a graph) is one of your orders, then there must be an edge from u to v. Usually
it is taken directly from the order of the input given. Again, any random order will work.Does
V-1 iteration + 1 to detect cycles: if cost decreases in the V-th iteration, than there is a
negative cycle, because all the paths are traversen up to the V-1 iteration.[ CITATION Bel19 \l
1033 ]
After selecting the order, we will relax the edges according to the relaxation formula. For a
given edge u-v going from u to v the relaxation formula is:
The algorithm of Bellman Ford is same as Dijkstra but in bellman ford we do V-1 Relaxation
to detect negative cycle and update the vertex to the minimum weights where we find it in
shortest path.
4
decrease the overall cost And a next cycle: decrease the cost again And again ...
Example 2
First step
5
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.
Second step
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).
6
Other steps are shown in the matrix.
The second iteration guarantees to give all shortest paths which are at most 2 edges long.
The algorithm processes all edges 2 more times. The distances are minimized after the
second iteration, so third and fourth iterations don’t update the distances. [ CITATION
Bel20 \l 1033 ]
1) Negative weights are found in various applications of graphs. For example, instead of
paying cost for a path, we may get some advantage if we follow the path.
2) Bellman-Ford works better (better than Dijkstra’s) for distributed systems. Unlike
Dijkstra’s where we need to find the minimum value of all vertices, in Bellman-Ford, edges
are considered one by one.
3) Bellman-Ford does not work with undirected graph with negative edges as it will
declared as negative cycle. [ CITATION Cre05 \l 1033 ]
Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the
shortest paths from given source to all other vertices.
7
By relaxing the edges of a weighted DAG (Directed Acyclic Graph) G = (V, E) according to
a topological sort of its vertices, we can figure out shortest paths from a single source in
∅(V+E) time. Shortest paths are always well described in a DAG, since even if there are
negative-weight edges, no negative-weight cycles can exist.[ CITATION Sin19 \l 1033 ]
Algorithm:
DAG (G, w, s)
4. do RELAX (u, v, w)
Example 3
Figure 4 DAG example
Other steps:
8
2-All Pairs Shortest Path
The all-pairs shortest path algorithm is also known as Floyd-Warshall algorithm is used to
find all pair shortest path problem from a given weighted graph. As a result of this algorithm,
it will generate a matrix, which will represent the minimum distance from any node to all
other nodes in the graph. [ CITATION All18 \l 1033 ]
9
Figure 5 Floyed Warshall Example
So follow the steps in the next pages to find the shortest path to all pairs
1-Create a matrix A0 of dimension n*n where n is the number of vertices. The row and the
column are indexed as i and j respectively. i and j are the vertices of the graph.
Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If there is no
path from ith vertex to jth vertex, the cell is left as infinity.
10
Fill each cell with the distance between ith and jth vertex
2- Now, create a matrix A1 using matrix A0 . The elements in the first column and the first
row are left as they are. The remaining cells are filled in the following way.
Let k be the intermediate vertex in the shortest path from source to destination. In this
step, k is the first vertex. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k]
[j]).
That is, if the direct distance from the source to the destination is greater than the path
through the vertex k, then the cell is filled with A[i][k] + A[k][j].
In this step, k is vertex 1. We calculate the distance from source vertex to destination vertex
through this vertex k.
11
Calculate the distance from the source vertex to destination vertex through this vertex k
For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the sum of the
distance from vertex 2 to 4 through vertex (ie. from vertex 2 to 1 and from vertex 1 to 4) is 7.
Since 4 < 7, A0[2, 4] is filled with 4.
3- Similarly, A2 is created using A1. The elements in the second column and the second row
are left as they are.
In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as in step
2.
Calculate the distance from the source vertex to destination vertex through this vertex 2
12
Calculate the distance from the source vertex to destination vertex through this vertex 3
Calculate the distance from the source vertex to destination vertex through this vertex 4
5- A4 gives the shortest path between each pair of vertices. [ CITATION Flo20 \l 1033 ]
13
Application of Floyd Warshall Algorithm
Ans/ The Floyd-Warshall algorithm is best suited for dense graphs since it is not at all
dependent on the number of edges. Performing Floyd-Warshall on a sparse graph erases its
main benefit.
What is TSP?
The traveling salesman problem consists of a salesman and a set of cities. The salesman has
to visit each one of the cities starting from a certain one returning to the same city. The
challenge of the problem is that the traveling salesman wants to minimize the total length of
the trip.[ CITATION Tra19 \l 1033 ]
The Traveling Salesman Problem:
The Traveling Salesman Problem was first formulated in 1930 by Merrill M. Flood,
who looked to solve a school bus routing problem.
It is one of the most intensively studied computational problems in optimization, used
14
as a benchmark for many other optimization methods.
The Traveling Salesman Problem (TSP) refers to the challenge of determining the
shortest yet most efficient route for a traveling salesman to take to visit a list of
specific destinations.
The goal is to find the shortest route from a set of different routes to minimize the
total distance traveled and the travel cost.
The TSP is classified under combinatorial optimization problems known as “NP-
complete.” It is classified as “NP-hard” because of two reasons:
There are no quick solutions.
The complexity of calculating the most optimum route increases when you add more
destinations to the TSP.
You can solve the TSP by analyzing every round-trip route to determine the shortest
one.
As the number of destinations increases, the corresponding number of routes increases
exponentially. This exponential increase surpasses the computational capabilities of
even the fastest computers.
A set of ten destinations alone can have more than 300 thousand permutations and
combinations of routes. A set of 15 destinations can have more than 87 billion
possible routes.
The Travelling Purchaser Problem (TPP) and the Vehicle Routing Problem (VRP) are
generalizations of the TSP.
The TSP has several applications, including planning and logistics.[ CITATION
Tra14 \l 1033 ]
15
The steps involved in solving the TSP using the Brute-Force Approach include:
o Calculating the total number of routes
o Drawing and listing all possible routes
o Calculating the distance traveled in each route
o Choosing the shortest route, which is the optimal solution.[CITATION Tra191 \l
1033 ]
The Branch-and-Bound method involves breaking the problem into a series of sub-
problems, each of which may have several possible solutions.
The solution selected for one sub-problem may affect the possible solutions of
subsequent sub-problems.
The steps involved in solving the TSP using the Branch-and-Bound Method are as
follows:
o Choose a start node and then set bound to a high value, say infinity.
o Select the cheapest arch between the unvisited and current node and then add the
distance to the current distance.
o Repeat the process until the current distance is less than the bound.
o Then add up the distance so that the bound equals the current distance.
o Repeat this process until all the arcs are covered.
16
4- Recent Solutions by Academics:
o Zero Suffix Method solves the classic symmetric TSP
o Biogeography‐based Optimization Algorithm
o The African Buffalo Optimization Algorithm
o Meta-Heuristic Multi Restart Iterated Local Search (MRSILS)
o Multi-Objective Evolutionary Algorithm solves multiple TSPs based on NSGA-II.
o Multi-Agent System solves the TSP of N cities with fixed resources.
Here I will discuss about The Nearest Neighbor Method which is easy but it is not the best
solution for this problem we also have Naive and Dynamic Programming, Approximate using
MST, and so many other algorithms but in 2010 three person found a new solution for TSP.
17
18
3.3 Nearest Neighbour Method
This procedure gives reasonably good results for the travelling salesman problem. The
method is as follows:
Step1: Select an arbitrary vertex and find the vertex that is nearest to this starting vertex to
form an initial path of one edge.
Step2: Let v denote the latest vertex that was added to the path. Now, among the result of the
vertices that are not in the path, select the closest one to v and add the path, the edge-
connecting v and this vertex. Repeat this step until all the vertices of graph G are included in
the path.
Step3: Join starting vertex and the last vertex added by an edge and form the circuit.
[ CITATION TSP17 \l 1033 ]
19
Solution:
20
Conclusion
Shortest path algorithm has real life use cases as in the report mentioned we have SSSP and
APSP, in SSSP we have to find shortest path from one source vertex to all other vertices, but
in APSP we have to find all pairs shortest path. SSSP has some algorithms for instance BFS
for unweighted graphs, Dijkstra algorithm for positive weighted graphs and it doesn’t work
with negative graphs it may give true answer but not perfect, another SSSP algorithm is
Bellman Ford works with negative and positive weighted graphs and it uses dynamic
programming and it is useful for detecting negative cycles, Bellman-Ford algorithm is used to
find the shortest path from the source vertex to every vertex in a weighted graph. Unlike
Dijkstra's algorithm, the bellman ford algorithm can also find the shortest distance to every
vertex in the weighted graph even with the negative edges. last thing in SSSP that mentioned
in this report is shortest path in directed acyclic graphs (DAG). All pairs shortest path we
have an algorithm which is Floyd Warshall It computes the shortest path between every pair
of vertices of the given graph. Floyd Warshall Algorithm is an example of dynamic
programming approach. The Salesman Problem (TSP) it’s a problem which a postman or a
school bus or the delivery guy has to do his work by the shortest path and coming back to his
place, it’s a famous problem in networking and computer science where it has some
algorithms for solving this problem. Shortest path algorithms can be used to solve word
ladder puzzles. Shortest path problems form the foundation of an entire class of optimization
problems that can be solved by a technique called column generation. Examples include
vehicle routing problem, survivable network design problem, amongst others.
21
References
1. Algorithm, B.–F. (2019). algorithm Tutorial - Bellman–Ford Algorithm. Retrieved
from Sodocumentation.net:
https://sodocumentation.net/algorithm/topic/4791/bellman-ford-algorithm
2. All-Pairs Shortest Paths. (2018). Retrieved from Tutorialspoint.com:
https://www.tutorialspoint.com/all-pairs-shortest-paths
3. Bellman–Ford algorithm - Wikipedia. (2020). Retrieved from En.wikipedia.org:
https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
22