l01 algorithm analaysis

24
Elementary numerical analysis: algorithm analysis  Algor it hm anal ys is

Upload: okjunwon

Post on 10-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 1/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

Page 2: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 2/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

³Good program is a unity of thought-out algorithm and 

efficient data structures´.

N. Wirth.

³Software is getting slower more rapidly than

hardware becomes faster´.

Wirth¶s law (1995).

Niklaus Wirth (born Feb. 15, 1934) is a Swiss

computer scientist, known for designing several

 programming languages, including Pascal,

and for pioneering several classic topics

in software engineering.

In 1984 he won the Turing Award

for developing a sequence of 

innovative computer languages.

1969

Page 3: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 3/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

³Algorithm´ and ³data structure´ are key terms in computer science.

Which algorithms and data structures are high quality and effective?

We need to use accurate methods of algorithm analysis.

First, natural criterion: time of execution.

Also important:

 ± amount of resourced required: memory and disk space;

 ± reliability and validity of solutions;

 ± stability of solutions (robustness)

In computer science algorithm is robust if it continues

to operate despite abnormalities in input, calculations, etc.

Various commercial products perform robustness testing of 

software systems.

There are many examples of robustness in

biology as well.

Page 4: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 4/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

At first, consider a time concept . How to evaluate a time for an arbitrary algorithm?

The easiest way to determine the time required to perform the algorithm

is to measure the time before start and after completion of the algorithm.

Time of execution of algorithm or data structure operation depends on several factors.

#include <time.h> #include <stdlib.h> 

#include <stdio.h> 

void main (void)

{

time_t before = time (NULL);

for (int i=0; i < 10E8; i++)(void)rand ();

 printf ("time was taken about %d seconds",time(NULL)- before);}

The result (for example):

time was taken about 17 seconds

Page 5: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 5/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

Direct measuring of execution time is not exact!

Reasons:

 ± modern operating systems are multitasking;

 ± multiple evaluation needed (due to random factors presence)

 ± different input data sets must be processed.

Time

Number of elements

Experimental data

Trend

Always needed

for non trivial

algorithms.

Interpolationand approximation

of results is possble

Page 6: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 6/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

Generally:

 ± the larger amount of data, the longer execution time;

 ± execution time depends on data structure;

 ± hardware is important (CPU, RAM, HDD, etc.);

 ± software is important (OS, programming language, compiler, etc.)

So, the analysis of algorithms empirically, is not really reliable.

Page 7: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 7/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

1. Limited data set

 Results obtained using another set are not taken into account 

Example: algorithm for calculating the sum of a series with

given accuracy delta

The main disadvantages of empirical analysis:

Algorithm will give wrong solution (equal to a finite number) because this series is d ivergent , and its sum is infinite.

Page 8: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 8/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

#include <math.h> #include <stdio.h> 

#include <conio.h> 

void main(void)

{

double sum = 0 , ai;int f = 1 , i = 1;

do {sum += (ai=1.0/f); f*=i++; }while (fabs(ai) >= 1E-5);

 printf ("exp = %lf" , sum);

getch ();

}

Code example:

Page 9: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 9/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

2. To compare two algorithms

the same hardware and software are required

The main disadvantages of empirical analysis:

3. It is necessary to implement and execute an algorithm for 

the experimental study.

So, we need general methods for algorithm analyzing.

Requirements:

 ± has to take into account the different types of input data;

 ± hardware and software independence;

 ± can be performed with algorithm description, no implementation

or run-time testing required.

Page 10: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 10/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

Algorithm  f (n1, «, nm)

General idea:

 ± number of data items for processing;

 ± accuracy;

 ± validity ( for large numbers computing, for example);

 ± «

The first stage of the algorithm developing is writing a pseud ocod e.

Page 11: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 11/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

 P seud ocod e is a compact language of algorithm d escription.

Example:

Title: An algorithm for counting the number of array elements equal to zeroBaseline data: Array A

Required: the number of elements equal to zero

Start

Put the number of K  = 0

CYCLEfor all elements of the array from

1to

ElementsNumber( A )

if the element A (i) = 0 THEN K  = K  + 1END OF CYCLEDisplaying K

End

It uses keywords of a programming language, but omits not significant details.The main purpose of pseudocode using is to provide understanding

of an algorithm, make it.

Page 12: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 12/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

Logarithmic and exponential functions are used widely for 

analyzing algorithms and data structures:

Page 13: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 13/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

Cycles lead to series appearance, so

the series are used widely in algorithm analysis:

F ( N ) =A(1) + A(2) + « + A( N )

 geometric series example

If integer  N>=0 and real 0 < a < > 1, then:

arithmetic series example

Page 14: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 14/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

During algorithm analyzing (counting the operation and running time)

³small details´ are not important .

For example, constant multipliers can be neglected.

³Big O´ is used in practice.

For example, counting zero elements of an arraycan be described as O(n),

where n ± number of elements in array.

The or d er of the algorithm is a function, dominating over exact

