Documente Academic
Documente Profesional
Documente Cultură
Ryan Patrick
Outline
Algorithm Analysis
Asymptotic Notation
Non-Recursive Algorithms
Recursive Algorithms
Review
Patrick
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recurrence Relations
Recursive
The Master Theorem
Counting
Special Cases
Binomial
Coefficient
Applications
31
Outline
Algorithm Analysis
Asymptotic Notation
Non-Recursive Algorithms
Recursive Algorithms
Review
Patrick
3
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recurrence Relations
Recursive
The Master Theorem
Counting
Special Cases
Binomial
Coefficient
Applications
31
Asymptotic Notation
Review
Patrick
Analysis
4
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
Counting
Relative or Absolute
Special Cases
Binomial
Coefficient
Applications
31
Asymptotic Notation
Notations
Review
Patrick
Notation
Mathematical Definition
Analysis
5
O (g (n))
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
Counting
(g (n))
Special Cases
Binomial
Coefficient
Applications
(g (n))
31
Non-Recursive Algorithms
MaxElement
Review
Patrick
maxElement.js
function maxElement ( array )
{
maxVal = array [ 0 ];
Analysis
6
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
Counting
Special Cases
Binomial
Coefficient
Applications
}
31
Non-Recursive Algorithms
MatrixMultiplication
matMult.js
function m a t r i x M u l t i p l i c a t i o n ( a , b )
{
// a and b are square , nxn matrices
for ( var i =
{
for ( var
{
c[ i
for (
{
0; i < n ; i ++ )
j = 0; j < n ; j ++ )
][ j ] = 0.0;
var k = 0; k < n ; k ++ )
c [ i ][ j ] = c [ i ][ j ] + a [ i ][ k ] * b [ k ][ j ];
}
}
}
return c ;
}
Non-Recursive Algorithms
Binary
Review
Patrick
Analysis
binary.js
function binary ( n )
{
var count = 1;
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
Counting
while ( n > 1 )
{
count = count + 1;
n = Math . floor ( n / 2 ) ;
}
Special Cases
Binomial
Coefficient
Applications
return count ;
}
31
Recursive Algorithms
Review
Patrick
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
Counting
Special Cases
Binomial
Coefficient
Applications
31
Recurrence Relations
Review
Patrick
Analysis
10
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
Counting
Special Cases
Binomial
Coefficient
Applications
31
Recurrence Relations
Example: T (n) = T (n 1) + 2
T (n)
= T (n 1)
= T (n 2)
= T (n 3)
Review
+2
+2
+2
+2
+2
Patrick
..
.
= T (n k)
= T (1)
= O (n)
+2k
+2 (n 1)
Analysis
+2
11
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
For k = n 1
Counting
Special Cases
Binomial
Coefficient
Applications
31
Recurrence Relations c
T (n)
= T (n k)
= T (1)
+nc
+ (n 1)c
+ (n 2)c
..
.
Pk1
+ i=0 (n i)c
P
c
+ n2
i=0 (n i)
= T (1)
+nc
= T (n 1)
= T (n 2)
= T (n 3)
= T (1)
= O nc+1
(n 1)
Review
+nc
+ (n 1)c
Patrick
Analysis
+nc
12
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
Counting
For k = n 1
Special Cases
Binomial
Coefficient
nc is largest term
in
polynomial,
summed n 1
times
Applications
+nc+1
31
Recurrence Relations
Example: T (n) = T
T (n)
n +1
1
= T n2
1
= T n4
1
= T n8
..
.
1
= T n 2k
+k
= T (c)
+k
Review
+1
+1
+1
Patrick
+1
+1
Analysis
+1
13
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
Counting
Special Cases
Binomial
Coefficient
if n 2k = c (a small constant)
..
.
Applications
31
Recurrence Relations
Example: T (n) = T
n + 1 (continued)
= n 2k
log2 c
1
2k
Review
Patrick
Analysis
log2 n
Rules of logarithms
14
2k
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
log2 n
log2 c
Counting
Special Cases
= logc n
Binomial
Coefficient
Applications
= log2 logc n
31
Recurrence Relations
Example: T (n) = T
T (n)
n + 1 (continued)
1
= T n2
1
= T n4
1
= T n8
=T n
1
2k
Review
+1
+1
+1
Patrick
+1
+1
..
.
Analysis
+1
15
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
+k
Counting
Special Cases
= T (c)
+k
Binomial
Coefficient
if n 2k = c (a small constant)
= T (c)
= O (log log n)
Applications
+ log2 logc n
31
Outline
Algorithm Analysis
Asymptotic Notation
Non-Recursive Algorithms
Recursive Algorithms
Review
Patrick
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recurrence Relations
16
Recursive
The Master Theorem
Counting
Special Cases
Binomial
Coefficient
Applications
31
17
Recursive
The Master Theorem
Counting
Binomial
Coefficient
Special Cases
Applications
31
T (n) = aT
n
b
+ f (n) T (n) nd if a< b d
T (n) nd logn if a = b d
T (n) nlogb a if a > b d
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
18
Counting
Special Cases
Binomial
Coefficient
Applications
Outline
Algorithm Analysis
Asymptotic Notation
Non-Recursive Algorithms
Recursive Algorithms
Review
Patrick
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recurrence Relations
Recursive
The Master Theorem
19
Counting
Special Cases
Binomial
Coefficient
Applications
31
Recursive
Feasibility
Confirmation
Counting
Special Cases
Binomial
Coefficient
Applications
31
Feasibility
Review
Patrick
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
5220 > 2 1034 possible 20-character long passwords that only contain
uppercase and lowercase letters
Is a brute force attack feasible if you can check 1, 000, 000, 000 possible
passwords per second?
Recursive
The Master Theorem
21
Counting
Special Cases
Binomial
Coefficient
Applications
31
Confirmation
Review
Patrick
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
22
Counting
Special Cases
Binomial
Coefficient
Confirmation simplified
Applications
31
Duplicates
How many possible unique permutations of baseball are there?
Review
Patrick
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
A. 8! (40, 320)
B.
C.
D.
E.
8!
3
8!
6
Recursive
(13, 440)
(6, 720)
Counting
23
8!
2!2!2! (5, 040)
8!
2! (20, 160)
Special Cases
Binomial
Coefficient
Applications
31
Ordering
Review
Patrick
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
2
3
1
3
Recursive
The Master Theorem
Counting
24
Special Cases
Binomial
Coefficient
Applications
31
Outline
Algorithm Analysis
Asymptotic Notation
Non-Recursive Algorithms
Recursive Algorithms
Review
Patrick
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recurrence Relations
Recursive
The Master Theorem
Counting
Special Cases
25
Binomial
Coefficient
Applications
31
Represented as:
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
n
k
Recursive
The Master Theorem
Counting
Special Cases
26
Binomial
Coefficient
Applications
31
The Formula
Review
Patrick
Analysis
n
n!
= k!(nk)!
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
Counting
Special Cases
27
Binomial
Coefficient
Applications
31
Example:
3
2
3!
2!(32)!
6
2
=3
Review
Patrick
Analysis
,,,
,,/
,/,
/,,
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
Counting
Special Cases
28
Binomial
Coefficient
Applications
31
Recursive
Counting
Special Cases
Binomial
Coefficient
2 3 21 = 126
29
31
Applications
2 centers, 3 forwards, 5 guards . . . and 2 players who can play either guard
or forward on roster
Recursive
Counting
2
{z
2
}
No Flex Players
2
3
5
1
|
2+
{z
One Flex Guard
2
3
5
1
|
2+
{z
One Flex Forward
2
3
5
1
|
1
{z
2
}
Special Cases
2
3
1
|
2
{z
2
5
1
|
Binomial
Coefficient
2
{z
30
2 3 10 + 2 3 5 2 + 2 3 10 2 + 2 3 5 2 + 2 3 + 2 10
= 326
31
Applications
Any Questions?
Review
Patrick
Analysis
Asymptotic
Non-Recursive
Recursive Algorithms
Recurrence Relations
Recursive
The Master Theorem
Counting
Special Cases
Binomial
Coefficient
31
31
Applications