23
U n i v e r s i t y o f H a i l 1 ICS 202 Data Structures and Algorithms Instructor : Da’ad Ahmad Albalawneh

Chapter 1 Algorithm Analysis

  • Upload
    will

  • View
    86

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 1 Algorithm Analysis. Outline. Introduction A Detailed Model of the Computer The Basic Axioms Example 1: Arithmetic Series Summation Array Subscribing Operations Example2: Horner’s Rule Analyzing Recursive Methods Example 3: Finding the Largest Element of an Array - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

1ICS 202 Data Structures and Algorithms Instructor : Da’ad Ahmad Albalawneh

Page 2: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

2

Outline

1. Introduction

2. A Detailed Model of the Computer• The Basic Axioms

• Example 1: Arithmetic Series Summation

• Array Subscribing Operations

• Example2: Horner’s Rule

• Analyzing Recursive Methods

• Example 3: Finding the Largest Element of an Array

• Average Running Times

• About Harmonic Numbers

• Best-Case and Worst-Case Running Times

• The Last Axiom

3. A Simplified Model of the Computer• Example 1: Geometric Series Summation

• About Geometric Series Summation

• Example 2: Computing Powers

Page 3: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

3

• What is an Algorithm?

• “A step-by-step procedure for accomplishing some end”

• Can be written in English, or using an appropriate mathematical formalism –

programming language

• Why do we analyze an algorithm and what can we analyze?

• To learn more about the algorithm (some specifications)

• To draw conclusions about how the implementation will perform

• We can analyze

• The running time of a program as a function of its inputs

• The total or maximum memory space needed for program data

• The total size of the program code

• The complexity of the program

• The robustness (powerful) of the program (how wll does it deal with unexpected inputs)

1. Introduction

Page 4: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

4

• Factors affecting the running time of a program

• The algorithm itself

• The input data

• The computer system used to run the program

• The hardware (Processor, memory available, disk available, …)

• The programming language

• The language compiler

• The computer operating system software

1. Introduction

Page 5: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

5

• Detailed model of the running time performance of JAVA programs

• The model is independent of the hardware and system software

• Modeling the execution of a JAVA program on the “Java Virtual Machine”

2. A Detailed Model of the Computer

Java Program Java Compiler Java bytecode

interpreter HardwareJava bytecode

Machine code

Physical Machine

Java Virtual Machine

Java system

Page 6: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

6

• Axiom 1

2. A Detailed Model of the Computer: The Basic Axioms

The time required to fetch an operand from memory is a constant, and the time

required to store a result in memory is a constant,

fetch

store

y = x; has running time storefetch

y = 1; has running time storefetch

The two statements have the same running time because the constant 1 needs to be stored in memory

Page 7: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

7

• Axiom 2

2. A Detailed Model of the Computer: The Basic Axioms

The times required to perform elementary operations, such as addition, subtraction,

multiplication, division, and comparison, are all constants. These times are denoted by:

respectively. and ,,,,

All of the simple operations can be accomplished in a fixed amount of time

The number of bits used to represent a value must be fixed

In Java, the number of bits range from 8 (byte) to 64 (long and double)

Page 8: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

8

2. A Detailed Model of the Computer: The Basic Axioms

Example:

By applying the axiom 1 and 2, the running time for the statement y = y + 1; is

Because we need to fetch two operands, y and 1, add them, and, store the result

back in y

storefetch 2

y+ = 1;

++y;

y++;

Have the same running time as y = y + 1;

Page 9: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

9

• Axiom 3

2. A Detailed Model of the Computer: The Basic Axioms

The time required to call a method is a constant, and the time required to return

from a method is a constant,

callreturn

When a method is called:

• The returned address must be saved

• Any partially completed computations must also be saved

• A new execution context (stack frame, …) must be allocated

Page 10: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

10

• Axiom 4

2. A Detailed Model of the Computer: The Basic Axioms

The time required to pass an argument to method is the same as the time required to

store a value in memorystore

Example:

y = f(x); has a running time:

Where is the running time of method f for input x.

We need one store time to pass the parameter x to f and a store time to assign the

