DSAIIMidBank
DSAIIMidBank
to
Asymptotic Analysis
CSE 2218 1
UMAMA RAHMAN
Today’s Goals
• Discuss Runtime of Programs.
• Compute and Classify growth of functions.
• Analyze complexity classes for algorithms.
2
DSA
▪ Textbook: Introduction to Algorithms
Cormen, Leiserson, Rivest, and Stein
3
What is an Algorithm?
▪ An algorithm is a sequence of computational steps that solves a
well-specified computational problem.
• An algorithm is said to be correct if, for every input
instance, it halts with the correct output
• An incorrect algorithm might not halt at all on some input
instances, or it might halt with other than the desired
output.
4
What is a Program?
▪ A program is the expression of an algorithm in a programming
language
5
Define a Problem, and Solve It
▪ Problem:
▪ Description of Input-Output relationship
▪ Algorithm:
▪ A sequence of computational steps that transform the input into
the output.
▪ Data Structure:
▪ An organized method of storing and retrieving data.
▪ Our Task:
▪ Given a problem, design a correct and good algorithm that solves
it.
6
Define a Problem, and Solve It
▪ Problem: Input is a sequence of integers stored in an array.
Output the minimum.
Algorithm
INPUT
m, a[i] Data-Structure
7
What do we Analyze?
o Correctness
o Does the input/output relation match algorithm requirement?
o Simplicity, clarity
o Verification and implementation.
o Optimality
o Is it impossible to do better?
8
Running Time
▪ Number of primitive steps that are executed
▪ Except for time of executing a function call most statements roughly
require the same amount of time
▪ y = m * x + b
▪ c = 5 / 9 * (t - 32 )
▪ z = f(x) + g(y)
9
Asymptotic Performance
10
Asymptotic Analysis
▪ Worst case
o Provides an upper bound on running time
o An absolute guarantee of required resources
▪ Average case
o Provides the expected running time
o Very useful, but treat with care: what is “average”?
o Random (equally likely) inputs
o Real-life inputs
▪ Best case
o Provides a lower bound on running time
▪ In general a function
▪ f(n) is O(g(n)) if there exist positive constants c and n0
such that 0 f(n) c g(n) for all n n0
▪ Formally
▪ O(g(n)) = { f(n): positive constants c and n0 such that
0 f(n) c g(n) n n0 }
12
Upper Bound Notation
time
c.g(n)
f(n)
n0 n
14
Lower Bound Notation
time
f(n)
c.g(n)
n0 n
▪ Theorem
▪ f(n) is (g(n)) iff f(n) is both O(g(n)) and (g(n))
16
Asymptotic Tight Bound
c2.g(n)
f(n)
time
c1.g(n
)
n0 n
18
Practical Complexity
Function Descriptor Big-Oh
c Constant O( 1 )
log n Logarithmic O( log n )
n Linear O( n )
n log n n log n O( n log n )
n2 Quadratic O( n2 )
n3 Cubic O( n3 )
nk Polynomial O( nk )
2n Exponential O( 2n )
n! Factorial O( n! )
Practical Complexity
For large input sizes, constant terms are insignificant
Program A with running time TA(n)= 100n
Program B with running time TB(n)= 2n2
TP(n)
TB(n) = 2n2
TA(n) = 100n
5000
Input Size n
50
20
Analysis of Algorithms
▪ What is the goal of analysis of algorithms?
▪ To compare algorithms mainly in terms of running time but also in
terms of other factors (e.g., memory requirements, programmer’s
effort etc.)
▪ What do we mean by running time analysis?
▪ Determine how running time increases as the size of the problem
increases.
Input Size
▪ Input size (number of elements in the input)
▪ size of an array
▪ # of elements in a matrix
proportional to n2.
function.
Example
▪ Associate a "cost" with each statement.
▪ Find the "total cost“ by finding the total number of times each
statement is executed.
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
... ...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 = (c2 + c1) x N + c2
29
More Examples …
▪ n4 + 100n2 + 10n + 50 is O(n4)
▪ 10n3 + 2n2 is O(n3)
▪ n3 - n2 is O(n3)
▪ 2n2 = O(n3)
▪ n2 = O(n2)
▪ 1000n2+1000n = O(n2)
▪ n = O(n2)
▪ constants
▪ 10 is O(1)
▪ 1273 is O(1)
Examples of big-omega
▪ 5n2 = (n)
c, n0 such that: 0 cn 5n2 cn 5n c = 5 and n0 = 1
2
▪ 100n + 5 ≠ (n2)
R→R
O(f) (f)
(f)
32
Examples of theta notation
▪ f(n) = n2/2 – n/2 = (n2)
▪ logn ≠ (n)
the base
log b x = log a x
log a b
1 < 𝑙𝑜𝑔𝑛 < 𝑛 < 𝑛 < 𝑛𝑙𝑜𝑔𝑛 < 𝑛2 < 𝑛2 𝑙𝑜𝑔𝑛 < 𝑛3 < … < 2𝑛 < 3𝑛 < ⋯ < 𝑛𝑛
34
More Examples
▪ For each of the following pairs of functions, either f(n) is O(g(n)), f(n)
is Ω(g(n)), or f(n) = Θ(g(n)). Determine which relationship is correct.
x −1
(x 1)
1
▪ Special case: |x| < 1: x
k =0
k
=
1− x
n
1 1 1
▪ Harmonic series:
k =1 k
= 1 +
2
+ ... +
n
ln n
lg k n lg n
▪ Other important formulas: k =1
n
1
k p = 1p + 2 p + ... + n p
k =1 p +1
n p +1
36
Other Asymptotic Notations
▪ A function f(n) is o(g(n)) if positive constants c and n0 such
that
f(n) < c g(n) n n0
38
Thank You
39
ANALYZING ALGORITHMS
COURTESY: FARIHA TABASSUM ISLAM
Asymptotic Notation
Analyzing Runtime
2
ASYMPTOTIC NOTATIONS
𝑂 𝑔(𝑛) : for sufficiently large input (𝑛 > 𝑛0 ), f(𝑛) is always less than c ∗ 𝑔(𝑛)
Ω 𝑔(𝑛) : for sufficiently large input (𝑛 > 𝑛0 ), f(𝑛) is always greater than c ∗ 𝑔(𝑛)
Θ(𝑔(𝑛)): exact bound of f(n). What does it mean?
3
ASYMPTOTIC NOTATIONS
Assume: 𝑇 𝑛 = 5𝑛3 + 4𝑛 + 1, 𝑔 𝑛 = 𝑛3
𝑇 𝑛 is 𝑂 𝑛3
𝑇(𝑛) is Ω 𝑛3
𝑇(𝑛) is Θ 𝑛3
𝑇 𝑛 is 𝑂 𝑛7
𝑇(𝑛) is Θ 𝑛2
4
ASYMPTOTIC ANALYSIS
𝑇1 𝑛 = 1010 𝑛2 ,
𝑇2 𝑛 = 10−8 𝑛3
If the max value of 𝑛 is 108 then 𝑇2 is cheaper than 𝑇1
However if 𝑛 → ∞, 𝑇1 is cheaper
5
ASYMPTOTIC ANALYSIS
𝑇1 𝑛 = 1010 𝑛2 ,
𝑇2 𝑛 = 10−8 𝑛3
If the max value of 𝑛 is 108 then 𝑇2 The term asymptotic means
is cheaper than 𝑇1 approaching a value (e.g. infinity) or
curve arbitrarily closely (i.e., as some
However if 𝑛 → ∞, 𝑇1 is cheaper
sort of limit is taken).
[Asymptotic]
Therefore, asymptotically, 𝑇1 𝑛 ≤
𝑇2 𝑛
6
ASYMPTOTIC ANALYSIS
7
EXACT COST ANALYSIS
BEST AND WORST CASE ANALYSIS
8
EXACT COST ANALYSIS: EXAMPLE 1
9
EXACT COST ANALYSIS: EXAMPLE 1
10
EXACT COST ANALYSIS: EXAMPLE 2
1 𝑛 c1 . 1
c1 . ( + 2)
5
2 𝑛 c2 . 1
c2 . ( + 1)
5
3 c3 .0 c3 . 1
𝑛
4 c4 . ( + 1) ∗ (log2 𝑛 + 2) c4 . 0
5
𝑛
5 c5 . ( + 1) ∗ (log2 𝑛 + 1) c5 . 0
5
Observe Line 1
value of 𝑖: 𝑛, 𝑛 − 5, 𝑛 − 10, … until
less than 0
𝑛
therefore, runs + 1 times
5
13
EXACT COST ANALYSIS: EXAMPLE 2
Observe Line 4
value of 𝑖: 1, 2, 4, 8, … , 𝒏
value of 𝑖: 20 , 21 , 22 , 23 , … , 𝟐𝒙
2𝑥 = 𝑛
𝑥 = log 2 𝑛
Therefore, inner statements of loop
in line 4 runs log 2 𝑛 + 1 + 1 times
14
EXACT COST ANALYSIS: EXAMPLE 2
The running time of this algorithm therefore belongs to both 𝜴(𝟏) and 𝑶(𝒏𝒍𝒈𝒏)
15
Following 2 statements has same time complexity
for (i=1; i<=n;i++)
for (i=n;i>=1;i--)
3 0 1
𝑛
4 (3 + 1). (log 4 𝑛 + 2) 0
𝑛
5 (3 + 1). (log 4 𝑛 + 1) 0
𝑛 𝑛 𝑛 𝑛
𝑇 𝑛 = 𝑐1 ∗ + 1 + 𝑐2 ∗ + 𝑐3 ∗ ∗ log 4 𝑛 + 1 ∗ 𝑐5 ∗ ∗ log 4 𝑛 18
3 3 3 3
= 𝐴 ∗ 𝑛 + 𝐵 𝑛 ∗ log 4 𝑛 + 𝑐
EXACT COST ANALYSIS: EXAMPLE 3
Observe Line 4
𝑛 𝑛 𝑛 𝑛 𝑛
value of 𝑖: 0 , , 2 , 3 , … , 1( 𝑥 )
4 4 4 4 4
𝑛
=1
4𝑥
𝑥 = log 4 𝑛
Therefore, inner statements of loop
in line 4 runs log 4 𝑛 + 1 + 1 times
19
EXACT COST ANALYSIS: EXAMPLE 3
The running time of this algorithm therefore belongs to both 𝜴(𝟏) and 𝑶(𝒏𝒍𝒈𝒏)
20
21
EXACT COST ANALYSIS: EXAMPLE 4
Asym
ptotic
Derive the running-time equations and
express in "O" notation
22
EXACT COST ANALYSIS: EXAMPLE 4
1 𝑐1 ∗ 𝑛 + 2
2 𝑛2 + 𝑛 + 2
𝑐2 ∗
2
3 𝑛2 − 𝑛
𝑐3 ∗
2
Asym
Derive the running-time equations and ptotic
express in "O" notation
23
How many times line 1 run?
if n=2 then
0, 1, 2, 3 (when 𝑖 = 3, it fails to execute
statements inside the loop)
𝒏+𝟏+𝟏
24
𝑖 #times Line 2 runs
0 0 + 1 // checks 𝑗 ≤ 𝑖 and fails. Inner
statements does not run.
1 0 + 1 // checks 𝑗 ≤ 𝑖 and fails. Inner
statements does not run.
2 1 + 1 // Inner statements run once
3 2 + 1 // Inner statements run twice
4 3 + 1 // Inner statements run thrice
𝐧 𝒏−𝟏 +𝟏
Total 1 + 2 + 3 + ⋯+ 𝑛 − 1 + 1 +⋯+ 1
cost =
𝑛 𝑛−1
+ 𝑛+1
2
= 𝑶(𝒏𝟐 )
25
𝑖 #times line 3 runs
0 0 // does not run.
1 0 // does not run.
2 1 // runs once
3 2 // runs twice
4 3 // runs thrice
𝑛 𝑛−1
Total 1 + 2 + 3 + ⋯+ 𝑛 − 1
cost of =
𝑛 𝑛−1
Line 3 2
= 𝑂(𝑛2 )
26
Line 1: 𝑐1 ∗ 𝑛 + 1 + 1
𝑛2 +𝑛+2
Line 2: 𝑐2 ∗
2
𝑛2 −𝑛
Line 3: 𝑐3 ∗
2
Best case: Ω 𝑛2
Worst case: 𝑂 𝑛2
Therefore, this is Θ(𝑛2 )
The running time of this algorithm therefore belongs to both 𝜴(𝒏𝟐 ) and 𝑶(𝒏𝟐 ), which means it
is in 𝚯(𝒏𝟐 )
27
EXACT COST ANALYSIS: EXAMPLE 5
𝑛 (𝑛2 − 1) + 1
30
EXACT COST ANALYSIS: EXAMPLE 6
32
EXACT COST ANALYSIS: EXAMPLE 7
Exact cost equation:
Cost Times T(n)= (c1+c2+c3+c4)+
m(c4+c5+c6)+ n(c7+c8+c10)+
c1 1 c11
c2 1 = Am+Bn+c
c3 1 Best Case:
c4 m+1 • The first loop will run fully m
c5 m times
• The second loop will run only
c6 m
once
c8 n+1 • T(n)=Am+B+C = O(n)
Worst Case:
c9 n
• First loop runs – m times
c10 1
• Secod loop runs - n times
c11 n • T(n) = Am+Bn+c = O(m+n)
c13 1
33
EXACT COST ANALYSIS: EXAMPLE 7
34
EXACT COST ANALYSIS: EXAMPLE 8
1,2 1 1
3 m+1 1
4 m*(n+1) 1
5 m*n 1
6 0 1
9 1 0
36
PRACTICE
37
QUESTION PATTERNS
Derive the best and worst case running-time equations and express in O
notation.
Derive the exact cost equation and express in O notation
Provide best and worst case examples
38
QUICK EVALUATION 1
39
QUICK EVALUATION 2
https://www.geeksforgeeks.org/practice-questions-time-
complexity-analysis/
40
QUICK EVALUATION 2
https://www.geeksforgeeks.org/practice-questions-time-
complexity-analysis/
41
QUICK EVALUATION 3
43
Recurrence relation
DSA II
UMAMA RAHMAN
Recurrence equation of
time complexity
of
Recursive functions
Example-1
int fib(int n) {
if (n <= 1) return n; O(1)/c (constant)
return fib(n - 1) + fib(n - 2); T(n-1) + T(n-2)
}
𝑇 𝑛 =𝑇 𝑛−1 +𝑇 𝑛−2 +𝑐
Example-2
int factorial(int n) {
if (n <= 1) return 1; O(1) / c (constant)
return n * factorial(n - 1); T(n-1)
}
𝑇 𝑛 =𝑇 𝑛−1 +𝑐
Example-3
1. int binarySearch(int ar[], int l, int r, int x) { Line 2-4: O(1)/c
2. if (l>r) return -1; From line 5 to 8:
3. int mid = l + (r - l) / 2; The array is divided into
4. if (a[mid] == x) return mid; equal 2 halves and based
on the condition we call
5. if (a[mid] > x)
recursive function for any
6. return binarySearch(a, l, mid - 1, x); half array. So, time
7. else complexity, T(n)=T(n/2)
8. return binarySearch(a, mid + 1, r, x);
9. }
𝑛
𝑇 𝑛 =𝑇 +𝑐
2
Example-4
1. int ternarySearch(int a[], int l, int r, int x) { Line 2-6: O(1)/c
2. if (l <= r) { From line 7 to 9:
3. int m1 = l + (r - l) / 3; The array is divided
4. int m2 = r - (r - l) / 3; into equal 3 parts and
based on the condition
5. if (a[m1] == x) return m1; we call recursive
6. if (a[m2] == x) return m2; function for any of the
7. if (x < a[m1]) return ternarySearch(a, l, m1 - 1, x); 3 arrays. So, time
complexity, T(n)=T(n/3)
8. else if (x > a[m2]) return ternarySearch(a, m2 + 1, r, x);
Line 11: O(1)
9. else return ternarySearch(a, m1 + 1, m2 - 1, x);
10. }
11. return -1;
12.}
𝑛
𝑇 𝑛 =𝑇 +𝑐
3
Example-5
1. def func(A,l,h): This function divides the range (h-l+1)
into 4 partition. Then call recursive
2. if l>=h: function for the 1st and last quarters.
3. return Considering, the size of the array n.
4. q=(h−l+1)/4 To solve each quarter it takes T(n/4)
time complexity.
5. func(A, l, l+q−1) Then, in line 7-8: it prints the values
6. func(A, l+3∗q, h) in the range
7. for i in range(l, h, 1): (h-l+1). It takes O(n) time
complexity.
8. print(A[i])
𝑛
𝑇 𝑛 = 2𝑇 +𝑂 𝑛
4
Find recurrence relation
• Consider a modified version of the Merge sort algorithm as follows:
• If the array size is less than or equal to 2, then it sorts the array at constant time.
• Otherwise, it divides the array of size n into 3 subarrays, each with a size of n/3. This division
takes O(n^2) time. Then the algorithm sorts the subarrays recursively, and then merges their
solutions in time O(nlogn). Write a recurrence relation for the running-time T(n) of this
algorithm.
Solving recurrence equation
Solving recurrence relation
• Recurrence relations can be solved using
• Master theorem
• Recursion tree
• Substitute method
Merge sort
Divide step is trivial – just split the list into two equal parts
Work is carried out in the conquer step by merging two
sorted lists
Partition
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9
7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6
7 2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
Merge Sort 9
Execution Example
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6
7 2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
Merge Sort 10
Execution Example
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6
72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
Merge Sort 11
Execution Example
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6
72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
Merge Sort 12
Execution Example
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6
72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
Merge Sort 13
Execution Example
Merge
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6
72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
Merge Sort 14
Execution Example
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6
72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
Merge Sort 15
Execution Example
Merge
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6
72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
Merge Sort 16
Execution Example
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8
72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
Merge Sort 17
Execution Example
Merge
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8
72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
Merge Sort 18
Merge Sort: Running Time
T(n) = (1) if n = 1
2T(n/2) + (n) if n > 1
equivalently
T(n) = b if n = 1
2T(n/2) + bn if n > 1
Draw the recursion tree for the recurrence relation and look for a
pattern:
b if n = 1
T ( n) =
2T (n / 2) + bn if n 2
time
depth T’s size
0 1 n bn
1 2 n/2 bn
i 2i n/2i bn
… … … …
a problem of size n
subproblem 1 subproblem 2
of size n/2 Divide of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
Index: 1 2 3 4 5 6 7 8 9
Array: 22 13 -5 -8 15 60 17 31 47
(1)
(6)
(3) (4)
Index: 1 2 3 4 5 6 7 8 9
Array: 22 13 -5 -8 15 60 17 31 47
(1)
Rmaxmin(1, 9, 60, -8)
(6)
1, 5, 22, -8 6, 9, 60, 17
(2) (5) (7) (8)
1, 2, 22, 13 3, 3, -5, -5
T(n) = (1) if n = 1 or 2
2T(n/2) + (1) if n > 2
equivalently
T(n) = b if n = 1 or 2
2T(n/2) + b if n > 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A 13 -3 -25 20 -3 -16 -23 18 20 -7 12 -5 -22 15 -4 7
maximum subarray
1 -4 -3
What is the brute-
-4 3 -1
force time?
Max! 3 2 5
1 -4 3 0
-4 3 2 1
1 -4 3 2 2
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Brute-Force Algorithm
Example: 2 -6 -1 3 -1 2 -2
sum from A[1]: 2 -4 -5 -2 -3 -1 -3
sum from A[2]: -6 -7 -4 -5 -3 -5
sum from A[3]: -1 2 1 3 1
sum from A[4]: 3 2 4 2
sum from A[5]: -1 1 -1
sum from A[6]: 2 0
sum from A[7]: -2
mid +1
A[mid+1..j]
mid +1 j
A[i..mid]
S[5 .. 5] = -3
S[4 .. 5] = 17 (max-left = 4)
S[3 .. 5] = -8
S[2 .. 5] = -11
S[1 .. 5] = 2
mid =5
1 2 3 4 5 6 7 8 9 10
S[6 .. 6] = -16
S[6 .. 7] = -39
S[6 .. 8] = -21
S[6 .. 9] = (max-right = 9) -1
S[6..10] = -8
maximum subarray crossing mid is S[4..9] = 16
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Divide-and-Conquer Algorithm
FIND-MAXIMUM-SUBARRAY (A, low, high)
if high == low
return (low, high, A[low]) // base case: only one element
else mid = low + high / 2
(left-low, left-high, left-sum) =
FIND-MAXIMUM-SUBARRAY(A, low, mid)
(right-low, right-high, right-sum) =
FIND-MAXIMUM-SUBARRAY(A, mid + 1, high)
(cross-low, cross-high, cross-sum) =
FIND-MAX-CROSSING-SUBARRAY(A, low, mid, high)
if left-sum ≧ right-sum and left-sum ≧ cross-sum
return (left-low, left-high, left-sum)
elseif right-sum ≧ left-sum and right-sum ≧ cross-sum
return (right-low, right-high, right-sum)
else return (cross-low, cross-high, cross-sum)
Initial call: FIND-MAXIMUM-SUBARRAY (A, 1, n)
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Divide-and-Conquer Algorithm
Analyzing time complexity
FIND-MAX-CROSSING-SUBARRAY : (n),
where n = high − low + 1
FIND-MAXIMUM-SUBARRAY
(1) if n = 1,
T (n ) =
2T (n / 2 ) + (n ) if n 1.
Time complexity:
𝑛
𝑇 𝑛 = 2𝑇 +𝑂 1
2
𝑇 𝑛 = 𝑂(𝑛)
44
Conclusion: Divide-and-Conquer
An Activity-Selection Problem
Knapsack Problem
Which items
should I take?
There are n items: the i-th item has value vi and weight wi
Goal:
wixi W and
xivi is maximum
20
$80
---
Item 3 30 +
Item 2 50 50
20 $100
Item 1 30
20 +
10 10 $60
5. w w – xiwi
w – the amount of space remaining in the knapsack (initially w = W)
Item 3 30 $120
50 50 +
Item 2 50
20 $100
Item 1 30
20 + 20 $100
10 10 $60
1
Greedy Algorithms
• Used for optimization problems
• Idea: When we have a choice to make, make the one that
looks best right now
– Make a locally optimal choice in hope of getting a globally optimal
solution
2
Data Compression
• Data compression problem – strings S and S’:
– S -> S’ -> S, such that |S’|<|S|
• Text compression by coding with variable-length code:
– Obvious idea – assign short codes to frequent characters:
“abracadabra”
Frequency table:
a b c d r
Frequency 5 2 1 1 2
Fixed-length code 000 001 010 011 100
Variable-length code 1 001 0000 0001 01
– How much do we save in this case?
3
Prefix code
• Optimal code for given frequencies: 11
0 1
– Achieves the minimal length of the
6 5
coded text 0 1
a
• Prefix code: no codeword is prefix of 4 2
0 1 r
another
2 2
– It can be shown that optimal coding can 0 1
b
be done with prefix code 1 1
c d
4
Huffman Code Problem
• Huffman’s algorithm achieves data
compression by finding the best variable
length binary encoding scheme for the
symbols that occur in the file to be
compressed.
5
Huffman Code Problem
• The more frequent a symbol occurs, the
shorter should be the Huffman binary word
representing it.
6
Overview
• Huffman codes: compressing data (savings of 20% to 90%)
• Huffman’s greedy algorithm uses a table of the
frequencies of occurrence of each character to build up an
optimal way of representing each character as a binary
string
C: Alphabet
7
Example
• Assume we are given a data file that contains only 6 symbols,
namely a, b, c, d, e, f With the following frequency table:
8
Huffman Algorithm - Idea
• Huffman algorithm,
builds the code trie
bottom up. Consider a
forest of trees: A+B
9
Huffman Algorithm Example:
Alphabet: A, B, C, D, E, F
Frequency table:
A B C D E F
10 20 30 40 50 60
11
Algorithm Run:
X 30 C 30 D 40 E 50 F 60
A 10 B 20
12
Algorithm Run:
Y 60 D 40 E 50 F 60
X 30 C 30
A 10 B 20
13
Algorithm Run:
D 40 E 50 Y 60 F 60
X 30 C 30
A 10 B 20
14
Z 90 Y 60 F 60
D 40 E 50 X 30 C 30
A 10 B 20
15
Y 60 F 60 Z 90
X 30 C 30 D 40 E 50
A 10 B 20
16
W 120 Z 90
Y 60 F 60 D 40 E 50
X 30 C 30
A 10 B 20
17
Z 90 W 120
D 40 E 50 Y 60 F 60
X 30 C 30
A 10 B 20
18
V 210
0 1
Z 90 W 120
1
0 0 1
D 40 E 50 Y 60 F 60
0 1
X 30 C 30
0 1
A 10 B 20
19
V 210
A: 1000 0 1
B: 1001
Z 90
C: 101 1
W 120
0 0 1
D: 00
E: 01 D 40 E 50 Y 60 F 60
1
F: 11 0
X 30 C 30
0 1
A 10 B 20
• Encoding:
– Given the characters and their frequencies, perform the
algorithm and generate a code. Write the characters using
the code
• Decoding:
– Given the Huffman tree, figure out what each character is
(possible because of prefix property)
22
Application on Huffman code
• Both the .mp3 and .jpg file formats use
Huffman coding at one stage of the
compression
23
Practice
The characters a to h have the following set of frequencies
a : 1, b : 1, c : 2, d : 3, e : 5, f : 8, g : 13, h : 21
110111100111010
24
a : 1, b : 1, c : 2, d : 3, e : 5, f : 8, g : 13, h : 21
25
Dynamic Programming:
Etymology.
Dynamic programming = planning over time.
Secretary of Defense was hostile to mathematical research.
Bellman sought an impressive name to avoid confrontation.
Fib(5)
Fib(4) Fib(3)
Fib(1) Fib(0)
Fib(n):
if n = 0 or 1 then return F[n]
if F[n 1] = NIL then F[n 1] := Fib(n 1)
if F[n 2] = NIL then F[n 2] := Fib(n 2)
return ( F[n 1] + F[n 2] )
F returns 3+2 = 5,
0 0 Fib(5)
fills in F[5] with 5
1 1
2 1 returns 2+1 = 3,
NIL Fib(4)
fills in F[4] with 3
3 2
NIL
4 3
NIL
Fib(3)
returns 1+1 = 2,
5 5
NIL fills in F[3] with 2
returns 0+1 = 1,
Fib(2)
fills in F[2] with 1
F0 F1 F2 F3 … Fn-2 Fn-1 Fn
Fib(n):
F[0] := 0; F[1] := 1;
for i := 2 to n do
F[i] := F[i 1] + F[i 2]
return F[n]
Can perform application-specific optimizations
e.g., save space by only keeping last two numbers
computed
Greedy solution is
optimal for any amount and “normal’’ set of denominations
The greedy method would not work if we did not have 5¢ coins
For 31 cents, the greedy method gives seven coins (25+1+1+1+1+1+1),
but we can do it with four (10+10+10+1)
The greedy method also would not work if we had a 21¢ coin
For 63 cents, the greedy method gives six coins (25+25+10+1+1+1), but
we can do it with three (21+21+21)
Value 1 2 3 4 5 6 7 8 9 10
Min # of coins 1 1 1
Value 1 2 3 4 5 6 7 8 9 10
Min # of coins 1 2 1 2 1 2 2 2
Value 1 2 3 4 5 6 7 8 9 10
Min # of coins 1 2 1 2 1 2 3 2 3 2
minNumCoins(M-1) + 1
min minNumCoins(M-3) + 1
minNumCoins(M) =
of
minNumCoins(M-5) + 1
minNumCoins(M-c1) + 1
min minNumCoins(M-c2) + 1
minNumCoins(M) =
of …
minNumCoins(M-cd) + 1
1. RecursiveChange(M, c, d)
2. if M = 0
3. return 0
4. bestNumCoins infinity
5. for i 1 to d
6. if M ≥ ci
7. numCoins RecursiveChange(M – ci , c, d)
8. if numCoins + 1 < bestNumCoins
9. bestNumCoins numCoins + 1
10. return bestNumCoins
77
76 74 70
75 73 69 73 71 67 69 67 63
... ..
70 70 70 70 70
.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
We Can Do Better
1. DPChange(M, c, d)
2. bestNumCoins0 0 Running time: O(M*d)
3. for m 1 to M
4. bestNumCoinsm infinity
5. for i 1 to d
6. if m ≥ ci
7. if bestNumCoinsm – ci+ 1 < bestNumCoinsm
8. bestNumCoinsm bestNumCoinsm – ci+ 1
9. return bestNumCoinsM
0 0 1 2 1 2 3 2
0 0 1 2 3 4 5 6
0 1
0 1 0 1 2 1 2 3 2 1
0 1 2 3 4 5 6 7
0 1 2
0 1 2
0 1 2 1 2 3 2 1 2
0 1 2 1 0 1 2 3 4 5 6 7 8
0 1 2 3
0 1 2 1 2 3 2 1 2 3
0 1 2 1 2 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4
0 1 2 1 2 3
c = (1, 3, 7)
0 1 2 3 4 5 M=9
Item 3 30 $120
50 50 50 +
Item 2
20 $100
Item 1 30
20 + 20 $100
10 10 $60
The best subset that has the total weight w, either contains item i
or not.
First case: w <wi. Item i can’t be part of the solution, since if it
was, the total weight would be > w, which is unacceptable.
Second case: w >= wi. Then the item i can be in the solution,
and we choose the case with greater value.
0 0 0 0 0 0 0 0 0 0 0 0
0 first
0 second
i-1 0
i 0
0
n 0
0 0 0 0 0 0 0 0 0 0 0 0
0
0
i-1 0
i 0
0
n 0
1 0 0 12 12 12 12
• Item 2
2 0 10 12 22 22 22
3 0 10 12 22 30 32 • Item 1
0
4 10 15 25 30 37
• Start at P(n, W)
• When you go left-up item i has been taken
• When you go straight up item i has not been taken
0-1 Knapsack Algorithm (DP)
for w = 0 to W
P[0, w] = 0
for i = 0 to n Running time: O(n*W)
P[i, 0] = 0
for w = 0 to W
if wi <= w // item i can be part of the solution
if (vi + P[i-1, w-wi] > P[i-1, w])
P[i, w] = vi + P[i-1, w- wi]
else
P[i, w] = P[i-1, w]
else P[i, w] = P[i-1, w] // wi > w
1. (a) Perform worst case runtime analysis of the following C functions and express in Θ notation. [2×5 = 10]
(b) Which of the sorting algorithms - Merge-Sort and Heapsort is better in terms of extra space usage besides
the provided input array? Explain very briefly. [2]
(c) Write down the recursion for the length of a Longest Common Subsequence (LCS) of the two strings
A = {a1 , a2 , . . . , am } and B = {b1 , b2 , . . . , bn }. Modify it to obtain a new recursion of length for the LCS
of 3 strings A = {a1 , a2 , . . . , am }, B = {b1 , b2 , . . . , bn } and C = {c1 , c2 , . . . , cp }. [3]
2. (a) Using recursion tree method find out a good asymptotic upper bound on the following recurrence:
T (n) = 3T (n/2) + n. [12]
(b) In a ternary heap H where the children of H[i] are H[3*i+1], H[3*i+2] and H[3*i+3] and the parent of
H[i] is H[i/3], what would be the runtime complexity of HEAPIFY(H,i,heapsize)? [3]
3. (a) Given below is the pseudocode of Bubble-Sort algorithm to sort an input array in non-decreasing order.
Perform runtime analysis to find the cost function in best case scenario and express the function in
Big-Oh notation. [10]
(b) Suppose that for a problem X with size n, a divide-and-conquer algorithm solves it as follows: If the
problem size is less than or equal to c, then it solves it at constant time. Otherwise, it divides the problem
into b subproblems, each with a size of n/d. This division takes time θ(log n). Then the algorithm solves
the b number of subproblems recursively, and then combines their solution at time θ(n log n). Design a
recurrence relation for the running-time T (n) of this algorithm. [3]
(c) Both the algorithmic paradigms: Divide-and-Conquer and Dynamic Programming solve a problem by
breaking it into smaller problem instances, and by solving them. What is the fundamental difference
between these two paradigms? [2]
4. (a) ”In order for radix sort to work correctly, the digit sorts (sorting values by some digit i) must be stable”
- justify this claim by providing appropriate examples. [5]
(b) Is it always possible to sort an array of positive integers belonging to any possible range using counting
sort? Why does the Ω(nlgn) lower bound not apply to counting sort? [2+3]
(c) What does the Find-Maximum-Subarray algorithm return when all the array elements are negative? [2]
(d) AlgoCoins are used by the people of AlgoLand to make daily purchases. You are given an abundant
supply of 23, 16, 9, and 1 AlgoCoins. You need to calculate the minimum number of AlgoCoins needed
to make up a specific amount. Give an example where the greedy approach does not provide an optimal
solution. [3]
6. (a) Demonstrate the simulation of the Find-Minimum-Subarray algorithm on the following sequence of num-
bers: < −2, 3, −2, 4, 6, −5, 2, −4 >. Clearly demonstrate the recursion-tree of the execution of the algo-
rithm. Besides each node of the tree, mention the following four values: solutions from the left and right
children, minimum crossing sum, and the minimum subarray sum that is to be returned to the parent
node. Note that, you are required to execute the Find-Minimum-Subarray algorithm, which is very
much similar to the Find-Maximum-Subarray algorithm. [12]
(b) While removing the maximum priority element from a Max-Priority Queue data structure, we replace
the root (index 1) of the corresponding heap with the last element of the heap, decrement the heapsize by
1, and then execute the Max-Heapify algorithm on index 1. Why do we choose the last element instead
of any other element? [3]
7. (a) Design a max-heap with height 2 and having the maximum possible number of elements. All the node
values need to be positive. Then replace the value at the root with −1, and execute the Max-Heapify
algorithm on this node. Clearly demonstrate each step. [2+3]
(b) Use a dynamic programming algorithm to find the length of the LCS of the following strings:
X = {a, b, c, d, d, c, b}, Y = {a, c, b, d, c, b}. You must show all the steps of your simulation. [10]
8. (a) You are given a rod of length n which you must cut into pieces and sell to obtain the maximum profit,
rn . The price pi of a rod of length i and the cost ci of cutting a rod at length i is provided in the array
p and c. When you calculate the profit of cutting and selling a rod at length i, you should consider the
following things:
2
i. The profit obtainable from the two parts
ii. The cost of cutting the rod at length i
For example, if you have a rod of length 8 and you wish to cut it at length 1. Assume that the profit
obtainable from the two parts are 4 Tk and 5 Tk respectively and the cost of cutting the rod at length
1 is 2 Tk. Then the total profit obtainable from cutting the rod at length 1 is Tk (4 + 5 – 2) = Tk 7.
Modify any one of the pseudocodes below, using dynamic programming to calculate the highest profit
obtainable from cutting the rod. What is the time complexity of this algorithm? [7+2]
(b) When the Merge-Sort algorithm is run on a sequence of n items (n > 1), if the running-time of the
algorithm is T (n), what are running-times of each of these three steps: divide, conquer and combine? [3]
(c) Suppose that for a Heap data structure, the heap size is n, and you need to determine if the i’th node
is a leaf or not. How can you check this in constant time? [3]
3
United International University
CSI 227: Algorithms, Summer 2017
Mid Term Exam
Total Marks: 90, Time: 1 hour 45 minutes
1. (a) Perform worst case running-time analysis of the following algorithms and express in O nota-
tion. [6 + 4]
(b) What do you understand by constant-time costs? Provide examples of a code fragment with constant-
time cost, and another not having a constant-time cost. [2+3]
2. Using the recursion tree method, determine a good asymptotic upper bound on the following recurrence:
n
T (n) = 2T ( ) + O(n). Show details of your calculation. [15]
3
3. (a) A Dynamic Programming algorithm for the classical Coin Change problem is provided at figure 1,
where given an amount n and m types of coins C1 , C2 , ..., Cm , you have to minimize the number of coins
to build up the amount n. Now, suppose that, each coin type Ci has a weight Wi . Modify the algorithm
such that now you have to minimize the total weight of coins to build the amount n. [9]
(b) Suppose that you have got a set of the following activities, with the given start and end times for the
Activity Selection problem:
{[2, 4], [1, 5], [9, 12], [6, 10], [1, 16], [11, 15], [7, 8]}
What will be the solution sets returned by two greedy algorithms: one using the end times as the
choosing criteria, another using activity lengths? Which one is optimal here? [2.5 + 2.5 + 1]
4. (a) Provide a Divide-and-Conquer algorithm to find the count of non-zero elements in an input array.
What would be the costs of each of the three steps of divide-and-conquer? [6+3]
(b) What is the basic difference between the algorithmic paradigms - greedy algorithms and dynamic
programming? [3]
(c) For the classical Rod cutting problem, design an example case where a greedy algorithm fails to find
an optimal solution. What are the optimal and the greedy solutions for your example? [3]
5. (a) Suppose that you are the owner of an Internet service provider company. The maximum bandwidth
capacity that you can supply is max capacity. There are n customers who are willing to acquire your
Internet service. Each customer has two attributes: the bandwidth Bi that he/she wants, and the
payment Pi that he/she will pay for that. Provide a Dynamic Programming algorithm to find out
the maximum total profit that you can make by providing bandwidths within your max capacity. [12]
(b) Design a min-heap with height 3 and the minimum possible number of elements. All the elements must
be negative here. [3]
6. (a) A dynamic programming (DP) algorithm to the classical Rod-Cutting problem is provided at figure
2. Simulate the steps of tabulation of the DP memoization table, along with the recursion tree
generated by the DP algorithm, for initial rod length 6 and the profit table P = {1, 2, 3, 3, 4, 5}. [9]
(b) Propose an algorithm to convert a provided min-heap to a max-heap. [6]
8. (a) What is the depth of the Merge-Sort algorithm’s recursion tree, when sorting an n-element array in
non-increasing order? What is the space complexity of Merge-Sort? Why? [2.5 + 2.5]
(b) At a Min-Priority-Queue of size n and unique elements, what are the possible indices of the maximum
element? [3]
(c) Suppose that you have a Max-Heap with n elements. Is it guaranteed to remain a max-heap if you
perform the following changes on it? Explain briefly. [2 + 2 + 3]
(i) increase the value of the root; (ii) decrease the value of a leaf; (iii) decrease the value at index 2,
and n ≥ 5.
Figure 4: SEARCH
Figure 3: DUMMY
2
United International University
CSI 227: Algorithms, Fall 2017
Mid Term Exam
Total Marks: 90, Time: 1 hour 45 minutes
1. (a) Derive the exact-cost equation for the running-time of the algorithm at Fig. 1, and express it in the
big-Oh (O) notation. [6]
(b) Demonstrate the full working process of the Divide-and-Conquer algorithm for the Find Minimum
Subarray problem, on the following array: [−2, 1, 0, −5, 3, −4, 7, −5]. [9]
Figure 1: Q. 1a Figure 2: Q. 4c
2. (a) Suppose you are developing a greedy algorithm for the Rod Cutting Problem where each cut is
greedily made using the maximum price. Now design a suitable price array for rod length 6 on which
the algorithm will not provide an optimal solution. What is the optimal solution for your example? [3]
(b) You are going to build a new computer system that requires t watts of power to run. But there might not
be power chips available at the market with exactly t watts. There are n types of power chips available,
with the following capacities: P1 , P2 , ..., Pn . You want to assemble the t watts using the minimum
number of power chips. Propose a Dynamic Programming algorithm to solve this problem. [12]
3. (a) Design an algorithm that provided two parameters n and m, runs in time O(n + m × logm). [7]
(b) Add memoization to the algorithm in Fig. 3 to get its Dynamic Programming version. Then calcu-
late CATALAN(3) using the memoized algorithm. You must demonstrate the recursion-tree generated.
[3+5]
Figure 4: Q. 7a
Figure 3: Q. 3b
4. (a) Trump coins are used by the people of the Trump land for everyday transactions. Only the following
coins are available at this system: 1, 7, 12, 25. Provide an example of an amount where the greedy
strategy for the Coin-Change problem does not provide an optimal solution. You must mention the
optimal solution, and the greedy solution. [3]
(b) Suppose that you run a server lending business, where you have got the the following server-use requests
for the next month. Each request is at the format: [start date, end date]. A server can be used by
at most one user at a time. Using a greedy algorithm, find out the minimum number of servers
required to satisfy all the requests. {[6, 10], [3, 5], [3, 8], [4, 9], [1, 7], [1, 2], [3, 7], [9, 11]} [6]
(c) Derive the exact-cost equation for the running-time of the algorithm at Fig. 2, and express it in the
big-Oh (O) notation. [6]
5. (a) Consider this modified version of the Merge sort algorithm as follows: divide the provided array of
n
size n into three subarrays of sizes roughly , sort each of these three subarrays recursively, and then
3
combine the three sorted subarrays in time O(n). Answer the following:
i. Design a recurrence relation for the running-time T (n) of this algorithm. [3]
ii. Using the recursion-tree method, solve the recurrence and derive its running-time in the big-Oh
(O) notation. [7]
(b) Given the arrival and the departure times of 6 trains for a railway platform, find out the maximum
number of trains that can use that platform without any collision, using a greedy algorithm. There
must exist at least 10 minutes of safety break between the departure of one train and arrival of a
next one. {[1000, 1030], [840, 1030], [850, 1040], [1700, 2000], [800, 835], 1300, 1800]} [5]
6. (a) Take a look at the algorithm at Fig. 5. Now provide both the best-case and worst-case examples
of the arrays A and B for |A| = n = 4 and |B| = m = 5, and val = 10. Also derive the running-time
complexities for both the cases in the big-Oh (O) notation. [3 + 3 + 5]
(b) For the Divide-and-Conquer algorithm for the Find Maximum Subarray problem, if the cost
for finding the maximum crossing sum is O(n2 ), then what will be the recursive equation for the
running-time of the algorithm? [4]
Figure 5: Q. 6a
Figure 6: Q. 8a
7. (a) Using the recursion-tree method, find out an asymptotic upper bound in the big-Oh (O) for the
recurrence in Fig. 4. [12]
(b) What is the fundamental difference between Greedy Strategies and Dynamic Programming? [3]
8. (a) A Dynamic Programming algorithm for the classical 0/1 Knapsack problem is provided in Fig.
6. Consider the following added restrictions to the problem: if you do not take the ith item, you have
to pay a penalty Pi , and if you take it, you have to pay a transportation fee Ti . Update the algorithm
such that it can solve this modified problem. [7]
(b) Provide a Divide-and-Conquer algorithm to find the count of negative (< 0) elements in an input
array A. Also, mention the running-times of each of the three steps of the divide-and-conquer strategy
in your algorithm. [5 + 3]
2
United International University
CSI 227: Algorithms, Spring 2018
Mid-Term Exam
Total Marks: 90, Time: 1 hour 45 minutes
Answer all the questions from section A, and any 3 out of the 5 in section B. (6 × 15 = 90).
Section A
1. (a) Derive the best and worst case running-time equations for the function in Fig. 1 and express in O
notation. [8]
(b) What are the 2 properties that make a problem a good candidate for the greedy algorithm? [7]
Figure 2: Q. 5(a)
Figure 1: Q. 1(a)
2. Propose a divide-and-conquer algorithm to find the count of even numbers in an input array. Then using
the recursion tree method, determine a good asymptotic upper bound on your solution. [5+10]
3. Harry was bored and hungry sitting idly in Hogwarts express. He requires exactly e more units of energy
to reach Hogwarts. Fortunately, Hermione’s knapsack contains m different types of potions of unlimited
supply, serving energy of E1 , E2 , . . . , Em units. Harry does not want to seem greedy so he decided to pick the
minimum number of potions to serve his requirement of e energy units. Propose a dynamic programming
Section B
4. (a) Provide the recurrence relation for the divide-and-conquer function in Fig. 3 in terms of input array
Figure 3: Q. 3(a)
(b) What is the optimal-substructure of the activity selection problem? Briefly explain with an example.
[5]
(c) Provide separate examples with exactly 4 activities where the greedy algorithm outputs sub-optimal
solution if a greedy choice is made by: (i) shortest interval, (ii) earliest start time. [5]
5. (a) For the function in Fig. 2 provide both best and worst case examples of A with size, n=8. [5]
(b) Assume that in the context of classic 0/1 knapsack problem, the capacity of the knapsack is C = 4.
The store has 4 different products of values V = {8, 7, 5, 4} with weights W = {2, 2, 1, 1}. Populate the
memoization table for this input set to find the maximum value a thief can acquire. [10]
6. (a) Given an array A = {3, −4, 2, −3, −1, 7, −5}, find the minimum sum continuous sub-array using
divide-and-conquer approach. You must show the recursion tree and clearly mention left, right and
(b) For the function in Fig. 4 derive the exact cost equation and express in O notation. [5]
Figure 4: Q. 6(b)
Figure 5: Q. 8(a)
7. (a) For activity selection problem we choose the earliest finished activity as our greedy choice. Prove that
with this greedy choice, activity selection problem shows both “Greedy Choice Property” and “Optimal
(b) What is the fundamental difference between divide-and-conquer and dynamic programming? [5]
(c) What are the resources that have trade-off relations in dynamic programming? [3]
8. (a) Add memoization the function in Fig. 5 to get its dynamic programming version. Then demonstrate
(b) Using the substitution method, determine a good asymptotic upper bound on the following recurrence:
2
United International University
CSI 227: Algorithms, Summer 2018
Mid-Term Exam
Total Marks: 90, Time: 1 hour 45 minutes
1. (a) Derive the running-time equations for the function in Fig. 1 and express in O notation. [5]
(b) For the function in Fig. 2 provide both best and worst case examples of A and B with size, len(A)=8.
Also, derive the worst case run-time and express in O notation. [3+3+4]
(c) Using the recursion tree method, determine a good asymptotic upper bound on the following recur-
2. (a) Propose a divide-and-conquer algorithm to find the count of prime digits in an input integer n. Your
m
algorithm should divide a problem of size m to 2 sub-problems of size 2. What are the time-complexities
(b) Given an array A = {−2, 1, 0, −5, 3, −4, 7, −5}, find the minimum sum continuous sub-array using
divide-and-conquer approach. You must show the recursion tree and clearly mention left, right and
3. (a) Provide an example where the greedy strategy fails for 0/1 knapsack problem for n = 4 items with the
(b) Given the arrival and the departure times of n trains for a railway platform, provide a greedy algorithm
to find out the maximum number of trains that can use that platform without any collision. Note that,
there must exist at least 10 minutes of safety break between the departure of one train and arrival of a
given an amount n and m types of coins C1 , C2 , ... , Cm , you have to minimize the number of coins to
build up the amount n. Now, suppose that, each coin type Ci has a weight Wi . Modify the algorithm
such that now you have to minimize the total weight of coins to build the amount n. [10]
Figure 3: Q. 4(b)
(b) Assume that in the context of classic 0/1 knapsack problem, the capacity of the knapsack is C = 5.
The store has 5 different products of values V = {8, 7, 5, 4, 9} with weights W = {2, 2, 1, 1, 2}. Using
dynamic programming solve the problem for this input set to find the maximum value a thief can
acquire. You must show the corresponding recursion-tree and the memoization table produced for your
solution. [15]
2
United International University
Department of Computer Science and Engineering
Course: CSI 227 Algorithms Trimester: Spring 2019
Midterm Exam Marks: 90 Time: 1 hour 45 minutes
1. a) Analyze both best and worst case time-complexities of the algorithm of Figure 1. 7+7
b) Show both best and worst case examples of the arrays A and B with n = 6 and m = 7 for 3+3
the algorithm of Figure 1.
2. a) Propose a divide-and-conquer algorithm to find the number of even numbers in an array of n integers. 6+4
If the recurrence equation for the time-complexity of the algorithm is
T(n) = 2T(n /2) + O(1) with T(1) = O(1)
then using the recursion tree method, determine a good asymptotic upper bound of the algorithm.
b) Consider a modified version of the Merge sort algorithm as follows: if the array size is less 3
than or equal to 2, then it sorts the array at constant time. Otherwise, it divides the array of
size n into 3 subarrays, each with a size of n/4. This division takes O(log n) time. Then the
algorithm sorts the subarrays recursively, and then merges their solutions at time O(n).
Write a recurrence relation for the running-time T(n) of this algorithm.
c) Using the substitution method, find upper bound on the following recurrence. 7
T(n) = 2T(n /4) + O(n) with T(1) = O(1)
Page 1 of 2
3. a) Write the principles of Greedy method and Divide-and-Conquer method for solving a problem. 2+3
b) Provide an example where the greedy strategy fails for the 0/1 knapsack problem. 4
Explain why the greedy strategy fails for the 0/1 knapsack problem, but works for the
fractional knapsack problem.
c) Provide separate examples with exactly 4 activities where the greedy algorithm outputs 3+3
sub-optimal solution if a greedy choice is made by:
i) earliest start time, and
ii) shortest interval.
d) Suppose that you have got a set of the following activities, with the given start and end 5
times for the Activity Selection problem:
{ [1, 6), [2, 5), [9, 15), [6, 9), [11, 15), [3, 6) }
Find two optimal solutions of the problem.
4. a) i) Write a Dynamic Programming algorithm for the classical Coin Change problem. 5+5
ii) Happy coins are used by the people of the Happyland. Assume that only the following coins
are available at this land: $1, $7, $11, and $15.
By using the Dynamic Programming method, find the minimum number of coins that add
up to a given amount of money of $20.
b) Solve the following instance of the knapsack problem with knapsack capacity 7 for 7+3
i) 0/1 knapsack problem
ii) fractional knapsack problem
Page 2 of 2
United International University (UIU)
Dept. of Computer Science & Engineering (CSE)
Mid Term Total Marks: 20 Fall-2020
Course Code: CSI 227 Course Title: Data Structure and Algorithms II
Time: 1 hour for answering. Another 15 minutes for submitting.
Any examinee found adopting unfair means will be expelled from the trimester / program
as per UIU disciplinary rules.
There are FOUR questions. Answer all of them. Figures in the right-hand margin indicate full marks.
1 Derive the exact-cost equation for the running-time of the algorithm at Figure 1. [3+2]
Provide both the best-case and the worst-case time-complexities in Big-Oh(O)
notation.
Figure 1
2 Using dynamic programming, solve the following instance of the 0/1 knapsack [5]
problem with knapsack capacity 7 for five items (weight, value) – (2, $100), (4,
$60), (3, $80), (7, $250), (1, $200). You must show each step in your calculation
of finding maximum value and selection of items.
3 Suppose, A problem X can be divided into four subproblems each of size (n/4), [5]
each of the problem can be solved recursively in time T(n/4) respectively. The cost
of dividing the problem and combining the results of the subproblems is (n).
Formulate the recurrence relation and solve it using the iteration method. [
Assuming that, T(1)=1]
4 Given the arrival and the departure times of trains for a railway platform where [ 2.5
each one is at the format: [arrival time, departure time) +2.5]
(a) Provide a greedy algorithm to find out the maximum number of trains that
can use that platform without any collision. Note that, there must exist at
least minutes of safety break between the departure of one train and the
arrival of a next one. [do not write pseudocode, explain your algorithm in
3-4 lines]
(b) If = and = and the train schedules are {[8, 12), [6, 9), [11, 14), [2,
7), [1, 7), [12, 20), [7, 12), [13, 19)}, then find the maximum number of trains
that can use the platform without any collision using your algorithm.
Clearly write which schedules you chose.
1
United International University (UIU)
Dept. of Computer Science & Engineering (CSE)
Mid Exam Year: 2021 Trimester: Summer
Course: CSE 2217/CSI 227 Data Structure and Algorithms II,
Total Marks: 20, Time: 1 hour, Upload & Download: 15 min
(Any examinee found adopting unfair means will be expelled from the trimester /
program as per UIU disciplinary rules)
There are THREE questions. Answer all of them. Figures in the right-hand margin
indicate full marks.
1. a) What does it mean when we say that an algorithm X is asymptotically more efficient than Y? [1]
b) Prove that divide and conquer method will give maximum sum subarray in O(nlogn) time [2]
when n>1
c) Suppose you have to choose among two algorithms to solve a problem: [2]
o Algorithm X solves an instance of size n by recursively solving four instances of size
n/2, and then combining their solutions in time O(n).
o Algorithm Y solves an instance of size n by recursively solving four instances of size
3n, and then combining their solutions in time O(1).
Which one is preferable, and why?
d) Analyze the time complexity of the following algorithm and express in big-O notation: [2]
// dip chips
Println("Pizza is here!")
// open box
1
for pizza := 0; pizza <= pizzaBox; pizza++ {
// slice pizza
Println("Pizza is gone.")
2. a) Find an optimal solution to the 0/1 knapsack instance of n = 4, W = 5, (v1, v2, v3, v4) = (50, [3+2]
30, 35, 60), and (w1, w2, w3, w4) = (2, 2, 1, 3).
Also find the optimal solution considering the fractional knapsack problem for the same instance.
b) Consider the following five symbols present in a file along with their frequencies: [2+1]
a b c d e
3 1 1 3 2
Construct the tree for finding codes from the Huffman encoding algorithm and write the optimal
encoding codes for each symbol. You must show the tree constructed by the algorithm and
for each intermediate constructed node mention the creation sequence number. For
example, if you form a node t with a and b at first mention 1 beside the node t and then if form a
node s with c and d next mention 2 beside the node s.
3. a) Discuss how the problem of computing the Fibonacci sequence demonstrates overlapping [2]
subproblems. Also, discuss how dynamic programming helps optimizing the computation.
b) How can you detect cycle in a directed graph using DFS? [1]
c) Maloins are used as coins in the Maliceland for daily transactions. In its monetary system, [2]
Maliceland has the following coins available: {1, 7, 12, 25}. Show an example of an amount
where the greedy strategy for the Coin-Change problem does not provide an optimal solution.
2
United International University (UIU)
Dept. of Computer Science & Engineering (CSE)
Midterm Exam Total Marks: 30 Fall 2021
Course Code: CSE 2217 Course Title: Algorithms
Time: 1 hour 45 minutes
Any examinee found adopting unfair means will be expelled from the trimester / program
as per UIU disciplinary rules.
There are FOUR questions. Answer all of them. Show full simulation/tabulations wherever
necessary. Figures in the right-hand margin indicate full marks.
1. (a) Derive the best-case and the worst-case running-time equations for the [4]
following function has_common and express those in Big-Oh (O) notation. Also
provide the best-case and the worst-case examples of the arrays A and B with n =4
and m=5 for the function has_common.
function has_common(A, B):
1. m = A.length
2. n = B.length
3. for i=1 to m do
4. for j=1 to n do
5. if A[i] == B[j] then
6. return true
7. end
8. end
9. return false
[2+2]
(b) Derive the exact-cost equation for the running-time of the following function
bubble_sort and prove that it is in ( ).
function bubble_sort(A):
1. n = A.length
2. for i=1 to n-1 do
3. for j=1 to n-i do
4. if A[j] > A[j+1] then
5. swap A[j] and A[j+1]
6. end
7. end
8. for i=1 to n do
9. print(A[i])
10. end
2. (a) Determine a good asymptotic upper bound for the following recurrence [2]
equation: ( ) = 2 ( ) + (1), where (1) = (1).
(b) Consider a modified version of the Merge sort algorithm as follows: [2]
If the array size is less than or equal to 2, then it sorts the array at constant time.
Otherwise, it divides the array of size n into 3 subarrays, each with a size of /3 .
This division takes ( ) time. Then the algorithm sorts the subarrays recursively,
and then merges their solutions at time ( ). Write a recurrence relation for the
running-time ( ) of this algorithm.
1
3. (a) Given an array A = {-2, 8, -4, 2, -6, 4}, find the maximum-sum continuous sub- [3]
array using divide-and-conquer approach. You must show the recursion tree and
clearly mention left, right and crossing sum for each tree node.
(b) What is the activity selection problem? Is it true that the activity selection [2]
problem has one unique optimal solution?
(c) “Data encoded using Huffman coding is uniquely decodable”- is the statement [1]
true or false? Justify your answer.
(d) Information to be transmitted over the internet contains the following characters
with their associated frequencies as shown in the following table:
Character a e l n o s t
Frequency 45 65 13 45 18 22 53
4. (a) What is Optimal Substructure property? How Dynamic Programming differs [1]
from Divide-and-Conquer problems in terms of subproblems?
(b) Suppose, on a cold winter morning, you went to Starbucks to buy a hot [3]
chocolate coffee. After paying the bill, the cashier needed to give you a total
change of 7 cents. There is a huge supply of 1 cent, 2 cents, and 5 cents coins
available in the cashbox. You don’t want to carry many coins, so you asked her to
return the change using a minimum number of coins. Determine how many coins
she should return in this scenario by applying the Dynamic Programming Process?
(c) Determine the maximum profit for the 0-1 Knapsack problem given in the [4]
following table using Dynamic Programming.
Knapsack Weight: 8 kg
Objects (1) (2) (3) (4)
Weight (kg) 5 4 6 3
Profit ($) 11 10 12 9
Also show that Greedy Strategy for the 0-1 Knapsack problem fails to achieve the
maximum profit for the same objects.
2
United International University (UIU)
Dept. of Computer Science & Engineering (CSE)
Midterm Exam Total Marks: 30 Spring 2022
Course Code: CSE 2217 Course Title: Algorithms
Time: 1 hour 45 minutes
There are FOUR questions. Answer all of them. Show full simulation/tabulations wherever
necessary. Figures in the right-hand margin indicate full marks.
1. (a) Derive the best-case and the worst-case running-time equations for the [2+2]
following function sum1 and express those in Big-Oh (O) notation. Also provide
the best-case and the worst-case examples of the arrays A and B with n =4 and m=5
for the function sum1.
function sum1(A, B):
1. m = A.length
2. n = B.length
3. s = 0; i = 1;
4. while i<=m do
5. s = s + A[i]
6. i = i+1
7. end
8. for j=1 to n do
9. if B[j]<0 then
10. return s
11. s = s + B[j]
12. end
13. return s
(b) Derive the exact-cost equation for the running-time of the following function
and prove that it is in 𝑶(𝒏). [2+2]
2. (a) Given an array A = {-2, 3, -1, 2, -4, 4}, find the maximum-sum continuous [3]
subarray using divide-and-conquer approach. You must show the recursion tree and
clearly mention left, right and crossing sum for each tree node.
(b) Given an array of integers A = {1, 3, -5, 2, -3, -2}, find the Maximum and [2]
Minimum using divide-and-conquer. Show the necessary steps to support your
answer.
(c) Explain with an example how merge sort is performed using divide-and- [2]
1
conquer.
3. (a) You are given the following table containing symbols and their frequencies: [3+1]
Symbol A B C D +
Frequency 40 10 20 15 15
I. Build the Huffman code tree and find the codeword for each character.
II. Decode 100010111001010 using the Huffman code that you generated.
(b) You are given the arrival and the departure times of eight trains for a railway
platform, and each one is in the format: [arrival time, departure time). Only one [3]
train can use the platform at a time. Suppose that you have got the following train-
use requests for the next day.
{ [8, 12), [6, 9), [11, 14), [2, 7), [1, 7), [12, 20), [7, 12) , [13, 19) }
Find the maximum number of trains that can use the platform without any collision
by using earliest departure time.
4. (a) What is Optimal Substructure? Show at least 2 valid differences between the [1]
Greedy approach and the Dynamic Programming approach.
(b) Suppose, Crimson Cup Coffee Shop charges 50 BDT (Bangladesh Taka) for [3]
each cup of small cream latte with an additional vat of 3% for any purchase.
You bought 2 cups of small cream latte and gave the cashier 110 taka. The
cashier has got a huge supply of 1 taka, 2 taka, and 5 taka coins available in the
cashbox. You don’t want to carry many coins, so you asked her to return the
change using a minimum number of coins.
Determine how many coins she should return in this scenario by applying the
Dynamic Programming Approach.
(c) Two infamous thieves, Denver and Nairobi, planned to rob the famous Louvre [2+2]
Museum. Before the scene, they both agreed on the fact on the fact that none of
them will break any item as all the items in the Louvre are too precious, and
taking a fraction of any item won’t sell in the black market. If it fits in the bag as a
whole, they will take it, otherwise, leave it as it is.
Both of them arrived at the Louvre with an empty knapsack weighing a total of 5
kg. Despite the fact that both thieves are experts in their fields, they take slightly
different approaches.
Denver believes he will use a Dynamic Programming Approach to rob the items
in the most efficient manner possible. Nairobi, on the other hand, believes that if
she chooses the Greedy Approach, she will make the most money.
I. What is the maximum profit Denver can make using his strategy?
II. Does Nairobi’s belief remain valid after the robbery? Prove it.
2
Any examinee found adopting unfair means will be expelled from the trimester / program
as per UIU disciplinary rules.
There are FOUR questions. Answer all of them. Show full simulation/tabulations wherever
necessary. Figures in the right-hand margin indicate full marks.
1. (a) Derive the best-case, and the worst-case running-time equations for the following [4]
function 𝑓𝑎𝑣𝑜𝑢𝑟𝑖𝑡𝑒𝑆𝑢𝑚 and represent using Asymptotic Notation.
(b) Derive the exact-cost equation for the running-time of the following function and
show that the time complexity is 𝑂(𝑛 𝑙𝑜𝑔4𝑛). [4]
2. (a) Express the time complexity of Maximum-sum Subarray problem when Brute [2]
Force method is applied to solve it. Explain why the Divide-and-Conquer approach
can improve the complexity.
1
(b) Given an array of integers A = {-2, 3, -2, 4, -1, 2, 1}, find the Maximum-sum [3]
continuous Subarray using divide-and-conquer. You must show the recursion tree and
clearly mention left, right and crossing sum for each tree node.
(c) Suppose we have two sorted sub-arrays: L: 1, 5, 8, 9, 10, 15 and R: 4, 6, 7, 11, 13, [2]
14. Perform the procedure Merge on L and R to find the final sorted array A. Show
each step of your answer and the number of comparisons required in each step.
3. (a) Given the arrival and the departure times (in minutes) of 8 trains for a railway [2]
platform, find out the maximum number of trains that can use that platform without
any collision, using a greedy algorithm. There must exist at least 10 minutes of safety
break between the departure of one train and arrival of the next one.
[1000, 1030], [840, 1030], [850, 1040], [1700, 2000], [800, 835], [1300, 1800], [1500,
1650], [1200, 1380]
Explain your strategy very briefly and show detailed calculations. No need to write
pseudocode.
(b) “Data encoded using Huffman coding is uniquely decodable”- is the statement [1]
true or false? Justify your answer.
(c) A document to be transmitted over the internet contains the following characters
with their associated frequencies as shown in the following table:
Character a e l n o s t
Frequency 84 111 54 45 71 57 69
4. (a) Given an infinite number of coins with denominations {1,2,3,4,7}, find the [4]
minimum number of coins required to make an amount of 15. You must show your
working in a tabular format. Also state which coins you are using, and how many of
them you are using.
(b) Describe the ‘Overlapping Subproblem’ property of dynamic programming using [2]
Fibonacci as a reference problem.
(c) Explain how dynamic programming improves running time of 0-1 Knapsack
[2]
problem.
2
United International University (UIU)
Dept. of Computer Science & Engineering (CSE)
Midterm Exam Total Marks: 30 Fall 2022
Course Code: CSE 2217 Course Title: Data Structure and Algorithms II
Time: 1 hour 45 minutes
Any examinee found adopting unfair means will be expelled from the trimester / program as per UIU disciplinary rules.
There are four questions. Answer all of them. Show full simulation/tabulations wherever necessary.
Figures in the right-hand margin indicate full marks.
1. (a) Suppose, A problem X of size n can be divided into three subproblems each of size [1.5]
n/4, each of the problem can be solved recursively in time T(n/4) respectively. The cost
of dividing the problem and combining the results of the subproblems is O(nlogn).
Formulate the recurrence relation assuming, T(1) = O(1).
(b) Solve the following recurrence equation: T(n) = 3T(n/3)+O(1), where T(1) = O(1). [2.5]
(c) Given an array of integers A = {2, -3, 2, -4, 1, -3, -2}, find the Maximum- [3]
sum Continuous Subarray using divide-and-conquer. You must show the recursion tree
and clearly mention left, right and crossing sum for each tree node.
(b) A document to be transmitted over the internet contains the following characters with [3+1]
their associated frequencies as shown in the following table:
Character A B C D F T –
Frequency 40 23 8 10 4 12 3
There are a total of 1000 characters in the document.
I. Build the Huffman code tree for the message and find the codeword for each
character.
II. Decode “0110001111” using the codewords generated in (i).
3. (a) Suppose you have computed a Fibonacci series using dynamic programming. [1.5*
Justify the following statements with an example: 2
I. Overlapping Subproblems property has been satisfied in your computation. =3]
II. Dynamic programming gives you a more efficient solution than an obvious
recursive algorithm.
1
(b) What is ‘Optimal Substructure’ property? How does Dynamic Programming differ [2]
from Divide-and-Conquer problems in terms of handling subproblems?
(c) Suppose, CoffeeLand Coffee Shop charges 50 BDT (Bangladesh Taka) for each cup [3]
of small Americano with an additional vat of 3%. You bought 2 cups of small Americano
and gave the cashier 110 taka. The cashier has got a huge supply of the following types
of coins: 1 taka, 2 taka, and 5 taka in the cashbox. You don’t want to carry many coins,
so you asked the cashier to return the change using a minimum number of coins.
Determine the number and type of coins the cashier should return in this scenario by
applying the Dynamic Programming Approach.
4. (a) Derive the best-case and the worst-case running-time equations for the following [4]
function calculate and represent using Asymptotic Notation.
(b) Derive the exact-cost equation for the running-time of the following function [4]
and show that the time complexity is O(n logn log5n):
2
United International University (UIU)
Dept. of Computer Science & Engineering (CSE)
Midterm Exam Total Marks: 30 Spring-2023
Course Code: CSE2217 Course Title: Data Structure and Algorithms II
Time: 1 hour 45 minutes
Any examinee found adopting unfair means will be expelled from the trimester / program
as per UIU disciplinary rules.
There are Four questions. Answer all of them. Show all the calculations/steps, where applicable. Figures
in the right-hand margin indicate full marks.
1 (a) Consider a modified version of the Merge sort algorithm as follows: [1.5]
If the array size is less than or equal to 2, then it sorts the array at constant time.
Otherwise, it divides the array of size n into 3 subarrays, each with a size of n/3. This
division takes O(n^2) time. Then the algorithm sorts the subarrays recursively, and then
merges their solutions in time O(nlogn). Write a recurrence relation for the running-
time T(n) of this algorithm.
(b) Prove that the divide and conquer method will sort an array in O(nlogn) time when [2.5]
n>1.
(c) Given an array of integers A = {70, -40, 20, -50, 10, -15, 20}, find the [3]
Maximum-sum Continuous Subarray using divide-and-conquer. You must show the
recursion tree and clearly mention left, right and crossing sum for each tree node.
2 (a) What is the main difference between Dynamic Programming and Divide-and- [3]
Conquer algorithms? When should we try to solve a problem using Dynamic
Programming?
(b) Suppose you are a motorcycle dealer buying cars wholesale and then selling them at [3.5]
retail price for a profit. You have a budget of 7 lac Tk. You went to the wholesale market,
and you saw the following items on sale:
You want to get the maximum profit from selling all these motorcycles, but your budget
restricts you from buying all of them. Find the maximum profit you can obtain by buying
some of these motorcycles and then selling them, provided the total wholesale cost of the
motorcycles you selected do not exceed your budget of 7 lac Tk. Note that you can
only buy one of these motorcycles at a time, so if you purchased a Yamaha R15 from the
wholesale market, you cannot purchase it again. Find the solution to this problem by
using dynamic programming and creating a lookup table.
1
(c) Suppose now you have an infinite supply of motorcycles in the wholesale market, [1.5]
which means that if you purchased a Yamaha R15 from the wholesale market, you could
purchase it again (if you have the required amount of money). What modification should
you make to the algorithm you used in question (b) to make it work for an infinite supply
of motorcycles? (You do not need to show any lookup tables here)
3 (a) Calculate the time complexity (Best Case and Worst Case) of the following code [2*3=
snippets 6]
(i) for (int i =1; i<n; i=i*2) {
p++;
}
for(int j=1; j<p; j=j*2){
printf(“hello”);
}
(b) Given f (n) = 5n3 + 6n2 + 3n +9 ; g (n) = n4 ; find the values of c and n0 such that [2]
when n > n0, f (n) = O (g (n))
4 (a) Find an optimal solution to the fractional knapsack instance of n = 4, W = 5, (v1, [3]
v2, v3, v4) = (50, 30, 35, 60), and (w1, w2, w3, w4) = (2, 2, 1, 3).
(b) Suppose we want to encode the symbols {A, B, C, D} in binary. Is the following a [2]
valid Huffman code?
{A: 0; B: 10; C: 110; D: 111}
If it is, build the code tree; if not, explain why you can't.
(c) You are given the arrival and the departure times of eight trains for a railway platform, [2]
and each one is in the format: [arrival time, departure time). Only one train can use the
platform at a time. Suppose that you have got the following train-use requests for the next
day.
{ [8, 12), [6, 9), [11, 14), [2, 7), [1, 7), [12, 20), [7, 12) , [13, 19) }
Find the maximum number of trains that can use the platform without any collision
by using earliest departure time.
2
United International University (UIU)
Dept. of Computer Science & Engineering (CSE)
Midterm Exam Total Marks: 30 Summer-2023
Course Code: CSE2217 Course Title: Data Structure and Algorithms II
Time: 1 hour 45 minutes
Any examinee found adopting unfair means will be expelled from the trimester / program
as per UIU disciplinary rules.
There are Four questions. Answer all of them. Show all the calculations/steps, where applicable. Figures
in the right-hand margin indicate full marks.
1 (a) Derive the best-case, and the worst-case running time equations for the following [4]
function connectDots and represent using Asymptotic Notation.
1 bool connectDots(int arr[], int n){
2 int numberOfDots = 0;
3 for(int i=0; i<n; i++){
4 if(arr[i]&1){
5 for(int j=i+1; j<=n-1; j++){
6 numberOfDots++;
7 }
8 for(int j=0;j<=i; j++){
9 numberOfDots *= 2;
10 }
11 }
12 int j = 1;
13 while(j<=n){
14 numberOfDots+=j;
15 j = j * 2;
16 }
17 }
18 return (numberOfDots&1);
19 }
(b) Derive the exact-cost equation for the running time of the following function and
[4]
find the time complexity in big-oh notation.
1 for (int i = 1; i <= n; i = i * 2){
2 for (int j = 1; j <= i; j++){
3 for (int k = n; k >= i; k--){
4 printf("%d ", k);
5 }
6 printf("\n");
7 }
8 printf("\n");
9 }
1
2 (a) Solve the following recurrence equation, where T(1) = O(1). [2]
T(n) = 4T(n/2) + O(n)
(b) You are given an array of integers A = {1, -3, 2, 1, -1, 4, -2, 3, -1, 2, -3, 4}, find the [3]
maximum sum subarray using divide-and-conquer approach. You must show the
recursion tree and clearly mention left, right and crossing sum for each tree node.
(c) Suppose we have two sorted sub-arrays: L: 1, 5, 7, 8, 10, 12 and R: 4, 6, 7, 9, 13, 14. [2]
Perform the procedure Merge on L and R to find the final sorted array A. Show each
step of your answer and the number of comparisons required in each step.
3 (a) What is optimal substructure property? Write down the optimal substructure [1+1]
property of the coin change problem.
(b) Demonstrate why the recursive approach to calculate a Fibonacci number is [1+1]
inefficient, by calculating the Fibonacci number . How does the dynamic
programming approach for the same solve this inefficiency? (Consider )
(c) A smuggler enters a warehouse to find the items listed in the following table. He has [4]
a bag to carry the smuggled goods, but it can carry only 8 kg weight at best. The
smuggler wants to leave with the items that will result in a maximum profit for him.
Note that he cannot take an item partially; he either will take the item, or will not.
Using dynamic programming, calculate the maximum profit the smuggler can earn.
Item no. 1 2 3 4
Weight 3 5 4 6
Profit 10 30 25 50
(b) A document to be transmitted over the internet contains the following characters
with their associated frequencies as shown in the following table:
Character a e l n o s t
Frequency 74 105 44 55 73 57 49
2
ii. What is the percentage saving if the data is sent with fixed-length code [1]
values without compression?
3
United International University (UIU)
Dept. of Computer Science & Engineering (CSE)
Midterm Exam Total Marks: 30 Fall-2023
Course Code: CSE2217 Course Title: Data Structure and Algorithms II
Time: 1 hour 45 minutes
Any examinee found adopting unfair means will be expelled from the trimester / program
as per UIU disciplinary rules.
There are Four questions. Answer all of them. Show all the calculations/steps, where applicable. Figures
in the right-hand margin indicate full marks.
1 (a) Given an array A = {-2, 3, -1, 3, -4, 4}, find the maximum-sum continuous [3]
subarray using divide-and-conquer approach. You must show the recursion tree and
clearly mention left, right and crossing sum for each tree node.
(b) Find out a good asymptotic upper bound on the following recurrence: [2]
T(n) = 3T(n/4) + O(n2).
You may use Recursion-tree or Master method to solve the recurrence.
(c) Given an array of integers A = {1, 3, -5, 2, -3, -2}, find the Maximum and Minimum [2]
using divide-and-conquer. Show the necessary steps to support your answer.
2 (a) After obtaining your BSCSE degree, you embarked on an entrepreneurial journey and [4]
established your own thriving software company. You've been consistently successful in
securing projects from a variety of clients, ensuring a steady flow of profits. However,
suddenly a situation arises where your decision-making and leadership skills are put to
the test.
You have just received 5 project offers from different clients, but you have only 7 days
to complete the projects. Your project manager prepares the following estimates for each
of the projects and presents them to you for your decision.
Duration 3 2 1 2 5
(In Days)
Being an adept CSE graduate, you decide to approach the problem using dynamic
programming. Determine which of the projects can be taken to maximize the net
profit.
Note that you cannot partially complete a project. Also, you are unable to work on two
projects at the same time on a particular day.
(b) Both the algorithmic paradigms: Divide-and-Conquer and Dynamic Programming [2]
solve a problem by breaking it into smaller problem instances, and by solving them. What
is the fundamental difference between these two paradigms?
1
(c) You have found a treasure containing an infinite supply of $23, $16, $9, and $1 Coins. [2]
To go back home you need to pay a total of $25. However, despite having an infinite
supply of coins you want to pay using the minimum number of coins. Give an example
where the greedy approach does not provide an optimal solution in this matter.
3 (a) Derive the best-case and the worst-case running time equations for the following [5]
function specialTask and represent using Asymptotic Notation.
1 void specialTask(int arr[], int n) {
2 for(int i=0; i<n; i++) {
3 for(int j=n-1; j>i; j--) {
4 if(arr[j]<arr[j-1]) {
5 swap(arr[j], arr[j-1]);
6 }
7 }
8 printArray(arr, n);
9 bool flag = true;
10 for(int j=n-1; j>0; j--){
11 if(arr[j]<arr[j-1]) {
12 flag = false;
13 }
14 }
15 if(flag == true) {
16 return;
17 }
18 }
19 return;
20 }
(b) Derive the exact-cost equation for the running time of the following function [3]
funCTION and find the time complexity in Big-Oh notation.
4 (a) Find an optimal solution to the fractional knapsack instance of n = 5, W = 7, (v1, [3+1]
v2, v3, v4, v5) = (50, 30, 35, 60, 25), and (w1, w2, w3, w4) = (2, 2, 1, 3, 2). Explain
why your solution satisfies the optimal substructure property.
2
(b) What is activity selection problem? Is it true that the activity selection problem has [1]
one unique optimal solution? Justify your answer.
(c) You are given the arrival and the departure times of eight trains for a railway [2]
platform, and each one is in the format: [arrival time, departure time). Only one train
can use the platform at a time. Suppose that you have got the following train-use
requests for the next day.
{ [8, 12), [6, 9), [11, 14), [2, 7), [1, 7), [12, 20), [7, 12) , [13, 19) }
Find the maximum number of trains that can use the platform without any collision
by using earliest departure time.
3
United International University (UIU)
Dept. of Computer Science & Engineering (CSE)
Midterm Exam Total Marks: 30 Spring-2024
Course Code: CSE2217 Course Title: Data Structure and Algorithms II
Time: / hour 30 minutes
Any examinee found adopting unfair means will be expelled from the trimester / program
as per UIU disciplinary rules.
There are Four questions. Answer all of them. Show all the calculations/steps, where applicable. Figures
in the relpAfand margin indicate full marks.
\t VG Imagine you have a land with an area of 1x10 m?, and you want to cover it using [4]
some tiles. There are three types of tiles available which varies in size: 1x1 m?, 1x5 m? and 1x6
m? (See figure). Each type has infinite amount of supplies, so you never run out of tiles. Now,
there are many ways that you can cover that land using these tiles, but you want to use as few
tiles as possible (Tiles are expensive!)
[= Tiles
=e
sole
a o*
G"s Land 1 x 10s om,oe? LFotho%e?
e
go 4°
ry
&
Using Dynamic Programming method, find the minimum number of tiles that can cover your
land. Which tiles should we use? Describe your solution with detailed calculation.
Suppose your wallet has capacity to hold only 8 grams of gold coins, and your best friend (4)
J just offered you 4 gold coins from his own collection. The weights and the values of the coins
are as follows: [5g, 4g, 6g, 3g] and [110$, 100$, 120$, 90$]. Using Dynamic Programming,
determine which coins you should take so that your total gain is maximized. Keep in mind that’
you cannot carry more than 8 grams of gold coins.
Find the Maximum-sum subarray using divide-and-conquer. You must show the recursion tree
and clearly mention left, right and crossing sum for each tree node.
of Scsian an algorithm using the divide and conquer approach that counts the number 3}
4 of elements in an array that end with the digit 'l'. The algorithm should make use of the modulus
operator to determine if an element ends with ‘l'. Include the pseudocode for your algorithm,
clearly defining the base case and detailing all necessary steps and calculations.
int sumA = 0;
for(int i-O;i<n; itt) (
sumA = sumA + A(i)?
)
int sumB = 0;
for(int i=0;i<mzit+) (
sumB = sumB + B[i);
}
0 int count = 0;
\—for(int i=0;i<n;i+t) (
i. ———feor(int j=0; j<m: j++) (-
——#
(A[i]£
*A[i]>B[i] || sumA>sumB) (
4 ~count++;
jelse(
\S break;
i )
}
}
Now determine the following:
& The exact-cost equation for the running-time.
#' The best case and worst case running time using the Big - Oh notation
Ja Examples of best case and worst case inputs
YF Given the arrival and the departure times (in minutes) of 8 trains for a railway (2+1]
platform, find out the maximum number of trains that can use that platform without
any collision, using a greedy algorithm. There must exist-at least'10 minutes of safety
break between the.departure
of ong train and arrival ofthe next one,
£4000;-4036], [840,1036; [850,1046},
11-700, 2006}, [800,835], 1300,1800], £1598,
4656},£1200, 1380)
Explain your strategy very briefly and show detailed calculations. No need to write
pseudocode. Explain why your solution satisfies the optimal substructure property.
“Data encoded using Huffman coding is uniquely decodable”- is the statement true or false? (1)
‘Justify your answer.
(End of paper)
`
DATA STRUCTURE
AND ALGORITHMS II
CSE 2217
SOLUTION BY
NURUL ALAM ADOR
nurulalamador.github.io/UIUQuestionBank
Index
Trimester Page
Spring 2024 3
Fall 2023 11
Summer 2023 19
Spring 2023 29
Fall 2022 37
Important Note
The simulation of various kinds of algorithms (such as maximum sum
subarray and minimum-maximum value) could vary by teacher. In this
solution, we follow Md. Tamzid Hossain sir's simulation technique.
nurulalamador.github.io/UIUQuestionBank 2
Spring 2024
1. a) Imagine you have a land with an area of 1x10 m2, and you want to cover it using
some tiles. There are three types of tiles available which varies in size: 1x1 m2, 1x5 m2
and 1x6 m2 (See figure). Each type has infinite amount of supplies, so you never run
out of tiles. Now, there are many ways that you can cover that land using these
tiles, but you want to use as few tiles as possible (Tiles are expensive!)
Tiles
Land 1 x 10
Using Dynamic Programming method, find the minimum number of tiles that can
cover your land. Which tiles should we use? Describe your solution with detailed
calculation.
Solution:
Given,
Total Land = 1 × 10 𝑚2 = 10 𝑚2
Available Tiles, 𝑇𝑖 = {𝑇1 , 𝑇2 , 𝑇3 }
= {1 × 6 𝑚2 , 1 × 5 𝑚2 , 1 × 1 𝑚2 }
= {6 𝑚2 , 5 𝑚2 , 1 𝑚2 }
Now,
Total Land, 𝐿 (𝑚2 )
0 1 2 3 4 5 6 7 8 9 10
1 0 1 2 3 4 5 6 7 8 9 10
Tiles, 𝑇 (𝑚2 )
5 0 1 2 3 4 1 2 3 4 5 2
6 0 1 2 3 4 1 1 2 3 4 2
1. b) Suppose your wallet has capacity to hold only 8 grams of gold coins, and your best
friend just offered you 4 gold coins from his own collection. The weights and the
values of the coins are as follows: [5g, 4g, 6g, 3g] and [110$, 100$, 120$, 90$]. Using
Dynamic Programming, determine which coins you should take so that your total
gain is maximized. Keep in mind that you cannot carry more than 8 grams of gold
coins.
nurulalamador.github.io/UIUQuestionBank 3
Solution:
Here,
Capacity = 8 g
Coin No 1 2 3 4
Weight (g) 5 4 6 3
Now,
Weight (g)
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
-1 10 -1 6 -2 -4 2 2 8 -4
Find the Maximum-sum subarray using divide-and-conquer. You must show the
recursion tree and clearly mention left, right and crossing sum for each tree node.
Solution:
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 4
(2, 9, 21)
1 2 3 4 5 6 7 8 9 10
-1 10 -1 6 -2 -4 2 2 8 -4
(2, 9, 21)
(2, 4, 15) (7, 8, 12)
1 2 3 4 5 6 7 8 9 10
-1 10 -1 6 -2 -4 2 2 8 -4
1 2 3 4 5 21 6 7 8 9 10
nurulalamador.github.io/UIUQuestionBank
-1 10 -1 6 -2 -4 2 2 8 -4
(2, 4, 15) (7, 8, 12)
1 2 3 4 5 6 7 8 9 10
(2, 2, 10) (4, 4, 6) (7, 8, 4) (9, 9, 8)
-1 10 -1 6 -2 -4 2 2 8 -4
15 12
1 2 3 4 5 6 7 8 9 10
-1 10 -1 6 -2 -4 2 2 8 -4
(2, 2, 10) (2, 3, 9) (3, 3, -1) (4, 4, 6) (4, 5, 4) (5, 5, -2) (7, 7, 2) (7, 8, 4) (8, 8, 2) (9, 9, 8) (9, 10, 4) (10, 10, -4)
1 2 1 2 3 3 4 4 5 5 6 7 6 7 8 8 9 9 10 10
-1 10 -1 10 -1 -1 6 6 -2 -2 -4 2 -4 2 2 2 8 8 -4 -4
9 4 4 4
(1, 1, -1) (1, 2, 9) (2, 2, 10) (6, 6, -4) (6, 7, -2) (7, 7, 2)
1 1 2 2 6 6 7 7
-1 -1 10 10 -4 -4 2 2
9 -2
5
2. b) Design an algorithm using the divide and conquer approach that counts the
number of elements in an array that end with the digit ‘1’. The algorithm should
make use of the modulus operator to determine if an element ends with ‘1’. Include
the pseudocode for your algorithm, clearly defining the base case and detailing all
necessary steps and calculations.
Solution:
The pseudocode has been written below:
if (left == right) {
if(arr[left] % 10 == 1){
return 1;
}
Base Case
else {
return 0;
}
}
else {
mid = (left + right) / 2;
}
If it is not base case, dividing the array in two nearly
equal halves and merging their return result
3. a) Using recursion tree method or the Master Theorem find out a good asymptotic
upper bound on the following recurrence:
T(n) = 3T(n/2) + O(n)
Solution:
Given,
𝑛
𝑇(𝑛) = 3𝑇 ( ) + 𝑂(𝑛)
2
𝑛 p
Comparing with base equation of Master method 𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑂(𝑛𝑘 log 𝑛),
𝑏
Here,
𝑎=3
𝑏=2
𝑘=1
𝑝=0
𝑏 𝑘 = 21 = 2
nurulalamador.github.io/UIUQuestionBank 6
∴ Time complexity, 𝑇(𝑛) = 𝑂(𝑛log𝑏 𝑎 )
= 𝑂(𝑛log2 3 )
= 𝑂(𝑛1.58 )
3. b) Consider the following function which takes two arrays (A and B) and their lengths
(n and m) as inputs respectively.
int sumB = 0;
for(int i=0; i<m; i++) {
sumB = sumB + B[i];
}
int count = 0;
2 1 1
3 𝑛+1 𝑛+1
4 𝑛 𝑛
7 1 1
8 𝑚+1 𝑚+1
9 𝑚 𝑚
12 1 1
nurulalamador.github.io/UIUQuestionBank 7
14 𝑛+1 𝑛+1
15 𝑛𝑚 + 𝑛 𝑛
16 𝑛𝑚 𝑛
17 𝑛𝑚 0
19 0 𝑛
A[ ] = 1 1 1 1 B[ ] = 10 10 10 10 n=4 m=4
A[ ] = 10 10 10 10 B[ ] = 1 1 1 1 n=4 m=4
4. a) Given the arrival and the departure times (in minutes) of 8 trains for a railway
platform, find out the maximum number of trains that can use the platform
without any collision, using a greedy algorithm. There must be exist at least 10
minutes of safety break between the departure of one train and arrival of the next
one.
[1000, 1030], [840, 1030], [850, 1040], [1700, 2000], [800, 835], [1300, 1800], [1500,
1650], [1200, 1380]
Explain your strategy very briefly and show detailed calculations. No need to write
pseudocode. Explain why your solution satisfies the optimal substructure
property.
Solution:
Sorting train based on departure time:
Train, 𝒕𝒊 𝒕𝟏 𝒕𝟐 𝒕𝟑 𝒕𝟒 𝒕𝟓 𝒕𝟔 𝒕𝟕 𝒕𝟖
Arrival Time 800 840 1000 850 1200 1500 1300 1700
Departure Time 835 1030 1030 1040 1380 1650 1800 2000
Now,
nurulalamador.github.io/UIUQuestionBank 8
1 [800, 835]
2 [840, 1030]
3 [1000, 1030]
4 [850, 1040]
5 [1200, 1380]
Train
6 [1500, 1650]
7 [1300, 1800]
8 [1700, 2000]
Time
∴ Maximum 5 trains [Scheduled for [800, 835], [1000, 1030], [1200, 1380], [1500, 1650]
and [1700, 2000]] can use the platform without any collision.
Here, we use Activity Selection strategy. At first, we sort all the train in ascending
order on the based on departure time. Then, we select the first train and select next
train which arrival time is at least after 10 minutes from first train departure time.
Our solution satisfies the optimal substructure property because here an optimal
solution to the entire problem can be constructed from optimal solutions to its sub-
problems. By selecting the train with the earliest departure time, the remaining
sub-problems can be solved using the same approach, leading to an overall optimal
solution.
4. c) Suppose you have a cup with capacity 750 ml. The following table shows that there
are 5 flavors of drinks. For each flavor, there is only 𝑑 ml available, and each flavor
of drink contains a total of 𝑠 grams sugar. At most how many grams of sugar can
you consume if you fill up the cup using greedy approach? You need to show all
the steps of your calculation.
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 9
Flavor Volume (𝑑 ) Total sugar (𝑠)
Apple 320 35
Orange 220 20
Pineapple 240 40
Cranberry 200 30
Strawberry 280 25
Solution:
Given,
No of juices, 𝑛 = 5
Capacity, 𝐷 = 750 𝑚𝑙
Index, 𝒊 1 2 3 4 5
Sugar, 𝑠𝑖 35 20 40 30 25
1 2 3 4 5
Apple Orange Pineapple Cranberry Strawberry
𝑠𝑖
0.109 0.091 0.167 0.150 0.089
𝑑𝑖
𝑠𝑖
Sorting item based on :
𝑑𝑖
Pineapple > Cranberry > Apple > Orange > Strawberry
𝑫
𝒊 Flavor 𝒔𝒊 𝒅𝒊 𝑫 𝒙𝒊 = min (𝟏, ) 𝑫′ = 𝑫 − 𝒅𝒊 𝒙𝒊
𝒅𝒊
3 Pineapple 40 240 750 1 750-240 = 510
∴ Maximum Sugar = 𝑠3 𝑥3 + 𝑠4 𝑥4 + 𝑠1 𝑥1
= 40 × 1 + 30 × 1 + 35 × 0.96875
= 103.90 𝑔
nurulalamador.github.io/UIUQuestionBank 10
1.
a)
(2, 4, 5)
Solution:
1 2 3 4 5 6
-2 3 -1 3 -4 4
(2, 2, 3) (6, 6, 4)
(2, 4, 5)
1 2 3 1 2 3 4 5 6 4 5 6
nurulalamador.github.io/UIUQuestionBank
-2 3 -1 -2 3 -1 3 -4 4 3 -4 4
5
(2, 2, 3) (2, 3, 2) (3, 3, -1) (4, 4, 3) (4, 6, 3) (6, 6, 4)
1 2 1 2 3 3 4 5 4 5 6 6
-2 3 -2 3 -1 -1 3 -4 3 -4 4 4
Fall 2023
2 3
(1, 1, -2) (1, 2, 1) (2, 2, 3) (4, 4, 3) (4, 5, -1) (5, 5, -4)
1 1 2 2 4 4 5 5
mention left, right and crossing sum for each tree node.
-2 -2 3 3 3 3 -4 -4
1 -1
2 3 4
3 -1 3 (Maximum Sum = 5)
Given an array A = {-2, 3, -1, 3, -4, 4}, find the maximum-sum continuous subarray
using divide-and-conquer approach. You must show the recursion tree and clearly
11
1. b) Find out a good asymptotic upper bound on the following recurrence:
𝑛
𝑇(𝑛) = 3𝑇 ( ) + 𝑂(𝑛2 )
4
You may use Recursion-tree or Master method to solve the recurrence.
Solution:
Given,
𝑛
𝑇(𝑛) = 3𝑇 ( ) + 𝑂(𝑛2 )
4
𝑛 p
Comparing with base equation of Master method 𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑂(𝑛𝑘 log 𝑛),
𝑏
Here,
𝑎=3
𝑏=4
𝑘=2
𝑝=0
𝑏 𝑘 = 42 = 16
Since 𝑎 < 𝑏 𝑘 and 𝑝 ≥ 0, therefore this recurrence belongs to 1st condition of case 3.
𝑝
∴ Time complexity, 𝑇(𝑛) = 𝑂(𝑛𝑘 log 𝑛)
= 𝑂(𝑛2 log 0 𝑛)
= 𝑂(𝑛2 )
1. c) Given an array of integers A = {1, 3, -5, 2, -3, -2}, find the Maximum and Minimum
using divide-and-conquer. Show the necessary steps to support your answer.
Solution:
(-5, 3)
1 3 -5 2 -3 -2
(-5, 3) (-3, 2)
1 3 -5 2 -3 -2
1 3 -5 2 -3 -2
1 3 2 -3
nurulalamador.github.io/UIUQuestionBank 12
2. a) After obtaining your BSCSE degree, you embarked on an entrepreneurial journey
and established your own thriving software company. You've been consistently
successful in securing projects from a variety of clients, ensuring a steady flow of
profits. However, suddenly a situation arises where your decision-making and
leadership skills are put to the test.
You have just received 5 project offers from different clients, but you have only 7
days to complete the projects. Your project manager prepares the following
estimates for each of the projects and presents them to you for your decision.
Net Profit
200 150 100 50 300
(In Million Dollars)
Duration
3 2 1 2 5
(In Days)
Being an adept CSE graduate, you decide to approach the problem using dynamic
programming. Determine which of the projects can be taken to maximize the net
profit.
Note that you cannot partially complete a project. Also, you are unable to work on
two projects at the same time on a particular day.
Solution:
Here,
Total times = 7 days
Project 1 2 3 4 5
Days 3 2 1 2 5
Now,
Duration
0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0
∴ 1st, 2nd and 3rd project can be selected for maximum profit.
nurulalamador.github.io/UIUQuestionBank 13
2. b) Both the algorithmic paradigms: Divide-and-Conquer and Dynamic Programming
solve a problem by breaking it into smaller problem instances, and by solving
them. What is the fundamental difference between these two paradigms?
Solution:
Both the algorithmic paradigms Divide-and-Conquer and Dynamic Programming
solve a problem by breaking it into smaller sub-problems but there are some
fundamental difference. These differences are:
Break the problem into independent Break the problem into overlapping sub-
sub-problems. problems.
Combines the solution of all sub- Avoid solving the same sub-problems
problems to get the final solution. multiple time by reusing stored solution.
2. c) You have found a treasure containing an infinite supply of $23, $16, $9, and $1
Coins. To go back home you need to pay a total of $25. However, despite having an
infinite supply of coins you want to pay using the minimum number of coins. Give
an example where the greedy approach does not provide an optimal solution in
this matter.
Solution:
We have infinite supply of:
$23, $16, $9, $1
But if we use any other approach, like Dynamic Programming, then we can pay
$25 in only 2 coins ($16, $9).
3. a) Derive the best-case and the worst-case running time equations for the following
function specialTask and represent using Asymptotic Notation.
nurulalamador.github.io/UIUQuestionBank 14
8 printArray(arr, n);
9 bool flag = true;
10 for(int j=n-1; j>0; j--){
11 if(arr[j]<arr[j-1]) {
12 flag = false;
13 }
14 }
15 if(flag == true) {
16 return;
17 }
18 }
19 return;
20 }
Solution:
2 𝑛+1 1
𝑛(𝑛 + 1)
3 +𝑛 𝑛
2
𝑛(𝑛 + 1)
4 𝑛−1
2
𝑛(𝑛 + 1)
5 0
2
8 𝑛 1
9 𝑛 1
10 𝑛×𝑛 𝑛
11 𝑛(𝑛 − 1) 𝑛−1
12 𝑛(𝑛 − 1) 0
15 𝑛 1
16 0 1
19 1 1
nurulalamador.github.io/UIUQuestionBank 15
3. b) Derive the exact-cost equation for the running time of the following function
funCTION and find the time complexity in Big-Oh notation.
Solution:
2 1
𝑛
3 +1
2
𝑛
4
2
𝑛 𝑛
5 × log 3 𝑚 +
2 2
𝑛
6 × log 3 𝑚
2
𝑛
7 × log 3 𝑚
2
10 1
𝑛 𝑛 𝑛 𝑛 𝑛 𝑛
∴ Time complexity, 𝑇(𝑛) = 1 + + 1 + + × log3 𝑚 + + × log3 𝑚 + × log3 𝑚 + 1
2 2 2 2 2 2
3 3
= 𝑛 + 𝑛 log 3 𝑚 + 3
2 2
= 𝑂(𝑛 log 𝑚)
3 3
∴ Exact cost equation is 𝑛 + 𝑛 log3 𝑚 + 3 and time complexity is 𝑂(𝑛 log 𝑚).
2 2
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 16
Item, 𝒊𝒊 𝒊𝟏 𝒊𝟐 𝒊𝟑 𝒊𝟒 𝒊𝟓
Value, 𝑣𝑖 50 30 35 60 25
Weight, 𝑤𝑖 2 2 1 3 2
𝑖1 𝑖2 𝑖3 𝑖4 𝑖5
𝑣𝑖
25 15 35 20 12.5
𝑤𝑖
𝑣𝑖
Sorting item based on :
𝑤𝑖
𝑖3 > 𝑖1 > 𝑖4 > 𝑖2 > 𝑖5
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
𝑖3 35 1 7 1 7-1 = 6
𝑖1 50 2 6 1 6-2 = 4
𝑖4 60 3 4 1 4-3 = 1
∴ Total Profit = 𝑣3 𝑥3 + 𝑣1 𝑥1 + 𝑣4 𝑥4 + 𝑣2 𝑥2
= 35 × 1 + 50 × 1 + 60 × 1 + 30 × 0.5
= 160
This solution satisfies the optimal substructure property. Because if the knapsack
capacity was 1, then 35 × 1 = 35 was the highest profit, if knapsack size was 3. than
35 × 1 + 50 × 1 = 85 was the highest profit and like that, every sub-problem in here
are the optimal solution for that stage.
4. b) What is activity selection problem? Is it true that the activity selection problem
has one unique optimal solution? Justify your answer.
Solution:
Activity selection is a problem where various activities are given along with their
starting and finishing time. We need to find the out maximum how many activities
we can select in a specific time period.
It is false that Activity Selection problem has one unique solution. If there are three
activities such that {[7,12), [8,12), [13,16)} then some algorithm will give solution
{[7, 12), [13,16)} and other algorithm may give solution {[8,12), [13,16)}. But number
of maximum selected activities will be same.
4. c) You are given the arrival and the departure times of eight trains for a railway
platform, and each one is in the format: [arrival time, departure time). Only one
nurulalamador.github.io/UIUQuestionBank 17
train can use the platform at a time. Suppose that you have got the following train-
use requests for the next day.
{ [8, 12), [6, 9), [11, 14), [2, 7), [1, 7), [12, 20), [7, 12) , [13, 19) }
Find the maximum number of trains that can use the platform without any
collision by using earliest departure time.
Solution:
Given,
Following train requested for use platform:
{ [8, 12), [6, 9), [11, 14), [2, 7), [1, 7), [12, 20), [7, 12) , [13, 19) }
Train, 𝒕𝒊 𝒕𝟏 𝒕𝟐 𝒕𝟑 𝒕𝟒 𝒕𝟓 𝒕𝟔 𝒕𝟕 𝒕𝟖
Arrival Time 1 2 6 7 8 11 13 12
Departure Time 7 7 9 12 12 14 19 20
Now,
1 [1, 7)
2 [2, 7)
3 [6, 9)
4 [7, 12)
5 [8, 12)
Train
6 [11, 14)
7 [13, 19)
8 [12, 20)
5 10 15 20
Time
∴ Maximum 3 trains [Scheduled for [1, 7), [7, 12) and [13, 19)] can use the platform
without any collision.
nurulalamador.github.io/UIUQuestionBank 18
Summer 2023
1. a) Derive the best-case, and the worst-case running time equations for the following
function connectDots and represent using Asymptotic Notation.
Solution:
2 1 1
3 𝑛+1 𝑛+1
4 𝑛 𝑛
𝑛(𝑛 + 1)
5 +𝑛 0
2
𝑛(𝑛 + 1)
6 0
2
𝑛(𝑛 + 1)
8 +𝑛 0
2
𝑛(𝑛 + 1)
9 0
2
12 𝑛 𝑛
13 𝑛 log 2 𝑛 + 𝑛 𝑛 log 2 𝑛 + 𝑛
14 𝑛 log 2 𝑛 𝑛 log 2 𝑛
15 𝑛 log 2 𝑛 𝑛 log 2 𝑛
18 1 1
nurulalamador.github.io/UIUQuestionBank 19
∴ Best case, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛 + 𝑛 + 𝑛 log2 𝑛 + 𝑛 + 𝑛 log2 𝑛 + 𝑛 log2 𝑛 + 1
= 3𝑛 log 2 𝑛 + 4𝑛 + 3
= 𝑂(𝑛 log 𝑛)
1. b) Derive the exact-cost equation for the running time of the following function and
find the time complexity in big-oh notation.
Solution:
1 log 2 𝑛 + 1
log 2 𝑛 (log 2 𝑛 + 1)
2 + log 2 𝑛
2
log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1)
3 × +
2 2 2
log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1)
4 ×
2 2
log 2 𝑛 (log 2 𝑛 + 1)
6
2
8 log 2 𝑛
nurulalamador.github.io/UIUQuestionBank 20
1 3
= (log 24 𝑛 + 2 log 23 𝑛 + log 22 𝑛) + (log 22 𝑛 + log 2 𝑛) + 3 log 2 𝑛 + 1
2 2
1 1 3 3
= log 24 𝑛 + log 23 𝑛 + log 22 𝑛 + log 22 𝑛 + log 2 𝑛 + 3 log 2 𝑛 + 1
2 2 2 2
1 5
= log 24 𝑛 + log 23 𝑛 + 2 log 22 𝑛 + log 2 𝑛 + 1
2 2
= 𝑂(log 4 𝑛)
1 5
∴ Exact cost equation is log24 𝑛 + log23 𝑛 + 2 log22 𝑛 + log2 𝑛 + 1 and time
2 2
4
complexity is 𝑂(log 𝑛) .
2. b) You are given an array of integers A = {1, -3, 2, 1, -1, 4, -2, 3, -1, 2, -3, 4}, find the
maximum sum subarray using divide-and-conquer approach. You must show the
recursion tree and clearly mention left, right and crossing sum for each tree node.
Solution:
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 21
(3, 12, 9)
1 2 3 4 5 6 7 8 9 10 11 12
1 -3 2 1 -1 4 -2 3 -1 2 -3 4
(3, 12, 9)
1 2 3 4 5 6 7 8 9 10 11 12
(3, 6, 6) (8, 12, 5)
1 -3 2 1 -1 4 -2 3 -1 2 -3 4
9
1 2 3 4 5 6 7 8 9 10 11 12
nurulalamador.github.io/UIUQuestionBank
1 -3 2 1 -1 4 -2 3 -1 2 -3 4
(3, 6, 6) (8, 12, 5)
1 2 3 4 5 6 7 8 9 10 11 12
(3, 3, 2) (6, 6, 4) (8, 8, 3) (12, 12, 4)
1 -3 2 1 -1 4 -2 3 -1 2 -3 4
6 5
1 2 3 4 5 6 7 8 9 10 11 12
1 -3 2 1 -1 4 -2 3 -1 2 -3 4
(1, 1, 1) (1, 3, 0) (3, 3, 2) (4, 4, 1) (4, 6, 4) (6, 6, 4) (8, 8, 3) (8, 9, 2) (9, 9, -1) (10, 10, 2) (10, 12, 3) (12, 12, 4)
1 2 1 2 3 3 4 5 4 5 6 6 7 8 7 8 9 9 10 11 10 11 12 12
1 -3 1 -3 2 2 1 -1 1 -1 4 4 -2 3 -2 3 -1 -1 2 -3 2 -3 4 4
0 4 2 3
(1, 1, 1) (1, 2, -3) (2, 2, 3) (4, 4, 1) (4, 5, 0) (5, 5, -1) (7, 7, -2) (7, 8, 1) (8, 8, 3) (10, 10, 2) (10, 11, -3) (11, 11, -3)
1 1 2 2 4 4 5 5 7 7 8 8 10 10 11 11
1 1 -3 -3 1 1 -1 -1 -2 -2 3 3 2 2 -3 -3
-2 0 1 -1
2 1 -1 4 -2 3 -1 2 -3 4 (Maximum Sum = 9)
22
2. c) Suppose we have two sorted sub-arrays: L: 1, 5, 7, 8, 10, 12 and R: 4, 6, 7, 9, 13, 14.
Perform the procedure Merge on L and R to find the final sorted array A. Show
each step of your answer and the number of comparisons required in each step.
Solution:
Let,
𝑖=1
𝑗=1
𝑘=1
Now,
1 2 3 4 5 6 1 2 3 4 5 6
L 1 5 7 8 10 12 R 4 6 7 9 13 14
i j
1 2 3 4 5 6 7 8 9 10 11 12
A 1 4 5 6 7 7 8 9 10 12 13 14
k
𝑖 = 1, 𝑗 = 1, 𝑘 = 1
1.
𝐿[1] ≤ 𝑅[1]; ∴ 𝐴[1] = 𝐿[1] = 1; 𝑖++; 𝑘++;
𝑖 = 2, 𝑗 = 1, 𝑘 = 2
2.
𝐿[2] ≥ 𝑅[1]; ∴ 𝐴[2] = 𝑅[1] = 4; 𝑗++; 𝑘++;
𝑖 = 2, 𝑗 = 2, 𝑘 = 3
3.
𝐿[2] ≤ 𝑅[2]; ∴ 𝐴[3] = 𝐿[2] = 5; 𝑖++; 𝑘++;
𝑖 = 3, 𝑗 = 2, 𝑘 = 4
4.
𝐿[3] ≥ 𝑅[2]; ∴ 𝐴[4] = 𝑅[2] = 6; 𝑗++; 𝑘++;
𝑖 = 3, 𝑗 = 3, 𝑘 = 5
5.
𝐿[3] ≥ 𝑅[3]; ∴ 𝐴[5] = 𝑅[3] = 7; 𝑗++; 𝑘++;
𝑖 = 3, 𝑗 = 4, 𝑘 = 6
6.
𝐿[3] ≤ 𝑅[4]; ∴ 𝐴[6] = 𝐿[3] = 7; 𝑖++; 𝑘++;
𝑖 = 4, 𝑗 = 4, 𝑘 = 7
7.
𝐿[4] ≤ 𝑅[4]; ∴ 𝐴[7] = 𝐿[4] = 8; 𝑖++; 𝑘++;
𝑖 = 5, 𝑗 = 4, 𝑘 = 8
8.
𝐿[5] ≥ 𝑅[4]; ∴ 𝐴[8] = 𝑅[4] = 9; 𝑗++; 𝑘++;
𝑖 = 5, 𝑗 = 5, 𝑘 = 9
9.
𝐿[5] ≤ 𝑅[5]; ∴ 𝐴[9] = 𝐿[5] = 10; 𝑖++; 𝑘++;
𝑖 = 6, 𝑗 = 5, 𝑘 = 10
10.
𝐿[6] ≤ 𝑅[5]; ∴ 𝐴[10] = 𝐿[6] = 12; 𝑖++; 𝑘++;
𝑖 = 6, 𝑗 = 5, 𝑘 = 11
𝐴[11] = 𝑅[5] = 13; 𝑗++; 𝑘++;
𝑖 = 6, 𝑗 = 6, 𝑘 = 12
𝐴[12] = 𝑅[6] = 14; 𝑗++; 𝑘++;
∴ We need 10 element comparisons. After that, there will be only one array (R) left
and that will not need comparisons for further steps.
nurulalamador.github.io/UIUQuestionBank 23
3. a) What is optimal substructure property? Write down the optimal substructure
property of the coin change problem.
Solution:
The optimal substructure property is a key concept of Dynamic Programming. It
essentially states that an optimal solution of a problem can be constructed from
optimal solutions to its sub-problems.
F5
Dynamic programming approach
F4 F3 for finding 𝐹5 :
F3 F2 F2 F1
F0 F1 F2 F3 F4 F5
F2 F1 F1 F0 F1 F0
F1 F0
3. c) A smuggler enters a warehouse to find the items listed in the following table. He
has a bag to carry the smuggled goods, but it can carry only 8 kg weight at best.
The smuggler wants to leave with the items that will result in a maximum profit for
him. Note that he cannot take an item partially; he either will take the item, or will
not.
Using dynamic programming, calculate the maximum profit the smuggler can
earn.
Item no. 1 2 3 4
Weight 3 5 4 6
Profit 10 30 25 50
nurulalamador.github.io/UIUQuestionBank 24
Solution:
Here,
Weight capacity = 8 kg
Item 1 2 3 4
Weight 3 5 4 6
Profit 10 30 25 50
Now,
Weight (kg)
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 10 10 10 10 10 10
2 0 0 0 10 10 30 30 30 40
Items
3 0 0 0 10 25 30 30 35 40
4 0 0 0 10 25 30 50 50 50
Rice Saffron
Item Salt Sugar
Grain Powder
Weight (kg), 𝑤 12 10 8 5
[ P.T.O]
nurulalamador.github.io/UIUQuestionBank 25
𝑣𝑖 70 87 250 100
𝑣𝑖
Sorting item based on :
𝑤𝑖
Saffron Powder > Sugar > Salt > Rice Grain
Thief 1:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Saffron
2000 8 9 1 9-8 = 1
Powder
Thief 2:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 400 4 9 1 9-4 = 5
Thief 3:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 435 5 9 1 9-5 = 4
Rice
840 12 4 0.33 4-(12×0.33) = 0
Grain
Thief 4:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Rice
560 8 9 1 9-8 = 1
Grain
nurulalamador.github.io/UIUQuestionBank 26
4. b) A document to be transmitted over the internet contains the following characters
with their associated frequencies as shown in the following table:
Character a e l n o s t
Frequency 74 105 44 55 73 57 49
l 44 t 49 n 55 s 57 o 73 a 74 e 105
93
l 44 t 49 n 55 s 57 o 73 a 74 e 105
112
n 55 s 57 o 73 a 74 93 e 105
l 44 t 49
147
o 73 a 74 93 e 105 112
l 44 t 49 n 55 s 57
198
l 44 t 49 n 55 s 57 o 73 a 74
nurulalamador.github.io/UIUQuestionBank 27
259
n 55 s 57 o 73 a 74 93 e 105
l 44 t 49
457
0 1
198 259
0 1
0 1
l 44 t 49 n 55 s 57 o 73 a 74
ii.
nurulalamador.github.io/UIUQuestionBank 28
Spring 2023
1. b) Prove that the divide and conquer method will sort an array in 𝑶(𝒏 𝐥𝐨𝐠 𝒏) time
when n>1.
Solution:
In divide and conquer method, for sorting an array, we divide the array into two
halves and solve them recursively.
So we have,
𝑛 𝑛 𝑛
𝑇 ( ) + 𝑇 ( ) = 2𝑇 ( )
2 2 2
Since 𝑎 = 𝑏 𝑘 and 𝑝 > -1, therefore this recurrence belongs to 1st condition of case 2 of
Master Theorem.
nurulalamador.github.io/UIUQuestionBank 29
𝑝+1
∴ Time complexity, 𝑇(𝑛) = 𝑂 (𝑛log𝑏 𝑎 log 𝑛)
= 𝑂(𝑛log2 2 log 0+1 𝑛)
= 𝑂(𝑛 log 𝑛)
∴ Divide and conquer method will sort an array in 𝑂(𝑛 log 𝑛). (Proved)
1. c) Given an array of integers A = {70, -40, 20, -50, 10, -15, 20}, find the
Maximum-sum Continuous Subarray using divide-and-conquer. You must show
the recursion tree and clearly mention left, right and crossing sum for each tree
node.
Solution:
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 30
(1, 1, 70)
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
nurulalamador.github.io/UIUQuestionBank
70 -40 20 -50 10 -15 20
70 -40 20 -50 10 -15 20
15
(1, 1, 70) (1, 3, 50) (3, 3, 20) (5, 5, 10) (5, 7, 15) (7, 7, 20)
1 2 1 2 3 4 3 4 5 6 5 6 7 7
50 15
(1, 1, 70) (1, 2, 30) (2, 2, -40) (3, 3, 20) (3, 4, -30) (4, 4, -50) (5, 5, 10) (5, 6, -5) (6, 6, -15)
1 1 2 2 3 3 4 4 5 5 6 6
30 -30 -5
70
31
2. a) What is the main difference between Dynamic Programming and Divide-and-
Conquer algorithms? When should we try to solve a problem using Dynamic
Programming?
Solution:
Divide-and-Conquer algorithm divide a problem into independent sub-problems
and solve each sub-problem and combines the solutions recursively. On the other
hand, Dynamic Programming algorithm break the problem into overlapping sub-
problems and avoid solving same repeated sub-problems multiple time by store
and reusing the solution of sub-problems. This is the main difference between
Dynamic Programming and Divide-and-Conquer.
2. b) Suppose you are a motorcycle dealer buying cars wholesale and then selling them
at retail price for a profit. You have a budget of 7 lac Tk. You went to the wholesale
market, and you saw the following items on sale:
You want to get the maximum profit from selling all these motorcycles, but your
budget restricts you from buying all of them. Find the maximum profit you can
obtain by buying some of these motorcycles and then selling them, provided the
total wholesale cost of the motorcycles you selected do not exceed your budget
of 7 lac Tk. Note that you can only buy one of these motorcycles at a time, so if
you purchased a Yamaha R15 from the wholesale market, you cannot purchase it
again. Find the solution to this problem by using dynamic programming and
creating a lookup table.
Solution:
Here,
Total budget = 7 Lac Tk
Index 1 2 3 4
Wholesale
3 6 1 4
Price, 𝒘𝒊
Retail Price 5 14 2 6
Profit, 𝒗𝒊 2 8 1 2
Now,
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 32
Budget (Lac Tk)
0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2
Motorcycles
2 0 0 0 2 2 2 8 8
3 0 1 1 2 3 3 8 9
4 0 1 1 2 3 3 8 9
2. c) Suppose now you have an infinite supply of motorcycles in the wholesale market,
which means that if you purchased a Yamaha R15 from the wholesale market, you
could purchase it again (if you have the required amount of money). What
modification should you make to the algorithm you used in question (b) to make
it work for an infinite supply of motorcycles? (You do not need to show any lookup
tables here).
Solution:
We use 0/1 knapsack algorithm in question (b). In the 0/1 knapsack, the algorithm is
based on choosing an item or not. For the infinity supply of item, we must consider
the possibility of taking the same item multiple times. Normally we use 2D array in
0/1 knapsack, but for this case we should reduce the 2D array to a 1D array since we
are no longer concerned with distinct items being selected only once. Let, our
capacity is 𝑗 and our array is 𝑑𝑝[𝑗]. For each capacity 𝑑𝑝[0], 𝑑𝑝[1] … 𝑑𝑝[𝑗], we have to
check all items to improves the value. This loop effectively considers each item
multiple times. Then we will get our final answer in last index of array, 𝑑𝑝[𝑗].
3. a) Calculate the time complexity (Best Case and Worst Case) of the following code
snippets.
nurulalamador.github.io/UIUQuestionBank 33
}
}
Solution:
Since there are no use of condition, return, break, continue; therefore best the best
case and worst case time complexity will be same for all of these code snippets.
1 log 2 𝑛 + 1
2 log 2 𝑛
4 log 2 𝑛 (log 2 𝑛) + 1
5 log 2 𝑛 (log 2 𝑛)
1 √𝑛 + 1
2 √𝑛
1 log 2 𝑛 + 1
log 2 𝑛 (log 2 𝑛 + 1)
2
2
log 2 𝑛 (log 2 𝑛 + 1)
3 +1
2
3. b) Given 𝑓(𝑛) = 5𝑛3 + 6𝑛2 + 3𝑛 + 9 ; 𝑔(𝑛) = 𝑛4 ; find the values of 𝒄 and 𝒏𝟎 such that
when 𝑛 > 𝑛0 , 𝒇(𝒏) = 𝑶(𝒈(𝒏))
Solution:
nurulalamador.github.io/UIUQuestionBank 34
Given,
𝑓(𝑛) = 5𝑛3 + 6𝑛2 + 3𝑛 + 9
𝑔(𝑛) = 𝑛4
Proving 𝑓(𝑛) ≤ 𝑐 ⋅ 𝑔(𝑛) for any value of 𝑐 and 𝑛0 such that 𝑛 > 𝑛0 is enough to prove
𝑓(𝑛) = 𝑂(𝑔(𝑛)).
Now,
𝑓(𝑛) ≤ 𝑐 ⋅ 𝑔(𝑛)
or, 5𝑛3 + 6𝑛2 + 3𝑛 + 9 ≤ 𝑐 ⋅ 𝑛4
5 6 3 9
or, + 2 + 3 + 4 ≤ 𝑐
𝑛 𝑛 𝑛 𝑛
∴ Big-Oh notation 𝑓(𝑛) = 𝑂(𝑔(𝑛)) holds for any value for 𝑛0 such that 1 > 𝑛0 and 𝑐 =
5 6 3 9
23. [ + 2 + 3 + 4 = 23]
1 1 1 1
Item, 𝒊𝒊 𝒊𝟏 𝒊𝟐 𝒊𝟑 𝒊𝟒
Value, 𝑣𝑖 50 30 35 60
Weight, 𝑤𝑖 2 2 1 3
𝑖1 𝑖2 𝑖3 𝑖4
𝑣𝑖
25 15 35 20
𝑤𝑖
𝑣𝑖
Sorting item based on :
𝑤𝑖
𝑖3 > 𝑖1 > 𝑖4 > 𝑖2
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
𝑖3 35 1 5 1 5-1 = 1
𝑖1 50 2 4 1 4-2 = 2
nurulalamador.github.io/UIUQuestionBank 35
∴ Total Profit = 𝑣3 𝑖3 + 𝑣1 𝑖1 + 𝑣4 𝑖4
= 35 × 1 + 50 × 1 + 60 × 0.667
= 125
If we can make Huffman tree for these given Huffman code without any collision,
then the given Huffman code is valid.
0 1
A
0 1
B
0 1
C D
Analyzing the Huffman code tree, we can see that every character is accessable and
there are not any overlapping issue.
∴ This is a valid Huffman code.
4. c) You are given the arrival and the departure times of eight trains for a railway
platform, and each one is in the format: [arrival time, departure time). Only one
train can use the platform at a time. Suppose that you have got the following train-
use requests for the next day.
{ [8, 12), [6, 9), [11, 14), [2, 7), [1, 7), [12, 20), [7, 12) , [13, 19) }
Find the maximum number of trains that can use the platform without any
collision by using earliest departure time.
Solution:
Repeat of Fall 2023 Question 4(c)
nurulalamador.github.io/UIUQuestionBank 36
Fall 2022
1. a) Suppose, A problem X of size n can be divided into three subproblems each of size
n/4, each of the problem can be solved recursively in time T(n/4) respectively. The
cost of dividing the problem and combining the results of the subproblems is
𝑂(𝑛𝑙𝑜𝑔𝑛). Formulate the recurrence relation assuming, 𝑇(1) = 𝑂(1).
Solution:
Recurrence relation for the given problem is:
𝑛
𝑇(𝑛) = 3𝑇 ( ) + 𝑂(𝑛 log 𝑛)
4
1. b) Solve the following recurrence equation: 𝑇(𝑛) = 3𝑇(𝑛/3) + 𝑂(1), where 𝑇(1) = 𝑂(1).
Solution:
Given,
𝑛
𝑇(𝑛) = 3𝑇 ( ) + 𝑂(1)
3
𝑛 p
Comparing with base equation of Master method 𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑂(𝑛𝑘 log 𝑛),
𝑏
Here,
𝑎=3
𝑏=3
𝑘=0
𝑝=0
𝑏 𝑘 = 30 = 1
1. c) Given an array of integers A = {2, -3, 2, -4, 1, -3, -2}, find the Maximum-
sum Continuous Subarray using divide-and-conquer. You must show the
recursion tree and clearly mention left, right and crossing sum for each tree
node.
Solution:
nurulalamador.github.io/UIUQuestionBank 37
(1, 1, 2)
1 2 3 4 5 6 7
2 -3 2 -4 1 -3 -2
(1, 1, 2) (5, 5, 1)
(3, 5, -1)
1 2 3 4 5 6 7
1 2 3 4 5 6 7
nurulalamador.github.io/UIUQuestionBank
2 -3 2 -4 1 -3 -2
2 -3 2 -4 1 -3 -2
-1
1 2 1 2 3 4 3 4 5 6 5 6 7 7
2 -3 2 -3 2 -4 2 -4 1 -3 1 -3 -2 -2
1 -4
(1, 1, 2) (1, 2, -1) (2, 2, -3) (3, 3, 2) (3, 4, -2) (4, 4, -4) (5, 5, 1) (5, 6, -2) (6, 6, -3)
1 1 2 2 3 3 4 4 5 5 6 6
2 2 -3 -3 2 2 -4 -4 1 1 -3 -3
-1 -2 -2
38
4. a) Following items are available in a grocery shop:
10 kilogram rice grain which costs 800 taka
10 kilogram salt which costs 890 taka
8 kilogram saffron powder which costs 2000 taka and
4 kilogram sugar which costs 500 taka
A group of thieves (Thief 1, Thief 2, … Thief M) have come to steal from that shop,
each with a knapsack of capacity 8 kg. The thieves are entering in serial, Thief 2
enters after Thief 1 is done with stealing, Thief 3 enters after Thief 2 is done with
stealing and so on. Since each thief wants to maximize his/her profit, how many
thieves will be needed in the group to empty the grocery shop and what are the
items that each of those thieves carry? Show details of the calculation.
Solution:
Given,
Capacity of each thief, 𝑊 = 8 𝑘𝑔
Rice Saffron
Item Salt Sugar
Grain Powder
Weight (kg), 𝑤 10 10 8 4
[ P.T.O]
𝑣𝑖 80 89 250 125
𝑣𝑖
Sorting item based on :
𝑤𝑖
Saffron Powder > Sugar > Salt > Rice Grain
Thief 1:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Saffron
2000 8 8 1 8-8 = 0
Powder
Thief 2:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 500 4 8 1 8-4 =4
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 39
Thief 3:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 534 6 8 1 8-6 = 2
Rice
800 10 2 0.2 2-(10×0.2) = 2-2 = 0
Grain
Thief 4:
𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Rice
640 8 8 1 8-8 = 0
Grain
Character A B C D F T _
Frequency 40 23 8 10 4 12 3
_ 3 F 4 C 8 D 10 T 12 B 23 A 40
_ 3 F 4 C 8 D 10 T 12 B 23 A 40
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 40
15
7 C 8 D 10 T 12 B 23 A 40
_ 3 F 4
22
D 10 T 12 15 B 23 A 40
7 C 8
_ 3 F 4
37
15 22 B 23 A 40
7 C 8 D 10 T 12
_ 3 F 4
60
B 23 37 A 40
15 22
7 C 8 D 10 T 12
_ 3 F 4
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 41
100
0 1
A 40 60
0 1
B 23 37
0 1
15 22
0 1 0 1
7 C 8 D 10 T 12
0 1
_ 3 F 4
nurulalamador.github.io/UIUQuestionBank 42
Recursive approach for calculating 𝐹5 :
F5
Dynamic programming approach
F4 F3 for calculating 𝐹5 :
F3 F2 F2 F1
F0 F1 F2 F3 F4 F5
F2 F1 F1 F0 F1 F0
F1 F0
A problem can have some repeated sub-problems, which are known as overlapping
sub-problems. In Divide-and-Conquer algorithm, it solve each of the sub-problems,
including repeated overlapping sub-problems. It create inefficiency for many
problems, such as Fibonacci number calculating. In Fibonacci number problem, we
have so many repeated sub-problems multiple time. Recursive algorithm solve and
merge each of repeated sub-problems. But Dynamic Programming solve and store
sub-problems solution and use it later and reduce inefficiency. It is the main
difference in terms of handling sub-problems between Divide-and-Conquer and
Dynamic Programming algorithm.
3. b) Suppose, CoffeeLand Coffee Shop charges 50 BDT (Bangladesh Taka) for each cup
of small Americano with an additional vat of 3%. You bought 2 cups of small
Americano and gave the cashier 110 taka. The cashier has got a huge supply of the
following types of coins: 1 taka, 2 taka, and 5 taka in the cashbox. You don’t want
to carry many coins, so you asked the cashier to return the change using a
minimum number of coins. Determine the number and type of coins the cashier
should return in this scenario by applying the Dynamic Programming Approach.
Solution:
Given,
Total charges = (50 × 2) + [(50 × 2) × 3%]
= 100 + (100 × 3%)
= 100 + 3
= 103
Given cash = 110
∴ Total Changes, 𝑀 = 110 − 103
=7
Coins, 𝐶𝑖 = {𝐶1 , 𝐶2 , 𝐶3 } = {1,2,5}
nurulalamador.github.io/UIUQuestionBank 43
Now,
Total Changes, M
0 1 2 3 4 5 6 7
1 0 1 2 3 4 5 6 7
2 0 1 1 2 2 3 3 4
Coins 5 0 1 1 2 2 1 2 2
4. a) Derive the best-case and the worst-case running time equations for the following
function calculate and represent using Asymptotic Notation.
Solution:
2 1 1
3 𝑛+1 𝑛+1
4 𝑛3 + 𝑛 𝑛3 + 𝑛
5 𝑛3 𝑛3
9 𝑝 1
10 0 1
11 0 1
14 𝑝−1 0
nurulalamador.github.io/UIUQuestionBank 44
17 1 1
4. b) Derive the exact-cost equation for the running time of the following function and
show that time complexity in 𝑂(𝑛 log 𝑛 log 5 𝑛):
1 int funFunction(int n)
2 {
3 int sum = 0;
4 for (int k = 0; k < n; k*=2){
5 for (int j = 2/n; j <= n; j++) {
6 for (int i = n; i >= 1; i=i/5) {
7 sum += (i+j+k);
8 }
9 }
10 }
11
12 cout<<sum<<endl;
13
14 }
Solution:
3 1
4 log 2 𝑛 + 1
𝑛
5 log 2 𝑛 × + log 2 𝑛
2
𝑛 𝑛
6 log 2 𝑛 × × log 5 𝑛 + log 2 𝑛 ×
2 2
𝑛
7 log 2 𝑛 × × log 5 𝑛
2
10 1
𝑛 𝑛
∴ Time complexity, 𝑇(𝑛) = 1 + log2 𝑛 + 1 + log2 𝑛 × + log2 𝑛 + log2 𝑛 × × log5 𝑛
2 2
𝑛 𝑛
+ log 2 𝑛 × + log 2 𝑛 × × log 5 𝑛 + 1
2 2
1 1
= 1 + log 2 𝑛 + 1 + 𝑛 log 2 𝑛 + log 2 𝑛 + 𝑛 log 2 𝑛 log 5 𝑛
2 2
1 1
+ 𝑛 log 2 𝑛 + 𝑛 log 2 𝑛 log 5 𝑛 + 1
2 2
nurulalamador.github.io/UIUQuestionBank 45
= 2 log 2 𝑛 + 𝑛 log 2 𝑛 + 𝑛 log 2 𝑛 log 5 𝑛 + 3
= 𝑂(𝑛 log 𝑛 log 5 𝑛)
∴ Exact cost equation is 2 log2 𝑛 + 𝑛 log2 𝑛 + 𝑛 log2 𝑛 log5 𝑛 + 3 and time complexity is
𝑂(𝑛 log 𝑛 log 5 𝑛). (Showed)
nurulalamador.github.io/UIUQuestionBank 46