0% found this document useful (0 votes)
17 views323 pages

DSAIIMidBank

The document provides an introduction to asymptotic analysis, focusing on the runtime of algorithms and the classification of function growth. It discusses key concepts such as algorithms, programs, complexity analysis, and various asymptotic notations (Big-O, Big-Theta, and Big-Omega). Additionally, it emphasizes the importance of analyzing algorithms in terms of running time and memory requirements as input sizes increase.

Uploaded by

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

DSAIIMidBank

The document provides an introduction to asymptotic analysis, focusing on the runtime of algorithms and the classification of function growth. It discusses key concepts such as algorithms, programs, complexity analysis, and various asymptotic notations (Big-O, Big-Theta, and Big-Omega). Additionally, it emphasizes the importance of analyzing algorithms in terms of running time and memory requirements as input sizes increase.

Uploaded by

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

Introduction

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

▪ A set of instructions which the computer will follow to solve a


problem

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

instance m:= a[1]; OUTPUT


for i:=2 to size of input
25, 90, 53, 23, 11, 34 if m > a[i] then 11
m:=a[i];
return m

m, a[i] Data-Structure

7
What do we Analyze?
o Correctness
o Does the input/output relation match algorithm requirement?

o Amount of work done (complexity)


o Basic operations to do task

o Amount of space used


o Memory used

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)

▪ We can be more exact if need to be

9
Asymptotic Performance

▪ We care most about asymptotic performance


• How does the algorithm behave as the problem size gets very
large?
o Running time
o Memory/storage requirements
o Bandwidth/power requirements/logic gates/etc.

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

Lower Bound  Running Time  Upper Bound 11


Upper Bound Notation
▪ We say Insertion Sort's run time is O(n2)
▪ Properly we should say run time is in O(n2)
▪ Read O as “Big-Oh” (you’ll also hear it as “order”)

▪ 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

We say g(n) is an asymptotic upper bound for f(n)


13
Lower Bound Notation
▪ We say InsertionSort’s run time is (n) (big-omega or just omega)
▪ In general a function
▪ f(n) is (g(n)) if  positive constants c and n0 such that
▪ 0  cg(n)  f(n)  n  n0
▪ Proof:
▪ Suppose run time is an + b
▪ Assume a and b are positive
▪ an  an + b

14
Lower Bound Notation
time
f(n)

c.g(n)

n0 n

We say g(n) is an asymptotic lower bound for f(n)


15
Asymptotic Tight Bound
▪ A function f(n) is (g(n)) if  positive constants c1, c2, and n0
such that

0  c1 g(n)  f(n)  c2 g(n)  n  n0

▪ 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

We say g(n) is an asymptotic tight bound for f(n)


17
Asymptotic Notation
• O notation: asymptotic “less than”:

f(n)=O(g(n)) implies: f(n) “≤” g(n)

•  notation: asymptotic “greater than”:

f(n)=  (g(n)) implies: f(n) “≥” g(n)

•  notation: asymptotic “equality”:

f(n)=  (g(n)) implies: f(n) “=” g(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

▪ # of bits in the binary representation of the input

▪ vertices and edges in a graph


How do we compare algorithms?
(1) Compare execution times?
Not good: times are specific to a particular computer !!

(2) Count the number of statements executed?


Not good: number of statements vary with the programming
language as well as the style of the individual programmer.
Ideal Solution

▪ Express running time as a function of the input size n (i.e., f(n)).


▪ Compare different functions corresponding to running times.
▪ Such an analysis is independent of machine time, programming style,
etc.
Rate of Growth
▪ The low order terms in a function are relatively insignificant
for large n
n4 + 100n2 + 10n + 50 ~ n4

i.e., we say that n4 + 100n2 + 10n + 50 and n4 have the same


rate of growth
Big-O Notation
▪ We say fA(n)=30n+8 is order n, or O(n)

It is, at most, roughly proportional to n.

▪ fB(n)=n2+1 is order n2, or O(n2). It is, at most, roughly

proportional to n2.

▪ In general, any O(n2) function is faster- growing than any O(n)

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

Both algorithms are of the same order: O(N)


Another Example
▪ Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2 = O(N2)
Big-O Visualization
O(g(n)) is the
set of functions
with smaller or
same order of
growth as g(n)

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)

▪ n = (n), n3 = (n2), n = (logn)


Relations Between Different Sets

▪ Subset relations between order-of-growth sets.

R→R
O(f) (f)

(f)

32
Examples of theta notation
▪ f(n) = n2/2 – n/2 = (n2)

▪ We can express f(n) = O(n2) and f(n) = (n2)

▪ f(n) = 6n3 ≠ (n2)

▪ 6n3 ≠ O(n2) even though 6n3 = (n2)

▪ logn ≠ (n)

▪ logn = O(n) but logn ≠ (n)


Logarithms and properties
▪ In algorithm analysis we often use the notation “log n” without specifying

the base

Binary logarithm lg n = log 2 n log x y = y log x


Natural logarithm ln n = log e n log xy = log x + log y
x
lg k n = (lg n )k log = log x − log y
y
lg lg n = lg(lg n )
a logb x = x b
log a

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.

▪ f(n) = log n2; g(n) = log n + 5 f(n) =  (g(n))

▪ f(n) = n; g(n) = log n2 f(n) = (g(n))


▪ f(n) = log log n; g(n) = log n f(n) = O(g(n))
▪ f(n) = n; g(n) = log2 n f(n) = (g(n))
▪ f(n) = n log n + n; g(n) = log n f(n) = (g(n))
▪ f(n) = 10; g(n) = log 10 f(n) = (g(n))

▪ f(n) = 2n; g(n) = 10n2 f(n) = (g(n))

▪ f(n) = 2n; g(n) = 3n f(n) = O(g(n))


Common Summations
n
n ( n + 1)
▪ Arithmetic series:  k = 1 + 2 + ... + n =
k =1 2
n
x n +1 − 1
▪ Geometric series:

k =0
x = 1 + x + x + ... + x =
k 2 n

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

▪ A function f(n) is (g(n)) if  positive constants c and n0 such


that
c g(n) < f(n)  n  n0
▪ Intuitively,

■ o( ) is like < ■ ( ) is like > ■ ( ) is like =


■ O( ) is like  ■ ( ) is like 

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

 Consider Line 3. How many times


the line 3 executes?
 Best case: 0
 Worst case: 𝑛

9
EXACT COST ANALYSIS: EXAMPLE 1

The running time of this algorithm


therefore belongs to both 𝜴(𝒏) and
𝑶(𝒏). which means it is in 𝚯(𝒏)
 Line 1: 𝑛 + 1
 Line 2: 𝑛
 Line 3
 Best case: 0
 Worst case: 𝑛

10
EXACT COST ANALYSIS: EXAMPLE 2

Line Worst Best

What is the time complexity of the 5


code? Derive the best and worst case
run-time and express in 𝑂 notation. Asym
ptotic
11
EXACT COST ANALYSIS: EXAMPLE 2

Line Worst Best

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

Asym 𝑂(𝑛 𝑙𝑜𝑔2 𝑛) 𝑂(1)


ptotic
12
EXACT COST ANALYSIS: EXAMPLE 2

 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

 Best case: Ω(1)


 Worst case: 𝑂 𝑛 log 2 𝑛 or
𝑂 𝑛𝑙𝑔𝑛

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--)

 Following 2 statements has same time complexity


 𝑘 ← 1; 𝑘 ≤ 𝑛; 𝑘 ← 𝑘 ∗ 2
 𝑘 ← 𝑛; 𝑘 ≥ 1; 𝑘 ← 𝑘/2
16
EXACT COST ANALYSIS: EXAMPLE 3

Line Worst Best


1
log 4 𝑛 = log 22 𝑛 =
2
∗ log 2 𝑛 1

Derive the running-time equations and 5


express in "O" notation
Asym
ptotic
17
EXACT COST ANALYSIS: EXAMPLE 3

Line Worst Best


1
log 4 𝑛 = log 2 𝑛 ∗ log 4 2 = ∗ log 2 𝑛 1 n/3+2 1
2
2 n/3+1 1

3 0 1
𝑛
4 (3 + 1). (log 4 𝑛 + 2) 0
𝑛
5 (3 + 1). (log 4 𝑛 + 1) 0

Asym 𝑂 𝑛 𝑙𝑜𝑔2 𝑛 𝑂(1)


Exact cost equation: ptotic

𝑛 𝑛 𝑛 𝑛
𝑇 𝑛 = 𝑐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

 Best case: Ω(1)


 Worst case: 𝑂 𝑛 log 2 𝑛 or
𝑂 𝑛𝑙𝑔𝑛

The running time of this algorithm therefore belongs to both 𝜴(𝟏) and 𝑶(𝒏𝒍𝒈𝒏)

20
21
EXACT COST ANALYSIS: EXAMPLE 4

Line Worst Best

Asym
ptotic
Derive the running-time equations and
express in "O" notation

22
EXACT COST ANALYSIS: EXAMPLE 4

Line Worst Best

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

Line Worst Best

Derive the running-time equations and 5


express in "O" notation
Asym
ptotic
28
EXACT COST ANALYSIS: EXAMPLE 5
𝑖 #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 22 − 1 + 1 // Inner statements run once

3 33 − 1 + 1 // Inner statements run twice

4 42 − 1 + 1 // Inner statements run thrice

𝑛 (𝑛2 − 1) + 1

Total (22 +32 + ⋯ + 𝑛2 ) − (1 + ⋯ + 1) +


cost (𝑛 + 1)
= ((12 + 22 + 32 + ⋯ + 𝑛2 − 1) − (1 +
⋯ + 1)) + (𝑛 + 1)
𝑛 𝑛+1 2𝑛+1
= −1−⋯ 29
6
𝟑
= 𝑶(𝒏 )
EXACT COST ANALYSIS: EXAMPLE 5

