AED Report

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Universidade de Aveiro 2023

Algoritmos e Estruturas de Dados (40437-AED)

Made by:
Bernardo Borges nº103592
João Ferreira nº95316
Jevgeni Arefjev nº117814
Introduction
In this project, we were tasked with creating and optimizing functions for an image loading and
manipulation program.
Within this report, we do a formal analysis of algorithmic complexity of the functions
ImageLocateSubImage and ImageBlur (which has 2 versions). In addition to that, we are going to
evaluate the complexity of the functions by using different images with different sizes.
This report also includes the comparison of performance between both implementations of the ImageBlur
function.

ImageLocateSubImage
Formal Analysis
This function locates the sub-image within a given image by taking two images as input and setting the
values of the pointers to the upper left pixel upon finding the correct sub-image.
Upon every iteration over the pixels of the original image, this function invokes the other function
ImageMatchSubImage. The function ImageMatchSubImage in turn compares the two input images pixel-
by-pixel.
In this analysis, the dimensions of the main image are denoted as w1 and h1 (being width and height), and
the dimensions of the sub-image are denoted by w2 and h2.

Best case
In best case scenario, the upper leftmost pixel of the sub-image coincides with the upper leftmost pixel of
the main image. As a result, it takes only a single call of the ImageMatchSubImage function, which
executes w2*h2 comparisons.
Result: w2*h2 comparisons.

Worst case
In the worst-case scenario, the function goes over all possible upper leftmost pixels for the sub-image,
which there are (w1 – w2) * (h1 – h2). In addition, every call to ImageMatchSubImage takes maximum
possible number of comparisons to complete (w2*h2).

Result: (w1 – w2) * (h1 – h2) * w2 * h2 comparisons.

Tests
Base Image Sub Image Avg_time Avg_caltime PixMem PixOp

pgm/medium/ire ireland- 0.001277 0.000802 306622 153311


land- 03_4x4.pgm
03_640x480.pg
m
pgm/medium/ire ireland- 0.001295 0.000773 304158 152079
land- 03_8x8.pgm
03_640x480.pg
m
pgm/medium/ire benchmark/inpu 0.001237 0.000739 282130 141065
land- t/med/ireland-
03_640x480.pg 03_50x50.pgm
m

pgm/medium/ire benchmark/inpu 0.001083 0.000661 264846 132423


land- t/med/ireland-
03_640x480.pg 03_100x100.pg
m m

pgm/medium/ire benchmark/inpu 0.000971 0.000575 260466 130233


land- t/med/ireland-
03_640x480.pg 03_200x200.pg
m m

pgm/medium/ire benchmark/inpu 0.000757 0.000442 154210 77105


land- t/med/ireland-
03_640x480.pg 03_320x240.pg
m m

These results make sense and are expected. The bigger the sub-image, the less possibilities we
have to place it in the base-image, meaning the program would exit earlier, thus making less
operations.
Base Pixcount Sub Pixcount Base/Sub count log(base/sub; avg_caltime)

307200 16 19200 N/A


307200 64 4800 -37.6408077711337
307200 2500 122.88 -81.9753885748634
307200 10000 30.72 -12.331384720049
307200 40000 7.68 -10.0395685224147
307200 76800 4 -2.47746791119325

ImageBlur (Naïve version)


This function allows the user to blur the image by using values of grey around each pixel. The number of
pixels used for blurring is specified while calling the function.

Formal Analysis
Within this analysis, we calculate the number of additions + the number of divisions made by the
function.

Additions
𝑤𝑖𝑑𝑡ℎ ∑ ℎ𝑒𝑖𝑔ℎ𝑡 ∑𝑑𝑥
∑𝑖=0 𝑑𝑦
𝑗=0 𝑚=−𝑑𝑥 ∑𝑛=−𝑑𝑦 1 =

= width * height * 2*dx * 2*dy times called

Divisions
ℎ𝑒𝑖𝑔ℎ𝑡
∑ 𝑤𝑖𝑑𝑡ℎ
𝑖=0 ∑𝑗=0 1=

= width * height times called

Operations done

Additions + Divisions = width *height * dx * dy + width * height ∈ O(n⁴)

ImageBlur (Optimized version)

This function allows the user to blur the image by using values of grey around each pixel. The number of
pixels used for blurring is specified while calling the function. However, with the optimizations made to
it, the function now runs faster and more smoothly.

ImageBlur Comparison

To conduct this comparison, the functions were benchmarked on pictures with different sizes and using
blur of various radii. More specifically, 3 images, with sizes 256*256, 512*512 and 1600*1200 have been
used. The images were identical in all tests. The tests were conducted using square blur with equal height
and width, all being powers of two.
In this analysis, it was assumed that all the algorithms have polynomial time complexity, based on the
theoretical analysis done beforehand. This assumption in the end was further supported by the
experimental results.
To find the exponent of the complexity polynomial n^a, a logarithm was taken based on the difference in
size of the input.
(𝑏⋅ 𝑛)𝑎 𝑏⋅ 𝑛 𝑎
So: = 𝑐 ⇒ ( ) = 𝑐 ⇒ (𝑏)𝑎 = 𝑐 ⇒ 𝑎 = log 𝑏 (𝑐)
(𝑛)𝑎 𝑛

Where b is the ratio of current test input against previous test input, and c is the ratio of current test result
vs the previous test result.
Regarding the Naïve version, the tests show:

Time Complexity Analysis of the Naïve ImageBlur depending on the image size.

Number of Pixels Calibrated Time Power of the polynomial O(n^a)

65536 0.0035752 -
262144 0.0140812 0.988837215370689
1920000 0.1049234 1.00863981946069

Time Complexity Analysis of the Naïve ImageBlur depending on the blur area.

Blur size Blur Total Area (pixels) Calibrated Time Power of the
polynomial O(n^a)

2*2 25 0.1049234 -
4*4 81 0.3225680 0.955349511709067
8*8 289 1.0791908 0.9494293582649
16 * 16 1089 4.2135932 1.02677231202173
32 * 32 4225 15.1955802 0.946103577035328

The experimental test results show that the Naïve ImageBlur has O(n_image * n_blur) time complexity, where
n_image is the amount of pixels in the image, and n_blur is the blur area.

Regarding the Optimised version, the tests show:


Time Complexity Analysis of the Optimised ImageBlur depending on the image size.

Number of Pixels Calibrated Time Power of the polynomial O(n^a)

65536 0.0004120 -
262144 0.0016438 0.99815926773809
1920000 0.0118988 0.994094187562808

Time Complexity Analysis of the Optimised ImageBlur depending on the blur area.

Blur size Blur Total Area (pixels) Calibrated Time Power of the
polynomial O(n^a)

2*2 25 0.0118988 -
4*4 81 0.0114088 -0.0357719643598951
8*8 289 0.0109362 -0.0332604930915711
16 * 16 1089 0.0110166 0.0055215607983529
32 * 32 4225 0.0109944 -0.0014878553602295

The last table shows that Optimised ImageBlur has O(n^0) = O(1) complexity regarding blur area. This
result indicates that with fixed image size, the blur area does not affect the time of execution.

The experimental test results show that the Optimised ImageBlur has O(n_image) time complexity, where n_image
is the amount of pixels in the image, and running time does not depend on the blur area.

Conclusion
To sum up, we made a possible implementation for this image program, allowing us to manipulate an
image. We believe this is a great program because it does its job quickly and with a decent number of
accesses to the memory. Not only that, but there are also no memory leaks while using this program.

You might also like