+ All Categories
Home > Documents > 01-Introduction (2)

01-Introduction (2)

Date post: 08-Apr-2018
Category:
Upload: arpita-sengupta
View: 219 times
Download: 0 times
Share this document with a friend
31
8/7/2019 01-Introduction (2) http://slidepdf.com/reader/full/01-introduction-2 1/31 Introduction to Algorithm Analysis Algorithm : Design & Analysis [1] As soon as an Analytical Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will then arise ± By what course of calculation can these results be arrived at by the machine in the shortest time? - Charles Babbage, 1864 As soon as an Analytical Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will then arise ± By what course of calculation can these results be arrived at by the machine in the shortest time? - Charles Babbage, 1864
Transcript
Page 1: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 1/31

Introduction toAlgorithm Analysis

Algorithm : Design & Analysis

[1]

As soon as an Analytical Engine exists, it will necessarily guide the future

course of the science. Whenever any result is sought by its aid, the

question will then arise ± By what course of calculation can these results

be arrived at by the machine in the shortest time?

- Charles Babbage, 1864

As soon as an Analytical Engine exists, it will necessarily guide the future

course of the science. Whenever any result is sought by its aid, the

question will then arise ± By what course of calculation can these results

be arrived at by the machine in the shortest time?

- Charles Babbage, 1864

Page 2: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 2/31

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 of Problems

Page 3: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 3/31

Page 4: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 4/31

Design and Analysis, in general Design

Understanding the goal

Select the tools What components are

needed

How the components

should be put together  Composing functions to

form 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

Page 5: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 5/31

Problem Solving In general

Understanding the

problem 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:

Page 6: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 6/31

Probably the Oldest Algorithm Euclid algorithm

input: nonnegative integer m,n

output: gcd(m,n)

procedure

E1. n divides m, the remainder pr 

E2. if r =0 then return n

E3. npm; r pn; goto E1 The Problem:

Computing the greatest 

common divisor of twononnegative integers

Specification

Page 7: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 7/31

Euclid Algorithm: Recursive Version Euclid algorithm

input: nonnegative integer m,n

output: gcd(m,n)

procedureEuclid(int m,n)

if n=0then return m

else return Euclid(n, m mod n)

Specification

Recursion

AlgorithmPseudocode

Page 8: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 8/31

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:Searching a list for a

specific key.Input:

an unordered array Ewith n entries, a key K to be matched

Output:

the location of K in E(or fail )

Page 9: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 9/31

Algorithmically Solvable Problem Informally speaking

A problem for which a computer program can be

written that will produce the correct answer for any valid input if we let it run long enough andallow it as much storage space as it needs.

Unsolvable(or un-decidable) problem Problems for which no algorithms exist

the Halting Problem for Turing Machine

Page 10: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 10/31

Computational Complexity Formal theory of the complexity of 

computable functions

The complexity of specific problems andspecific algorithms

Page 11: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 11/31

Criteria for Algorithm Analysis Correctness

Amount of work done

Amount of space used

Simplicity, clarity

Optimality

Page 12: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 12/31

Correctness Describing the ³correctness´: the specification

of a specified problem:Preconditions vs. post-conditions

Establishing the method:Preconditions+Algorithm> post-conditions

Proving the correctness of the implementationof the algorithm

Page 13: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 13/31

Correctness of Euclid Algorithm

Euclid algorithm

input: nonnegative integer m,n

output: gcd(m,n)

procedureEuclid(int m,n)

if n=0

then return m

else return Euclid(n, m mod n)

(m mod n) is always less than n, so,the algorithm must terminate

1

if d is a common divisor of m

and n, it must be a common

divisor of n and (m mod n)2

 GCD recursion theorem:For any nonnegative integer a and positiveinteger 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)

Page 14: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 14/31

How to Measure?

Not too general Giving some indication to make useful comparison

for algorithms Not too precise

Machine independent Language independent Programming style independent Implementation independent

Page 15: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 15/31

Focusing the View

Counting the number of the passes through a loopwhile ignoring the size of the loop

The operation of interest Search or sorting an array comparison Multiply 2 matrices multiplication

Find the gcd bits of the inputs Traverse a tree processing an edge Non-iterative procedure procedure invocation

Page 16: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 16/31

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

Page 17: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 17/31

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 �Dn}, Dn is the set of input

Page 18: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 18/31

Worst-Case Complexity of Euclid¶s

For any integer k u1, if m>nu1 and n<F k+1, then the callEuclid(m,n) makes fewer than k  recursive calls. (to be proved)

Since F k is approximately , the number of recursivecalls in Euclid is O(lgn).

 Euclid(int m,n)if n=0

then return melse return Euclid(n, m mod n)

measured by the number of recursive calls

5/k J 

For your reference:

J = (1+¥5)/2}1.6180«

Page 19: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 19/31

