You are on page 1of 6

University M'hamed Bougara, Boumerdes.

Institute of Electrical and Electronics


Engineering.

Data Structure and Algorithms.


EE-424: Lab.

Experiment 08:
Implementation of Dijkstra’s and Floyd Warshall’s
Algorithms.

Done by:
OULD AMARA Massil.
BOT Mohammed.

Option: Control. Supervisor:


Subgroup: 01. Mrs.Cherifi.

2017/2018
1. Introduction:
In Euclidean geometry, the shortest path between two points is a straight line. What if
we don’t have a straight line connecting two points? What if we don’t care about distance,
but cost? Thus the shortest path problem is introduced. This is as straightforward as it gets.
We will present two algorithms here: Dijkstra’s Algorithm and the Floyd-Warshall Algorithm.

2. Objectives:
The objective of this laboratory assignment is to implement Dijkstra’s and Floyd
Warshall’s algorithms for a weighted digraph.

3. Theoretical Part:
3.1. Dijkstra’s Algorithm:
Dijkstra’s algorithm finds a shortest path tree from a single source node, by building a
set of nodes that have minimum distance from the source. Here's a description of the
algorithm:
1. Mark your selected initial node with a current distance of 0 and the rest with infinity.
2. Set the non-visited node with the smallest current distance.
3. For each neighbour N of your current: add the current distance with the weight of
the edge connecting the current node to N. If it's smaller than the current distance
of N, set it as the new current distance of N.
4. Mark the current node as visited.
5. If there are non-visited nodes, go to step 2.

3.2. Floyd Warshall’s Algorithm:


The Floyd–Warshall algorithm is a simple and widely used algorithm to compute
shortest paths between all pairs of vertices in an edge weighted directed graph. In order to
find all shortest paths simultaneously, the algorithm needs to save a matrix that contains the
current cost for all pairs of nodes. Row and column indices of this matrix represent the nodes
and each entry contains the corresponding current cost.
Assume the graph is specified by its weight matrix W. Then the matrix entry W[i,j] is
the weight of the edge (i,j), if this edge exists. If not edge from i to j exists then W[i,j] will be
infinity.

The Floyd-Warshall algorithm uses the concept of dynamic programming. First of all, the
algorithm is being initialized:
 Initialize the matrix D of shortest distances with the same entries as the weight
matrix W.
 The algorithm executes the main loop with k ranging from 1 to n. In each iteration of
this loop the algorithm tries to improve all (i,j)paths by the paths (i, k) and (k, j).
 To do so consider the distances between all pairs of nodes (i,j) in each iteration. The
algorithm checks whether (i,k) concatenated with (k,j) is shorter than the current
distance of (i,j)
 If the combined distances between i, k and k, j are in fact shorter than the current
distance, then the distance between i and j will be updated.

4. Programing Part:
In this part, we are asked to write a C++ program that accepts as an input a weighted
digraph then it gives a choice to the user to choose either Dijkstra or Floyd Warshall algorithm
to compute the shortest path he/she wants to calculate.

4.1. Dijkstra’s C++ Program:


4.2. Floyd Warshall’s C++ Program :
5. Execution & Results:
We are going to work by the weighted digraph given below:

5.1. Choosing Dijkstra’s Algorithm:


 We choose 1 as the source node.

 Dijkstra’s algorithm gives only the shortest paths from the source node to the other
vertices.
5.2. Choosing Floyd Warshall’s Algorithm:

 Floyd Warshall’s algorithm gives all shortest paths between all pairs of vertices.

6. Analysis:
So which algorithm do we use? That depends entirely on the problem description. If
we have enough memory and time, Floyd-Warshall is clearly better because it takes much less
time to code. But if we don’t want every possible path, Floyd-Warshall may waste time by
calculating too many unwanted shortest paths. In that case, we can go with Dijkstra.

7. Conclusion:
At the end of this laboratory assignment, we were able to implement a Dijkstra and
Floyd Warshall algorithms. To conclude, both Floyd’s and Dijkstra’s algorithm may be used for
finding the shortest path between vertices. The biggest difference is that Floyd’s algorithm
finds the shortest path between all vertices and Dijkstra’s algorithm finds the shortest path
between a single vertex and all other vertices. The space overhead for Dijkstra’s algorithm is
considerably more than that for Floyd’s algorithm. In addition, Floyd’s algorithm is much
easier to implement.

You might also like