CS702 Mid Term SolvedMegaFile Khurshid PDF
CS702 Mid Term SolvedMegaFile Khurshid PDF
CS702 Mid Term SolvedMegaFile Khurshid PDF
Categorize algorithms based on asymptotic growth rate e.g. linear, quadratic, polynomial,
and exponential
Ignore small constant and small inputs
Estimate upper bound and lower bound on growth rate of time complexity function
Describe running time of algorithm as n grows to ∞.
Describes behavior of function within the limit.
Limitations
Not always useful for analysis on fixed-size inputs.
All results are for sufficiently large inputs.
Asymptotic Notations
Asymptotic Notations Θ, O, Ω, o, ω
We use Θ to mean “order exactly”
O to mean “order at most”
Ω to mean “order at least”
o to mean “tight upper bound”
ω to mean “tight lower bound”
Solution:
Question: Theorem
Prove, by mathematical induction, that computational cost of generating
permutations is n!.
Proof
• If n = 1, then the statement is true, because 1! =1
• If there are k elements in set then number of permutation = k!
• If we add one more element in any of the permutations, there will be k+1
number of ways to add it, resulting k+1 no. of permutations.
• Now total number of permutations = k!(k+1) = (k+1)!
• Hence true for all n.
Question: Theorem
Statement: If a and b are any integers, not both zero, then gcd(a, b) is the smallest
positive element of the set {ax + by : x, y Z} of linear combinations of a and b.
Proof
Let s be the smallest positive such linear combination of a and b, i.e.
s = ax + by, for some x, y Є Z
By quotient remainder theorem
a = qs + r = qs + a mod s, where q = ⌊a/s⌋.
a mod s = a – qs = a - q(ax + by) = a (1 - qx) + b(-qy)
• Hence a mod s is a linear combination of a and b.
• But, since a mod s < s, therefore, a mod s = 0
• Now a mod s = 0 ⇒ s | a
• Similarly we can prove that, s | b.
• Thus, s is a common divisor of both a and b,
• Therefore, s gcd(a, b) (1)
• We know that if d | a and d | b then d | ax + by for all x, y integers.
• Since gcd(a, b)| a and gcd(a, b) | b, hence gcd(a, b) | s and s > 0 imply that
gcd(a, b) ≤ s (2)
• By (1) and (2) proved that gcd(a, b) = s
Theeta
Question: Prove that a.n2 + b.n + c = Θ(n2) OR [an2 + bn + c]
Where a, b, c are constants and a > 0
Theeta
Question: Prove that 2.n2 + 3.n + 6 = Θ (n3) [2n2 + 3n + 6]
Solution:
Solution 2
2n 3 3n 10 O ( n 4 )
let suppose f(n)=2n 3 3n 10 and g(n)=n 4
f ( n) O ( g ( n)) ?
we have to find c1 and n 0 n n 0
0 2n 3 3n 10 cn 4
let c1 1 we need to n 0 for which above is true
testing different values from 1 to onward
2(3)3 3(3) 10 34
73 81
hence c=1 for n 3
hence f ( n) O ( g ( n)) 2n 3 3n 10 O ( n 4 )
c=1 for n 3
Question: Use the logical equivalences above to show that ¬(p ∨ ¬(p ∧ q)) is a
contradiction.
¬ (p ∨ ¬ (p ∧ q))
(Solution-2 By Instructor)
(Solution-2 By Instructor)
By rules of inference
Taking contraposition
(A ˄ (A → B)) → B
If A is true and A→ B is true then B is true
Hence proved.
(Solution-2 by Instructor)
3. Rules of Inference
4. Contradiction
Solution:
p q r p∨q ¬p (¬ p ∨ r) (p ∨ q) ∧ (¬ p ∨ r) (q ∨ r) (p ∨ q) ∧ (¬ p ∨ r) → (q ∨ r)
T T T T F T T T T
T T F T F F F T T
T F T T F T T T T
T F F T F F F F T
F T T T T T T T T
F T F T T T T T T
F F T F T T F T T
F F F F T T F F T
Solution:
Solution:
Question: Prove that each of the following pairs of propositional formulae are
equivalent using propositional equivalences.
(b) ¬p → (q → r) q → (p ∨ r)
Question: Prove that each of the following propositional formulae are tautologies by
showing they are equivalent to T.
(b). (p ∧ q) ∨ (p ∧ r) → (q ∨ r)
(c). (p ∧ q) ∨ (¬p ∧ q) ∨ ¬q
Question: Construct Truth Tables for each of the following compound propositions.
(a) (p ∧ q) ∨ (p ∧ r)
(b) (q ∧ p) ↔ (q ⊕ p)
This is not a tautology as seen by the case where p and r are True and q is False,
or the case where p and r are False and q is True.
Thus the operator → and ∧ are transitive but ⊕ is not transitive.
bk = a.bk-1 if b0 = c
Solution:
bk = 5bk-1 - 6bk-2 ꓯ k ≥ 2
With initial condition: b0 = 2 and b1 = 5
Then find explicit formula for b0, b1, b2 , . . . , using characteristic equation
of the above recursion.
Solution:
tn = n if n = 0, 1, 2
tn = 7.tn-2 + 6.tn-3 otherwise
Find the general solution of the recurrence above.
tn = n if n = 0, 1, 2
tn = 5tn-1 - 8tn-2 + 4tn-3 otherwise
Find general solution of the recurrence above.
tn – 2tn-1 = 3n
Solution:
tn - 2tn-1 = (n + 5) 3n ; n≥1
Solution:
Non-Homogeneous Recurrences
Question: Consider the recurrence
tn = 2tn-1 + n + 2n otherwise
tn = n if n = 0, 1, 2
tn = 6.tn-1 - 11.tn-2 + 6.tn-3 otherwise
Find the general solution of the recurrence above.
Solution:
Rewriting the recurrence,
x3 – 6x2 + 11x – 6 = 0
Testing whether 1 or -1 is a root of the equation or not, we find that 1 is a root of the equation,
hence
∑ 𝟐𝒊 = 𝒎(𝒎 + 𝟏)
𝒊=𝟏
Instructor Solution
∑ 2i = m(m + 1)
i=1
∑ 2 ∗ 1 = 1(1 + 1)
i=1
2 = 2 (True)
∑ 2i = k(k + 1)
i=1
∑ 2i = (k + 1)(k + 2)
i=1
= (k+1) ((k+1) + 1)
= True
Hence, proved.
Question: Given two integers a and b, how to find their greatest common divisor
(gcd(a, b))?
Proof:
Euclid’s rule: If x and y are positive integers with x ≥ y, then
gcd(x, y) = gcd(x mod y, y).
It is enough to show the slightly simpler rule
gcd(x, y) = gcd(x − y, y).
Any integer that divides both x and y must also divide x − y, so
gcd(x, y) ≤ gcd(x − y, y).
Likewise, any integer that divides both x − y and y must also divide both x and y,
so gcd(x, y) ≥ gcd(x − y, y).
bk = 6bk-1 - 9bk-2
or
bk - 6bk-1 + 9bk-2 = 0
The characteristic equation for the above is given below
Let consider the followings
bk = x2
Therefore, bk-1 = x
Similarly, bk-2 = x0
The characteristic equation will become as;
x2 - 6x + 9 = 0
By simplifying
(x)2 – 2(x)(3) + (3)2 = 0
(x – 3)2 = 0
⇒ t = 3, is repeated root
Thus
3n and n.3n are the sequences which satisfy the recurrence relation
Now, suppose general solution is:
bn = X.3n + Z.n.3n
Which satisfies the original recurrence, here X and Z are constants.
Question: Show by mathematical induction that any amount in cents ≥ 20 cents can be
obtained using 5 cents and 6 cents coins only?
Solution:
Fibonacci Sequences
Computing Values using Mathematical Model
Maxima(P[1..n])
1. Sort the points in ascending order w. r .t. X axis
2. If |S| = 1, then return it, else
Find a line perpendicular to X-axis which separates S into SL and SR, each
of which consisting of n/2 points.
3. Recursively fined the maxima’s SL and SR
4. Project the maxima’s of SL and SR onto L and sort these points according to
their y-values.
5. Conduct a linear scan on the projections and discard each of maxima of S L if
its y-value is less than the y-value of some maxima’s of SR.
Time Complexity
Question: Why the Optimal Weight Convex Polygon Problem cannot be solved using Divide
and Conquer Approach?
Solution:
Optimal Weight Convex polygon problem cannot be solved using divide and conquer
approach because:
Its sub problems are not independent from each other.
We need an optimal solution of the problem, which may not be found using divide and
conquer approach.
Question: Consider the problem of a chain of A1, A2 and A3 of three matrices, suppose
the dimensions of the matrices are 10x100, 100x5, and 5x50. For the sequence of
matrices given above compute the order of product A1, A2 and A3 is such a way that
minimum total number of scalar multiplications.
Solution:
Main Diagonal
m i, i 0, i 1, 2,3
m i, j min m i, k m k 1, j pi 1. pk . p j
i k j
m 1,1 0 , m 2, 2 0 , m 3,3 0
Putting k = 1
m 1, 2 min m 1,1 m 2, 2 p0 . p1. p2
1 k 2
m 1, 2 5, 000
s 1, 2 k 1
Computing m[2,3]
m i, j min m i, k m k 1, j pi 1. pk . p j
ik j
Putting k = 2
m 2, 3 min m 2, 2 m 3,3 p1. p2 . p3
2 k 3
Computing m[1,3]
m i, j min m i, k m k 1, j pi 1. pk . p j
ik j
Putting k = 1 and k = 2
m 1,3 min m 1,1 m 2,3 p0 . p1. p3 , m 1, 2 m 3,3 p0 . p2 . p3
1 k 3
m 1,3 min 0 25, 000 10 100.50 , 5, 000 0 10 5 50
1 k 3
m 1,3 7500
s 1,3 k 2
0 5000 7500 1 4 6
0 25,000 2 5
0 3
k’s Values Leading to - Minimum m[i,j]
0 1 2
0 2
0
1 A3
A1 A2
𝐴1 𝐴2 𝐴3 𝐴4
. . .
20 𝑥 10 10 𝑥 30 30 𝑥 15 15 𝑥 25
m[i,i] = 0 ꓯ i = 1,2,3,4
m[i,j] = min i ≤ k < j (m[i,k] + m[k+1, j] + Pi-1 . Pk . Pj)
Order of Computation
1 5 8 10
2 6 9
3 7
4
0 1 1 1
0 2 3
0 3
0
From above final cost matrix, the computation shows that the minimum cost for multiplying
A1.A2.A3.A4 matrices, using Dynamic Programming is 13250 i.e. m[1,4].
Order of Computation to minimize the total number of scalar multiplications:
The optimal order for multiplication is A1 . ( ( A2 . A3 ) . A4)
m[i,i] = 0 ꓯ i = 1,2,3,4
m[i,j] = min i ≤ k < j (m[i,k] + m[k+1, j] + Pi-1 . Pk . Pj)
First super diagonal
m[1,2]=m[1,1] + m[2,2] + p0.p1.p2 = 0+0+7.5.4 = 140
m[2,3]=m[2,2] + m[3,3] + p1.p2.p3 = 0+0+5.4.6= 120
m[3,4]=m[3,3] + m[4,4] + p2.p3.p4 = 0+0+4.6.8 = 192
Second super diagonal
m[1,3]=m[1,1] + m[2,3] + p0.p1.p3 = 0+120+7*5*6 = 330
m[1,3]=m[1,2] + m[3,3] + p0.p2.p3 = 140+0+7*4*6 = 308
Minimum [1,3] = 308
for m[2,4]
Solution:
You have prepared a list of n objects for which you are interested to buy, The
items are numbered as i1, i2, . . ., in
Capacity of bag is W
Each item i has value vi, and weigh wi
We want to select a set of items among i1, i2, . . ., in which do not exceed (in total
weight) capacity W of the bag
Total value of selected items must be maximum
How should we select the items?
Here, the subset {2,3} gives maximum value i.e. 160 with total weight of 30 (which is less
than the capacity of knapsack i.e. 32).
Hence, choosing items 2 and 3 gives us the maximum value, with weights within the
capacity of the knapsack.
Procedure:
Points that dominate (2,6) = (4,16), (8,8), (8,20), (14,12), (16,16), (22,8)
Points that dominate (4,16) = (8,20), (16,16)
Points that dominate (8,8) = (8,20), (14,12), (16,16), (22,8)
Points that dominate (8,20) = No point dominate (8,20) So it is maximal
Points that dominate (10,4) = (14,12), (16,16), (22,8)
Points that dominate (14,12) = (16,16)
Points that dominate (16,16) = No point dominate (16,16) So it is maximal
Points that dominate (18,2) = (22,8)
Set of maximal points = (8,20) , (16,16), (22,8)
Question: We have to compute the set of maximal points from following points, using
Brute Force Approach:
(2, 8), (6, 16), (8, 8), (10, 20), (12, 6), (16, 14), (18, 18), (20, 2), (24, 8)
Solution:
Points that dominate (2,8) = (6,16), (8,8), (10,20), (16,14), (18,18), (24,8)
Points that dominate (6,16) = (10, 20), (18, 18)
Points that dominate (8,8) = (10, 20), (16, 14), (18, 18), (24, 8)
Points that dominate (10,20) = No point dominate (10,20) So it is maximal
Points that dominate (12,6) = (16, 14), (18, 18), (24, 8)
Points that dominate (16,14) = (18, 18)
Points that dominate (18,18) = No point dominate (18,18) So it is maximal
Points that dominate (20,2) = (24, 8)
Set of maximal points = (10,20), (18,18), (24,8)
Algorithm
This is related to a famous function in combinatorics called the Catalan numbers. Catalan
numbers are related with the number of different binary trees on n nodes.
4𝑛
P(n) Є
𝑛3/2
The dominating term is the exponential 4n thus P(n) will grow large very quickly and hence
this approach is not economical.
Another generalization is to solve the problem when many parallel processors are
available.
In this case, instead of adding the costs of computing each subsequence, we just take
the maximum, because we can do them both simultaneously.
This can drastically affect both the minimum cost and the final optimal grouping
But of course more balanced groupings that keep all the processors busy is more
favorable solution
There exists some more sophisticated approaches to solve this problem
The figure on the next slide (sample solution) shows how the calculations are performed
Since the table c is (m+1) x (n+1) and each entry takes O(1) to compute, the complexity of
the algorithm is (mn)
Note: There can be many Longest Common Subsequences of length 6 (e.g. as shown in the
table).
Reasoning:
A simple polygon is convex if given any two points on its boundary or in its interior all
points on the line segment drawn between them are contained in the polygon’s boundary or
interior.
Optimal Weight Convex polygon problem cannot be solved using divide and conquer
approach because:
Basic Concepts
Question: In the optimal weight triangulation problem, prove that for a triangulation
of a convex polygon with n vertices, there will be n-1 number of leaf nodes for the
resultant binary tree generated from the dual graph.
Solution:
Find the order of multiplication which minimizes the number of scalar multiplications.
Note:
Order of A1 is p0 x p1,
Order of A2 is p1 x p2,
Order of A3 is p2 x p3, etc.
Order of A1 x A2 x A3 is p0 x p3,
Order of A1 x A2 x . . . x An is p0 x pn
There are p . r total entries in C and each takes O(q) time to compute, thus the
total time to multiply these two matrices is dominated by the number of scalar
multiplication, which is p . q . r.
Question: Algorithm for Closest Pair in 2-D using Divide and Conquer
Closest Pair in 2-D Algorithm using Divide and Conquer approach.
Note: Steps 1-3 form the basis of a dynamic programming solution to a problem.
Step 4 can be omitted only if the value of an optimal solution is required.
Solution:
minDist = infinity
for i = 1 to length(P) - 1
for j = i + 1 to length(P)
let p = P[i], q = P[j]
if dist(p, q) < minDist:
minDist = dist(p, q)
closestPair = (p, q)
return closestPair
-----------------------------------------------------------------
Solution:
Notice that
Solution:
Let
fi[j] = Denotes the fastest time from starting point station Si, j
f1[j] = Denotes the fastest possible time from starting point through jth station of
assembly line 1.
f2[j] = Denotes the fastest possible time from starting point through jth station of
assembly line 2.
li[j] = The line number, 1 or 2, whose station j-1 is used in a fastest way through
station Si, j.
It is to be noted that li[1] is not required to be defined because there is no station before
1
ti[j-1] = Denotes the transfer time cost from assembly line i to station S i-1, j or Si+1, j
Objective function f* = min(f1[n] + x1, f2[n] + x2)
l* = Denotes the line number whose last or nth station is used in a fastest way through
entire factory.
f1[1] = e1 + a1,1
f2[1] = e2 + a2,1
f1[j] = min (f1[j-1] + a1,j , f2[j-1] + t2,j-1 + a1,j) for j ≥ 2;
f2[j] = min (f2[j-1] + a2,j , f1[j-1] + t1,j-1 + a2,j) for j ≥ 2;
Base Cases
f1[1] = e1 + a1,1 f1[1] = 3 + 5 f1[1] = 8
f2[1] = e2 + a2,1 f2[1] = 1 + 3 f2[1] = 4
j 1 2 3 4 5 6
f1[j] 8 11 14 20 28 33
f2[j] 4 8 16 21 25 32
j 2 3 4 5 6
l1[j] 2 1 1 1 2
l2[j] 2 2 1 2 2