Lab 1 TCP2101 ALGORITHM DESIGN AND ANALYSIS
Lab 1 TCP2101 ALGORITHM DESIGN AND ANALYSIS
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
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 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.
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).
(a) (10 min) Based on the partialSum algorithm above, fill up the S[i] column
in the table below.
3
(b) (10 min) Count the number of primitive operations of each step in
partialSum. What is the time complexity of partialSum?
(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;
int main() {
const int n = 8;
int* A = new int[n]{11, 23, 5, 27, 33, 1, 45, 18};
int* S = new int[n];
// 4. Call partialSum.
partialSum(A,S,n);
4
// 6. Calculate duration.
chrono::duration<double> duration = end - start;
// 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.
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.
#include <iostream>
// 1. Add chrono library.
#include <chrono>
using namespace std;
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];
// 4. Call partialSum.
partialSum(A,S,n);
9
// 6. Calculate duration.
chrono::duration<double> duration = end - start;
// 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