0% found this document useful (0 votes)
122 views12 pages

Lab 1 TCP2101 ALGORITHM DESIGN AND ANALYSIS

The document describes an analysis of algorithms lab assignment. It introduces two algorithms - arrayMax to find the maximum element in an array, and partialSum to calculate partial sums. It asks students to implement the algorithms in C++, measure their time complexities by running them on larger input sizes, and improve the partialSum algorithm from O(n^2) to O(n). Charts are included to record runtimes and compare the original vs improved partialSum algorithms.

Uploaded by

Husna Zulkefli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views12 pages

Lab 1 TCP2101 ALGORITHM DESIGN AND ANALYSIS

The document describes an analysis of algorithms lab assignment. It introduces two algorithms - arrayMax to find the maximum element in an array, and partialSum to calculate partial sums. It asks students to implement the algorithms in C++, measure their time complexities by running them on larger input sizes, and improve the partialSum algorithm from O(n^2) to O(n). Charts are included to record runtimes and compare the original vs improved partialSum algorithms.

Uploaded by

Husna Zulkefli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

TCP2101 Algorithm Design and Analysis

Lab 1 Analysis of Algorithms

Learning Outcomes
● Understand how to measure the real execution time of a C++ code segment
● Implement algorithms based on pseudocodes
● Compute and compare the complexities of some basic algorithms

1. As discussed in Lecture 1, arrayMax(A, n) is an algorithm to find the largest


number in an array A of n integers. The complexity of arrayMax(A, n) is O(n).

Algorithm arrayMax(A, n)
Input: An array A storing n ≥ 1 integers
Output: The maximum element in A
currentMax ← A[0];
for i←1 to n-1 do
if currentMax < A[i] then
currentMax ← A[i]
return currentMax

(a) (15 min) A C++ implementation of arrayMax function is listed below. C++
chrono library is used to measure the duration of executing arrayMax. Compile
and run the program. Make sure your compiler is configured to support C++17
(Code::Blocks > Settings > Compiler).

filename: lab01q01a.cpp
#include <iostream>
#include <chrono>
#include <cstdlib>
using namespace std;

int arrayMax (int A[], int n) {


int currentMax = A[0];
for (int i=1; i <= n-1; i++) {
if (A[i] > currentMax)
currentMax = A[i];
}
return currentMax;
}

template <typename T>


void printArray (T A[], int n) {
for (int i = 0; i < n; i++)
cout << A[i] << " ";
cout << endl;
}

int main() {
const int n = 500;
srand (time(0)); // Init random number generator.
int* A = new int[n];
for (int i = 0; i < n; i++)

1
A[i] = rand() % RAND_MAX; // generate randon number range 0-32767.

cout << "Array A:\n";


printArray (A, n);

auto start = chrono::system_clock::now();


int currentMax = arrayMax (A, n);
auto end = chrono::system_clock::now();
chrono::duration<double> duration = end - start;

cout << "The maximum integer in A is "


<< currentMax << endl;
cout << "Duration: " << duration.count() << "s\n";

delete [] A;
}

2
(b) (5 min) Try re-run the program with bigger array size such as 10 million
and 100 million. Comment out the printArray function to save running time.
Notice the duration. It does not increase much. This is normal for an algorithm
with time complexity of O(n).

2. partialSum(A,S,n) is an algorithm to find the partial sums of an array of n


integers.

Algorithm partialSum (A,S,n)


Input: An array A storing n≥1 integers
Output: An array S storing the partial sums of A.
S[0] A[0]
for i 1 to n-1 do
S[i] A[0]
for j 1 to i do
S[i] S[i] + A[j]

(a) (10 min) Based on the partialSum algorithm above, fill up the S[i] column
in the table below.

i A[i] S[i] after the inner "for" loop executed


0 11 11
1 23 34
2 5 39
3 27 66
4 33 99
5 1 100
6 45 145
7 18 163

3
(b) (10 min) Count the number of primitive operations of each step in
partialSum. What is the time complexity of partialSum?

Algorithm Number of primitive operations


