Download Understanding Efficient Algorithm Design: Principles & Techniques and more Assignments Design and Analysis of Algorithms in PDF only on Docsity!
Introduction to Algorithm Analysis Algorithm : Design & Analysis
[1]
As soon as an Analytical Engine exists, it will necessarily guide the futurecourse of the science. Whenever any result is sought by its aid, thequestion will then arise – By what course of calculation can these resultsbe arrived at by the machine in the shortest time?
- Charles Babbage, 1864
As soon as an Analytical Engine exists, it will necessarily guide the futurecourse of the science. Whenever any result is sought by its aid, thequestion will then arise – By what course of calculation can these resultsbe arrived at by the machine in the shortest time?
- Charles Babbage, 1864
Introduction to Algorithm Analysis^ ^ Goal of the Course^ ^ Algorithm, the concept^ ^ Algorithm Analysis: the criteria^ ^ Average and Worst-Case Analysis^ ^ Lower Bounds and the Complexity ofProblems
Design and Analysis, in general^ ^ Design^ ^ Understanding the goal^ ^ Select the tools^ ^ What components areneeded^ ^ How the componentsshould be put together^ ^ Composing functions toform a process
^ Analysis^ ^ How does it work?^ ^ Breaking a system downto known components^ ^ How the componentsrelate to each other^ ^ Breaking a process downto known functions
Problem Solving^ ^ In general^ ^ Understanding theproblem^ ^ Selecting the strategy^ ^ Giving the steps^ ^ Proving the correctness^ ^ Trying to improve
^ Using computer^ ^ Describing the problem:^ ^ Selecting the strategy:^ ^ Algorithm:^
Input/Output/Step: Analysis: Correct or wrong “good” or “bad” Implementation: Verification:
Euclid Algorithm: Recursive Version^ ^ Euclid algorithm^ ^ input: nonnegative integer
m , n
^ output: gcd(
m , n )
^ procedure^ Euclid(int
m , n ) if^ n =0^ then return
m else return
Euclid(
n ,^ m^ mod
n )
Specification Recursion AlgorithmPseudocode
Sequential Search, another Example^ ^ Procedure:^ Int^
seqSearch(
int [] E,^
int^ n,^ int
K)
int^ ans, index; ans=-1; for^ (index=0; index<n; index++)^ if^ (K==E[index])
ans=index; break ; Return^
ans;
The Problem
: The Problem Searching a list for aspecific key.^ Input : an unordered array Ewith n entries, a key Kto be matched^ Output : the location of K in E(or^ fail )
: Searching a list for aspecific key. Input : an unordered array Ewith n entries, a key Kto be matched Output : the location of K in E(or^ fail )
Computational Complexity^ ^ Formal theory of the complexity ofcomputable functions^ ^ The complexity of specific problems andspecific algorithms
Criteria for Algorithm Analysis^ ^ Correctness^ ^ Amount of work done^ ^ Amount of space used^ ^ Simplicity, clarity^ ^ Optimality
Correctness of Euclid Algorithm^ ^ Euclid algorithm^ ^ input: nonnegative integer
m , n
^ output: gcd(
m , n )
^ procedure^ Euclid(int
m , n ) if^ n =0^ then return
m else return
Euclid(
n ,^ m^ mod ( m^ mod^ n ) is always less than^ n )
n , so,
if^ d^ is a common divisor of 1 the algorithm must terminate
m
and^ n , it must be a commondivisor of
n^ and ( m
mod^ n ) GCD recursion theorem:For any nonnegative integer^2
a^ and positive
integer^ b : gcd(
a , b ) = gcd(
b , ( a^ mod
b ))
Proof: gcd(
a , b ) | gcd(
b , ( a^ mod
b )), and gcd( b , ( a^
mod^ b )) | gcd(
a , b )
GCD recursion theorem:For any nonnegative integer
a^ and positive
integer^ b : gcd(
a , b ) = gcd(
b , ( a^ mod
b ))
Proof: gcd(
a , b ) | gcd(
b , ( a^ mod
b )), and gcd( b , ( a^
mod^ b )) | gcd(
a , b )
How to Measure?^ ^ Not too general^ ^ Giving some indication to make useful comparisonfor algorithms^ ^ Not too precise^ ^ Machine independent^ ^ Language independent^ ^ Programming style independent^ ^ Implementation independent
Presenting the Analysis Results^ ^ Amount of work done usually depends on thesize of the inputs^ ^ Amount of work done usually doesn’t dependon the size solely
Worst-case Complexity^ ^ Worst-case complexity, of a specifiedalgorithm
A^ for a specified problem
P^ of size n:
^ Giving the maximum number of operationsperformed by
A^ on any input of size n
^ Being a function of n ^ Denoted as W(n) W ( n )=max{
t ( I ) |^ I ∈
D },^ D n
is the set of inputn
Euclid Algorithm and Fibonacci^ ^ If^ m
> n ≥1 and the invocation Euclid(
m , n )
performs
k ≥1 recursive calls, then
m ≥ F k+
and
n ≥ F k+
^ Proof by induction ^ Basis:
k =1, then
n ≥1= F
. Since 2
m > n ,^ m
≥2= F.^3
^ For larger
k , Euclid(
m , n ) calls Euclid(
n ,^ m^ mod
n )
which makes
k -1 recursive calls. So, by inductive
hypothesis,
n ≥ F k+
, ( m^ mod
n )≥ F .k^
Note that
m^ ≥^ n +(
m - ⎣ m / n
⎦ n ) =^ n
+( m^ mod
n )^ ≥
F + F k+
=^ F k k+
The Bound is Tight^ ^ The upper bound for Euclid(
m , n ) is tight, by
which we mean that: “if
b < F k+
, the call
Euclid(
a , b ) makes fewer than
k^ recursive calls”
is best possible result. There do exist some inputs for which thealgorithm makes the same number of recursivecalls as the upper bound.^ ^ Euclid (
F ,^ F k+^
) recurs exactlyk
k -1 times.