Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Complexity Analysis (Part I)
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
Analysis of Algorithms (Chapter 4)
Complexity Analysis (Part I)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
Fall 2006CSC311: Data Structures1 Chapter 4 Analysis Tools Objectives –Experiment analysis of algorithms and limitations –Theoretical Analysis of algorithms.
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
Analysis of Performance
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
CSCE 3110 Data Structures & Algorithm Analysis Algorithm Analysis I Reading: Weiss, chap.2.
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Lecture 2 Computational Complexity
Analysis of Algorithms
Analysis of Algorithms1 The Goal of the Course Design “good” data structures and algorithms Data structure is a systematic way of organizing and accessing.
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Data Structures Lecture 8 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Analysis of Algorithms1 Running Time Pseudo-Code Analysis of Algorithms Asymptotic Notation Asymptotic Analysis Mathematical facts.
Analysis of Algorithms
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright ©
Complexity of Algorithms
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.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Algorithm Analysis (Big O)
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.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Complexity Analysis (Part I)
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
GC 211:Data Structures Algorithm Analysis Tools
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Lecture 3 of Computer Science II
Analysis of Algorithms
CS 3343: Analysis of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Revision of C++.
Analysis of Algorithms
At the end of this session, learner will be able to:
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Complexity Analysis (Part I)
Analysis of Algorithms
Complexity Analysis (Part I)
Presentation transcript:

Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Algorithm A step-by-step procedure to solve a problem  Start from an initial state and input  Proceed through a finite number of successive states  Stop when reaching a final state and producing output 2 Lamp doesn’t work. Lamp plugged in? Bulb burned out? Repair lamp. Replace bulb. Plug in lamp.

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Algorithm Performance Algorithm performance is measured by the amount of computer memory and running time required to run a algorithm. Performance measurement  Empirical analysis Compute memory space in use and running time Results are not general—valid only for tested inputs. Same machine environment must be used to compare two algorithms.  Theoretical analysis Compute asymptotic bound of space and running time Sometimes, it is difficult to measure average cases; worst case analysis are often used. Machine independent 3

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Empirical Analysis Measurement criteria  Actual memory space in use  Running time of algorithms Example  Algorithm1 and 2 took 0.3 and 0.5 milliseconds for Input1, respectively.  Algorithm1 may be faster than Algorithm2.  Algorithm1 and 2 took 1.0 and 0.8 milliseconds for Input2, respectively.  How about now? 4 Algorithm1 Input Output Algorithm2

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Theoretical Analysis Measurement criteria  Space: amount of memory in bytes that algorithm occupies  Time Running time Typically measured by the number of primitive operations However, different primitive operations may take different running time! Algorithm analysis  Use a pseudocode of the algorithm  Need to take all possible inputs into account  Evaluate the algorithm speed independent of the machine environment 5

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Pseudocode High-level description of an algorithm  Independent of any programming language  More structured than English proses  Less detailed than program codes  Hiding program design issues  Easy to understand 6 Algorithm arrayMax(A, n) Input array A of n integers Output maximum element of A currentMax <- A[0] for i <- 1 to n - 1 do if A[i] >= currentMax then currentMax <- A[i] return currentMax

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Space Complexity Amount of necessary memory for an algorithm  The space complexity may define an upper bound on the data that the algorithm uses. Why do we care about space complexity?  We may not have sufficient memory space in our computer.  When we solve a large-scale problem, memory space is often a critical bottleneck. 7

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Space Complexity Data space depends on  Computer architecture and compiler  Implementation  Algorithm: e.g., dense matrix vs. sparse matrix 8 double array1[100]; int array2[100]; // The size of array1 is twice bigger than that of array (index, value) {(2,4), (5,8), (9,1)} Dense matrix: Sparse matrix: 10 integers 6 integers

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Time Complexity Amount of time required to run an algorithm Why do we care about time complexity?  Some applications require real-time responses.  If there are many solutions for a problem, we typically prefer the fastest one. How do we measure?  Count a particular operation (operation counts) e.g., comparisons in sorting  Count the number of steps (step counts)  Asymptotic complexity 9

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example: Insertion Sort How many comparisons are made? n=3 i= i= j=0 Algorithm insertionSort(A, n) Input array A of n integers Output the sorted array A for i <- 1 to n - 1 do t <- A[i] for j <- i-1 to 0 do if t < A[j] then A[j+1] <- A[j] else break A[j] <- t return A Comparison happens here.

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Worst Case Analysis Why using the worst case?  Average case is sometimes difficult to analyze.  The time complexity of the worst case is often very important. An example: insertion sort.  All elements are reversely sorted.  Total number of comparisons: 11 Algorithm insertionSort(A, n) Input array A of n integers Output the sorted array A for i <- 1 to n - 1 do t <- A[i] for j <- i-1 to 0 do if t < A[j] then A[j+1] <- A[j] else break A[j] <- t return A

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Primitive Operations Basic computations performed by an algorithm  Constant time assumed for execution  Identifiable in pseudocode Largely independent from the programming language Examples  Assign a value, a <- b  Comparison, a < b  Returning from a method 12 Algorithm insertionSort(A, n) Input array A of n integers Output the sorted array A for i <- 1 to n - 1 do t <- A[i] for j <- i-1 to 0 do if t < A[j] then A[j+1] <- A[j] else break A[j] <- t return A

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Primitive Operation Counts By inspecting the pseudocode, we can determine the maximum number of primitive operations executed by an algorithm, as a function of the input size. 13 Algorithm insertionSort(A, n) Input array A of n integers Output the sorted array A for i <- 1 to n - 1 do t <- A[i] for j <- i-1 to 0 do if t < A[j] then A[j+1] <- A[j] else break A[j] <- t return A # operations total # operations 0 n-1 0 (n-1)n 0 n-1 1 Total

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Running Time Analysis 14

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Growth Functions 15

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Growth Functions 16

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Growth Functions 17

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 A Table of Growth Functions Overly complex algorithm may be impractical in some system

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 A Graph of Growth Functions 19

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Asymptotic Complexity Asymptotic  Definition: approaching a given value as an expression containing a variable tends to infinity Asymptotic complexity  Describe the growth rate of the time or space complexity with respect to input size  Is not directly related to the exact running time Three notations  Big O (O) notation provides an upper bound for the growth rate of the given function.  Big Omega (Ω) notation provides a lower bound.  Big Theta (  ) notation is used when an algorithm is bounded both above and below by the same kind of function. 20

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Big O 21

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 More Big O Examples 22

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Big Omega 23

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Big Theta 24

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Caveat 25

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 26 int Example1(int[][] a) { int sum = 0; for (int i=0; i<a.length; i++) { for (int j=0; j<i; j++) { sum = sum + a[i][j]; } return sum; } Number of operations

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 27 int Example2(int[] a) { int sum = 0; for (int i=a.length; i>0; i/=2) { sum = sum + a[i]; } return sum; } Number of operations

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 28 Number of operations int Example3(int[] a) { int sum = 0; for (int i=0; i<a.length; i++) { for (int j=a.length; j>0; j/=2) { sum = sum + a[i] + a[j]; } return sum; }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 29 int Example4(int[][] a) { int sum = 0; for (int i=0; i<a.length; i++) { for (int j=0; j<100; j++) { sum = sum + a[i][j]; } return sum; } Number of operations

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 30 int Example5(int n) { int sum1 = 0; int sum2 = 0; for (int i=0; i<n; i++) { sum1 += i; for (int j=10*n; j<100*n; j++) { sum2 += sum1; } return sum; } Number of operations

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 31 int[][] multiplySquareMatrices(int[][] a, int[][] b) { int[][] c = new int[a.length][a.length]; int n = a.length; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { for (int k=0; k<n; k++) { c[i][j] = c[i][j] + a[i][k] * b[k][j]; } return c; } Number of operations

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Trade-off Space complexity and time complexity may not be independent.  There is a trade-off between the two complexities.  Algorithm A may be faster than Algorithm B, but consume memory space more than Algorithm B. 32 Space complexityTime complexity of A[i] Dense matrix4*10 bytesO(1) Sparse matrix4*6 bytesO(n) Dense matrix A: Sparse matrix A: (index, value) {(2,4), (5,8), (9,1)} 481

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Summary Complexity analysis mostly involves examining loops. Attempt to characterize growth of functions.  This will be important when we examine searching and sorting Big O notation helps us simplify complexity definitions – worst case  It ignores constant and lower order terms.  Big Omega (Ω) examines lower bounds.  Big Theta (Θ) examines specific case. Computer scientists often use proofs to defend complexity claims. 33

34