CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Chapter 1 – Basic Concepts
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Insertion Sort for (i = 1; i < n; i++) {/* insert a[i] into a[0:i-1] */ int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];
Time Complexity s Sorting –Insertion sorting s Time complexity.
Chapter 2. Complexity Space Complexity Space Complexity Instruction Space Instruction Space Data Space Data Space Enviroment Space Enviroment Space Time.
Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1]
Chapter 1 Algorithm Analysis
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Program Performance & Asymptotic Notations CSE, POSTECH.
Analysis of Algorithms
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Asymptotic Notation (O, Ω, )
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
Data Structure Introduction.
Binary search and complexity reading:
1 Chapter 2 Program Performance. 2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithm Analysis 1.
Complexity Analysis (Part I)
Introduction to Algorithms
Mathematical Foundation
Analysis of Algorithms
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
GC 211:Data Structures Week 2: Algorithm Analysis Tools
GC 211:Data Structures Algorithm Analysis Tools
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
Stacks.
Algorithm Analysis CSE 2011 Winter September 2018.
Building Java Programs
CSE 143 Lecture 5 Binary search; complexity reading:
Insertion Sort for (int i = 1; i < a.length; i++)
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
GC 211:Data Structures Algorithm Analysis Tools
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Algorithm Efficiency Chapter 10.
Lecture 18 Arrays and Pointer Arithmetic
Lecture 5: complexity reading:
Programming and Data Structure
Analysis of Algorithms
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Programming and Data Structure
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Building Java Programs
Searching, Sorting, and Asymptotic Complexity
Asst. Dr.Surasak Mungsing
CSE 373: Data Structures & Algorithms
Sorting Rearrange a[0], a[1], …, a[n-1] into ascending order.
Introduction to Algorithms
Analysis of Algorithms
Building Java Programs
More on Asymptotic Analysis and Performance Measurement
Revision of C++.
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
CSE 373 Data Structures and Algorithms
Insertion Sort for (int i = 1; i < n; i++)
More on Asymptotic Analysis + Performance Measurement
Analysis of Algorithms
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Lecture 4: Introduction to Code Analysis
CSE 373, Copyright S. Tanimoto, 2001 Asymptotic Analysis -
Insertion Sort for (int i = 1; i < n; i++)
Complexity Analysis (Part I)
Analysis of Algorithms
Analysis of Algorithms
Algorithms and data structures: basic definitions
Complexity Analysis (Part I)
Presentation transcript:

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis - Space complexity Instance characteristics Time complexity Computing the rank of an element Rank sort Horner’s rule for polynomial evaluation CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis - Warm-up Activity Write in Java a method to take a Vector of objects as input, along with one separate object, and determine the index of the first occurrence, if any, of that object in the vector. But do NOT use the built in method indexOf. Use elementAt(int i), size(). CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Some Reference Code: A Loop to List the List the Elements of a Vector void listElements(Vector v) { for (int i=0; i < v.size(); i++) { System.out.println( v.elementAt(i)) } CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

One solution to the rank problem. int rank(Vector v, Object ob) { for (int i=0; i<v.size(); i++) { if (v.elementAt(i).equals(ob)) return i; } return -1; CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis - Space Complexity Amount of memory required to run some program or algorithm. Instruction space Data space Environment stack space c + SP(instance characteristics) CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Instance Characteristics (Definition) Suppose we have a class of problems, such as (a) sorting lists of integers or (b) finding all misspelled words in documents. An instance characteristic is a measure (typically with integer value) of the magnitude of an instance of the problem. It's essentially a function whose domain is a set of problems and whose range is the set of nonnegative integers. CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Instance Characteristics (Some of the Alternatives) n = number of data items to be sorted. n = magnitude of an input parameter. n = number of bits used to represent the input ( e.g., log2 magnitude of parameter) n = max of width and height of an image (in pixels) n = number of pixels in the image CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Instance Characteristics (A more interesting example) The class of problems: "Web site analysis" Given a home page, find all pages within the same site that are reachable via links, and index each word found. What is an appropriate instance characteristic? (Discussion in class) CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis - Time Complexity How should we describe the running time of an algorithm? Using the number of seconds it takes to run on a particular problem and machine? CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Time Complexity: Issues & Approaches Non-algorithmic factors: hardware, OS, compiler. Abstract measures: Virtual machines (e.g., JVM) Operation counts Asymptotic growth -- Big O notation. CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis - Runtime tP(instance characteristics) tP(n) = caADD(n) + csSUB(n) + cmMUL(n) + cdDIV(n) + ... CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis - Operation Counts Choose a key operation as representative of the dependency of computation time on instance characteristics. e.g., Comparison: "Is x < y ?" (calls to CompareTo operations -- Sahni, p.40) e.g., all Arithmetic operations count 1. CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis - Rank of an Element Rank of an element E in an array A is the number of elements of A less than E plus the number of elements equal to E appearing to the left of E. Let A = [3, 7, 4, 2, 1, 9, 3] Rank of 7 in A is 5. Rank of the last 3 in A is 3. All ranks of A: [2, 5, 4, 1, 0, 6, 3] CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis - Computing the Ranks public static void rank(Comparable [] a, int [] r) { if (r.length < a.length) throw new IllegalArgumentException ("Rank array too short"); for (int i = 0; i < a.length; i++) r[i] = 0; for (int i = 1; i < a.length; i++) { for (int j = 0; j < i; j++) if (a[j].compareTo(a[i])) <= 0) r[i]++; else r[j]++; } //(Sahni, p.79) CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis - Performance of rank Assume n = a.length = r.length; Space: Assume 1 unit of storage for each array element (but note that rank does not allocate these arrays.) Space = c + 2 * n Time: Assume we count “object” comparisons only. Time = n * (n - 1) / 2 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis - Rearranging public static void rearrange(Comparable [] a, int [] r) { Comparable [] u = new Comparable [a.length]; // Sort into u using ranks: for (int i = 0; i < a.length; i++) { u[r[i]] = a[i]; // Copy items back to array a. a[i] = u[i]; } //(Sahni, p.80) CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Performance of rearrange Space: n additional memory locations (for array u) Time: Assume we count object assignments only. Time = n * 2 Overall time for sorting by ranking and rearranging must be expressed with a unit that is the same for each piece. So now let’s consider both comparisons and assignments: Sorting time = n * (n - 1)/2 Comp + 2 * n Assign. CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Polynomial Evaluation P(x) = c0xn + c1xn-1 + . . . + cn-1 x + cn Suppose we are given a value for x. How should we compute P(x) ? CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Polynomial Evaluation: One Way P(x) = c0xn + c1xn-1 + . . . + cn-1 x + cn A slow method: first compute xn and multiply it by c0 Next compute x n-1 and multiply it by c1, etc. This requires more than (n - 1) (n - 2) / 2 multiplications. A more direct Method: Compute x, x2, x3, etc. saving them. Then multiply each by its corresponding coefficient and add the products. This requires n memory locations to store the powers, and it requires 2n - 1 multiplications. CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Polynomial Eval. w/ Horner’s Rule P(x) = c0xn + c1xn-1 + . . . + cn-1 x + cn Compute c0x Add c1 getting t1 Computer t1x Add c2 getting t2 Compute t2x ... Add cn getting the answer. Requires 1 memory location to hold current result ti, and needs n multiplications (and n additions). CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -