Beruflich Dokumente
Kultur Dokumente
Chapter 1
Outline
Mathematical Induction Concept: Algorithms design and analysis process Efficiency, Analysis, and Order Some algorithms Asymptotic notation
Mathematical Induction
Mathematical Induction
A very important type of reasoning which permeates much of mathematical thought. Review (Proof by induction).
Mathematical Induction
Mathematical induction is used to establish with logical certainty the correctness of a theorem for an infinite sequence of cases. Let A denote a statement referring to an arbitrary natural number n. For example, A might be the statement The sum of the interior angles in a simple polygon of n + 2 sides is n times 180 degrees or n.
General formulation
To prove a statement of this type it is not sufficient to prove it for the first 10 or the first 100 or even the first 1000 values of n. Need principle of mathematical induction.
These two conditions are sufficient to prove the correctness of all A1, A2, A3, constitutes the Principle of mathematical induction.
Proof By Induction
Claim: S(n) is true for all n >= k
Basis Step:
Show formula is true when n = k
Review: Induction
Suppose
S(k) is true for fixed constant k
Often k = 0
(a + b)n = an + (n/1)an - 1b + (n(n -1)/(1 x 2))an2b2 + (n(n -1)(n -2)/(1 x 2 x 3))an-3b3 + + (n(n -1)(n -2) x x 2 x 1)/(1 x 2 x 3 x x n))bn
2n = 2n+1 2.
Prove that For n > 1, 12 + 23 + 34 + ... + (n)(n+1) = (n)(n+1)(n+2)/3.
Induction
Weve been using weak induction Strong induction also holds
Basis: show S(0) Hypothesis: assume S(k) holds for arbitrary k <= n Step: Show S(n+1) follows
Another variation:
Basis: show S(0), S(1) Hypothesis: assume S(n) and S(n+1) are true Step: show S(n+2) follows
Algorithms
Problem: is a question to which we seek an answer. Algorithm: Problem solutions. Example: Sort a list S of n numbers in nondecreasing order. The answer is the numbers in sorted sequence. The purpose of studying these techniques (algorithms) is that when confronted with a new problem, you have a repertoire of techniques to consider as possible ways to solve the problem.
Note: This starts Chapter 1.
Algorithms
1.1 - Sequential search 1.2 - Binary search
Algorithms
1.1 - Sequential search Examples:
Sequential Search Add Array Members Exchange Sort Matrix Multiplication
Algorithms
Binary search algorithm
Array Size
Table 2: A comparison
n n+1 2n/2 Execution time (Iterative)
41 ns 61 ns 81 ns 101 ns 121 ns 161 ns 201 ns
1,048,576 1.1 x 109 1.1 x 1012 1.1 x 1015 1.2 x 1018 1.2 x 1024 1.3 x 1030
Analysis of Algorithms
Analysis of Algorithms
1) Input size? 2) Basic operation? - One instruction or group of instructions. 3) T(n) is defined as the number of times the algorithm does the basic operation for an instance of size n. T(n) is called the every-case time complexity of the algorithm. The determination of T(n) is called an every-case time complexity analysis.
Analysis of Algorithms
Analysis of Algorithms
Complexity functions
What is a function?
A complexity function can be any function that maps the positive integers to the nonnegative reals. We will use f(n) and g(n) to represent complexity functions. Example 1.6 (page 23).
0.1n2
0.1n2 + n + 100
10 20 50 100 1,000
Order
Is 0.01n2 > 100n? Linear-time algorithms Quadratic-time algorithms ( (n2 ) algorithm) Pure quadratic Complete quadratic
Asymptotic Performance
Asymptotic Notation
By now you should have an intuitive feel for asymptotic (big-O) notation:
What does O(n) running time mean? O(n2)? O(n log n)? How does asymptotic running time relate to asymptotic memory usage?
Our first task is to define this notation more formally and completely
Analysis of Algorithms
Analysis is performed with respect to a computational model We will usually use a generic uniprocessor randomaccess machine (RAM) All memory equally expensive to access No concurrent operations All reasonable instructions take unit time Except, of course, function calls Constant word size Unless we are explicitly manipulating bits
Input Size
Time and space complexity This is generally a function of the input size E.g., sorting, multiplication How we characterize input size depends: Sorting: number of input items Multiplication: total number of bits Graph algorithms: number of nodes & edges etc
Running Time
Algorithm FindMax Int findMax (E, n) 1. max = E [0]; 2. for (index = 1; index < n; index ++) 3. if (max < E [index]) 4. max = E [index]; 5. return max;
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 be
Analysis
Worst case
Provides an upper bound on running time An absolute guarantee
Average case
Provides the expected running time Very useful, but treat with care: what is average?
Random (equally likely) inputs Real-life inputs
Summary
Principle of mathematical induction. Algorithmic design and analysis concepts (chapter 1). Computational models for analysis. Asymptotic notation. Appendix A (textbook).
The End