1 / 23

Chapter 1 Algorithm Analysis

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

will
Download Presentation

Chapter 1 Algorithm Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 1Algorithm Analysis ICS 202  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. 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. 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. Java system Java Virtual Machine Physical Machine Java Program Java bytecode Machine code Java Compiler Java bytecode interpreter Hardware 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”

  6. 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 2. A Detailed Model of the Computer: The Basic Axioms • Axiom 1 The two statements have the same running time because the constant 1 needs to be stored in memory

  7. The times required to perform elementary operations, such as addition, subtraction, multiplication, division, and comparison, are all constants. These times are denoted by: respectively. 2. A Detailed Model of the Computer: The Basic Axioms • Axiom 2 • 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. The time required to call a method is a constant, and the time required to return from a method is a constant, 2. A Detailed Model of the Computer: The Basic Axioms • Axiom 3 • 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. 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. A Detailed Model of the Computer: The Basic Axioms • Axiom 4 The time required to pass an argument to method is the same as the time required to store a value in memory

  11. Analyze the running time of a program to compute 2. A Detailed Model of the Computer: Example 1: Arithmetic Series Summation 1publicclass Example1 2 { 3publicstaticint sum (int n) 4 { 5int result = 0; 6for(int i = 1; i <= n; ++i) 7 result += i; 8return result; 9 } 10 }

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

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

  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, 1publicclass Example 2 { 3publicstaticint findMaximum (int [ ] a) 4 { 5int result = a [0]; 6 for(int i = 1; i <a.length; ++i) 7if(a[i]) > result) 8 result = a[i]; 9 return result; 10 } 11 }

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

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

  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. Analyze the running time of a program to compute using Horner’s rule 3. A Simplified Model of the Computer: Example 2: Geometric Series Summation 1publicclass Example2 2 { 3publicstaticint geometricsum (int x, int n) 4 { 5int sum = 0; 6for(int i = 0; i <= n; ++i) 7 sum = sum * x +1; 8return sum; 9 } 10 }

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

  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

More Related