𝑖 #times Line 3 runs

0 0 // does not run.


1 0 // does not run.
2 22 − 1
3 33 − 1
4 42 − 1
𝑛 (𝑛2 − 1)
Total (22 +32 + ⋯ + 𝑛2 ) − (1 + ⋯ + 1)
cost =
𝑛 𝑛+1 2𝑛+1
−1−⋯
6
3
= 𝑂(𝑛 )

30
EXACT COST ANALYSIS: EXAMPLE 6

Line Worst Best


1
2
3
4
5

Derive the running-time equations and


express in "O" notation Asym
ptotic 31
EXACT COST ANALYSIS: EXAMPLE 6

Line Worst Best


1 𝑛 same
𝑐1 ∗ ( + 2)
2
𝑛
2 𝑐2 ∗
2
+ 1 ∗ (log 2 𝑛 + 1) same
3 𝑛 same
𝑐3 ∗ + 1 ∗ log 2 𝑛
2
7 𝑐7 ∗ (𝑚 + 2) same
n
8 𝑐8 ∗ 𝑚 + 1 ∗ ( + 1)
2
same
n
9 𝑐8 ∗ 𝑚 + 1 ∗
2
same
Derive the running-time equations and Asym 𝑂(𝑚𝑛) same
express in "O" notation ptotic

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

Example of best case:


A= any array
B={-1,2,3,4,5}
//1st element is negative and
other values can be anything

Example of worst case:


A= any array
B={3,5,6,2,6}
Any array with all positive
elements

34
EXACT COST ANALYSIS: EXAMPLE 8

Line worst best

1,2 1 1

3 m+1 1

4 m*(n+1) 1

5 m*n 1

6 0 1

9 1 0

Asymptotic O(mn) O(1)


In best case, the condition in line 5 will 35
notation
be true for the 1st iteration and return.
EXACT COST ANALYSIS: EXAMPLE 8
Example of best case:
Both arrays have same 1st
element
A={1,2,3,4}
B={1,4,4,5}

Example of worst case:


No common elements in
the arrays
A={1,2,3,4}
B={6,7,8,9}

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

 Which picture shows the asymptotic tight bound?


 Show that 𝑓 𝑛 = 𝑎𝑛3 + 𝑏𝑛2 + 𝑐𝑛 + 𝑑 is 𝑂(𝑛3 )
 Show that 𝑓 𝑛 = 𝑎𝑛2 + 𝑏𝑛 + 𝑐 is not 𝑂(𝑛)
 Show that 𝑓 𝑛 = 𝑎𝑛2 + 𝑏𝑛 + 𝑐 is 𝑂(𝑛3 )
 Show that 𝑓 𝑛 = 𝑎𝑛2 + 𝑏𝑛 + 𝑐 is Θ(𝑛2 )

39
QUICK EVALUATION 2

 What is the time complexity of the


code?
 Derive the exact cost equation and
express in O notation

https://www.geeksforgeeks.org/practice-questions-time-
complexity-analysis/

40
QUICK EVALUATION 2

 What is the time complexity of the


code?
 Derive the exact cost equation and
express in O notation

https://www.geeksforgeeks.org/practice-questions-time-
complexity-analysis/

41
QUICK EVALUATION 3

 What is the time complexity of the


code?
 Derive the exact cost equation and
express in O notation

The condition for Line 2 will always be true.


Hence the outer loop will be an infinite loop.
Time complexity, T(n)=∞ 42
QUICK EVALUATION 4

 Find the best case and worst case


time complexity and represent them
using asymptotic notation.

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

We will discuss the 1st 2 methods in this course.


Master theorem
• Consider the recurrence relation

where n = size of the problem


a = number of subproblems in the recursion and a >= 1
n/b = size of each subproblem
b > 1, k >= 0 and p is a real number.
Then,
1.if a > bk, then T(n) = θ(𝑛log𝑏 𝑎 )

2.if a = bk, then


(a) if p > -1, then T(n) = θ(𝑛log𝑏 𝑎 logp+1n)
(b) if p = -1, then T(n) = θ(𝑛log𝑏 𝑎 loglogn)
(c) if p < -1, then T(n) = θ(𝑛log𝑏 𝑎 )

3.if a < bk, then


(a) if p >= 0, then T(n) = θ(nk logpn)
(b) if p < 0, then T(n) = θ(nk)
Examples
More practices
Recurrence relation solution
n 𝑂(𝑛)
𝑇 𝑛 = 2T +O 1
2
𝑛 𝑂(𝑛2 )
𝑇 𝑛 = 4𝑇 + 𝑂(𝑛)
2
𝑛 𝑂(𝑛 log 𝑛)
𝑇 𝑛 = 2𝑇 + 𝑂(𝑛)
2
𝑛 𝑂(𝑛2 (log 𝑛)6 )
𝑇 𝑛 = 4𝑇 + 𝑛2 (log 𝑛)5
2
𝑛 𝑛 𝑂(𝑛 log log 𝑛)
𝑇 𝑛 = 2𝑇 +
2 log 𝑛
𝑛 𝑂(𝑛)
𝑇 𝑛 = 2𝑇 + 𝑛(log 𝑛)−2
2
Some Important Formulas
𝑛(𝑛+1)
1) 1 + 2 + …+ 𝑛 =
2
2) 20 + 21 + 22 + … + 2 𝑛
= σ𝑛𝑘=0 2𝑘 = 2𝑛+1 − 1
𝑥 𝑛+1 −1
3) 1 + 𝑥 + 𝑥2 + 𝑥3+ …+ 𝑥𝑛 𝑛
= σ𝑘=0 𝑥 = 𝑘
𝑥−1
1− 𝑥 𝑛+1
if 𝑥 < 1 , then = σ𝑛𝑘=0 𝑥 𝑘 =
1−𝑥
4) log 1 + log 2 + log 3 + … + log 𝑛 ≈ 𝑛 log 𝑛
1
5) 1𝑝 +2𝑝 + 3𝑝 + … + 𝑛𝑝 = σ𝑛𝑘=1 𝑘 𝑝 ≈ 𝑛𝑝+1
𝑝+1

***Important note: if 𝑥 < 1 , then 𝑥 𝑠𝑜𝑚𝑒𝑡ℎ𝑖𝑛𝑔 < 1


Solving recurrence using recursive tree

• Steps to solve recurrence relation using recursion


tree method:
1.Draw a recursive tree for given recurrence relation
2.Calculate the cost at each level and count the total no of levels
in the recursion tree.
3.Count the total number of nodes in the last level and calculate
the cost of the last level
4.Sum up the cost of all the levels in the recursive tree
𝑛
𝑇 𝑛 = 2𝑇 + 𝑘𝑛
2
Draw recursive tree level Size of each Cost at each level
subproblem (cost for each subproblem=kn
of size n))
0 𝑛 1 ∗ 𝑘𝑛 = 𝑘𝑛
20
1 𝑛 𝑘𝑛
21 2 ∗ = 𝑘𝑛
2
2 𝑛 𝑘𝑛
22 4 ∗ = 𝑘𝑛
4
… … …
p(last) 𝑛 𝑘𝑛
2𝑝 2𝑝 ∗ 𝑝 = 𝑘𝑛
2
𝑛
At last level, size of each problem is 1. solving 2𝑘 = 1, we get p=log 2 𝑛
Total levels = log 2 𝑛
Total cost = kn (cost of level 0)+ kn* log 2 𝑛 = 𝑶(𝒏 log 𝒏)
Divide-and-Conquer Technique

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Divide-and-Conquer
Divide-and-Conquer is a general
algorithm design paradigm:
Divide the problem into a number of
subproblems that are smaller
instances of the same problem
Conquer the subproblems by solving
them recursively
Combine the solutions to the
subproblems into the solution for the
original problem
The base case for the recursion are
subproblems of constant size
Analysis can be done using recurrence
equations

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Divide-and-Conquer
We will discuss the following problems
Merge sort
Find maximum and minimum
Find maximum subarray

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Divide-and-Conquer Technique:
Merge Sort

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Merge Sort and Quick Sort

Two well-known sorting algorithms adopt this divide-and-


conquer strategy

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

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Merge Sort: Algorithm

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Merge Sort: Algorithm

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Merge Sort: Example

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Execution Example

Partition
7 2 9 43 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

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

Merge Sort 9
Execution Example

Recursive call, partition


7 2 9 43 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

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

Merge Sort 10
Execution Example

Recursive call, partition


7 2 9 43 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

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

Merge Sort 11
Execution Example

Recursive call, base case


7 2 9 43 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

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

Merge Sort 12
Execution Example

Recursive call, base case


7 2 9 43 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

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

Merge Sort 13
Execution Example

Merge
7 2 9 43 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

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

Merge Sort 14
Execution Example

Recursive call, …, base case, merge


7 2 9 43 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

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

Merge Sort 15
Execution Example

Merge
7 2 9 43 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

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

Merge Sort 16
Execution Example

Recursive call, …, merge, merge


7 2 9 43 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 6 8

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

Merge Sort 17
Execution Example

Merge
7 2 9 43 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 6 8

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

Merge Sort 18
Merge Sort: Running Time

The recurrence for the worst-case running time T(n) is

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

Solve this recurrence by


(1) iteratively expansion
(2) using the recursion tree

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Merge Sort: Running Time (Iterative Expansion)
T (n ) = 2T (n / 2) + bn
= 2(2T (n / 22 )) + b(n / 2)) + bn
= 22 T (n / 22 ) + 2bn
= 23 T (n / 23 ) + 3bn
= 24 T (n / 24 ) + 4bn
= ...
= 2i T (n / 2i ) + ibn

Note that base, T(n) =b, case occurs when 2i = n.


That is, i = log n.
So, T ( n ) = bn + bn log n

