0% found this document useful (0 votes)
5 views

Lab 02 - Complexity Analysis

The lab focuses on calculating and analyzing time complexity using various code snippets and visualizing their growth functions. Students are required to derive growth functions, create datasets in Excel, and plot graphs to compare algorithm efficiency. Additionally, tasks involve validating truck tags based on specific criteria and generating expressions reflecting Hulk's feelings based on input layers.

Uploaded by

Zohaib Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 02 - Complexity Analysis

The lab focuses on calculating and analyzing time complexity using various code snippets and visualizing their growth functions. Students are required to derive growth functions, create datasets in Excel, and plot graphs to compare algorithm efficiency. Additionally, tasks involve validating truck tags based on specific criteria and generating expressions reflecting Hulk's feelings based on input layers.

Uploaded by

Zohaib Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CC-213L – Data Structures and Algorithms

Fall 2024
LAB-02 Issue Date: February 14, 2025

The objective of this lab is to:


1. Learn to calculate the time complexity using steps count method.
2. Analyze the time complexity of iterative programs and derive their growth functions.
3. Visualize the upper bounds of these programs by plotting graphs in Microsoft Excel and analyzing
their behavior.
4. Understand algorithm efficiency visually and develop an intuition for how different functions scale
with input size.

ALERT!

1. You are encouraged to bring your laptop to attend the lab.


2. This is an individual lab, you are strictly NOT allowed to discuss your solution with fellow colleagues,
even not allowed asking how is he/she is doing, it may result in negative marking. You can ONLY
discuss with your TAs or with me.
3. Anyone caught in act of cheating would be awarded an “F” grade in this Lab.

Task 01: [25+5 Marks]

1. Consider the following code snippets. Derive the growth function for each of the following codes.

int sum = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n * n; j++)
1. {
sum++;
}
}

for (int i = 1; i < n; i++)


{
for (int j = n / 2; j < n; j++)
2. {
cout << n;
}
}

int sum = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < i; j++)
3.
{
sum++;
}
}

void display(char* s)
{
for (int i = strlen(s) - 1; i >= 0; i++)
4.
{
cout << s[i] << endl;
}
}
Faculty of Computing and Information Technology Page 1 of 3
Dr. Madiha Khalid
CC-213L – Data Structures and Algorithms
Fall 2024
LAB-02 Issue Date: February 14, 2025

int func(int N, int M)


{
int result = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < M; k++)
{
int x = 0;
while (x < N)
{
5. result++;
x += 3;
}
}
for (int k = 0; k < 2 * M; k++)
result++;
}
}
int j = 0;
for (int i = 0; i < N; i++)
j++;
return result;
}

2. Now that you have derived the growth functions for all of the code snippets given in Part 1, do the
following tasks:
i. From each growth function derived in Part 1, pick the most dominant term (the term that
grows the fastest as n increases e.g., n², n³, n log n, etc). This term represents how the
algorithm scales as n increases.
ii. In Microsoft Excel, create a dataset for different values of n (e.g., n = 10, 50, 100, 200, 500,
1000, 2000, 5000, 10000, 20000, 30000, 50000 etc).
iii. For each dominant term, compute its value for each n. Example: If the dominant term is n²,
compute 10², 50², 100², 5002... and so on.
iv. Use Excel’s graph plotting tool (line graph feature) to plot the dominant terms. Plot all terms
(e.g., n, n log n, n², n³) on the same graph to compare their growth.
v. Label the axes as:
X-axis: values of n
Y-axis: dominant terms
vi. Observe the plot curves and see how different dominant terms scale as n increases. Compare
how slower-growing functions behave compared to faster-growing ones. On the basis of your
comparison write the dominant terms in the increasing order of their growth rate.

Task 02: [10 Marks]

Bermeja Island in Mexico has surrounded by the pirates. A truck enters the Island. The driver claims the load
is food and medicine. Captain Swann is one of the soldiers in Bermeja Island. He doubts about the truck,
maybe it's from the pirates. Every truck has a tag displayed on it. Captain Swann is instructed to allow the
vehicles to enter the Island only if they have a valid tag on them. He knows that a tag is valid if the sum of
every two consecutive digits of it is even and its letter is not a vowel. He needs to determine whether the tag
of the truck is valid or not. We consider the letters "A", "E", "I", "O", "U", "Y" to be vowels for this problem.
The format of the tag is "DDXDDD-DD", where D stands for a digit (non-zero) and X is an uppercase English
letter. Your task is to write a program to help Captain Swann by determining whether the tag is valid or not.

Faculty of Computing and Information Technology Page 2 of 3


Dr. Madiha Khalid
CC-213L – Data Structures and Algorithms
Fall 2024
LAB-02 Issue Date: February 14, 2025

Sample Execution 1:
Input: 12C345-67
Output: Invalid

Explanation: The tag is invalid because the sum of first and second digit of it is odd (also the sum
of 4th and 5th, 5th and 6th and 8th and 9th are odd). Also, the letter in not a vowel.

Sample Execution 2:
Input: 31Y573-42
Output: Valid

Explanation: The tag is valid because the letter “Y” is a vowel and the sum every two consective degts
are even i.e. sum of first and second digit is even, also the sum of 4th and 5th, 5th and 6th and 8th and 9th
are even).

Write a driver program as well to check the correctness of your function. You have to derive the growth
function of your program to analyze the time complexity.

Task 03: [10 Marks]

Dr. Bruce Banner hates his enemies. As we all know, he can barely talk when he turns into the incredible
Hulk. That's why he asked you to help him to express his feelings. Hulk likes the Inception so much, and like
that his feelings are complicated. They have n layers. The first layer is hate, second one is love, third one is
hate and so on... For example, if 𝑛 = 1, then his feeling is "I hate it" or if 𝑛 = 2, it's "I hate that I love it",
and if 𝑛 = 3 it's "I hate that I love that I hate it" and so on. Please help Dr. Banner by writing a program to
express his feelings.

Sample Execution 1:
Input: 3
Output: I hate that I love that I hate it.

Sample Execution 2:
Input: 4
Output: I hate that I love that I hate that I love it.

Derive the growth function for your program.

"Time complexity is like interest rates


—small differences compound massively as input grows."
Faculty of Computing and Information Technology Page 3 of 3
Dr. Madiha Khalid

You might also like