0% found this document useful (0 votes)
69 views5 pages

Data Structures and Algorithms Assignment 3: Minimum Spanning Trees and Shortest Path Algorithms

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

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:

1. Understand and use a Priority Queue


2. Visualize graph network
3. Implement and apply Minimum Spanning Tree algorithm in real world problem
4. Implement and apply Shortest Path algorithm in real world problem

2. Installation of PyGraphviz on Google Colaboratory

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.

4. Visualizing an Edge Weighted Graph

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.

5. Minimum Spanning Tree

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.

6. Shortest Path Algorithm

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.

7. Designing the Network for a Cable Car System

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.

Distances Travel times Costs


Node 1: Orchard Node 2: City Hall 5 km 10 min 50 million
Node 1: Orchard Node 3: Ang Mo Kio 10 km 20 min 100 million
Node 2: City Hall Node 4: Marina Bay 3 km 7 min 10 million
Node 2: City Hall Node 5: Bedok 11 km 14 min 200 million
Node 5: Bedok Node 6: Tampines 6 km 7 min 51 million
Node 6: Tampines Node 7: Pasir Ris 4 km 5 min 27 million
Node 6: Tampines Node 3: Ang Mo Kio 15 km 18 min 158 million
Node 3: Ang Mo Kio Node 8: Woodlands 12 km 12 min 46 million
Node 4: Marina Bay Node 9: Dover 18 km 13 min 211 million
Node 9: Dover Node 10: Jurong 9 km 11 min 168 million
Node 9: Dover Node 8: Woodlands 8 km 8 min 88 million

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.

b. Reduce Overall Construction Costs

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.

c. Getting from Point A to Point B Fast

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy