Professional Documents
Culture Documents
0 f n cg n , for all n n o
Big-Omega Notation ()
0 cg n f n for all n n o
Theta Notation ()
0 c1 g n f n c2 g n for all n n o
Little-Oh Notation
0 f n cg n , for all n n o
Little-Omega Notation
0 cg n f n for all n n o
Distinct Roots
an Crn Dsn
Repeated Roots
bn C.2n D.n.2n
Order of A1 is p0 x p1,
Order of A2 is p1 x p2,
Order of A1 x A2 x A3 is p0 x p3,
Order of A1 x A2 x . . . x An is p0 x pn
A
A1
A2
A4
.
. 3 .
10 100 100 5 5 50 50 20
P0 = 10
P1 = 100
P2 = 5
P3 = 50
P4 = 20
Brute Force Chain Matrix Multiplication Algorithm
Since in each entry c[i, j], there are q number of scalar of multiplications
Find order of multiplication that minimizes number of scalar multiplications using brute
force approach
Recurrence Relation: After kth matrix, create two sub-lists, one with k and other with n - k
matrices i.e.
(A1 A2A3A4A5 . . . Ak) (Ak+1Ak+2An)
Let P(n) be the number of different ways of parenthesizing n items
m[i, i] 0, i 1,..., 4
m[i, j ] min(m[i, k ] m[k 1, j ] pi 1. pk . p j )
ik j
Chain-Matrix-Order(p)
1. n length[p] 1
2. for i 1 to n
3.
do m[i, i] 0
4. for l 2 to n,
5.
do for i 1 to n-l+1
6.
do j i+l-1
7.
m[i, j]
8.
for k i to j-1
do q m[i, k] + m[k+1, j] + pi-1 . pk . pj
9.
10.
if q < m[i, j]
11.
then m[i, j] = q
12.
s[i, j] k
l is chain length
n i
T (n) n ( j i ) k
i 1 j i 1
i 1 k 1
(n i)(n i 1)
2
i 1
n
T ( n) n
T (n) n
1 n 2
(n 2ni i 2 n i)
2 i 1
n
n
n
n
1 n
T (n) n ( n2 2ni i 2 n i)
2 i 1
i 1
i 1
i 1
i 1
n
n
n
n
n
1
T (n) n (n2 1 2n i i 2 n1 i)
2
i 1
i 1
i 1
i 1
i 1
T ( n) n
1 2
n(n 1) n(n 1)(2n 1)
n(n 1)
(n .n 2n.
n.n
)
2
2
6
2
T ( n) n
1 3
n( n 1)(2n 1)
n( n 1)
(n n 2 (n 1)
n2
)
2
6
2
T ( n) n
1
(6n3 6n3 6n 2 2n3 3n 2 n 6n 2 3n 2 3n)
12
T ( n)
1
1
1
(12n 2n3 2n)
(10n 2n 3 ) (5n n 3 )
12
12
6
Cost Comparison
Dynamic Programming
(n2 . n) = (n3)
f1[1] = e1 + a1,1
f2[1] = e2 + a2,1
j-1
+ a1, j)
Symmetrically
For j = 2, 3, . . ., n
f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
Objective function = f* = min(f1[n] + x1, f2[n] + x2)
then 1[ j] = 1[ j - 1] + a1, j
l1[ j] = 1
8
9
l1[ j] = 2
if 2[ j - 1] + a2, j 1[ j - 1] + t1, j - 1 + a 2,
10 then 2[ j] = 2[ j - 1] + a2, j
11
l2[ j] = 2
12
13
l2[ j] = 1
14 if 1[n] + x1 2[n] + x2
15
16
17
18
then * = 1[n] + x1
l* = 1
else * = 2[n] + x2
l* = 2
1. Print-Stations (l, n)
2.
i = l*
3.
4.
for j = n downto 2
5.
6.
do i = li[j]
print line i , station j - 1
f[i, j] = min{f[1, j-1] + t[1, j-1] + a[i, j], f[2, j-1] + t[2, j-1] + a[i, j], . . , f[n, j-1] + t[n, j-1] + a[i, j]}
f[1, 1] = e1 + a[1, 1]; f [2, 1] = e2 + a[2, 1], ,f [n, 1] = en + a[n, 1]
f* = min{f[1, n] +x1, f[2, n] + x2, . . , f[n, m] + xn}
l* = line number of mth station used
FASTEST-WAY(a, t, e, x, n, m)
1 for i = 1 to n
2
3 for j = 2 to m
4
5
6
7
8
9
for i = 1 to n
[i, j] f[1, j-1] + t[1, j-1] + a[i, j]
L[1, j] = 1
for k 2 to n
if f[i,j] > f[k, j-1] + t[2, j-1] + a[i, j]
then f[i,j] f[k, j-1] + t[2, j-1] + a[i, j]
L[i, j] = k
10
end if
* [1, m] + x[1]
11
13
14
15
16
17
l* = 1
for k 2 to n
if f* > f[k, m] + x[k]
then * [k, m] + x[k]
l* = k
i = l*
3.
4.
for j = m downto 2
5.
do i = li[j]
6.
keep[i, w] = 1;
else
V[i, w] = V[i-1,w];
keep[i, w] = 0;
K = W;
for (i = n down to 1)
if keep[i, K] = = 1
output i
K = K wi
Return V[n, W]
Lemma 1 (polygon)
Proof
Proof is done using mathematical induction
Basis Step
Suppose that there three vertices, polygon will be a triangle, i.e. there are 3 - 2 = 1
number of triangles
If there are 4 vertices, polygon will be in fact a rectangle, divide it into two triangles. The
result is true. Hence statement is true for n = 4
Inductive Hypothesis
Let us suppose that statement is true for n = k, i.e., if there are k vertices then there are
k-2 number of triangles
Claim
Now we have to prove that if there are k+1 vertices there must be k+1-2 = k-1, number
of triangles.
Since for k vertices there are k-2 triangles. Insert one more point at boundary of polygon
In fact point will be inserted at boundary of one of the triangles. So the triangle will
become rectangle. Divide it into two triangles. It will increase one more triangle in the
division. Hence it becomes;
k 2 + 1 = k - 1, number of triangles.
It proves the claim. Hence by mathematical induction it proves that for n number of
vertices there are n - 2 number of triangles.
Lemma 2 (polygon)
Proof
Proof not difficult, and it can be proved following the steps of proof in lemma 1.
If there are three points, it will be triangle. To make a chord in a polygon, it requires at
least four points.
Basis Step
Suppose that there are four number of vertices, in this case polygon will be a rectangle,
there must be 4 - 3 = 1 number of chords
Inductive Hypothesis
Let us suppose that the statement is true for n = k, i.e., if there are k vertices then there
are k - 3 number of chords of the polygon
Claim
Now we have to prove that if there are k+1 vertices there must be k+1-3 = k-2, number
of chords.
Since for k vertices there are k-3 chords. Insert one more point at boundary of polygon
In fact point will be inserted at boundary of one of the triangles. So the triangle will
become rectangle. Divide it into two triangles. It will increase one more chord in the
division. Hence it becomes;
t[i, i] = 0;
3. for l = 2 to n do
4.
for i = 1 to n l+1 do
5.
j = i + l-1;
6.
t[i, j] = ;
7.
for k = i to j - 1 do
8.
9.
10.
11.
v[i, j] = k;
Computational Cost
n
n i
T (n) n ( j i ) k
i 1 j i 1
i 1 k 1
(n i)(n i 1)
2
i 1
n
T ( n) n
1 n 2
1 n
(n n 1) ((1 2n)i i 2 )
2 i 1
2 i 1
n
n
1
1
1 n
n (n 2 n 1)1 (1 2n) i i 2
2
2
2 i 1
i 1
i 1
T ( n) n
(n3 )
If X = (x1, x2,. . ., xm), and Y = (y1, y2, . . ., yn) be sequences and let us suppose that Z =
(z1,z2, . . ., zk) be a longest common sub-sequence of X and Y
Let, Xi = (x1, x2, , xi), Yj = (y1, y2, , yj) and Zl = (z1, z2, , zl) are prefixes of X, Y and Z res.
Proof of Theorem
Case 1
Now, the prefix Zk-1 is a length-(k - 1) common subsequence of Xm-1 and Yn-1.
Suppose, there is a common subsequence W of Xm-1 and Yn-1 with length greater than k 1.
Case 2
If there were a common subsequence W of Xm-1 and Y with length greater than k, then
W would also be a common subsequence of Xm and Y, contradicting the assumption that
Z is an LCS of X and Y.
Case 3
Lemma
Statement
If n N, n > 1 is not prime then n is divisible by some prime number p square root of
n.
Proof
Proof
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.
Time Complexity
n 1
c
i 1 ji 1
n 1
c(n i )
i 1
n 1
n 1
i 1
i 1
c( n i )
( n 1) n
2
2
n
n
c(n 2 n
)
2
2
2
n
n
c(
) ( n 2 )
2
2
cn( n 1) c
6.
Time Complexity
n 1
c
i 1 ji 1
n 1
c(n i )
i 1
n 1
n 1
i 1
i 1
c ( n i )
( n 2 )
6.
Time Complexity
n 1
cn
i 1 ji 1
n 1
cn(n i )
i 1
n 1
n 1
c( n in)
2
i 1
i 1
( n 3 )
Maximal Points
Brute Force Algorithm in n-dimension
A = ;
do maximal true
for j 1 to m
do
if (i j) &
do
P[i].x[k] P[j].x[k]
10
if maximal
11
then A = A P[i]
Merge-sort Algorithm
Merge-sort(A, f, l)
1.
if f < l
2.
then m = (f + l)/2
3.
Merge-sort(A, f, m)
4.
Merge-sort(A, m + 1, l)
5.
Merge(A, f, m, l)
Merge(A, f, m, l)
1. T[f..l]
2. i f; k f; j m + 1
3. while (i m) and (j l)
4. do if (A[i] A[j])
5.
6.
\\comparison of elements
7. while (i m)
8. do T[k++] A[i++]
\\copy from A to T
9. while (j l)
10. do T[k++] A[j++]
\\copy from A to T
11. for i p to r
12. do A[i] T[i]
\\copy from T to A
Let T(n) be the time taken by this algorithm to sort an array of n elements dividing A into
sub-arrays A1 and A2.
It is easy to see that the Merge (A1, A2, A) takes the linear time. Consequently,
T(n) = T(n/2) + T(n/2) + (n)
T(n) = 2T (n/2) + (n)
n
n
n
) 2.T ( 3 ) 2
22
2
2
T(
n
n
n
) 2.T ( 4 ) 3 . . .
3
2
2
2
T(
n
n
n
) 2.T ( k ) k 1
2k 1
2
2
n
n
T (n) 2.T ( ) ( n) 2 2.T ( 2 ) n n
2
2
T (n) 22.T (
n
)nn
22
T ( n) 23.T (
n
)nnn
23
T (n) 2k.T (
n
) n n . . . n
2k
k times
T (n) 2 k .T (
n
) k .n
2k
Hence, T (n) nT
. (1) n.log 2 n n n.log 2 n
T (n) (n.log 2 n)
Time Complexity
T(n) =
2T(n/2) + 1 , n > 2
1
,n2
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 maximas of SR
Time Complexity
T(n) =
Since n = 2k
Hence
T(n) = 2k + 2kn
T(n) = 2k + 2kn
n = 2k k = log(n)
,n2
,n<2
append P[i] to S
09 Sort S on y-coordinate
10 for j 1 to size_of(S)-1 do
11 Check if any of d(S[j],S[j]+1), ..., d(S[j],S[j]+7) is smaller than d, if so set d to the smallest of
them
12 return d
Show by mathematical induction that any amount in cents 20 cents can be obtained using
5 cents and 6 cents coins only.
Solution:
Let P(n) n cents can be formed using only 5 and 6 cent coins
P(n) n = 5s + 6t s 0, and t 0
Basic Step:
P(20) is true, since 20 = 5 4;
P(21) is true, since 21 = 5 3 + 6 1;
P(22) is true, since 22 = 5 2 + 6 2;
P(23) is true, since 23 = 5 1+ 6 3 ;
P(24) is true, since 24 = 6 4 ;
n 20
Inductive Step:
Assume P(20), P(21), , P(k) are true. Now prove for P(k + 1)
Suppose k-4 = 5 s + 6 t.
Then k +1 = 5 (s + 1) + 6 t. true for n = k + 1.
By Strong Induction, P(n) is true if n Z and n 20.
Fk Fk 1 Fk 2
k 2
Fk Fk 1 Fk 2
Let tk is solution to this, then characteristic equation
t2 t 1 0
1 1 4
2
1 5
1 5
t1
, t2
2
2
1 5
1 5
Fn C
D
2
2
n0
0
1 5
1 5
F0 C
D
2
2
F0 C D
C D 0 1
n0
F0 0
Now n 1
1 5
1 5
F1 C
D
2
2
1 5
1 5
C
D 1
2
2
F1 1
1 1 5 1 1 5
Fn
5 2 5 2 5
1 1 5
1 1 5
Fn
5 2
5 2
which is called the explicit formula for the Fibonacci sequence recurrence relation.
1 5
1 5
Let
and
then
2
2
1 n
1 n
Fn
5
5