CS102_Lab14_Spring2025
CS102_Lab14_Spring2025
Lab Week 14
1 Introduction 2
1.1 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Marking scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Lab Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Late submission policy . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 Use of AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.6 Viva . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Prerequisites 4
2.1 Pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Lab Exercises 5
3.1 Minimum Priority Queue . . . . . . . . . . . . . . . . . . . . . . . 5
3.1.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1.2 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Shortest Path between two nodes using Dijkstra . . . . . . . . . . 6
3.2.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2.2 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Shortest Path in a Grid . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.3.2 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 Shortest Distance between Cities of Pakistan . . . . . . . . . . . . . 9
3.4.1 Reading CSV files in Python . . . . . . . . . . . . . . . . . . 9
3.4.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.4.3 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1
Introduction
1.1 Instructions
• This lab will contribute 1% towards your final grade.
• The deadline for submission of this lab is at the end of lab time.
• The lab must be submitted online via CANVAS. You are required to submit
a .zip file that contains all the .py files.
• The .zip file should be named as lab14 aa12345.zip where aa12345 will be
replaced with your student id.
• Files that don’t follow the appropriate naming convention will not
be graded.
• Your final grade will comprise of both your submission and your
lab performance.
2
1.5 Use of AI
Taking help from any AI-based tools such as ChatGPT is strictly prohibited and
will be considered plagiarism.
1.6 Viva
Course staff may call any student for Viva to provide an explanation for their
submission.
3
Prerequisites
2.1 Pytest
Pytest is a popular testing framework in Python known for its simplicity and
power in facilitating efficient and scalable testing for applications and software. In
order to use Pytest, one first needs to install it and create test functions prefixed
with test in a Python file. To install Pytest, you can write the following command
on the terminal:
pip install pytest
After installing Pytest, one can execute all the tests by typing the following com-
mand on the terminal:
pytest
If you want to test any particular file, then you will have to specify the filename
as follows:
pytest < filename >. py
In case the above mentioned pytest commands do not work on your system, even
though pytest is installed, try using the following commands:
py -m pytest
However, if all the tests fail, then you should receive a similar output.
4
Lab Exercises
Note: EnQueue and IsEmpty have already been implemented for you.
3.1.1 Example
>>> queue = []
>>> EnQueue ( queue , 'A ' ,1)
>>> EnQueue ( queue , 'B ' ,2)
>>> EnQueue ( queue , 'C ' ,3)
>>> EnQueue ( queue , 'D ' ,4)
>>> EnQueue ( queue , 'E ' ,5)
>>> EnQueue ( queue , 'F ' ,6)
>>> EnQueue ( queue , 'G ' ,7)
>>> print ( queue )
[( 'A ' , 1) , ( 'B ' , 2) , ( 'C ' , 3) , ( 'D ' , 4) , ( 'E ' , 5) , ( 'F ' , 6) ,
( 'G ' , 7) ]
>>> print ( DeQueue ( queue ) )
'A '
>>> print ( queue )
[( 'B ' , 2) , ( 'C ' , 3) , ( 'D ' , 4) , ( 'E ' , 5) , ( 'F ' , 6) , ( 'G ' , 7) ]
3.1.2 Testing
To test your function, type the following command in the terminal:
pytest tests / test_q1 . py
5
3.2 Shortest Path between two nodes using Di-
jkstra
This question should be implemented in the file q2.py.
6
3.2.1 Example
>>> graph = { 'A ': [( 'D ' , 2) , ( 'E ' , 6) , ( 'B ' , 7) ] , 'B ': [( 'C '
, 3) , ( 'A ' , 7) ] , 'C ': [( 'B ' , 3) , ( 'D ' , 2) , ( 'G ' , 2) ] , 'D '
: [( 'A ' , 2) , ( 'C ' , 2) , ( 'F ' , 8) ] , 'E ': [( 'A ' , 6) , ( 'F ' ,
9) ] , 'F ': [( 'D ' , 8) , ( 'E ' , 9) , ( 'G ' , 4) ] , 'G ': [( 'C ' , 2) ,
( 'F ' , 4) ]}
>>> print ( GetShortestPath ( graph , " A " ," G " ) )
[( 'A ' , 'D ' , 2) , ( 'D ' , 'C ' , 2) , ( 'C ' , 'G ' , 2) ]
3.2.2 Testing
To test your function, type the following command in the terminal:
pytest tests / test_q2 . py
7
the shortest path does not exist, then the function returns -1.
Note: In the grid, we can only move left, right, up, and down. No
diagonal movements are allowed. In addition, each node is a tuple of
the form (i,j) representing the grid coordinates and each edge has a
weight of 1.
3.3.1 Example
For example, if we have a grid as follows:
1 1 1
−1 1 1
1 −1 1
then the corresponding graph is as shown in Figure 3.2
3.3.2 Testing
To test your function, type the following command in the terminal:
pytest tests / test_q3 . py
8
3.4 Shortest Distance between Cities of Pakistan
This question should be implemented in the file q4.py.
3.4.2 Example
Islamabad Taxila Murree Nathiagali Abbottabad Mansehra Balakot Kaghan Naran Chilas Bisham Gilgit Hunza Khunjerab Pass Malam Jabba Skardu Muzaffarabad
Islamabad 0 46 49 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Taxila 46 0 -1 -1 74 -1 -1 -1 -1 -1 -1 -1 -1 -1 254 -1 -1
Murree 49 -1 0 36 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 68
Nathiagali -1 -1 36 0 34 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Abbottabad -1 74 -1 34 0 23 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Mansehra -1 -1 -1 -1 23 0 37 -1 -1 -1 113 -1 -1 -1 -1 -1 -1
Balakot -1 -1 -1 -1 -1 37 0 59 -1 -1 136 -1 -1 -1 -1 -1 -1
Kaghan -1 -1 -1 -1 -1 -1 59 0 22 -1 -1 -1 -1 -1 -1 -1 -1
Naran -1 -1 -1 -1 -1 -1 -1 22 0 113 -1 -1 -1 -1 -1 -1 -1
Chilas -1 -1 -1 -1 -1 -1 -1 -1 113 0 199 133 306 -1 -1 -1 -1
Bisham -1 -1 -1 -1 -1 113 136 -1 -1 199 0 -1 -1 -1 61 -1 -1
Gilgit -1 -1 -1 -1 -1 -1 -1 -1 -1 133 -1 0 186 -1 -1 208 -1
Hunza -1 -1 -1 -1 -1 -1 -1 -1 -1 306 -1 186 0 151 -1 381 -1
Khunjerab Pass -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 151 0 -1 -1 -1
Malam Jabba -1 254 -1 -1 -1 -1 -1 -1 -1 -1 61 -1 -1 -1 0 -1 -1
Skardu -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 208 381 -1 -1 0 484
Muzaffarabad -1 -1 68 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 484 0
9
3.4.3 Testing
To test your function, type the following command in the terminal:
pytest tests / test_q4 . py
10