.Assignment 1
.Assignment 1
ASSIGNMENT- I
Term: First Semester Academic Year: 2024-25
Q1A: Using limit compare the order of the growth of two functions. (1 Mark)
(b) 3n and 4n
Q1B: For each of the following functions C(n), indicate how much the running time T(n)
will change if its input is increased twofold. (1 Mark)
(b) 𝒏𝟑
Doubling n:
𝑇ሺ2𝑛ሻ = ሺ2𝑛ሻ3 = 8𝑛3 = 8 𝑇ሺ𝑛ሻ .
The running time increases by a factor of 8.
Q2A: Indicate whether the first function of each of the following pairs has a smaller,
same, or larger order of growth (to within a constant multiple) than the second function.
(1 Mark)
𝑛−1
ሺ𝒂ሻ 2 𝑎𝑛𝑑 2𝑛 ሺ𝒃ሻ 𝑙𝑜𝑔23 𝑛 𝑎𝑛𝑑 𝑙𝑜𝑔2 𝑛2
𝑛−1
ሺ𝒂ሻ 2 𝒂𝒏𝒅 𝟐𝒏
Total Comparisons:
Summary of Results
Algorithm S(n)
Write a pseudo-code for the non-recursive algorithm and determine its efficiency.
Algorithm SumOfCubes(n)
Algorithm S(n)
sum = 0
for i = 1 to n do
sum = sum + i * i * i
end for
return sum
Efficiency Analysis
𝑇ሺ𝑛ሻ = 𝑇ሺ𝑛 − 1ሻ + 𝑛
Substitute 𝑇ሺ𝑛 − 1ሻ
𝑇ሺ𝑛 − 1ሻ = 𝑇ሺ𝑛 − 2ሻ + ሺ𝑛 − 1ሻ
So:
𝑇ሺ𝑛ሻ = 𝑇ሺ𝑛 − 2ሻ + ሺ𝑛 − 1ሻ + 𝑛
𝑇ሺ𝑛ሻ = 𝑇ሺ1ሻ + 2 + 3 + ⋯ + ሺ𝑛 − 1ሻ + 𝑛
So:
𝑇ሺ𝑛ሻ = 1 + 2 + 3 + ⋯ + 𝑛
𝑛ሺ𝑛 + 1ሻ
𝑇ሺ𝑛ሻ =
2
ሺ𝒃ሻ𝑇ሺ𝑛ሻ = 𝑇ሺ𝑛/3ሻ + 1 𝑓𝑜𝑟 𝑛 > 1, 𝑇 ሺ1ሻ = 1ሺ𝑖𝑛𝑖𝑡𝑖𝑎𝑙 𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛ሻ ሺ𝑠𝑜𝑙𝑣𝑒 𝑓𝑜𝑟 𝑛 = 3𝑘ሻ
1. Assume 𝑛 = 3𝑘 𝑛
Let 𝑛 = 3𝑘 𝑛 Then, the recurrence becomes:
𝑇ሺ3𝑘ሻ = 𝑇ሺ3𝑘−1 ሻ + 1
𝑇ሺ3𝑘ሻ = 𝑇ሺ3𝑘−1 ሻ + 1
Substitute 𝑇ሺ3𝑘−1 ሻ
𝑇ሺ3𝑘ሻ = 𝑇ሺ3𝑘−2 ሻ + 1
So:
𝑇ሺ3𝑘 ሻ = 𝑇ሺ3𝑘−2 ሻ + 1 + 1 = 𝑇ሺ3𝑘−2 ሻ + 2
Continue expanding:
𝑇ሺ3𝑘 ሻ = 𝑇ሺ3𝑘−3 ሻ + 3
Express in Terms of n:
𝑘
Since 𝑛 = 3 , we have 𝑘 = 𝑙𝑜𝑔3𝑛. Substitute this into the equation:
𝑇ሺ𝑛ሻ = 1 + 𝑙𝑜𝑔3𝑛
Q5. Answer the following questions: (2 Marks)
(a) In solving the string-matching problem, would there be any advantage in comparing
pattern and text characters right-to-left instead of left-to-right?
1. When comparing characters from right-to-left, mismatches are often detected earlier,
especially if the mismatch occurs near the end of the pattern.
2. This allows the algorithm to skip unnecessary comparisons and shift the pattern more
efficiently, reducing the total number of comparisons.
2. Efficient Shifts:
• Right-to-left comparison enables the use of powerful heuristics like the bad character
rule and the good suffix rule (used in the Boyer-Moore algorithm).
• These rules rely on the rightmost mismatched character to compute the maximum safe shift
of the pattern, making the algorithm more efficient.
3. Reduced Comparisons:
• In practice, right-to-left comparison can reduce the total number of character comparisons,
especially when the pattern and text share common prefixes but differ in later characters.
The Boyer-Moore algorithm is a classic example of a string-matching algorithm that uses right-to-
left comparison. Its key features include:
• If mismatches frequently occur near the beginning of the pattern, left-to-right comparison
might be equally efficient.
• Right-to-left comparison requires more complex logic and preprocessing, which can increase
the overhead of the algorithm.
(b) Sort the list Z, X, V, T, Q, N, A, B in alphabetical order by bubble sort.
Initial List:
⮚ Submit the soft copy of your assignment on Blackboard. Do not submit the hard copy
image.
⮚ Submit the Assignment soft copy BEFORE 23:59hrs of the due date.
⮚ If you submit your assignment late. Each day, 10% penalty will incur of the total
marks scored by the students.
COURSE TEACHER
Mohammad Shahnawaz Nasir