S[0] A[0] 3≈1
for i 1 to n-1 do for i1 to n-1 do 1+n-1+1(last
time) = 1+n ≈ n
S[i] A[0] S[i] A[0] 3(n-1) ≈ n
for j 1 to i do for j1 to i do 1+2+3+4+5+…n-1 ≈
n^2
(n-1)*(n-1) = n^2
S[i] S[i] + A[j] S[i] S[i] + A[j] 5(1+2+3+4+…n-1)
≈ n^2
T(n) O(n^2)

(c) (20 min) Implement the above algorithm in C++. You may use the
following code.
#include <iostream>
// 1. Add chrono library.
#include <chrono>
using namespace std;

// 2. Add partialSum function.


void partialSum(int A[],int S[],int n){ //partialSum is using void because no
return
S[0]=A[0];
for(int i=1; i<=n-1; i++){
S[i] = A[0];
for(int j=1; j<=1; j++)
S[i] += A[j];
}
}

template <typename T>


void printArray (T A[], int n) {
for (int i = 0; i < n; i++)
cout << A[i] << " ";
cout << endl;
}

int main() {
const int n = 8;
int* A = new int[n]{11, 23, 5, 27, 33, 1, 45, 18};
int* S = new int[n];

// 3. Take start time.


auto start = chrono::system_clock::now();

// 4. Call partialSum.
partialSum(A,S,n);

// 5. Take end time.


auto end = chrono::system_clock::now();

4
// 6. Calculate duration.
chrono::duration<double> duration = end - start;

//cout << "A:\n";


//printArray (A, n);
//cout << "Partial sum S:\n";
//printArray (S, n);

// 7. Display duration.
cout << "Duration: " << duration.count() << "s\n";

delete [] A;
delete [] S;
}

5
6
(d) (10 min) Re-run the partialSum function with the following array size and
record its duration in the first row (try the first few array sizes in the lab).
Comment out the printArray function to save running time. You may have to
increase the random number if 32767 is not big enough to be representative.

Array size n 10k 50k 100k 250k 500k 750k 1m 5m 10m


2(c) 865.60
partialSum 0.1470 0.141 12.622 82.619 349.38 3s
duration (s) 36s 03s s 2s 6s
2(f) 0s 0s 0.0010 0.0009 0.0030 0.0029 0.003 0.020 0.0439
Improved 024s 968s 064s 955s 9903s 0106s 977s
partialSum
duration (s)

7
8
(e) (15 min) The partialSum algorithm could be improved to O(n) complexity
time. Write the improved partialSum algorithm in the table below.

Algorithm Number of primitive operations


S[0] A[0] 3≈1
for i 1 to n-1 do 1+n ≈ n
S[i] S[i-1] + A[i] 6(n-1) ≈ n
T(n) O(n)

(f) (15 min) Implement the improved partialSum in C++.

#include <iostream>
// 1. Add chrono library.
#include <chrono>
using namespace std;

// 2. Add partialSum function.


void partialSum(int A[],int S[],int n){ //partialSum is using void because no
return
S[0] = A[0];
for(int i=1; i<=n-1; i++){
S[i] = S[i-1] + A[i]; //Big O applied to partialSum function
}
}

template <typename T>


void printArray (T A[], int n) {
for (int i = 0; i < n; i++)
cout << A[i] << " ";
cout << endl;
}

int main() {
const int n = 10000000;
int* A = new int[n];
for (int i = 0; i < n; i++)
A[i] = rand() % RAND_MAX; // generate randon number range 0-32767.
int* S = new int[n];

// 3. Take start time.


auto start = chrono::system_clock::now();

// 4. Call partialSum.
partialSum(A,S,n);

// 5. Take end time.


auto end = chrono::system_clock::now();

9
// 6. Calculate duration.
chrono::duration<double> duration = end - start;

//cout << "A:\n";


//printArray (A, n);
//cout << "Partial sum S:\n";
//printArray (S, n);

// 7. Display duration.
cout << "Duration: " << duration.count() << "s\n";

delete [] A;
delete [] S;
}

(g) (10 min) Re-run the improved partialSum function with the array size
specified in 2(d). Record its duration in the second row.

10
11
(h) (10 min) Based on the durations obtained in 2(d), plot a graph with
Duration vs Array size. The original algorithm should produce a quadratic line
while the improved algorithm should produce a linear line.

12

You might also like