expression of time complexity.

It describes algorithm efficiency as a function of number of 

 processed data.

Page 15: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 15/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

Let k  is a constant,  f  and g are functions.

 f O f k O !1.

(constant multipliers are not important)

 g O f O g  f O g O f O g  f O !! ,2.

21717 N O N  N O N O N O N O N O N  N O !!!!Example:

is equal to dominant of   g  f O 3.  f O  g Oand

525 N O N  N O !Example:

Page 16: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 16/24

Page 17: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 17/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

Example 1:

Evaluate complexity of the algorithm:

 P n(x) = an x n + an-1 x n-1 + ... + ai  x i + ... + a1 x 1 + a0

 f O f k O !1.

 g O f O g  f O g O f O g  f O !! ,2.

is equal to dominant of   g  f O 3.  f O  g Oand

Computing of the i-th item (i = 1 ..n) requires i multiplications.

Totally: 1 + 2 + 3 + ... + n = n(n+1)/2  multiplications.

n+1 additions.

Totally: n(n+1)/2 + n + 1= n2/2 + 3n/2 + 1 operations. 2nO

Page 18: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 18/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

Example 2:

 f O f k O !1.

 g O f O g  f O g O f O g  f O !! ,2.

is equal to dominant of   g  f O 3.  f O  g Oand

Reduce the expression as follows:

 P n(x) = a0 + x(a1 + x(a2 + ... ( ai + .. x(an-1 + an x))).

 P 3(x) = a3 x^3 + a2 x^2 + a1 x^1 + a0 = a0 + x(a1 + x(a2 + a3 x))

For example,

1 *, 1+

n-1 brack ets totally

1*, 1+

Totally: n multiplications + n additions = 2n.

nO

 P n(x) = an x n + an-1 x n-1 + ... + ai  x i + ... + a1 x 1 + a0

Page 19: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 19/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

1) 578.212.720 23 nnn

nn logloglog3 2)

21003)

n54)

?O

?O

1O

¹ º

 ¸©ª

¨

nO

1

3nO

nO log

?O

?O

Exercise:

 f O f k O !1.

 g O f O g  f O g O f O g  f O !! ,2.

is equal to dominant of   g  f O 3.  f O  g Oand

Page 20: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 20/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

A problem: find a name in a telephone book of  N  pages.Evaluate a complexity of search algorithm.

Example:

Opening a book in the middle halves ³the rest´ of the problem.

(We assume that names are sorted alphabetically).

So, the search requires time no more than log2 N .

If   N = 1000 pages, we have to open the book  log2 1000 § 10 times.

If   N = 100 000 pages, we have to open the book  log2 100000 § 17 times.

nO 2logTime complexity of the algorithm is

Page 21: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 21/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis

Donald Knuth ³The Art of Computer Programming ́

http://www-cs-faculty.stanford.edu/~knuth/taocp.html

Donald Knuth at a reception for the Open Content Alliance,

October 25, 2005 (From Wikipedia).

Donald Knuth

 born January 10, 1938

Homepage: http://www-cs-staff.stanford.edu/~uno/

TheArt of Computer Programming 1:

( 3)

ý(Donald E. Knuth) | |

| 2006 09 |()

Page 22: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 22/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis: Homework 1

Homework 1

Write a program tabulating a function.

The function Y(X) is defined in your program code.

User enters three numbers: X1, X2 and S.

User enters a way of result output (print on a screen or write to a text file).

The program computes values of Y(X) between X1 and X2 with the step S and

 prints a table on a screen or to a file.

C:\>program.exe

Num | X | Y(X) |

-------------------------------

0 | 1.5 | 2.74 |

1 | 1.6 | 3.21 |

...

...

computer screen

Page 23: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 23/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis: Homework 1

Deadline: 14/09/2010

Present your working program (your_file.exe) andsource code (files *.c (or *.cpp) and *.h).

 N ote: write your student number and english name in the first 

line of your program as a comment:

/* <student number> <your name>*/

Send the files to e-mail: [email protected]

or bring me your files on USB flash drive. E-mail is the best.

Page 24: L01 Algorithm Analaysis

8/8/2019 L01 Algorithm Analaysis

http://slidepdf.com/reader/full/l01-algorithm-analaysis 24/24

Elementary numerical analysis: algorithm analysis

 Algorithm analysis: Homeworks

Homeworks (details will be given later)

1.Tabulating a function (by 14.09)

2. Finding prime numbers (by 28.09)

3.Vector operations (addition, subtraction, length, scalar product,

vector product, use structures for vectors storing) (by 12.10)

Sorting (bubble sort, insertion sort or selection sort, shell sort;

illustrate the difference in time efficiency of the used algorithms) (by 2.11)

5.Matrix operations (input from keyboard and file, adding/subtracting,

multiplying, transpose, output to screen and to file) for arbitrary-sizedmatrices (by 16.11)

6.Solving a system of linear equation using Gaussian elimination

method. Using pivoting is a bonus (by 7.12)