result to y.

)(2 xfcallstorefetch T

)(xfT

Page 11: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

11

2. A Detailed Model of the Computer: Example 1: Arithmetic Series Summation

n

i

i1

Analyze the running time of a program to compute

1 public class Example1

2 {

3 public static int sum (int n)

4 {

5 int result = 0;

6 for(int i = 1; i <= n; ++i)

7 result += i;

8 return result;

9 }

10 }

Page 12: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

12

2. A Detailed Model of the Computer: Example 1: Arithmetic Series Summation

Statement Time Code

5 result = 0

6a i = 1

6b i <= n

6c ++i

7 result += i

8 return result

)1()2( nfetch

storefetch

nstorefetch )2(

nstorefetch )2(

storefetch

returnfetch

)25()226()( returnstorefetchstorefetchnnT

Page 13: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

13

2. A Detailed Model of the Computer: Array Subscribing Operations

• Axiom 5

The time required for the address calculation implied by an array subscribing

operation, for example, a[i], is a constant, This time does not include the time to

compute the subscript expression, nor does it include the time to access (i.e., fetch or

store) the array element.

[.]

• The elements of a one-dimensional array are stored in consecutive (sequential)

memory locations

• We need to know only the address of the first element of the array to determine

the address of any other element

Page 14: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

14

2. A Detailed Model of the Computer: Array Subscribing Operations

Example:

y = a [i]; has a running time:

Three operand fetches:

• The first to fetch a (the base address of the array)

• The second to fetch i (the index into the array)

• The third the fetch array element a[i]

storefetch [.]3

Page 15: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

15

2. A Detailed Model of the Computer: Example 3: Finding the largest Element of an Array

1 public class Example

2 {

3 public static int findMaximum (int [ ] a)

4 {

5 int result = a [0];

6 for(int i = 1; i <a.length; ++i)

7 if(a[i]) > result)

8 result = a[i];

9 return result;

10 }

11 }

Analyze the running time of a program to find the largest element of an array of

n non-negative integers, 110 ,...,, naaa inia

0max

Page 16: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

16

2. A Detailed Model of the Computer: Example 3: Finding the largest Element of an Array

Statement Time Code

5 result = a [0]

6a i = 1

6b i < a.length

6c ++i

7 if(a[i] > result)

8 result = a[i]

9 return result

nfetch )2(

storefetch [.]3

)1()2( nstorefetch

)1()4( [.] nfetch

storefetch

returnfetch

?)3( [.] storefetch

Page 17: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

17

2. A Detailed Model of the Computer: Best-Case and Worst-Case Running Times

The worst case scenario occurs when line 8 is executed in every iteration of the loop.

The input array is ordered from smallest to largest

Page 18: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

18

2. A Detailed Model of the Computer: Best-Case and Worst-Case Running Times

The best case scenario occurs when line 8 is never executed.

The input array is ordered from largest to smallest

Page 19: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

19

• Axiom 6

2. A Detailed Model of the Computer: The Last Axiom

Example:

Integer ref = new Integer (0); has a running time:

)(2 Integercallstorefetchnew

The time required to create a new object instance using the new operator is a

constant, . This time does not include any time taken to initialize the object

instance.

new

Where is the running time of the Integer constructor)( Integer

Page 20: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

20

• The performance analysis is easy to do but less accurate

• All the arbitrary timing parameters of the detailed model are eliminated

• All timing parameters are expressed in units of clock cycles , T =1

• To determine the running time of a program, we simply count the total number of

cycles taken

3. A Simplified Model of the Computer

Page 21: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

21

3. A Simplified Model of the Computer: Example 2: Geometric Series Summation

n

i

ix0

Analyze the running time of a program to compute using Horner’s rule

1 public class Example22 {3 public static int geometricsum (int x, int

n)4 {5 int sum = 0;6 for(int i = 0; i <= n; ++i)7 sum = sum * x +1;8 return sum;9 }10 }

Page 22: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

22

3. A Simplified Model of the Computer: Example 2: Geometric Series Summation

Statement Time Code

5 2 sum = 0

6a 2 i = 0

6b i <=n

6c ++i

7 sum = sum*x + 1

8 2 return sum

2213 nTotal

)2(3 n

)1(4 n

)1(6 n

Page 23: Chapter 1 Algorithm Analysis

U n

i v

e r

s i t

y

o f

H

a i l

23

3. A Simplified Model of the Computer: About Geometric Series Summation

,...,,,1 32 xxxThe series is a geometric series and the summation

Is called the geometric series summation

n

i

in xs

0

1

11

0

x

xxs

nn

i

in