Thus, T(n) is O(n log n).


Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Merge Sort: Running Time (Recursion Tree)

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

… … … …

Total time = bn + bn log n


(last level plus all previous levels)

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Divide-and-Conquer Technique:
Finding Maximum & Minimum

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Divide-and-Conquer

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

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Finding Maximum and Minimum

• Input: an array A[1..n] of n numbers


• Output: the maximum and minimum value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A 13 -13 -25 20 -3 -16 -23 18 20 -7 12 -5 -22 15 -4 7

List List 1 List 2


n elements n/2 elements n/2 elements

min, max min1, max1 min2, max2

min = MIN ( min1, min2 )


max = MAX ( max1, max2 )
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Finding Maximum and Minimum

The straightforward algorithm:

max ← min ← A (1);


for i ← 2 to n do
if (A (i) > max) then max ← A (i);
if (A (i) < min) then min ← A (i);

No. of comparisons: 2(n – 1)

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Finding Maximum and Minimum
The Divide-and-Conquer algorithm:
procedure Rmaxmin (i, j, fmax, fmin); // i, j are index #, fmax,
begin // fmin are output parameters
case:
i = j: fmax ← fmin ← A[i];
i = j –1: if A[i] < A[j] then fmax ← A[j];
fmin ← A[i];
else fmax ← A[i];
fmin ← A[j];
else: mid ← (i + j)/2;
call Rmaxmin (i, mid, gmax, gmin);
call Rmaxmin (mid+1, j, hmax, hmin);
fmax ← MAX (gmax, hmax);
fmin ← MIN (gmin, hmin);
end
end;
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Finding Maximum and Minimum
Example: find max and min in the array:
22, 13, -5, -8, 15, 60, 17, 31, 47 ( n = 9 )

Index: 1 2 3 4 5 6 7 8 9
Array: 22 13 -5 -8 15 60 17 31 47

(1)
(6)

(2) (5) (7) (8)

(3) (4)

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Finding Maximum and Minimum
Example: find max and min in the array:
22, 13, -5, -8, 15, 60, 17, 31, 47 ( n = 9 )

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, 3, 22, -5 4, 5, 15, -8 6, 7, 60, 17 8, 9, 47, 31


(3) (4)

1, 2, 22, 13 3, 3, -5, -5

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Finding Maximum and Minimum

The recurrence for the worst-case running time T(n) is

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

By solving the recurrence, we get


T(n) is O( n )

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Divide-and-Conquer Technique:
Maximum Subarray problem

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Maximum Subarray Problem

• Input: an array A[1..n] of n numbers


– Assume that some of the numbers are negative,
because this problem is trivial when all numbers
are nonnegative
• Output: a nonempty subarray A[i..j] having the
largest sum S[i, j] = ai + ai+1 +... + aj

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

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Target array : 1 -4 3 2
What is a maximum
1 1
subarray?
All the sub arrays:

-4 -4 Ans: The subarray


3 3
with the largest sum
2 2

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

All possible contiguous subarrays


A[1..1], A[1..2], A[1..3], ..., A[1..(n-1)], A[1..n]
A[2..2], A[2..3], ..., A[2..(n-1)], A[2..n]
...
A[(n-1)..(n-1)], A[(n-1)..n]
A[n..n]

How many of them in total? O(n2)

Algorithm: For each subarray, compute the sum.


Find the subarray that has the maximum sum.
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

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Brute-Force Algorithm

Outer loop: index variable i to indicate start of subarray,


for 1 ≤ i ≤ n, i.e., A[1], A[2], ..., A[n]
for i = 1 to n do ...

Inner loop: for each start index i, we need to go through


A[i..i], A[i..(i+1)], ..., A[i..n]
use an index j for i ≤ j ≤ n, i.e., consider A[i..j]
for j = i to n do ...

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Brute-Force Algorithm
max = -∞
for i = 1 to n do Time
complexity?
begin
O(n2)
sum = 0
for j = i to n do
begin
sum = sum + A[j]
if sum > max
then max = sum
end
end

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Divide-and-Conquer Algorithm
Possible locations of a maximum subarray A[i..j] of
A[low..high], where mid = (low + high)/2
▪ entirely in A[low..mid] (low  i  j  mid)
▪ entirely in A[mid+1..high] (mid < i  j  high)
▪ crossing the midpoint (low  i  mid < j  high)

crossing the midpoint

low mid high

mid +1

entirely in A[low..mid] entirely in A[mid+1..high]


Possible locations of subarrays of A[low..high]
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Divide-and-Conquer Algorithm
FIND-MAX-CROSSING-SUBARRAY (A, low, mid, high)
left-sum = -∞ // Find a maximum subarray of the form A[i..mid]
sum = 0
for i = mid downto low
sum = sum + A[i ]
if sum > left-sum
left-sum = sum
max-left = i
right-sum = -∞ // Find a maximum subarray of the form A[mid + 1 .. j ]
sum =0
for j = mid +1 to high
sum = sum + A[j]
if sum > right-sum
right-sum = sum
max-right = j
// Return the indices and the sum of the two subarrays
return (max-left, max-right, left-sum + right-sum)
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Divide-and-Conquer Algorithm

A[mid+1..j]

low i mid high

mid +1 j

A[i..mid]

A[i..j] comprises two subarrays A[i..mid] and A[mid+1..j]

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Divide-and-Conquer Algorithm
mid =5
1 2 3 4 5 6 7 8 9 10

A 13 -3 -25 20 -3 -16 -23 18 20 -7

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

A 13 -3 -25 20 -3 -16 -23 18 20 -7

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.

T(n) = 2T(n/2) + (n)


= (n lg n) (similar to merge-sort)

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Divide-and-Conquer Technique:
More problems

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Count even numbers
• Count the even numbers in an array using divide and
conquer technique.
• Write a pseudocode and simulate it.
• Also find the time complexity.

Time complexity:
𝑛
𝑇 𝑛 = 2𝑇 +𝑂 1
2
𝑇 𝑛 = 𝑂(𝑛)

44
Conclusion: Divide-and-Conquer

▪ This Divide and conquer algorithm is clearly substantially


faster than any of the brute-force methods. It required
some cleverness, and the programming is a little more
complicated – but the payoff is large.

▪ Divide and conquer is just one of several powerful


techniques for algorithm design
▪ Divide-and-conquer algorithms can be analyzed using
recurrences
▪ Can lead to more efficient algorithms

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Algorithms:
Greedy Method

An Activity-Selection Problem

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Greedy Algorithms: Principles

 A greedy algorithm always makes the


choice that looks best at the moment.
 A greedy algorithm works in phases.
At each phase:
 You take the best you can get right now,
without regard for future consequences.
 You hope that by choosing a local optimum
at each step, you will end up at a global
optimum.
 For some problems, it works.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


An Activity Selection Problem
 Input: A set of activities S = {a1,…, an}
 Each activity ai has a start time si and a finish time fi,
where 0 ≤ si < fi < ∞
 If selected, activity ai takes place during the half-open
time interval [si, fi)

 Two activities are compatible if and only if their


intervals do not overlap

 Output: a maximum-size subset of mutually


compatible activities

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


The Activity Selection Problem
 Here are a set of start and finish times
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fi 4 5 6 7 8 9 10 11 12 13 14

 What is the maximum number of activities that can be


completed?
• {a3, a9, a11} can be completed
• But so can {a1, a4, a8, a11} which is a larger set
• But it is not unique, consider {a2, a4, a9 , a11}

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Interval Representation
 Here are a set of start and finish times
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fi 4 5 6 7 8 9 10 11 12 13 14

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Early Finish Greedy
 Select the activity with the earliest finish
 Eliminate the activities that could not be scheduled
 Repeat!

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Assuming activities are sorted by finish time

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Algorithms:
Greedy Method

Knapsack Problem

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Greedy Algorithms: Principles

 A greedy algorithm always makes the


choice that looks best at the moment.
 A greedy algorithm works in phases.
At each phase:
 You take the best you can get right now,
without regard for future consequences.
 You hope that by choosing a local optimum
at each step, you will end up at a global
optimum.
 For some problems, it works.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


The Knapsack Problem
The famous Knapsack Problem:
A thief breaks into a museum. Fabulous paintings, sculptures, and
jewels are everywhere. The thief has a good eye for the value of
these objects, and knows that each will fetch hundreds or thousands
of dollars on the clandestine art collector’s market. But, the thief
has only brought a single knapsack to the scene of the robbery, and
can take away only what he can carry. What items should the thief
take to maximize the haul?

Which items
should I take?

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


The Knapsack Problem

There are two versions of the problem:


(1) “0-1 knapsack problem”
Items are indivisible: you either take an item or
not. Solved with dynamic programming.

(2) “Fractional knapsack problem”


Items are divisible: you can take any fraction
of an item. Solved with a greedy algorithm.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


The Knapsack Problem
 More formally, the 0-1 knapsack problem:
 The thief must choose among n items, where the ith item
worth vi dollars and weighs wi pounds
 Carrying at most W pounds, maximize value
 Note: assume vi, wi, and W are all integers
 “0-1” because each item must be taken or left in entirety

 A variation, the fractional knapsack problem:


 Thief can take fractions of items
 Think of items in 0-1 problem as gold ingots, in fractional
problem as buckets of gold dust.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Optimal Substructure Property
 Both problems exhibit the optimal substructure property.
 To show this for both the problems, consider the most valuable
load weighing at most W pounds
 Q: If we remove item j from the load, what do we know about
the remaining load?
 A: The remaining load must be the most valuable load weighing
at most W - wj that the thief could take from the n-1 original
items excluding item j.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Fractional Knapsack Problem
 Knapsack capacity: W

 There are n items: the i-th item has value vi and weight wi

 Goal:

 find xi such that for all 0  xi  1, i = 1, 2, .., n

 wixi  W and

 xivi is maximum

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Fractional Knapsack - Example

