Escolar Documentos
Profissional Documentos
Cultura Documentos
Issues:
Approaches:
T(n) copC(n)
running time execution time for basic operation Number of times basic operation is executed
2
Search for key in list Number of items in of n items list n Multiply two matrices of floating point numbers Compute an Dimensions of matrices n #vertices and/or edges
Design and Analysis of Algorithms - Chapter 2
Graph problem
a specific (typical) sample of inputs Use physical unit of time (e.g., milliseconds) OR Count actual number of basic operations Analyze the empirical data
Given a list of n elements and a search key K, find an element equal to K, if any. Algorithm: Scan the list and compare its successive elements with K until either a matching element is found (successful search) or the list is exhausted (unsuccessful search) Worst case Best case Average case
Design and Analysis of Algorithms - Chapter 2 6
formula e.g., C(n) = n(n-1)/2 Formula indicating order of growth with specific multiplicative constant e.g., C(n) 0.5 n2 Formula indicating order of growth with unknown multiplicative constant e.g., C(n) cn2
Design and Analysis of Algorithms - Chapter 2 7
Order of growth
Most
table 2.1
Design and Analysis of Algorithms - Chapter 2 8
Table 2.1
Big-oh
11
Big-omega
12
Big-theta
13
limn T(n)/g(n) =
c>0
Examples: 10n
vs.
2 n2
n(n+1)/2
logb n
vs.
vs.
n2
logc n
14
LHpitals rule
If
Then lim
n
f(n) g(n) =
16
A simple example
Algorithm MaxElement(A[0..n-1])
// Determines the value of the largest element in a given array // Input: An array A[0..n-1] of real numbers // Output: The value of the largest element in A
for i0 to n-2 do for ji+1 to n-1 do if A[i]=A[j] return false return true
Design and Analysis of Algorithms - Chapter 2 20
Other examples
Matrix
21
Matrix multipliacation
22
Selection sort
24
Insertion sort
25
Mystery algorithm
for i := 1 to n-1 do max := i ; for j := i+1 to n do if |A[j,i]| > |A[max,i ]| then max := j ; for k := i to n+1 do swap A[i,k] with A[max,k]; for j := i+1 to n do for k := n+1 downto i do A[j,k] := A[j,k] - A[i,k]*A[j,i]/A[i,i] ;
Design and Analysis of Algorithms - Chapter 2 26
Recursive evaluation of n !
Definition: n ! = 1*2**(n-1)*n Recursive definition of n!: n ! = (n-1)!*n Algorithm: if n=0 then F(n) := 1 else F(n) := F(n-1) * n return F(n) Recurrence for number of multiplications: M(n) = M(n-1) + 1 for n>0 M(0) = 0 Method: backward substitution
Towers of Hanoi
Problem: to count the number of moves Recurrence for number of moves: M(n) = M(n-1) + 1 + M(n-1) M(1) = 1 for n>1
29
Algorithm BinRec(n)
// Input: A positive decimal integer n // Output: The number of binary digits in ns binary representation if n=1 return 1 else return BinRec(n/2)+1
A(1) = 0
Definition based recursive algorithm Nonrecursive brute-force algorithm Explicit formula algorithm
4.
31
Algorithm F(n):
// Computes the n-th Fibonacci number recursively // Input: A nonnegative integer n // Output: The n-th Fibonacci number if n <= 1 return n else return F(n-1)+F(n-2)
Design and Analysis of Algorithms - Chapter 2
32
2nd order linear homogeneous recurrence relation with constant coefficients Characteristic equation Solution F(n) = n/5 n/5 = (1+5)/2 (golden ratio) = (1-5)/2
F(n) n/5
Construct a tree for the number of recursive calls Recursive algorithms solutions, not a panacea
Design and Analysis of Algorithms - Chapter 2 33
Alternatively algorithmic use of equation F(n) n/5 with smart rounding Relies on the exponentiation algorithm Solutions: (n), (logn)
35
It holds that
F(n-1)
F(n)
0 1
1 1
F(n) F(n+1)
for n1,
36
quadratic
n log n
37
3.
a < bk a = bk a > bk
38
Characteristic equation (quadratic) Solve to obtain roots r1 and r2 e.g.: A(n) = 3A(n-1) - 2(n-2) General solution to RR: linear combination of r1n and r2n Particular solution: use initial conditions e.g.:A(0) = 1 A(1) = 3
Design and Analysis of Algorithms - Chapter 2 39