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.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 2 Some of the sides are exported from different sources.
Fundamentals of Python: From First Programs Through Data Structures
CS0007: Introduction to Computer Programming Array Algorithms.
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
CompSci 102 Discrete Math for Computer Science
Chapter 1 – Basic Concepts
1 Data Structures Performance Analysis. 2 Fundamental Concepts Some fundamental concepts that you should know: –Dynamic memory allocation. –Recursion.
CHAPTER 11 Space and Time Complexity in Chapter 1 All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed.
Data Structures Performance Analysis.
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];
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
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.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Chapter 1 Algorithm Analysis
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Program Performance & Asymptotic Notations CSE, POSTECH.
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/ Objective : 1.To understand primitive storage structures and types 2.To.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Asymptotic Notation (O, Ω, )
The Fundamentals: Algorithms, the Integers & Matrices.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
3 – SIMPLE SORTING ALGORITHMS
Chapter 9 Efficiency of Algorithms. 9.3 Efficiency of Algorithms.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Algorithm Analysis Part of slides are borrowed from UST.
SNU IDB Lab. Ch4. Performance Measurement © copyright 2006 SNU IDB Lab.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
Program Performance 황승원 Fall 2010 CSE, POSTECH. Publishing Hwang’s Algorithm Hwang’s took only 0.1 sec for DATASET1 in her PC while Dijkstra’s took 0.2.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Searching and Sorting Searching algorithms with simple arrays
Algorithm Analysis 1.
Analysis of Algorithms
Source: wikipedia
Source:
Algorithm Analysis CSE 2011 Winter September 2018.
Building Java Programs
Algorithm design and Analysis
Analysis Algorithms.
CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Algorithm Analysis and Design
Sorting Rearrange a[0], a[1], …, a[n-1] into ascending order.
Insertion Sort for (int i = 1; i < n; i++)
Insertion Sort for (int i = 1; i < n; i++)
Presentation transcript:

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 Asymptotic notations Measuring the actual run time using a clocking function.

3 Applications Sequential and binary search Rank sort, selection sort, bubble sort, and insertion sort Evaluating a polynomial using Horner’s rule Matrix operations such as add, transpose, and multiply

4 Performance of a program Performance of a program: the amount of computer memory and time needed to run the program Performance analysis: Use analytical methods to determine the performance of a program Performance measurement: Use experimental methods to determine the performance of a program

5 Space and time complexity Space complexity: the amount of memory needed to run the program Time complexity: the amount of computer time needed to run the program

6 Components of space complexity Instruction space Data space –Constants –Variables Environment stack space

7 Instruction space The compiler used The compiler options The target computer

8 Three possible codes for the evaluation of a+b+b*c+(a+b-c)/(a+b)+4

9 Data space

10 Data space for structural variables Double a[100];100*8 = 800 bytes int maze[rows][cols];2*rows*cols bytes

11 Environment stack The return address Local variables and value formal parameters The binding of all reference and const reference parameters

12 An example of recursive calls

13 Division of space needed S(P) = c + S p (instance characteristics) c - the fix part independent of the instance characteristics: instruction space, space for simple variables, fixed-size component variables, constants, etc S p - the variable part: space needed by component variables whose size depends on the particular problem instance, dynamically allocated space, and the recursion stack space

14 Example 1: Instance Characteristic is n c = 12 bytes when T = int; SSequentialSearch(n) = 0

15 Example 2: Assuming T is int, 6 bytes for each recursive call (to save formal parameters and return address. SRsum(n) = 6(n+1)

16 Time complexity Components of time complexity T(P) = compile time + run time (t p ) t p = c a ADD(n) + c s SUB(n) + c m MUL(n) + c d DIV(n) + … Time needed for an arithmetic operation depends on the type of data Machine dependent

17 Operation counts Select one or more operations, and determine how many of each is done Identify the operations that contribute most to the time complexity

18 Polynomial Evaluation P(x) = Σ i=0,n c i x n c n <> 0 The number of addition = n The number of multiplication = 2n

19 Horner’s rule: P(x) = (…(c n * x + c n-1 ) * x + c n-2 ) * x + c n-3 ) * x …) * x + c 0 The number of additions = n The number of multiplications = n

20 Ranking The rank of an element in a sequence is the number of smaller elements in the sequence plus the number of equal elements to its left. Example: –sequence [4, 3, 9, 3, 7] –Rank [2, 0, 4, 1, 3]

21 Ranking Total number of comparisons = … + n-1 = (n-1)n/2

22 Rank sort Comparisons: (n-1)n/2Moves: 2n

23 Selection Sort Need to sort list in ascending order: a[0] ≤ a[1] ≤ … ≤ a[n-1] –Determine largest element and swap it with a[n-1] –Determine the largest of the remaining n-1 elements and swap it with a[n-2] –And so on.

24 Selection Sort Comparisons: … + n-1 = (n-1)n/2 Moves: 3(n-1)

25 Bubble Sort Use “bubbling strategy” to get the largest element to the right. In each bubble pass: –Pairs of adjacent elements are compared –If left element is larger swap them –At end of pass, largest element ends up in rightmost position Bubble sort: repeat above to place each element in proper position

26 A bubbling pass Comparisons: n-1

27 Bubble Sort Comparisons: (n-1)n/2

28 Best, Worst, and Average Operation Counts Performance may depend on more than one factor op (n 1, n 2, …, n k ) is a function of the characteristics n 1, n 2, …, n k Let S(n 1, n 2, …, n k ) = {I | Instance I has the characteristics n 1, n 2, …, n k } O p BC (n 1, n 2, …, n k ) = min{operation p (I) | I є S(n 1, n 2, …, n k ) } O p WC (n 1, n 2, …, n k ) = max{operation p (I) | I є S(n 1, n 2, …, n k ) } O p AVG (n 1, n 2, …, n k ) = (1/|S(n 1, n 2, …, n k )|)Σ I є S(n1, n2, …, nk) operation p (I)

29 Insertion Comparisons: Minimum: 1; Maximum: n Average:

30 Rank Sort Revisited Swaps: Minimum: 0 Maximum: 2(n-1)

31 Selection Sort Revisited Comparisons: –Min: n-1 –Max: (n-1)n/2 Swaps: –Min: 0 –Max: n-1

32 Bubble Sort Revisited Comparisons: –Min: n-1 –Max: (n-1)n/2

33 Insertion Sort 1 Comparisons –Min: n-1 –Max: (n-1)n/2

34 Insertion Sort 2 Comparisons –Min: n-1 –Max: (n-1)n/2

35 End of Chapter 2 Part 1