This assignment implements Prim’s algorithm using a custom heap-based priority queue to compute the Minimum Spanning Tree (MST) of a weighted undirected graph. The implementation meets the required time complexity of O((|V| + |E|) log |V|).
- Implement a heap-based priority queue with support for:
heap(keys, n)in_heap(id)is_empty()min_key(),min_id()delete_min()decrease_key(id, new_key)
- Use this heap in Prim's algorithm starting from vertex 1
- Parse an input graph file and output:
- Adjacency list
- Edges in the MST
- Total MST weight
| File Name | Description |
|---|---|
asn3.py |
Python program implementing Prim’s algorithm with heap |
asn3.sh |
Shell script to run the program using asn3.py |
asn3_solution.pdf |
Written answers for questions 1 to 6 of the assignment |
infile |
Input graph file used to test MST algorithm |
asn3.pdf |
Assignment instructions (for context/reference) |
The infile contains:
- First line: Integer
nrepresenting number of vertices - Each following line:
i j wdescribing an edge between verticesiandjwith weightw
The program outputs:
- Adjacency list for the input graph
- Edges in the MST in the format
(i, j), weight: w, whereiis the parent ofj - Total weight of the MST
On any Unix environment:
./asn3 < infilepython3 asn3.py < infileAdjacency List:
(1, 2), weight: 28
(1, 3), weight: 15
...
Minimum Spanning Tree:
(1, 2), weight: 28
(2, 4), weight: 10
...
Weight of the Minimum Spanning Tree: 131
For academic use only — University of Western Ontario, CS 3340b: Analysis of Algorithms