You are on page 1of 25

November 2021

SALAHADDIN
UNIVERSITY

College of Engineering
Software & Informatics
department

Shortest Path Algorithms

Supervisor:
Salar Jamal Abdulhameed

Report by:
Zryan Muhammed Jaafer
Table of Contents
Introduction ................................................................................................................................ 1

1-Single Source Shortest Path .................................................................................................... 2


1.1 Dijkstra Algorithm ........................................................................................................... 2
1.2 Bellman Ford Algorithm .................................................................................................. 4
1.2.1 Negative Cycle .......................................................................................................... 4

2-All Pairs Shortest Path ............................................................................................................ 9


2.1 Floyd Warshall Algorithm ............................................................................................... 9
Floyed Warshal Complexity ............................................................................................. 13
Application of Floyd Warshall Algorithm........................................................................ 13

3-Traveling Salesman Problem (TSP) ..................................................................................... 13


3.1 Popular Solutions for traveling salesman problem (TSP) .............................................. 15
3.2 Computer Scientists Break Traveling Salesperson Record ............................................ 16
3.3 Nearest Neighbour Method ............................................................................................ 18

Conclusion ............................................................................................................................... 20

References ................................................................................................................................ 21
TABLE OF FIGURES

FIGURE 1 FIGURE (SSSP) DIJKSTRA EX ...................................................................................... 3

FIGURE 2 NEGATIVE CYCLE ........................................................................................................ 5

FIGURE 3 BELLMAN FORD EXAMPLE .......................................................................................... 5

FIGURE 4 DAG EXAMPLE ............................................................................................................ 8

FIGURE 5 FLOYED WARSHALL EXAMPLE .................................................................................... 9

FIGURE 6 NEAREST NEIGHBOUR METHOD ................................................................................ 18


Abstract

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. (Discrete Mathematics and its Applications, 2011,
p. 707)

Single Source Shortest Path (SSSP):

1. BFS.
2. Dijkstra Algorithm.
3. Bellman Ford Algorithm.
4. SSSP in Directed Acyclic Graph (DAG).

All Pairs Shortest Path (ASAP):


1. Floyd Warshal.

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

1.1 Dijkstra Algorithm


It was constructed by computer scientist Edsger Dijkstra in 1956, It is a greedy algorithm that
solves the single-source shortest path problem for a directed graph G = (V, E) with nonnegative
edge weights, i.e., w (u, v) ≥ 0 for each edge (u, v) ∈ E. (GeeksforGeeks, 2021)
Dijkstra’s algorithm is a Greedy algorithm and time complexity is O(V+E LogV).

Algorithm:

1. The very first step is to mark all vertices as unvisited.


2. Mark the picked starting vertex with a current distance of 0 and the rest vertices with
infinity.
3. Now, fix the starting vertex as the current vertex.
4. For the current vertex, analyse all of its unvisited adjacent and measure their distances
by adding the current distance of the current vertex to the weight of the edge that
connects the adjacent vertex and current vertex.
5. Compare the recently measured distance with the current distance assigned to the
adjacent vertex and make it as the new current distance of the adjacent vertex.
6. After that, consider all of the unvisited adjacent of the current vertex, mark the current
vertex as visited.
7. If the destination vertex has been marked visited then stop, an algorithm has ended, and
8. Else, choose the unvisited node that is marked with the least distance, fix it as the new
current node, and repeat the process again from step 4. (Tyagi, 2019)

2
Example 1:
Find the Shortest Path by Dijkstra Algorithm from vertex 0 to all the others
Figure 1 Figure (SSSP) Dijkstra Ex

First Step: Second step:

Third step: Forth step:

And the final answer is:

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. (Algorithm, 2019)

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:

If d[u]+w(u,v)<d[v] then d[v]=d[u]+w(u,v) else d[v]=d[v]

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.

1.2.1 Negative Cycle


What is the problem?
If we would like to find a path with the minimum cost we have to go A → B → C → A to
decrease the overall cost And a next cycle: decrease the cost again And again ...

4
Figure 2 Negative Cycle

it will be clearer if you see an example

Example 2

Figure 3 Bellman Ford Example

First step

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.

5
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).

Other steps are shown in the matrix.

6
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. (Bellman–Ford algorithm
- Wikipedia, 2020)

Some Important Notes:

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. (BY-NC-SA, 2005)

1.3 Shortest Path in Directed Acyclic Graph (DAG)

Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the shortest
paths from given source to all other vertices.
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. (Single Source Shortest Path in a directed
Acyclic Graphs - javatpoint, 2019)
Algorithm:

DAG (G, w, s)

1. Topologically sort the vertices of G.

2. for each vertex u taken in topologically sorted order

3. do for each vertex v ∈ Adj [u]

4. do RELAX (u, v, w)

7
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. (All-Pairs Shortest Paths, 2018)

2.1 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. (Floyd-Warshall Algorithm | Brilliant Math & Science Wiki, 2019)

Lets discuss about the algorithm in an example to be more clear.


Example 4
Figure 5 Floyed Warshall Example

Lets the given graph be

So follow the steps in the next pages to find the shortest path to all pairs

9
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.

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.

10
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

11
4- Similarly, A3 and A4 is also created.

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. (Floyd-Warshall Algorithm, 2020)

12
Floyed Warshal Complexity

Time Complexity O(n3)


Space Complexity O(n2)

Application of Floyd Warshall Algorithm

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


• It helps in testing whether the undirected graph is bipartite
• It helps to find the shortest path in a directed graph
• Different versions of the Floyd Warshall algorithm help to find the transitive closure of
a directed graph
• This algorithm helps to find the regular expression the are accepted by finite automata.
• It helps in finding the similarity between the graphs
• Floyd Warshall algorithm helps in finding the optimal routing i.e the maximum flow
between two vertices (Floyd Warshall Algorithm (Python) | Dynamic Programming |
FavTutor, 2017)

Q/ Is the Floyd-Warshall algorithm better for sparse graphs or dense graphs?

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.

3-Traveling Salesman Problem (TSP)

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.
(Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming) - GeeksforGeeks,
2019)

13
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
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. (Traveling
Salesman Problem, 2014)

14
3.1 Popular Solutions for traveling salesman problem (TSP)

1-The Brute-Force/Naive Approach:

• The Brute-Force Approach calculates and compares all possible permutations of


routes to find the shortest unique solution.
• 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. (TSP, 2019)

2-The Branch-and-Bound Method:

• 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.

3-The Nearest Neighbor Method:

• This method is perhaps the simplest method to solve the TSP


• This method involves always visiting the nearest destination and then going back to
the first destination when all other destinations are visited.
• The steps involved in solving the TSP using the Nearest Neighbor method are as
follows:

15
o Choose a random destination
o Look for the nearest unvisited destination and go there.
o Once all destinations are visited, the salesman must return to the first destination.

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.

3.2 Computer Scientists Break Traveling Salesperson Record


In 2010, Oveis Gharan, Saberi and Mohit Singh of the Georgia Institute of Technology started
wondering if it might be possible to improve on Christofides’ algorithm by choosing not the
shortest tree connecting all the cities, but a random tree from a carefully chosen collection.
They took inspiration from an alternate version of the traveling salesperson problem in which
you are allowed to travel along a combination of paths — maybe you get to Denver via 3/4 of
the route from Chicago to Denver plus 1/4 of the route from Los Angeles to Denver.
Unlike the regular traveling salesperson problem, this fractional problem can be solved
efficiently. And while fractional routes don’t make physical sense, computer scientists have
long believed that the best fractional route should be a rough guide to the contours of good
ordinary routes.
So to create their algorithm, Oveis Gharan, Saberi and Singh defined a random process that
picks a tree connecting all the cities, so that the probability that a given edge is in the tree
equals that edge’s fraction in the best fractional route. There are many such random
processes, so the researchers chose one that tends to produce trees with many evenly
connected cities. After this random process spits out a specific tree, their algorithm plugs it
into Christofides’ scheme for matching cities with odd numbers of edges, to convert it into a
round trip. (Klarreich, 2020)

16
17
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.
(TSP(Nearest), 2017)

Figure 6 Nearest Neighbour Method


Example 5

18
Solution:

1- Select edge V1-V3 the Weight is 4


2- Select edge V3-V2 the Weight is 2
3- Select edge V2-V4 the Weight is 2
4- Select edge V4-V5 the Weight is 2
5- Select edge V5-V1 the weight is 9

So The total distance is 18.

19
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.

20
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

4. BY-NC-SA, C. C. (2005). Introduction to Algorithms (SMA 5503). Retrieved from


MIT OpenCourseWare: https://www.youtube.com/watch?v=Ttezuzs39nk

5. Floyd Warshall Algorithm (Python) | Dynamic Programming | FavTutor. (2017).


Retrieved from FavTutor: https://favtutor.com/blogs/floyd-warshall-algorithm
6. Floyd-Warshall Algorithm | Brilliant Math & Science Wiki. (2019). Retrieved from
Brilliant.org: https://brilliant.org/wiki/floyd-warshall-algorithm/
7. Floyd-Warshall Algorithm. (2020). Retrieved from Programiz.com:
https://www.programiz.com/dsa/floyd-warshall-algorithm

8. GeeksforGeeks. (2021). Dijkstra Algorithm. Retrieved from geeksforgeeks.org:


https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/
9. Klarreich, E. (2020). Computer Scientists Break Traveling Salesperson Record.
Retrieved from quantamagazine.org: https://www.quantamagazine.org/computer-
scientists-break-traveling-salesperson-record-20201008/

10. Rosen, K. H. (2011). Discrete Mathematics and its Applications.


11. Single Source Shortest Path in a directed Acyclic Graphs - javatpoint. (2019).
Retrieved from www.javatpoint.com: https://www.javatpoint.com/single-source-
shortest-path-in-a-directed-acyclic-graphs
12. Traveling Salesman Problem. (2014). Retrieved from Fleetroot.com:
https://fleetroot.com/blog/what-is-the-traveling-salesman-problem/
13. Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming) -
GeeksforGeeks. (2019). Retrieved from GeeksforGeeks:
https://www.geeksforgeeks.org/travelling-salesman-problem-set-1/

14. TSP. (2019). Retrieved from https://fleetroot.com/: https://fleetroot.com/blog/what-is-


the-traveling-salesman-problem/
15. TSP(Nearest). (2017). Retrieved from javatpoint:
https://www.javatpoint.com/discrete-mathematics-travelling-salesman-problem
16. Tyagi, N. (2019). What is Dijkstra’s Algorithm? Examples and Applications of
Dijkstra's Algorithm. Retrieved from Analyticssteps.com:
https://www.analyticssteps.com/blogs/dijkstras-algorithm-shortest-path-algorithm

21

You might also like