Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Algorithm Analysis

Similar presentations


Presentation on theme: "Chapter 1 Algorithm Analysis"— Presentation transcript:

1 Chapter 1 Algorithm Analysis
ICS  Data Structures and Algorithms  Instructor : Da’ad Ahmad Albalawneh

2 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 Average Running Times About Harmonic Numbers Best-Case and Worst-Case Running Times The Last Axiom A Simplified Model of the Computer Example 1: Geometric Series Summation About Geometric Series Summation Example 2: Computing Powers

3 Why do we analyze an algorithm and what can we analyze?
1. Introduction 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)

4 Factors affecting the running time of a program
1. Introduction 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

5 2. A Detailed Model of the Computer
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” Java Program Java Compiler Java bytecode interpreter Hardware Machine code Physical Machine Java Virtual Machine Java system

6 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, y = x; has running time y = 1; has running time The two statements have the same running time because the constant 1 needs to be stored in memory

7 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. 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)

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 y+ = 1; ++y; y++; Have the same running time as y = y + 1;

9 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, 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

10 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 memory 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.

11 2. A Detailed Model of the Computer: Example 1: Arithmetic Series Summation
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 }

12 Statement Time Code 5 result = 0 6a i = 1 6b i <= n 6c ++i 7
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

13 2. A Detailed Model of the Computer: Array Subscribing Operations
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 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.

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]

15 2. A Detailed Model of the Computer: Example 3: Finding the largest Element of an Array
Analyze the running time of a program to find the largest element of an array of n non-negative integers, 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) if(a[i]) > result) result = a[i]; return result; } }

16 Statement Time Code 5 6a 6b 6c 7 8 9
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

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

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

19 2. A Detailed Model of the Computer: The Last Axiom
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. Example: Integer ref = new Integer (0); has a running time: Where is the running time of the Integer constructor

20 3. A Simplified Model of the Computer
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

21 3. A Simplified Model of the Computer: Example 2: Geometric Series Summation
Analyze the running time of a program to compute using Horner’s rule 1 public class Example2 2 { 3 public static int geometricsum (int x, int n) 4 { int sum = 0; for(int i = 0; i <= n; ++i) sum = sum * x +1; return sum; } 10 }

22 3. A Simplified Model of the Computer: Example 2: Geometric Series Summation
Statement Time Code 5 2 sum = 0 6a i = 0 6b i <=n 6c ++i 7 sum = sum*x + 1 8 return sum

23 3. A Simplified Model of the Computer: About Geometric Series Summation
The series is a geometric series and the summation Is called the geometric series summation


Download ppt "Chapter 1 Algorithm Analysis"

Similar presentations


Ads by Google