20
$80
---
Item 3 30 +

Item 2 50 50
20 $100
Item 1 30
20 +
10 10 $60

$60 $100 $120 $240

$6/pound $5/pound $4/pound

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Fractional Knapsack Problem
Greedy strategy:
 Pick the item with the maximum value per pound vi/wi

 If the supply of that element is exhausted and the thief can


carry more: take as much as possible from the item with the
next greatest value per pound

 It is good to order items based on their value per pound


v1 v2 vn
  ... 
w1 w2 wn

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Fractional Knapsack Problem
Alg.: Fractional-Knapsack (W, v[n], w[n])

1. while w > 0 and as long as there are items remaining

2. pick item with maximum vi/wi

3. xi  min (1, w/wi)

4. remove item i from list

5. w  w – xiwi
 w – the amount of space remaining in the knapsack (initially w = W)

 Running time: (n) if items already ordered; else (nlogn)

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


0-1 Knapsack problem
 Thief has a knapsack with maximum capacity W, and a set S
consisting of n items
 Each item i has some weight wi and benefit value vi (all wi , vi
and W are integer values)
 Problem: How to pack the knapsack to achieve maximum total
value of packed items?
 Goal:
find xi such that for all xi  {0, 1}, i = 1, 2, .., n
 wixi  W and
 xivi is maximum

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


0-1 Knapsack - Greedy Strategy Fails

Item 3 30 $120

50 50 +
Item 2 50
20 $100
Item 1 30
20 + 20 $100
10 10 $60

$60 $100 $120 W $160 $220

$6/pound $5/pound $4/pound

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Greedy Algorithms

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

• Greedy algorithms don’t always yield an optimal solution


• Makes the choice that looks best at the moment in order to
get 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

 We can store all codewords in a binary tree – very easy to decode


 Coded characters in leaves
 Each node contains the sum of the frequencies of all descendants

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.

• The Huffman code is a prefix-free code.


– No prefix of a code word is equal to another
codeword.

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:

• Find a variable length prefix-free encoding scheme that compresses


this data file as much as possible?

8
Huffman Algorithm - Idea
• Huffman algorithm,
builds the code trie
bottom up. Consider a
forest of trees: A+B

– Initially – one separate 0 1


A B A B
node for each character.
– In each step – join two
trees into a larger tree

 Repeat this until one tree remains.


 Which trees to join? Greedy choice – the trees with the smallest frequencies!

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

Total File Length: 210 characters


We need at least 3 bits to represent the 6 characters.
Total file size= 210*3 = 630 bits
10
Algorithm Run:
A 10 B 20 C 30 D 40 E 50 F 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

File Size: 10x4 + 20x4 + 30x3 + 40x2 + 50x2 + 60x2 =


40 + 80 + 90 + 80 + 100 + 120 = 510 bits 20
Note the savings:

The Huffman code:


Required 510 bits for the file.
Fixed length code:
Need 3 bits for 6 characters.
File has 210 characters.

Total: 630 bits for the file.


Percentage of savings = (630-510)*100 / 630
= 19% (approx.)
21
Huffman Code
• Reduce size of data by 20%-90% in general

• If no characters occur more frequently than others,


then no advantage over ASCII

• 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

What is the sequence of characters corresponding to the


following code?

110111100111010

24
a : 1, b : 1, c : 2, d : 3, e : 5, f : 8, g : 13, h : 21

25
Dynamic Programming:

Computing Fibonacci Numbers

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Algorithmic Paradigms
 Greedy: Build up a global solution incrementally, myopically
by optimizing some local criterion.

 Divide-and-conquer: Break up a problem into disjoint (non-


overlapping) sub-problems, solve the sub-problems recursively,
and then combine their solutions to form solution to the original
problem. Brand-new subproblems are generated at each step of
the recursion.

 Dynamic programming: Break up a problem into a series of


overlapping sub-problems, and build up solutions to larger and
larger sub-problems. Typically, same subproblems are
generated repeatedly when a recursive algorithm is run.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 2


Dynamic Programming History
 Bellman. [1950s] Pioneered the systematic study of
dynamic programming.

 Etymology.
 Dynamic programming = planning over time.
 Secretary of Defense was hostile to mathematical research.
 Bellman sought an impressive name to avoid confrontation.

Reference: Bellman, R. E. Eye of the Hurricane, An Autobiography.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 3


Dynamic Programming Applications
 Areas.
 Bioinformatics.
 Control theory.
 Information theory.
 Operations research.
 Computer science: theory, graphics, AI, compilers,
systems, ….

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 4


Properties of a Problem that can be
Solved with Dynamic Programming
 Simple Subproblems
 We should be able to break the original problem to smaller
subproblems that have the same structure
 Optimal Substructure of the Problems
 The solution to the problem must be a composition of
subproblem solutions
 Subproblem Overlap
 Optimal subproblems to unrelated problems can contain
subproblems in common
 The Number of Distinct Subproblems is Small
 The total number of distinct subproblems is a polynomial
in the input size

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Computing Fibonacci Numbers
 Fibonacci numbers:
 F0 = 0
 F1 = 1
 Fn = Fn - 1 + Fn - 2 for n > 1
Sequence is 0, 1, 1, 2, 3, 5, 8, 13, …

 Obvious recursive algorithm (Sometimes can be inefficient):


Fib(n):
if n = 0 or 1 then
return n
else
return ( Fib(n  1) + Fib(n  2) )

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Recursion Tree for Fib(5)

Fib(5)

Fib(4) Fib(3)

Fib(3) Fib(2) Fib(2) Fib(1)

Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0)

Fib(1) Fib(0)

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


How Many Recursive Calls?
 If all leaves had the same depth, then there would be about 2n
recursive calls.
 But this is over-counting.
 However with more careful counting it can be shown that it is
Ω((1.6)n)
 Still exponential!

 Wasteful approach - repeat work unnecessarily


 Fib(2) is computed three times
 Instead, compute Fib(2) once, store result in a table, and
access it when needed

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


More Efficient Recursive Algorithm

 F[0] := 0; F[1] := 1; F[n] := Fib(n);

 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] )

 computes each F[i] only once, store result in a table, and


access it when needed.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Example of Memoized Fib

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

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Get Rid of the Recursion
 Recursion adds overhead
 extra time for function calls
 extra space to store information on the runtime stack about
each currently active function call
 Avoid the recursion overhead by filling in the table
entries bottom up, instead of top down.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Subproblem Dependencies
 Figure out which subproblems rely on which other
subproblems
 Example:

F0 F1 F2 F3 … Fn-2 Fn-1 Fn

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Order for Computing Subproblems
 Then figure out an order for computing the subproblems
that respects the dependencies:
 when you are solving a subproblem, you have already
solved all the subproblems on which it depends
 Example: Just solve them in the order
F0 , F1 , F2 , F 3 , …

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


DP Solution for Fibonacci

 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

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Algorithms:
Dynamic Programming

Coin Change Problem

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem

Given unlimited amounts of coins of denominations c1 > … > cd ,


give change for amount M with the least number of coins.

Example: c1 = 25, c2 =10, c3 = 5, c4 = 1 and M = 48


Greedy solution: 25*1 + 10*2 + 1*3 = c1 + 2c2 + 3c4

Greedy solution is
 optimal for any amount and “normal’’ set of denominations

 may not be optimal for arbitrary coin denominations

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Greedy Choice Principles
 Suppose you want to count out a certain
amount of money, using the fewest
possible coins.
 At each step, take the largest possible
coin that does not overshoot.

 Example: To make Tk. 157/-, you,


 Choose a Tk. 100/- note,
 Choose a Tk. 50/- note,
 Choose a Tk. 5/- coin,
 Choose a Tk. 2/- coin.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Greedy Choice Principles: Failure
 To find the minimum number of US coins to make any amount,
the greedy method always works
 At each step, just choose the largest coin that does not overshoot the
desired amount: 31¢ = (25+5+1)

 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)

 The greedy algorithm results in a solution, but always not in an


optimal solution
 How can we find the minimum number of coins for any given
coin set?
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Coin Change Problem: Example
Given the denominations 1, 3, and 5, what is the
minimum number of coins needed to make change
for a given value?

Value 1 2 3 4 5 6 7 8 9 10
Min # of coins 1 1 1

Only one coin is needed to make change for the


values 1, 3, and 5

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: Example
Given the denominations 1, 3, and 5, what is the
minimum number of coins needed to make change
for a given value?

Value 1 2 3 4 5 6 7 8 9 10
Min # of coins 1 2 1 2 1 2 2 2

However, two coins are needed to make change for


the values 2, 4, 6, 8, and 10.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: Example
Given the denominations 1, 3, and 5, what is the
minimum number of coins needed to make change
for a given value?

Value 1 2 3 4 5 6 7 8 9 10
Min # of coins 1 2 1 2 1 2 3 2 3 2

Lastly, three coins are needed to make change for the


values 7 and 9

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: Recurrence

This example is expressed by the following


recurrence relation:

minNumCoins(M-1) + 1
min minNumCoins(M-3) + 1
minNumCoins(M) =
of
minNumCoins(M-5) + 1

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: Recurrence
Given the denominations c: c1, c2, …, cd, the
recurrence relation is:

minNumCoins(M-c1) + 1

min minNumCoins(M-c2) + 1
minNumCoins(M) =
of …
minNumCoins(M-cd) + 1

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: A Recursive Algorithm

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

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


RecursiveChange is not Efficient

 It recalculates the optimal coin combination for a given


amount of money repeatedly
 i.e., M = 77, c = (1, 3, 7):
 Optimal coin for 70 cents is computed 9 times!

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


The RecursiveChange Tree

77

76 74 70

75 73 69 73 71 67 69 67 63

747268 686662 706864 686662 626056


