You are on page 1of 6

COLLEGE OF ENGINEERING & TECHNOLOGY

UNIVERSITY OF SARGODHA

CE 416: Data Structure and Algorithms (Lab)

Lab 12 Manual
Shortest Path Selection in Graphs

Instructor & Demonstrator: Engr. Nauman Ahmad Tariq

Student Name OBAID UR REHMAN

Roll No. ELEN51F20R010

Date Performed

Marks obtained

Instructor Signature
CLO-1 Illustrate understanding of key concepts of Data structures
and Algorithms using Python PyCharm Platform
CLO-2 Design a programming application by applying concepts of
Data Structures and algorithms leading to the solution of a moderate
scale-programming problem.
CLO-3 Write lab notes, effective communication and the analysis of
the given problem to perform in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or
individually with respect to the contribution.
OBJECTIVES:
The objective of this experiment is to design, develop and implement a Python code that implements Floyd-
Warshall algorithm used for finding the shortest path between all the pairs of vertices in a weighted graph.

ABOUT THE EXPERIMENT:


Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of vertices in a
weighted graph. This algorithm works for both the directed and undirected weighted graphs. But, it does not
work for the graphs with negative cycles (where the sum of the edges in a cycle is negative).

A weighted graph is a graph in which each edge has a numerical value associated with it.

How Floyd-Warshall Algorithm Works:


Let the given graph be:

Follow the steps below to find the shortest path between all the pairs of vertices.

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 i th vertex to the jth vertex. If there is no path from
ith vertex to jth vertex, the cell is left as infinity.
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.

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 (i.e. from vertex 2 to 1 and from vertex 1 to 4) is 7. Since 4 < 7, A 0[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.
4. Similarly, A3 and A4 is also created.

5. A4 gives the shortest path between each pair of vertices.

ALGORITHM:
n = no of vertices
A = matrix of dimension n*n
for k = 1 to n
for i = 1 to n
for j = 1 to n
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
return A
Python Program Code:
import time
# The number of vertices
nV = 4
INF = 999
# Algorithm implementation
def floyd_warshall(G):
distance = [list(row) for row in 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=" ")
print(" ")

if __name__ == "__main__":
G = [[0, 3, INF, 5],
[2, 0, INF, 4],
[INF, 1, 0, INF],
[INF, INF, 2, 0]]

start_time = time.time()
floyd_warshall(G)
end_time = time.time()

execution_time = end_time - start_time


print(f"Execution Time: {execution_time:.6f} seconds")

OUTPUT:
0 3 7 5
2 0 6 4
3 1 0 5
5 3 2 0
Execution Time: 0.000966 seconds

Process finished with exit code 0


Assessment Rubric for Lab 12
Method of Evaluation: Lab report.

Outcomes Assessed:

CLO-1 Illustrate understanding of key concepts of Data structures and Algorithms using
Python PyCharm Platform.
CLO-2 Design a programming application by applying concepts of Data Structures and
algorithms leading to the solution of a moderate scale-programming problem.
CLO-3 Write lab notes, effective communication and analysis of the given problem to perform
in the laboratory environment.
CLO-4 Demonstrate involvement in the Project as a team or individually with respect to the
contribution.

Performance 5 Excellent 4 Good 3 Satisfactory 2-1 Needs Marks


Improvement
Realization of Fully understand & Close to fully Partially Unable to
Experiment able to illustrate understand & able understands & able understand & able
CLO-1 Warshall to illustrate to illustrate to illustrate
Algorithm. Warshall Warshall Warshall
Algorithm. Algorithm. Algorithm.
Conducting Completely able to Close to Partially able to Unable to
Experiment successfully completely able to successfully successfully
CLO-2 design and compile successfully design and compile design and compile
Python programs design and compile Python programs Python programs
for Warshall Python programs for Warshall for Warshall
Algorithm. for Warshall Algorithm. Algorithm.
Algorithm.
Data Collection Completely Close to Partially Unable to
and Data Analysis understand & able completely understand & able understand & able
CLO-3 to demonstrate understand & able to demonstrate to demonstrate
syntax of Warshall to demonstrate syntax of Warshall syntax of Warshall
Algorithm. syntax of Warshall Algorithm. Algorithm.
Algorithm.

Individual/Team Completely able to Close to Partially able to Unable to execute


Work execute different completely able to execute different different programs
CLO-4 programs for execute different programs for for Warshall
Warshall programs for Warshall Algorithm.
Algorithm. Warshall Algorithm.
Algorithm.

Total

You might also like