Data Structures and Algorithms Assignment 3: Minimum Spanning Trees and Shortest Path Algorithms
Data Structures and Algorithms Assignment 3: Minimum Spanning Trees and Shortest Path Algorithms
Data Structures and Algorithms Assignment 3: Minimum Spanning Trees and Shortest Path Algorithms
All the source code (should be saved as. ipynb file). Please include the Google Colab link in the
comments field of dropbox.
1. Objectives
In this lab, you will learn to make use of the graph algorithms learnt in the lectures in real world
problems. The objectives of this lab are as follows:
The Jupyter notebook for this assignment is available in the following Google Colaboratory
(Google Colab) link:
https://colab.research.google.com/drive/1M9HCrgG2wYOiElo9ltw32sxnXmOpb- j_?
usp=sharing
Open the link above and save a copy of the code inside your Google drive account. You will be
able to work on this lab directly inside the Google Colab environment with your copy of the
notebook.
Run the first cell to install the pygraphviz library within the Google Colab environment. The
required files for this assignment will also be downloaded from Github automatically. Note that
as files downloaded into your Colab session and additional libraries installation on Google Colab
are not persistent, you will need to rerun this cell to install the pygraphviz library and download
the required files each time you come back to this notebook.
3. Priority Queue
Priority Queue is a useful data structure that is commonly used in applications such as
scheduling and simulation. It is also used in the Minimum Spanning Tree and Shortest Path
algorithms.
a. Study the API for the Indexed Priority Queue IndexPQ.py class. Run and explain the
output of the following program.
Page | 1
b. Study the API for the Edge.py class. In particular, note the comparison operators “lt”,
“gt”, “ge”, “le” and “eq” implemented in the Edge.py class to allow comparison of two
edges. Run and explain the output of the following program.
Run the code in section 4 of the IPython notebook and check that you have the following
graph diagram generated.
The readGraph() function has been provided for you to read a graph from an external file
“demo.txt”. Both the ID of the node and the edge weight between the nodes are drawn.
Study the readGraph() function and the format of the example graph file “demo.txt” to
understand how an edge weighted graph is specified in an external file, and how the edges
of the graph are added to a edge weighted graph.
Study the drawGraph() function to understand how the edges can be accessed from the
graph to draw the picture of the graph. Also note how the color of the edges in a path
specified by the createPath() function are changed to red in the drawGraph() function.
In this experiment you will learn to make use of a Minimum Spanning Tree library. Study the
Prim’s Minimum Spanning Tree algorithm in section 5 of the IPython notebook.
a. An example for calculating the minimum spanning tree for the graph in the file
“tinyEWG.txt” using the Prim’s algorithm code is provided in the IPython notebook.
Write a program to draw the complete graph for “tinyEWG.txt” (similar to the graph in
section 4) and highlight the edges in the minimum spanning tree in red.
b. Implement the Kruskal algorithm. You can make use of the UnionFind.py code provided.
Using the Kruskal algorithm implemented, generate the minimum spanning tree for
“tinyEWG.txt” and check that the result is the same as part Q5a.
In this experiment you will learn to make use of a Dijkstra Shortest Path algorithm library.
Study the Dijkstra Shortest Path algorithm in the file DijkstraSP.py. An example of how the
class can be used is provided in the comment section at the end of the file.
a. The code to calculate the shortest path from node 5 to node 6 for the graph in the file
“tinyEWG.txt” using the DijkstraSP class is provided in the IPython notebook. Write a
program to draw the complete graph and highlight the edges in the shortest path in red.
b. Write a program to find all vertices within a given distance d of a given vertex in an
edge- weighted graph. For example, in the graph “tinyEWG.txt”, the vertices 1, 4, 7 are
within a distance of 0.36 from the vertex 5.
c. (Optional) Implement the Bellman-Ford algorithm and check that the shortest path from
node 5 to node 6 are the same as that obtained for part Q5a.
A new elevated cable car system is being proposed to connect populated urban areas in
Singapore. The following table indicates the proposed stations, distances, travel time and
construction costs between these population centres.
Create a file “cablecar.txt” and generate the graph as shown below showing distances
between the population centres.
a. Reduce Overall Cable Length
In the first design of the cable car network, suppose the cable car company wants to reduce
the overall length of the cable (which is proportional to the distance between population
centers) used in the entire network such that all population centres would be connected.
Use suitable graph input file and algorithms, find a suitable network and show the sub-graph
(highlighted in red) generated of the resultant connections.
In the second design of the cable car network, suppose the cable car company wants to
reduce the overall construction costs of the entire network such that all population centres
would be connected. Use suitable graph input file and algorithms, find a suitable network
and show the sub-graph (highlighted in red) generated of the resultant connections.
Assuming all the cable car links in the table are constructed, if a passenger would like to
travel from Jurong to Pasir Ris, which graph algorithm can be used to plan this trip such that
the passenger minimizes the travel time? Using the chosen algorithm and suitable input file
to generate the planned route (highlighted in red) from Jurong to Pasir Ris.