Euclid Algorithm and Fibonacci

If m>nu1 and the invocation Euclid(m,n)performs k u1 recursive calls, then muF k+2 and

nuF k+1. Proof by induction Basis: k =1, then nu1=F 2. Since m>n, mu2=F 3. For larger k , Euclid(m,n) calls Euclid(n, m mod n)

which makes k -1 recursive calls. So, by inductivehypothesis, nuF k+1, (m mod n)uF k .Note that m u n+(m- m/n½n) = n+(m mod n) uF k+1+F k = F k+2

Page 20: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 20/31

The Bound is Tight

The upper bound for Euclid(m,n) is tight, bywhich we mean that: ³if b<F k+1, the call

Euclid(a,b) makes fewer than k  recursive calls´is best possible result.

There do exist some inputs for which the

algorithm makes the same number of recursivecalls as the upper bound. Euclid (F k+1, F k ) recurs exactly k -1 times.

Page 21: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 21/31

Average Complexity

Weighted average A(n)

How to get Pr(I) Experiences

Simplifying assumption On a particular application

§�

!nDI 

I t I nA )()Pr()(

 Pr(I ) is the probabilityof ocurrence of input I 

Page 22: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 22/31

Page 23: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 23/31

Average Behavior Analysis of Sequential Search

Case 2: K may be not in E Assume that q is the probability for K in E

A(n) = Pr(succ)Asucc(n)+Pr(fail) Afail(n)

=q((n+1)/2)+(1-q)n

Issue for d iscussion:

Reasonabl e Assumpt ions

Page 24: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 24/31

Optimality

³The best possible´How much work is necessary and sufficient to solve the

problem. Definition of the optimal algorithm

For problem P , the algorithm A does at most WA(n) stepsin the worst case (upper bound)

For some function F , it is provable that for any algorithmin the class under consideration, there is some input of size

n for which the algorithm must perform at least F (n) steps(lower bound) If WA=F, then A is optimal.

Page 25: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 25/31

Complexity of the Problem

F  is a lower bound for a class of algorithmmeans that: F or any algorithm in the class,

and any input of size n, there is some input of size n for which the algorithm must perform

at least F( n) basic operations.

Page 26: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 26/31

Page 27: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 27/31

Bounds: Upper and Lower 

For a specific algorithm (to solve a given problem),the ³upper bound´ is a cost value no less than the

maximum cost for the algorithm to deal with theworst input.

For a given problem, the ³lower bound´ is a costvalue no larger than any algorithm (known or 

unknown) can achieve for solving the problem. A computer scientist want to make the two bounds

meet.

Page 28: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 28/31

Home Assignment

pp.61 ±  1.5

1.12 1.16 ± 1.19

Additional Other than speed, what other measures of efficiency might

one use in a real-world setting? Come up with a real-world problem in which only the best

solution will do. Then come up with one in which asolution that is ³approximately´ the best is good enough.

Page 29: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 29/31

Algorithm vs. Computer Science Sometimes people ask: ³What really is computer science? Why

don¶t we have telephone science? Telephone, it might be argued,are as important to modern life as computer are, perhaps evenmore so. A slightly more focused question is whether computer science is not covered by such classical disciplines asmathematics, physics, electrical engineering, linguistics, logicand philosophy.We would do best not to pretend that we can answer these

questions here and now. The hope, however, is that the coursewill implicitly convey something of the uniqueness anduniversality of the study of algorithm, and hence something of the importance of computer science as an autonomous field of study.

- adapted from H ar el : ³Algor ithmics, the S pir it  of  C omput ing´

Page 30: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 30/31

References

Classics Donald E.Knuth. T he Ar t  of  C omputer Pr og r amming 

Vol.1 Fundamental Algorithms

Vol.2 Semi-numerical Algorithms Vol.3 Sorting and Searching

Popular textbooks Thomas H.Cormen, etc. Int r od uct ion t o Algor ithms

Robert Sedgewick.Algo

r ithms (with different versions using differentprogramming languages)

Advanced mathematical techniques Graham, Knuth, etc. C oncr ete M athemat ics: A F ound at ion for 

C omputer S cience

Page 31: 01-Introduction (2)

8/7/2019 01-Introduction (2)

http://slidepdf.com/reader/full/01-introduction-2 31/31

You Have Choices

Design Techniques Oriented Textbooks Anany Levitin. Int r od uct ion t o the Desig n and Analysis of   Algor ithms

M.H.Alsuwaiyel. Algor ithms Desig n T echniques and Analysis

G.Brassard & P.Bratley: Fund ament al s of   Algor ithmics

Evergreen Textbook  Aho, Hopcroft and Ullman. T he Desig n and Analysis of   

C omputer Algor ithm

Something New J.Kleinberg & E.Tardos: Algor ithm Desig n


Recommended