The K-Means algorithm is one of the most widely used unsupervised machine learning algorithms. It partitions a dataset into K clusters such that each data point belongs to the cluster whose mean (centroid) it is closest to. Unlike KNN, K-Means does not use labelled data — it discovers natural groupings entirely on its own through iteration.
In this post we walk through a well-commented C++ implementation of K-Means that clusters 10 integer values into 2 groups. We cover the algorithm, explain each line of code, trace through the iterative process, and analyse the output in detail.
What is the K-Means Algorithm?
K-Means works by alternating between two steps until the cluster centres stop changing:
Assignment step: Assign each data point to the cluster whose mean it is closest to.
Update step: Recalculate the mean of each cluster based on its current members.
The algorithm converges when the means no longer change between iterations — meaning the clusters have stabilised. In this implementation:
The Nearest Neighbour Algorithm is one of the foundational concepts in Data Warehousing and Mining (DWM). It is used to analyse a graph or dataset and classify its nodes into two groups based on a given threshold distance. A node is placed in the close neighbourhood (K1) if it is within the threshold, and in the far neighbourhood (K2) if it exceeds it.
In this post we walk through a well-commented C++ implementation that reads an adjacency matrix of 5 vertices, accepts a starting vertex and a distance threshold, and partitions all connected vertices into the two neighbourhood sets K1 and K2.
What is the Nearest Neighbour Algorithm?
Given a graph represented as an adjacency matrix where adjMatrix[i][j] stores the distance (edge weight) between vertex i and vertex j, the algorithm works as follows for a chosen source vertex s:
Read the distance from s to every other vertex from the adjacency matrix row corresponding to s.
For each vertex v: if the distance is greater than 0 (there is an edge) and less than or equal to the threshold, add v to K1.
If the distance exceeds the threshold, add v to K2.
Also print all direct connections along with their distances.
A value of 0 on the diagonal represents the distance from a vertex to itself (no self-loops). A large value like 99 is typically used to represent no direct connection (infinity).
The K-Nearest Neighbors (KNN) algorithm is one of the simplest and most intuitive supervised machine learning algorithms. It classifies a new data point based on the majority class among its K closest neighbors in the training dataset. There is no explicit training phase — the algorithm memorizes the entire dataset and makes decisions at prediction time, which is why it is also called a lazy learner.
In this post, we will walk through a C++ implementation of the KNN algorithm that classifies a person’s height as short, medium, or tall using a small training set of 12 records. We cover the algorithm step by step and explain the output in detail.
What is the KNN Algorithm?
KNN works on a very simple principle: similar things exist in close proximity. Given an unlabeled data point, the algorithm:
Calculates the distance between the new point and every point in the training dataset.
Sorts all training points by distance (ascending).
Picks the K nearest points (the threshold).
Assigns the class that appears most frequently among those K neighbors.
This program uses a single feature — height — and the distance metric is the simple absolute difference (Manhattan distance in 1D). Gender is stored in the dataset but is not used as a feature for classification in this implementation.
Apriori is a cornerstone algorithm in association rule mining, widely used in market basket analysis (e.g., identifying which items are frequently bought together in stores). The algorithm’s power lies in its ability to uncover hidden patterns in large transactional datasets.
What Does the Apriori Algorithm Do?
The Apriori algorithm is used for discovering frequent itemsets—groups of items that appear together in a dataset with frequency above a specified threshold (called minimum support). These itemsets form the basis for generating association rules—for example, “if someone buys bread and butter, there’s a good chance they’ll also buy jam.”