In this post, we implement Depth-First Search (DFS) in Java using an adjacency matrix and recursion. DFS is a fundamental graph traversal algorithm that starts at a source node and explores as far as possible along each branch before backtracking. It is used in cycle detection, topological sorting, maze solving, and AI game tree searches.
What is DFS?
DFS explores a graph by diving deep into one branch before exploring others. In this implementation, we use a recursive approach: starting from node 1, we immediately follow the first unvisited neighbour, then the first unvisited neighbour of that node, and so on. Only when we reach a dead end do we backtrack and try the next neighbour.
The graph is represented as an adjacency matrix. An array q[] is used both as the visited set and to record the traversal order. The variable qpos tracks how many nodes have been visited so far.
In this post, we implement a simple calculator in Prolog that performs basic arithmetic operations: addition, subtraction, multiplication, and division. Prolog is a logic programming language widely used in artificial intelligence and symbolic computing. Unlike imperative languages (Java, C), Prolog is declarative — you describe what relationships hold, not how to compute them step by step. The Prolog engine then uses unification and backtracking to derive answers.
What is a Prolog Predicate?
In Prolog, a predicate defines a relationship between its arguments. When you query a predicate, Prolog attempts to unify the query with the predicate’s head and then evaluates the body. For arithmetic, the is operator (or direct unification with =) binds a variable to the result of an expression. In our calculator, ADD(A, B, SUM) :- SUM = A + B. means: “SUM holds if SUM equals A plus B.”
In this post, we implement Breadth-First Search (BFS) in Java using an adjacency matrix. BFS is a fundamental graph traversal algorithm that visits all nodes level by level, starting from a source node and exploring all its direct neighbours before moving deeper into the graph. It is widely used in shortest path problems, network analysis, and AI search strategies.
What is BFS?
BFS explores a graph by using a queue data structure. It starts at a chosen node (node 1 in our implementation), adds it to the queue, and then repeatedly dequeues a node, visits all its unvisited neighbours, and enqueues them. This guarantees that all nodes at depth d are visited before any node at depth d+1.
The graph in this implementation is represented as an adjacency matrix — a 2D array where m[i][j] = 1 means there is an edge between node i+1 and node j+1, and 0 means no edge.
In this post, we implement image thresholding in MATLAB. Thresholding is one of the simplest and most widely used segmentation techniques. It converts a grayscale image into a binary image by comparing each pixel’s intensity against a threshold value: pixels below the threshold are set to 0 (black) and pixels at or above the threshold are set to 255 (white). This cleanly separates foreground objects from the background.
MATLAB Code
% Image Thresholding in MATLAB
% Converts a grayscale image to binary using a user-supplied threshold.
% Pixels below the threshold -> 0 (black)
% Pixels >= threshold -> 255 (white)
clc; % Clear the command window
clear all; % Clear all workspace variables
% --- Load image ---
% Place your image in the MATLAB working directory.
originalImage = imread('sample.jpg'); % Read the colour image
grayImage = rgb2gray(originalImage); % Convert to grayscale
imageSize = size(grayImage); % [rows, cols]
% --- Get threshold from user ---
thresholdValue = input('Enter threshold value (0-255) : '); % e.g. 128
% --- Apply threshold ---
binaryImage = grayImage; % Copy to preserve border pixels
for i = 1 : imageSize(1)
for j = 1 : imageSize(2)
if grayImage(i, j) black
else
binaryImage(i, j) = 255; % At or above threshold -> white
end
end
end
% --- Display results ---
subplot(1, 2, 1);
imshow(grayImage);
title('Original Grayscale Image');
subplot(1, 2, 2);
imshow(binaryImage);
title(['Thresholded at ', num2str(thresholdValue)]);
In this post, we implement a spatial-domain Low Pass Filter (LPF) in MATLAB using a 3×3 averaging kernel. A low pass filter smooths an image by replacing each pixel with the average of itself and its 8 neighbours. This reduces noise and high-frequency detail (sharp edges), producing a blurred version of the original. It is the spatial counterpart of frequency-domain low-pass filtering and is the basis of many image smoothing techniques.
MATLAB Code
% Low Pass Filter (LPF) in MATLAB
% Applies a 3x3 averaging kernel (box filter) to a grayscale image.
% Each output pixel is the mean of the 3x3 neighbourhood centred on it.
clc; % Clear the command window
clear all; % Clear all workspace variables
% --- Load image ---
% Place your image in the MATLAB working directory.
originalImage = imread('sample.jpg'); % Read the colour image
grayImage = rgb2gray(originalImage); % Convert to grayscale
imageSize = size(grayImage); % [rows, cols]
% Initialise the output image (same size as input)
lpfImage = grayImage;
% --- Apply 3x3 averaging kernel ---
% Skip the border row/column (index 1 and last) to avoid out-of-bounds access.
for i = 2 : imageSize(1) - 1
for j = 2 : imageSize(2) - 1
% Sum the 3x3 neighbourhood and divide by 9 (average)
lpfImage(i, j) = ( ...
double(grayImage(i-1, j-1)) + double(grayImage(i-1, j)) + double(grayImage(i-1, j+1)) + ...
double(grayImage(i, j-1)) + double(grayImage(i, j)) + double(grayImage(i, j+1)) + ...
double(grayImage(i+1, j-1)) + double(grayImage(i+1, j)) + double(grayImage(i+1, j+1)) ...
) / 9;
end
end
% --- Display results ---
subplot(1, 2, 1);
imshow(grayImage);
title('Original Grayscale Image');
subplot(1, 2, 2);
imshow(lpfImage);
title('Low Pass Filtered (Blurred)');
In this post, we implement Histogram Equalisation in MATLAB. Histogram equalisation is an image enhancement technique that redistributes the pixel intensity values of an image so that the resulting histogram is approximately uniform. This increases the global contrast of the image, especially when the usable image data is represented by close contrast values. It is widely used in medical imaging, satellite imagery, and photography.
MATLAB Code
% Histogram Equalisation in MATLAB
% Reads a colour image, converts to grayscale, reduces contrast
% intentionally (to simulate a low-contrast image), applies
% histogram equalisation, and displays both images with their histograms.
clc; % Clear the command window
clear all; % Clear all workspace variables
% --- Load and prepare image ---
% Place your image in the MATLAB working directory.
originalImage = imread('sample.jpg'); % Read the colour image
grayImage = rgb2gray(originalImage); % Convert to grayscale
grayImage = imresize(grayImage, [256, 256]); % Resize to 256x256
% Reduce contrast by scaling pixel values to half range (simulate low contrast)
lowContrastImage = uint8(0.5 * double(grayImage));
% --- Apply histogram equalisation ---
% histeq() redistributes intensity values to flatten the histogram.
equalizedImage = histeq(lowContrastImage, 256);
% --- Display results: images and histograms ---
figure;
subplot(2, 2, 1);
imshow(lowContrastImage);
title('Low Contrast (Original)');
subplot(2, 2, 2);
imshow(equalizedImage);
title('Histogram Equalised');
subplot(2, 2, 3);
imhist(lowContrastImage);
title('Original Histogram');
subplot(2, 2, 4);
imhist(equalizedImage);
title('Equalised Histogram');
In this post, we implement a spatial-domain High Pass Filter (HPF) in MATLAB. A high pass filter suppresses low-frequency (smooth) regions of an image and enhances high-frequency content such as edges and sharp transitions. Unlike the Low Pass Filter which blurs an image, the HPF makes edges more visible by computing the Laplacian of the image at every pixel.
MATLAB Code
% High Pass Filter (HPF) in MATLAB
% Applies a 3x3 Laplacian high-pass kernel to a grayscale image.
% The kernel: [-1 -1 -1; -1 8 -1; -1 -1 -1]
% Computes: HPF(i,j) = 8*center - sum_of_8_neighbours
clc; % Clear the command window
clear all; % Clear all workspace variables
% --- Load image ---
% Place your image in the MATLAB working directory.
originalImage = imread('hills.jpg'); % Read the colour image
grayImage = rgb2gray(originalImage); % Convert to grayscale
imageSize = size(grayImage); % [rows, cols]
% Initialise output as double to avoid uint8 overflow
hpfImage = double(grayImage);
% --- Apply Laplacian HPF kernel ---
% Skip the border pixels (row/col 1 and last row/col)
% to avoid out-of-bounds indexing.
for i = 2 : imageSize(1) - 1
for j = 2 : imageSize(2) - 1
% Laplacian: multiply centre by 8, subtract all 8 neighbours
hpfImage(i, j) = ...
-double(grayImage(i-1, j-1)) ...
-double(grayImage(i-1, j )) ...
-double(grayImage(i-1, j+1)) ...
-double(grayImage(i, j-1)) ...
+8*double(grayImage(i, j )) ...
-double(grayImage(i, j+1)) ...
-double(grayImage(i+1, j-1)) ...
-double(grayImage(i+1, j )) ...
-double(grayImage(i+1, j+1));
end
end
% --- Display results ---
subplot(1, 2, 1);
imshow(grayImage);
title('Original Grayscale Image');
subplot(1, 2, 2);
imshow(hpfImage, []);
title('High Pass Filtered Image');