1 Data Structures Performance Analysis. 2 Fundamental Concepts Some fundamental concepts that you should know: –Dynamic memory allocation. –Recursion.

Slides:



Advertisements
Similar presentations
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Advertisements

Computer science is a field of study that deals with solving a variety of problems by using computers. To solve a given problem by using computers, you.
Analysys & Complexity of Algorithms Big Oh Notation.
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
Data Structures Performance Analysis.
Cmpt-225 Algorithm Efficiency.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.
COMP s1 Computing 2 Complexity
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.
Program Performance & Asymptotic Notations CSE, POSTECH.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
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.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Basic Concepts 2014, Fall Pusan National University Ki-Joune Li.
1 Big-Oh Notation CS 105 Introduction to Data Structures and Algorithms.
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
Complexity Analysis Chapter 1.
 DATA STRUCTURE DATA STRUCTURE  DATA STRUCTURE OPERATIONS DATA STRUCTURE OPERATIONS  BIG-O NOTATION BIG-O NOTATION  TYPES OF DATA STRUCTURE TYPES.
Complexity 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, Ω, )
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
MS 101: Algorithms Instructor Neelima Gupta
Data Structure Introduction.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Lecture 2 Analysis of Algorithms How to estimate time complexity? Analysis of algorithms Techniques based on Recursions ACKNOWLEDGEMENTS: Some contents.
Algorithm Analysis (Big O)
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
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 Program Performance. 2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Complexity Analysis (Part I)
Mathematical Foundation
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
COMP108 Algorithmic Foundations Algorithm efficiency
Analysis of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Big-O notation.
What is an Algorithm? Algorithm Specification.
Lecture 2 Analysis of Algorithms
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
Analysys & Complexity of Algorithms
Analysis of Algorithms
DS.A.1 Algorithm Analysis Chapter 2 Overview
GC 211:Data Structures Algorithm Analysis Tools
Searching, Sorting, and Asymptotic Complexity
Asst. Dr.Surasak Mungsing
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
Complexity Analysis (Part I)
Analysis of Algorithms
Complexity Analysis (Part I)
Presentation transcript:

1 Data Structures Performance Analysis

2 Fundamental Concepts Some fundamental concepts that you should know: –Dynamic memory allocation. –Recursion. –Performance analysis.

3 Performance Analysis There are problems and algorithms to solve them. Problems and problem instances. Example: Sorting data in ascending order. –Problem: Sorting –Problem Instance: e.g. sorting data ( ) –Algorithms: Bubble sort, Merge sort, Quick sort, Selection sort, etc. Which is the best algorithm for the problem? How do we judge?

4 Performance Analysis Two criteria are used to judge algorithms: (i) time complexity (ii) space complexity. Space Complexity of an algorithm is the amount of memory it needs to run to completion. Time Complexity of an algorithm is the amount of CPU time it needs to run to completion.

5 Space Complexity Memory space S(P) needed by a program P, consists of two components: –A fixed part: needed for instruction space (byte code), simple variable space, constants space etc.  c –A variable part: dependent on a particular instance of input and output data.  S p (instance) S(P) = c + S p (instance)

6 Space Complexity: Example 1 1. Algorithm abc (a, b, c) 2. { 3. return a+b+b*c+(a+b-c)/(a+b)+4.0; 4. } For every instance 3 computer words required to store variables: a, b, and c. Therefore S p ()= 3. S(P) = 3.

7 Space Complexity: Example 2 1. Algorithm Sum(a[], n) 2. { 3. s:= 0.0; 4. for i = 1 to n do 5. s := s + a[i]; 6. return s; 7. }

8 Space Complexity: Example 2. Every instance needs to store array a[] & n. –Space needed to store n = 1 word. –Space needed to store a[ ] = n floating point words (or at least n words) –Space needed to store i and s = 2 words S p (n) = (n + 3). Hence S(P) = (n + 3).

9 Time Complexity Time required T(P) to run a program P also consists of two components: –A fixed part: compile time which is independent of the problem instance  c. –A variable part: run time which depends on the problem instance  t p (instance) T(P) = c + t p (instance)

10 Time Complexity How to measure T(P)? –Measure experimentally, using a “stop watch”  T(P) obtained in secs, msecs. –Count program steps  T(P) obtained as a step count. Fixed part is usually ignored; only the variable part t p () is measured.

11 Time Complexity What is a program step? –a+b+b*c+(a+b)/(a-b)  one step; –comments  zero steps; – while ( ) do  step count equal to the number of times is executed. – for i= to do  step count equal to number of times is checked.

12 Time Complexity: Example 1 Statements S/EFreq.Total 1Algorithm Sum(a[],n)0-0 2{0-0 3 S = 0.0;111 4 for i=1 to n do1n+1 5 s = s+a[i];1nn 6 return s;111 7}0-0 2n+3

13 Time Complexity: Example 2 Statements S/EFreq.Total 1Algorithm Sum(a[],n,m) 0-0 2{ for i=1 to n do; 1n+1 4 for j=1 to m do 1n(m+1) 5 s = s+a[i][j]; 1nm 6 return s; 111 7} 0-0 2nm+2n+2

14 Performance Measurement Which is better? –T(P1) = (n+1) or T(P2) = (n 2 + 5). –T(P1) = log (n 2 + 1)/n! or T(P2) = n n (nlogn)/n 2. Complex step count functions are difficult to compare. For comparing, ‘rate of growth’ of time and space complexity functions is easy and sufficient.

15 Big O Notation Big O of a function gives us ‘rate of growth’ of the step count function f(n), in terms of a simple function g(n), which is easy to compare. Definition: [Big O] The function f(n) = O(g(n)) (big ‘oh’ of g of n) iff there exist positive constants c and n 0 such that f(n) =n 0. See graph on next slide. Example: f(n) = 3n+2 is O(n) because 3n+2 = 2. c = 4, n 0 = 2. Here g(n) = n.

16 Big O Notation = n 0

17 From DS and Prog. Design in C++, R. Kruse et al.

18 From DS and Prog. Design in C++, R. Kruse et al.

19 From DS and Prog. Design in C++, R. Kruse et al.

20 Big O Notation Example: f(n) = 10n 2 +4n+2 is O(n 2 ) because 10n 2 +4n+2 =5. Example: f(n) = 6*2 n +n 2 is O(2 n ) because 6*2 n +n 2 =4. Algorithms can be: O(1)  constant; O(log n)  logrithmic; O(nlogn); O(n)  linear; O(n 2 )  quadratic; O(n 3 )  cubic; O(2 n )  exponential.

21 Big O Notation Now it is easy to compare time or space complexities of algorithms. Which algorithm complexity is better? –T(P1) = O(n) or T(P2) = O(n 2 ) –T(P1) = O(1) or T(P2) = O(log n) –T(P1) = O(2 n ) or T(P2) = O(n 10 )

22 Some Results Sum of two functions: If f(n) = f 1 (n) + f 2 (n), and f 1 (n) is O(g 1 (n)) and f 2 (n) is O(g 2 (n)), then f(n) = O(max(|g 1 (n)|, | g 2 (n) | )). Product of two functions: If f(n) = f 1 (n)* f 2 (n), and f 1 (n) is O(g 1 (n)) and f 2 (n) is O(g 2 (n)), then f(n) = O(g 1 (n)* g 2 (n)).