2026-03-05
By the end of this lab session, you should be able to:
This lab consists of three main activities:
A Minimum Spanning Tree (MST) of a connected, undirected, weighted graph is a spanning tree with the minimum possible total edge weight.
Key properties:
Minimum Spanning Trees have numerous real-world applications:
Given the weighted graph below, determine the order in which edges are selected when applying Kruskal’s algorithm and Prim’s algorithm (starting from vertex a).

Submission (xSITe Quizzes, 10 marks). Matching the order of edges for both algorithms from xSITe Quiz “Week 09 Lab Exercise 1”.
You are given an array of points where points[i] = \([x_i, y_i]\) represents a point on a 2D plane. Your task is to find the minimum cost to make all points connected, where the cost of connecting two points \([x_i, y_i]\) and \([x_j, y_j]\) is the Manhattan distance between them: \(|x_i - x_j| + |y_i - y_j|\).
This is essentially finding the Minimum Spanning Tree (MST) of a complete undirected graph where:

Download min_cost.zip from xSITe. The file min_cost.cpp contains a stub for function
Implement minCost using Kruskal’s or Prim’s algorithm to find the MST.
Build a complete graph where each edge weight is the Manhattan distance between two points.
Use appropriate data structure (Disjoint Sets or min-heap) to efficiently select the minimum-weight edge at each step.
Make and test your implementation locally using the provided test.txt.
Submission (Gradescope, 20 marks). Submit your completed min_cost.cpp and min_cost.h to the “Week 09 Lab Exercise 2” assignment on Gradescope.
Calculate Manhattan distance: \(|x_i - x_j| + |y_i - y_j|\)
Prim’s Algorithm approach:
visited array to track vertices already in the MSTKruskal’s Algorithm approach:
You are given a small grayscale image. Each pixel can be modelled as a vertex in a graph. Two neighbouring pixels (up, down, left, right) are connected by an edge whose weight is the absolute difference in pixel intensity between the two pixels (their dissimilarity).
An MST on this graph connects all pixels while minimizing the total dissimilarity between neighbouring pixels. If we cut (remove) the largest-weight edges in the MST, the image is split into regions (segments) of similar intensity.
Key idea: MST ensures that within each segment, neighbouring pixels are as similar as possible, and segmentation is achieved by removing the largest dissimilarities (heaviest edges) in the tree.
Consider the following \(3 \times 3\) grayscale image, where each number is a pixel intensity:
Consider the following \(3 \times 3\) grayscale image, where each number is a pixel intensity:
Below is one of the valid MSTs:
Download: Get image_segmentation.zip from xSITe. The starter file image_segmentation.cpp contains the stub:
Input format:
rows cols (image height and width)rows lines: each line has cols integer pixel intensities in \([0, 255]\) (grayscale values)Output format: You do NOT need to actually output the segmentation. Just compute the MST total “dissimilarity” cost of the image.
Implementation hints:
idx = row * cols + colSubmission (Gradescope, 30 marks). Upload completed image_segmentation.cpp file to Gradescope “Week 09 Lab Exercise 3”.
By the end of this lab you should be able to:
This lab introduced Minimum Spanning Trees as a fundamental graph algorithm. The remaining weeks cover:
Week 09: Disjoint Sets and MST (Lab)