727066 727066 666460 666460

... ..
70 70 70 70 70
.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
We Can Do Better

 We are re-computing values in our algorithm more than


once

 Save results of each computation for 0 to M

 This way, we can do a reference call to find an already


computed value, instead of re-computing each time

 Running time M*d, where M is the value of money and d


is the number of denominations

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: Dynamic Programming

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

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


DPChange: Example

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

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem

Goal: Convert some amount of money M into given


denominations, using the fewest possible number of coins

Input: An amount of money M, and an array of d


denominations c = (c1, c2, …, cd), in a decreasing order of
value (c1 > c2 > … > cd)

Output: A list of d integers i1, i2, …, id such that


c1i1 + c2i2 + … + cdid = M and i1 + i2 + … + id is minimal

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Algorithms:
Dynamic Programming

0-1 Knapsack Problem

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


The Knapsack Problem (Review)

There are two versions of the problem:


(1) “0-1 knapsack problem”
Items are indivisible: you either take an item or not.
Solved with dynamic programming.

(2) “Fractional knapsack problem”


Items are divisible: you can take any fraction of
an item. Solved with a greedy algorithm.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Optimal Substructure Property
 Both problems exhibit the optimal substructure property.
 To show this for both the problems, consider the most valuable
load weighing at most W pounds
 Q: If we remove item j from the load, what do we know about the
remaining load?
 A: The remaining load must be the most valuable load weighing
at most W - wj that the thief could take from the n-1 original items
excluding item j.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


0-1 Knapsack Problem
 Thief has a knapsack with maximum capacity W, and a set S
consisting of n items
 Each item i has some weight wi and benefit value vi (all wi , vi
and W are integer values)
 Problem: How to pack the knapsack to achieve maximum total
value of packed items?
 Goal:
find xi such that for all xi  {0, 1}, i = 1, 2, .., n
 wixi  W and
 xivi is maximum

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


0-1 Knapsack - Greedy Strategy Fails

Item 3 30 $120

50 50 50 +
Item 2
20 $100
Item 1 30
20 + 20 $100
10 10 $60

$60 $100 $120 W $160 $220

$6/pound $5/pound $4/pound

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


0-1 Knapsack: Brute-Force Approach

 Since there are n items, there are 2n possible combinations of


items.
 We go through all combinations and find the one with the
most total value and with total weight less or equal to W.
 Running time will be O(2n).

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


0-1 Knapsack - Dynamic Programming

 P(i, w) – the maximum profit that can be


obtained from items 1 to i, if the
knapsack has size w
 Case 1: thief takes item i
P(i, w) = vi + P(i - 1, w-wi)
 Case 2: thief does not take item i
P(i, w) = P(i - 1, w)

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Recursive Formula
 P[i  1, w] if w  wi
P[i, w]  
 max{vi  P[i  1, w  wi ], P[i  1, w]} else

 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.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Dependencies among Subproblems

Item i was taken Item i was not taken

P(i, w) = max {vi + P(i - 1, w-wi), P(i - 1, w) }


0: 1 w - wi w W

0 0 0 0 0 0 0 0 0 0 0 0
0 first
0 second
i-1 0

i 0
0
n 0

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Overlapping Subproblems

P(i, w) = max {vi + P(i - 1, w-wi), P(i - 1, w) }


0: 1 w W

0 0 0 0 0 0 0 0 0 0 0 0
0
0
i-1 0

i 0
0
n 0

E.g.: All the subproblems shown in grey may


depend on P(i-1, w)
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
W = 5
Item Weight Value
Example:
1 2 12
P(i, w) = max {vi + P(i - 1, w-wi), P(i - 1, w) }
2 1 10
0 1 2 3 4 5 3 3 20
0 0 0 0 0 0 0 P(1, 1) = P(0, 1) = 0 4 2 15
1 0 0 12 12 12 12 P(1, 2) = max{12+0, 0} = 12
2 0 10 12 22 22 22 P(1, 3) = max{12+0, 0} = 12
3 0 10 12 22 30 32 P(1, 4) = max{12+0, 0} = 12
4 0 10 15 25 30 37 P(1, 5) = max{12+0, 0} = 12

P(2, 1)= max{10+0, 0} = 10 P(3, 1)= P(2,1) = 10 P(4, 1)= P(3,1) = 10


P(2, 2)= max{10+0, 12} = 12 P(3, 2)= P(2,2) = 12 P(4, 2)= max{15+0, 12} = 15
P(2, 3)= max{10+12, 12} = 22 P(3, 3)= max{20+0, 22}=22 P(4, 3)= max{15+10, 22}=25
P(2, 4)= max{10+12, 12} = 22 P(3, 4)= max{20+10,22}=30 P(4, 4)= max{15+12, 30}=30
P(2, 5)= max{10+12, 12} = 22 P(4, 5)= max{20+12,22}=32 P(4, 5)= max{15+22, 32}=37
Reconstructing the Optimal Solution
0 1 2 3 4 5
0 0 0 0 0 0 0 • Item 4

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

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


0 0 0 0 0 0
0
0
0
0

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


United International University
Department of Computer Science and Engineering
CSI 227: Algorithms, Mid Exam, Spring 2017
Total Marks: 90, Time: 1 hour 45 minutes

Answer any 6 out of 8 questions (6 × 15 = 90).

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]

5. (a) Show that 5n2 + 2n + 1 = Θ(n2 ) [5]


(b) Describe an algorithm that returns the second maximum element from a Max-Priority Queue data
structure in time O(log n) or better. [Hint: How about temporarily deleting the maximum element
from the priority queue?] [7.5]
(c) Why is the worst case running-time of the Max-Heapify algorithm O(log n)? [2.5]

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

Answer any 6 out of the following 8 questions (6 × 15 = 90).

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]

Figure 1: COIN-CHANGE Figure 2: CUT-ROD


7. (a) Provide a recursive equation for the running-time T (n) for the Divide-and-Conquer algorithm provided
at figure 3, in terms of the input array (A) size n, where n = r − l + 1. You must mention the costs of
each of the step of this divide-and-conquer algorithm. [6]
(b) Design examples with |A| = n = 6 items of both a worst-case and a best-case for the algorithm
provided at figure 4. Also mention the runtime complexities for both cases in O notation. [3 + 3 + 3]

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

Answer any 6 out of the following 8 questions (6 × 15 = 90).

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

algorithm to solve his problem. [15]

Section B

4. (a) Provide the recurrence relation for the divide-and-conquer function in Fig. 3 in terms of input array

(A) size n, where n = h − l + 1. [5]

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

crossing sum for each tree node. [10]

(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

Substructure Property”. [7]

(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

the recursion tree for calculation of expecto patronum(3). [8]

(b) Using the substitution method, determine a good asymptotic upper bound on the following recurrence:

T (n) = T (n − 1) + n. Show details of your calculation. [7]

2
United International University
CSI 227: Algorithms, Summer 2018
Mid-Term Exam
Total Marks: 90, Time: 1 hour 45 minutes

Answer all questions

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-

rence: T (n) = 4T ( n3 ) + n2 . Show details of your calculation. [15]

Figure 1: Q. 1(a) Figure 2: Q. 1(b)

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

of each of the divide, conquer and combine steps? [9+3]

(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

crossing sum for each tree node. [8]

3. (a) Provide an example where the greedy strategy fails for 0/1 knapsack problem for n = 4 items with the

max-capacity of W = 60kg. [5]

(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

next one. [10]


4. (a) A Dynamic Programming algorithm for the classic Coin Change problem is provided at Fig. 3 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. [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

There are FOUR questions. Answer ALL questions.

1. a) Analyze both best and worst case time-complexities of the algorithm of Figure 1. 7+7

n = length[A]; m = length[B]; i = 1; sum = 0;


while (i <= n) do {
print A[ i ];
for ( j ← n; j >= 1; j = j / 3) do {
if (A[ i ] + A[ j ] < 100) then
break;
for ( k ← 1; k <= n; k = k + 2) do
print A[ k ];
}
i = i + 1;
}
for ( i ← 1; i < m; i ++) do {
if (B[ i ] > 50) then
return –1;
else
sum += B[ i ] * B[ i ];
}
Figure 1: Algorithm for Q. 1(a) and Q. 1(b)

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.

c) Prove that 8n3 + 7n2 + 5 = O(n3), but 8n3 + 7n 2 + 5  O(n). 5+5

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

Item Weight Value


1 3 150
2 3 180
3 2 170
4 3 120
5 3 210

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]

func eatChips(int bowlOfChips) {

Println("Have some chips!")

for chips := 0; chips <= bowlOfChips; chips++ {

// dip chips

Println("No more chips.")

func pizzaDelivery(int boxesDelivered) {

Println("Pizza is here!")

for pizzaBox := 0; pizzaBox <= boxesDelivered; pizzaBox++ {

// open box

1
for pizza := 0; pizza <= pizzaBox; pizza++ {

// slice pizza

for slice := 0; slice <= pizza; slice++ {

// eat slice of 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

Use Huffman technique to answer the following questions:


i. Build the Huffman code tree for the message and find the codeword [3]
for each character.
ii. If the data consists of only these characters, what is the total number [1]
of bits to be transmitted? What is the percentage saving if the data is
sent with 8-bit ASCII values without compression?

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.

The objects in the Louvre Museum are listed below.

Objects Jewelry Sculpture Paintings


Profit 7 9 6
Weight 3 5 4

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

There are a total of 10000 characters in the document.

Use Huffman technique to answer the following questions:


i. Build the Huffman code tree for the message and find the codeword for [3]
each character. Encode “stolen” using the codewords.
[1]
ii. What is the percentage saving if the data is sent with fixed-length code
values without compression?

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.

2. (a) Following items are available in a grocery shop: [3]


➢ 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.

(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:

Car Name Yamaha Harley Apache Honda CBR


R15 Davidson RTR 160 150R R
Wholesale price (in lac 3 6 1 4
Tk)
Retail price (in lac Tk) 5 14 2 6

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”);
}

(ii) for(int i =1; i*i<n; i++){


printf(“hello”);
}

(iii) for(int i =1; i<n; i=i*2){


for(int j=1; j<i; j++){
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

4 (a) Following items are available in a grocery shop: [3]


➢ 12 kilogram rice grain which costs 840 taka
➢ 10 kilogram salt which costs 870 taka
➢ 8 kilogram saffron powder which costs 2000 taka and
➢ 5 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 9 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.

(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

Use Huffman technique to answer the following questions:


i. Build the Huffman code tree for the message and find the codeword for
each character. Encode “stolen” using the codewords. [3]

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.

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.

(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.

1 int funCTION(int n, int m) {


2 int sumUP = 0;
3 for(int i=1; i<=n; i=i+2) {
4 int r = m;
5 while(r>=1) {
6 sumUP = sumUP+r;
7 r = r/3;
8 }
9 }
10 return sumUP;
11 }

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.

(3)Consider the following array [4]


-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.

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.

(Please Turn Over)


Aw
yr Using recursion tree method or the Master Theorem find out a good asymptotic upper [3]
und on the following recurrence:
T(n) = 3T(n/2) + O(n)
OF Consider the following function which takes two arrays (A and B) and their lengths (n [142+
and m) as inputs respectively. 2=5]

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.

(Please Turn Over)


or Suppose you have a cup with capacity 750 ml. The following table shows that there are 5 [3]
flavors of drinks. For each flavor, there is only d ml available, and each flavor of drink contains
atotal of s 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.

Flavor Volume (d)_|_ Total sugar (s)


Apple 320 35
Orange 220 20
Pineapple 240 40
Cranberry 200 30
Strawberry 280 25

(End of paper)
`

MID-TERM QUESTION SOLUTIONS

DATA STRUCTURE
AND ALGORITHMS II
CSE 2217

SOLUTION BY
NURUL ALAM ADOR

UPDATED TILL SPRING 2024

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!)

1x6 1x5 1x1

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

∴ Minimum tiles needed = 2


Tiles should be use: 2 tiles of 1 x 5 m2

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

Values ($) 110 100 120 90

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 0 0 0 0 0 110 110 110 110

2 0 0 0 0 100 110 110 110 110


Coins

3 0 0 0 0 100 110 120 120 120

4 0 0 0 90 100 110 120 190 200

∴ Total maximize gain = 200$


Coin should take = 1st (110$) and 4th (90$)

2. a) Consider the following array

-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

∴ Maximum continuous subarray is:


2 3 4 5 6 7 8 9

10 -1 6 -2 -4 2 2 8 (Maximum Sum = 21)

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:

CountEndsWithOne(arr[], left, right) {

if (left == right) {
if(arr[left] % 10 == 1){
return 1;
}
Base Case
else {
return 0;
}
}

else {
mid = (left + right) / 2;

leftCount = countEndsWithOne(arr, left, mid);


rightCount = countEndsWithOne(arr, mid + 1, right);

return leftCount + rightCount;


}

}
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

Since 𝑎 > 𝑏 𝑘 , therefore this recurrence belongs to case 1 of Master Theorem.

nurulalamador.github.io/UIUQuestionBank 6
∴ Time complexity, 𝑇(𝑛) = 𝑂(𝑛log𝑏 𝑎 )
= 𝑂(𝑛log2 3 )
= 𝑂(𝑛1.58 )

∴ Asymptotic upper bound of given recurrence is 𝑂(𝑛1.58 ).

3. b) Consider the following function which takes two arrays (A and B) and their lengths
(n and m) as inputs respectively.

void func1 (int A[], int B[], int n, int m) {


int sumA = 0;
for(int i=0; i<n; i++) {
sumA = sumA + A[i];
}

int sumB = 0;
for(int i=0; i<m; i++) {
sumB = sumB + B[i];
}

int count = 0;

for(int i=0; i<n; i++) {


for(int j=0; j<m; j++) {
if(A[i]*A[i]>B[i] || sumA>sumB) {
count++;
} else {
break;
}
}
}
}

Now determine the following:


i. The exact-cost equation for the running-time.
ii. The best case and worst case running time using the Big - Oh notation
iii. Examples of best case and worst case inputs
Solution:

Line Worst Case Best Case

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 𝑛

i) Time complexity, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛 + 1 + 𝑚 + 1 + 𝑚 + 1 + 𝑛 + 1 + 𝑛𝑚 + 𝑛 + 2𝑛𝑚


= 3𝑛𝑚 + 4𝑛 + 2𝑚 + 6

∴ Exact-cost equation is 3𝑛𝑚 + 4𝑛 + 2𝑚 + 6

ii) Best case, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛 + 1 + 𝑚 + 1 + 𝑚 + 1 + 𝑛 + 1 + 𝑛 + 𝑛 + 𝑛


= 6𝑛 + 2𝑚 + 6
= 𝑂(𝑛 + 𝑚)

Worst case, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛 + 1 + 𝑚 + 1 + 𝑚 + 1 + 𝑛 + 1 + 𝑛𝑚 + 𝑛 + 𝑛𝑚 + 𝑛𝑚


= 3𝑛𝑚 + 4𝑛 + 2𝑚 + 6
= 𝑂(𝑛𝑚)

iii) Example of best case inputs:

A[ ] = 1 1 1 1 B[ ] = 10 10 10 10 n=4 m=4

Example of worst case:

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]

500 1000 1500 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. b) “Data encoded using Huffman coding is uniquely decodable” - is the statement


true or false? Justify your answer.
Solution:
The following statement is true. Huffman coding ensures unique decodability
through its prefix property, where no code is a prefix of any other code. This property
allows the encoded data to be parsed unambiguously from left to right without
additional delimiters. During decoding, each bit sequence can be traced in the
Huffman tree to a unique leaf node, ensuring that each sequence corresponds to a
unique symbol. Consequently, Huffman coding guarantees that the encoded data
can be uniquely and correctly decoded.

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

Flavor Apple Orange Pineapple Cranberry Strawberry

Sugar, 𝑠𝑖 35 20 40 30 25

Volume, 𝑑𝑖 320 220 240 200 280

Finding sugar per volume:

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

Adding item in knapsack:

𝑫
𝒊 Flavor 𝒔𝒊 𝒅𝒊 𝑫 𝒙𝒊 = min (𝟏, ) 𝑫′ = 𝑫 − 𝒅𝒊 𝒙𝒊
𝒅𝒊
3 Pineapple 40 240 750 1 750-240 = 510

4 Cranberry 30 200 510 1 510-200 = 310

1 Apple 35 320 310 0.96875 310-(320*0.96875) = 0

∴ Maximum Sugar = 𝑠3 𝑥3 + 𝑠4 𝑥4 + 𝑠1 𝑥1
= 40 × 1 + 30 × 1 + 35 × 0.96875
= 103.90 𝑔

∴ At most 103.90 grams of sugar can be consumed.

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

∴ Maximum sub continuous subarray is:

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 )

∴ Asymptotic upper bound of given recurrence is 𝑂(𝑛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, -5) (-3, 2) (-2, -2)

1 3 -5 2 -3 -2

(1, 1) (3, 3) (2, 2) (-3, -3)

1 3 2 -3

∴ Maximum value is 3 and minimum value is -5.

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

Net Profit 200 150 100 50 300

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

1 0 0 0 200 200 200 200 200

2 0 0 150 200 200 350 350 350


Projects

3 0 100 150 250 300 350 450 450

4 0 100 150 250 300 350 450 450

5 0 100 150 250 300 350 450 450

∴ Total maximize profit = 450 Million Dollars


Selected projects = 1, 2, 3

∴ 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:

Divide and Conquer Dynamic Programming

Break the problem into independent Break the problem into overlapping sub-
sub-problems. problems.

Solve sub-problems in a bottom-up


Solve each sub-problem recursively. approach, often store previously
encountered sub-problems to use later.

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

We need to pay $25.

In this scenario, if we use greedy method,


We will need to use these coins:
$23, $1, $1.
∴ Total 3 coin will needed in greedy method.

But if we use any other approach, like Dynamic Programming, then we can pay
$25 in only 2 coins ($16, $9).

∴ 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
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 }

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:

Line Worst Case Best Case

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

∴ Best case, 𝑇(𝑛) = 1 + 𝑛 + 𝑛 − 1 + 1 + 1 + 𝑛 + 𝑛 − 1 + 1 + 1 + 1


= 4𝑛 + 4
= 𝑂(𝑛)

𝑛(𝑛 + 1) 𝑛(𝑛 + 1) 𝑛(𝑛 + 1)


∴ Worst case, 𝑇(𝑛) = 𝑛 + 1 + +𝑛+ + +𝑛+𝑛+𝑛×𝑛
2 2 2
+𝑛(𝑛 − 1) + 𝑛(𝑛 − 1) + 𝑛 + 1
𝑛(𝑛 + 1)
= 4𝑛 + 3 + 4 + 𝑛2 + 𝑛2 − 𝑛 + 𝑛2 − 𝑛 + 𝑛
2
3
= 3𝑛2 + 𝑛(𝑛 + 1) + 4𝑛 + 4
2
= 𝑂(𝑛2 )

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.

1 int funCTION(int n, int m) {


2 int sumUP = 0;
3 for(int i=1; i<=n; i=i+2) {
4 int r = m;
5 while(r>=1) {
6 sumUP = sumUP+r;
7 r = r/3;
8 }
9 }
10 return sumUP;
11 }

Solution:

Line Time Cost

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

4. a) Find an optimal solution to the fractional knapsack instance of n = 5, W = 7, (v1, 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.
Solution:
Given,
𝑛=5
𝑊=7

[ P.T.O ]

nurulalamador.github.io/UIUQuestionBank 16
Item, 𝒊𝒊 𝒊𝟏 𝒊𝟐 𝒊𝟑 𝒊𝟒 𝒊𝟓
Value, 𝑣𝑖 50 30 35 60 25

Weight, 𝑤𝑖 2 2 1 3 2

Finding price per unit:

𝑖1 𝑖2 𝑖3 𝑖4 𝑖5
𝑣𝑖
25 15 35 20 12.5
𝑤𝑖

𝑣𝑖
Sorting item based on :
𝑤𝑖
𝑖3 > 𝑖1 > 𝑖4 > 𝑖2 > 𝑖5

Adding item in knapsack:

𝑾
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

𝑖2 30 2 1 0.5 1-(2×0.5) = 1-1 = 0

∴ 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) }

Sorting train based on departure time:

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.

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
7 }
8 for(int j=0;j<=i; j++){
9 numberOfDots *= 2;
10 }
11 }
12 int j = 1; 13
13 while(j<=n){
14 numberOfDots+=j;
15 j = j * 2;
16 }
17 }
18 return (numberOfDots&1);
19 }

Solution:

Line Worst Case Best Case

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) 𝑛(𝑛 + 1) 𝑛(𝑛 + 1) 𝑛(𝑛 + 1)


∴ Worst case, 𝑇(𝑛) = 1 + 𝑛 + 1 + +𝑛+ + +𝑛+ +𝑛
2 2 2 2
+𝑛 log 2 𝑛 + 𝑛 + 𝑛 log 2 𝑛 + 𝑛 log 2 𝑛 + 1
𝑛2 + 𝑛
=4 + 3𝑛 log 2 𝑛 + 5𝑛 + 3
2
= 2𝑛2 + 7𝑛 + 3𝑛 log 2 𝑛 + 3
= 𝑂(𝑛2 )

1. b) Derive the exact-cost equation for the running time of the following function and
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 }

Solution:

Line Time Cost

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 𝑛

log2 𝑛 (log2 𝑛 + 1) log 𝑛 (log2 𝑛 + 1)


∴ Time complexity, 𝑇(𝑛) = log2 𝑛 + 1 + + log2 𝑛 + 2
2 2
log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1)
× + +
2 2 2
log 2 𝑛 (log 2 𝑛 + 1) log 2 𝑛 (log 2 𝑛 + 1)
× + + log 2 𝑛
2 2
log 2 𝑛 (log 2 𝑛 + 1) × log 2 𝑛 (log 2 𝑛 + 1) log 𝑛 (log 2 𝑛 + 1)
= 2[ ]+3
4 2
+3 log 2 𝑛 + 1

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. a) Solve the following recurrence equation, where 𝑇(1) = 𝑂(1).


𝑛
𝑇(𝑛) = 4𝑇 ( ) + 𝑂(𝑛)
2
Solution:
Given,
𝑛
𝑇(𝑛) = 4𝑇 ( ) + 𝑂(𝑛)
2
𝑛 p
Comparing with base equation of Master method 𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑂(𝑛𝑘 log 𝑛),
𝑏
Here,
𝑎=4
𝑏=2
𝑘=1
𝑝=0
𝑏 𝑘 = 21 = 2

Since 𝑎 > 𝑏 𝑘 , therefore this recurrence belongs case 1 of Master Theorem.

∴ Time complexity, 𝑇(𝑛) = 𝑂(𝑛log𝑏 𝑎 )


= 𝑂(𝑛log2 4 )
= 𝑂(𝑛2 )

∴ The solution of recurrence equation is 𝑂(𝑛2 ).

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

∴ Maximum continuous subarray is:


3 4 5 6 7 8 9 10 11 12

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.

In this concept, every sub-problem should be a ultimate optimal solution at that


sub-problem’s current stage. For example, we have a coin change problem with
total coins 𝐶 = {1,2,3,5} and we will need to make $10. Now in a sub-problems of that
problem where we have only coins 𝐶 = {1,2}, then here we will got the ultimate
optimal solution for making $10 with only coins 𝐶 = {1,2}.

2. b) Demonstrate why the recursive approach to calculate a Fibonacci number is


inefficient, by calculating the Fibonacci number 𝐹5 . How does the dynamic
programming approach for the same solve this inefficiency? (Consider 𝐹0 =0, 𝐹1 =1)
Solution:
Recursive approach for finding 𝐹5 :

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

In recursive approach, we are solving same repeated sub-problems for multiple


time such as 𝐹3 , 𝐹2 for calculating Fibonacci number 𝐹5 . One the other hand, in
dynamic programming approach, we store the sub-problem after solving it and
reuse it later instead of solving same overlapping sub-problem multiple times. In
this way, dynamic approach solve the inefficiency to calculate a Fibonacci number.

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

∴ Total maximize profit = 50


Selected item: 4th item

4. a) Following items are available in a grocery shop:


 12 kilogram rice grain which costs 840 taka
 10 kilogram salt which costs 870 taka
 8 kilogram saffron powder which costs 2000 taka and
 5 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 9 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, 𝑊 = 9 𝑘𝑔

Rice Saffron
Item Salt Sugar
Grain Powder

Value (taka), 𝑣 840 870 2000 500

Weight (kg), 𝑤 12 10 8 5

Finding price per unit:

[ P.T.O]

nurulalamador.github.io/UIUQuestionBank 25
𝑣𝑖 70 87 250 100

𝑤𝑖 Rice Salt Saffron Sugar


Grain Powder

𝑣𝑖
Sorting item based on :
𝑤𝑖
Saffron Powder > Sugar > Salt > Rice Grain

Thief 1:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Saffron
2000 8 9 1 9-8 = 1
Powder

Sugar 500 5 1 0.2 1-(5×0.2) = 0

Now, Sugar new weight, 𝑤𝑖′ = 𝑤𝑖 − 𝑤𝑖 𝑥𝑖 = 5 − 5 × 0.2 = 4


Sugar new value, 𝑣𝑖′ = 𝑣𝑖 − 𝑣𝑖 𝑥𝑖 = 500 − 500 × 0.2 = 400

Thief 2:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 400 4 9 1 9-4 = 5

Salt 870 10 5 0.5 5-(10×0.5) = 0

Now, Salt new weight, 𝑤𝑖′ = 𝑤𝑖 − 𝑤𝑖 𝑥𝑖 = 10 − 10 × 0.5 = 5


Salt new value, 𝑣𝑖′ = 𝑣𝑖 − 𝑣𝑖 𝑥𝑖 = 870 − 870 × 0.5 = 435

Thief 3:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Sugar 435 5 9 1 9-5 = 4

Rice
840 12 4 0.33 4-(12×0.33) = 0
Grain

Now, Rice Grain new weight, 𝑤𝑖′ = 𝑤𝑖 − 𝑤𝑖 𝑥𝑖 = 12 − 12 × 0.33 = 8


Rice Grain new value, 𝑣𝑖′ = 𝑣𝑖 − 𝑣𝑖 𝑥𝑖 = 840 − 840 × 0.33 = 560

Thief 4:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Rice
560 8 9 1 9-8 = 1
Grain

Here, all items in the grocery shop have been stolen.


∴ Total 4 thieves will be needed in the group to empty the grocery shop.

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

Use Huffman technique to answer the following questions:


i. Build the Huffman code tree for the message and find the codeword for
each character. Encode “stolen” using the codewords.
ii. What is the percentage saving if the data is sent with fixed-length code
values without compression?
Solution:
i. Sorting character based on frequency:

l 44 t 49 n 55 s 57 o 73 a 74 e 105

Building Huffman code tree:

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

93 e 105 112 147

l 44 t 49 n 55 s 57 o 73 a 74

nurulalamador.github.io/UIUQuestionBank 27
259

112 147 198

n 55 s 57 o 73 a 74 93 e 105

l 44 t 49

457
0 1

198 259
0 1
0 1

93 e 105 112 147


0 1 0 1 0 1

l 44 t 49 n 55 s 57 o 73 a 74

This is our final Huffman coding tree.

Finding codeword for each character from Huffman tree:


e: 01 a: 111
o: 110 s: 101
n: 100 t: 001
l: 000
∴ Encode of “stolen” = 10100111000001100

ii.

nurulalamador.github.io/UIUQuestionBank 28
Spring 2023

1. a) 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 𝑶(𝒏𝟐 ) time. Then the algorithm sorts the subarrays recursively, and
then merges their solutions in time 𝑶(𝒏 𝐥𝐨𝐠 𝒏). Write a recurrence relation for the
running time 𝑻(𝒏) of this algorithm.
Solution:
Recurrence relation for the running time 𝑇(𝑛) of this algorithm is:
𝑂(1) if 𝑛 ≤ 2
𝑇(𝑛) = { 𝑛
3𝑇 ( ) + 𝑂(𝑛2 ) + 𝑂(𝑛 log 𝑛) if 𝑛 > 2
3

In the recursive relation, the time complexity includes:


𝑛
 3𝑇 (3) for sorting 3 subarrays recursively
 𝑂(𝑛2 ) for the time taken for the division into 3 subarrays
 𝑂(𝑛 log 𝑛) for merging the sorted subarrays

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

Finally we merge these subarray, which take 𝑂(𝑛) times.


∴ Here our recursive relation is,
𝑛
𝑇(𝑛) = 2𝑇 ( ) + 𝑂(𝑛)
2
𝑛 p
Comparing with base equation of Master method 𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑂(𝑛𝑘 log 𝑛),
𝑏
Here,
𝑎=2
𝑏=2
𝑘=1
𝑝=0
𝑏 𝑘 = 21 = 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

70 -40 20 -50 10 -15 20


(1, 1, 70) (7, 7, 20)
(1, 7, 15)

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

70 -40 70 -40 20 -50 20 -50 10 -15 10 -15 20 20

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

70 70 -40 -40 20 20 -50 -50 10 10 -15 -15

30 -30 -5

∴ Maximum sub continuous subarray is:

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.

We should try to solve a problem using Dynamic Programming when we have


more chance to having repeated overlapping sub-problems to remove inefficiency.
For example, calculating Fibonacci number.

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:

Yamaha Harley Apache Honda CBR


Car Name
R15 Davidson RTR 160 150R R
Wholesale price (in lac
3 6 1 4
Taka)
Retail Price (in lac Taka) 5 14 2 6

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

Motorcycle Yamaha Harley Apache Honda CBR


Name R15 Davidson RTR 160 150R R

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

∴ Total maximize profit = 9 Lac Tk


Selected motorcycles : 2 (Harley Davidson), 3 (Apache RTR 160)

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.

(i) for (int i =1; i<n; i=i*2) {


p++;
}
for(int j=1; j<p; j=j*2){
printf(“hello”);
}

(ii) for(int i =1; i*i<n; i++){


printf(“hello”);
}

(iii) for(int i =1; i<n; i=i*2){


for(int j=1; j<i; j++){
printf(“hello”);

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.

i) Line Time Cost

1 log 2 𝑛 + 1
2 log 2 𝑛
4 log 2 𝑛 (log 2 𝑛) + 1
5 log 2 𝑛 (log 2 𝑛)

∴ Time complexity, 𝑇(𝑛) = log2 𝑛 + 1 + log2 𝑛 + log2 𝑛 (log2 𝑛) + 1 + log2 𝑛 (log2 𝑛)


= 2 log 2 𝑛 + 2 log 2 𝑛 (log 2 𝑛) + 2
= 𝑂(log 𝑛)

ii) Line Time Cost

1 √𝑛 + 1
2 √𝑛

∴ Time complexity, 𝑇(𝑛) = √𝑛 + 1 + √𝑛


= 𝑂(√𝑛)

iii) Line Time Cost

1 log 2 𝑛 + 1
log 2 𝑛 (log 2 𝑛 + 1)
2
2
log 2 𝑛 (log 2 𝑛 + 1)
3 +1
2

log2 𝑛 (log2 𝑛 + 1) log 𝑛 (log2 𝑛 + 1)


∴ Time complexity, 𝑇(𝑛) = log2 𝑛 + 1 + +1+ 2
2 2
2
log 2 𝑛 + log 2 𝑛
= log 2 𝑛 + 2 ( )+2
2
= log 22 𝑛 + 2 log 2 𝑛 + 2
= 𝑂(log 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

4. a) Find an optimal solution to the fractional knapsack instance of n = 4, W = 5, (v1, v2,


v3, v4) = (50, 30, 35, 60), and (w1, w2, w3, w4) = (2, 2, 1, 3).
Solution:
Given,
𝑛=5
𝑊=7

Item, 𝒊𝒊 𝒊𝟏 𝒊𝟐 𝒊𝟑 𝒊𝟒
Value, 𝑣𝑖 50 30 35 60

Weight, 𝑤𝑖 2 2 1 3

Finding price per unit:

𝑖1 𝑖2 𝑖3 𝑖4
𝑣𝑖
25 15 35 20
𝑤𝑖

𝑣𝑖
Sorting item based on :
𝑤𝑖
𝑖3 > 𝑖1 > 𝑖4 > 𝑖2

Adding item in knapsack:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
𝑖3 35 1 5 1 5-1 = 1

𝑖1 50 2 4 1 4-2 = 2

𝑖4 60 3 2 0.667 2-(3×0.667) = 2-2 = 0

nurulalamador.github.io/UIUQuestionBank 35
∴ Total Profit = 𝑣3 𝑖3 + 𝑣1 𝑖1 + 𝑣4 𝑖4
= 35 × 1 + 50 × 1 + 60 × 0.667
= 125

4. b) Suppose we want to encode the symbols {A, B, C, D} in binary. Is the following a


valid Huffman code?
{𝐴: 0; 𝐵: 10; 𝐶: 110; 𝐷: 111}
If it is, build the code tree; if not, explain why you can't.
Solution:
Given,
{𝐴: 0; 𝐵: 10; 𝐶: 110; 𝐷: 111}

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

In the recursive relation, the time complexity includes:


𝑛
 4𝑇 (4) for solving 4 sub-problems size of 𝑛/4 recursively
 𝑂(𝑛 log 𝑛) for merging the solved sub-problems

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

Since 𝑎 > 𝑏 𝑘 , therefore this recurrence belongs to case 1 of Master Theorem.

∴ Time complexity, 𝑇(𝑛) = 𝑂(𝑛log𝑏 𝑎 )


= 𝑂(𝑛log3 3 )
= 𝑂(𝑛)

∴ Solution of following recurrence equation is 𝑂(𝑛).

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, 1, 2) (1, 3, 1) (3, 3, 2) (5, 5, 1) (5, 7, -4) (7, 7, -2)

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

∴ Maximum sub continuous subarray is:

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

Value (taka), 𝑣 800 890 2000 500

Weight (kg), 𝑤 10 10 8 4

Finding price per unit:

[ P.T.O]
𝑣𝑖 80 89 250 125

𝑤𝑖 Rice Salt Saffron Sugar


Grain Powder

𝑣𝑖
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

Salt 890 10 4 0.4 4-(10×0.4) = 4-4 = 0

Now, Salt new weight, 𝑤𝑖′ = 𝑤𝑖 − 𝑤𝑖 𝑥𝑖 = 10 − 10 × 0.4 = 6


Salt new value, 𝑣𝑖′ = 𝑣𝑖 − 𝑣𝑖 𝑥𝑖 = 890 − 890 × 0.4 = 534

[ 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

Now, Rice Grain new weight, 𝑤𝑖′ = 𝑤𝑖 − 𝑤𝑖 𝑥𝑖 = 10 − 10 × 0.2 = 8


Rice Grain new value, 𝑣𝑖′ = 𝑣𝑖 − 𝑣𝑖 𝑥𝑖 = 800 − 800 × 0.2 = 640

Thief 4:

𝑾
Item 𝒗𝒊 𝒘𝒊 𝑾 𝒙𝒊 = min (𝟏, ) 𝑾′ = 𝑾 − 𝒘𝒊 𝒙𝒊
𝒘𝒊
Rice
640 8 8 1 8-8 = 0
Grain

Here, all items in the grocery shop have been stolen.


∴ Total 4 thieves will be needed in the group to empty the grocery shop.

2. 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 B C D F T _

Frequency 40 23 8 10 4 12 3

There are a total 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 codewords generated in (i).
Solution:
i. Sorting character based on frequency:

_ 3 F 4 C 8 D 10 T 12 B 23 A 40

Building Huffman code tree:

_ 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

This is our final Huffman coding tree.

Finding codeword for each character from Huffman tree:


A: 0 B: 10
C: 1101 D: 1110
F: 11001 T: 1111
_: 11000

ii. Decoding “0110001111” using codewords from (i):


0110001111 = A_T

3. a) Suppose you have computed a Fibonacci series using dynamic programming.


Justify the following statements with an example:
I. Overlapping Subproblems property has been satisfied in your computation.
II. Dynamic programming gives you a more efficient solution than an obvious
recursive algorithm.
Solution:
The overlapping sub-problems property means that a problem can be broken down
into sub-problems that are reused multiple times. When compute a Fibonacci
number, we get same sub-problems for solving repeatedly.
For example, for computing 𝐹(5),
𝐹(5) requires 𝐹(4) and 𝐹(3)
𝐹(4) requires 𝐹(3) and 𝐹(2)
𝐹(3) requires 𝐹(2) and 𝐹(1)
𝐹(2) requires 𝐹(1) and 𝐹(0)
As a result, 𝐹(3) is computed twice, 𝐹(2) is computed multiple times, and so on. This
repetition indicates overlapping sub-problems. In recursive algorithm, we solve and
merge solution of each sub-problems with overlapping sub-problems. But in
dynamic programming, we store the results of these sub-problems and reuse them
and reduce the inefficiency which created on recursive algorithm.

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

3. b) What is ‘Optimal Substructure’ property? How does Dynamic Programming differ


from Divide-and-Conquer problems in terms of handling subproblems?
Solution:
A problem is said to have optimal substructure if an optimal solution can be
constructed from optimal solutions of its sub-problems. In other words, a problem
exhibits optimal substructure if an optimal solution to the problem contains within
it optimal solutions to its sub-problems.

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

∴ Minimum coins needed = 2


Selected coins = 1 coins of 2 Taka, 1 coins of 5 Taka

4. a) Derive the best-case and the worst-case running time equations for the following
function calculate and represent using Asymptotic Notation.

1 void calculate(int n, int p, int A[]) {


2 int prod = 0;
3 for(int i = 1; i<=n; i++) {
4 for(int j = 1; j <= i*i; j++) {
5 prod *= pow(i,j)
6 }
7 }
8
9 for(int m = 2; m <= P; m++) {
10 if(A[m] < 100 ) {
11 break;
12 }
13
14 prod = prod * A[m];
15 }
16
17 cout<<prod<<endl
18 }

Solution:

Line Worst Case Best Case

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

∴ Best case, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛3 + 𝑛 + 𝑛3 + 1 + 1 + 1 + 1


= 2𝑛3 + 2𝑛 + 6
= 𝑂(𝑛3 )

∴ Worst case, 𝑇(𝑛) = 1 + 𝑛 + 1 + 𝑛3 + 𝑛 + 𝑛3 + 𝑝 + 𝑝 − 1 + 1


= 2𝑛3 + 2𝑛 + 2𝑝 + 2
= 𝑂(𝑛3 ) [Considering 𝑛3 > 𝑝]

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:

Line Time Cost

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

You might also like