Algorithm Analysis. Math Review – 1.2 Exponents –X A X B = X A+B –X A /X B =X A-B –(X A ) B = X AB –X N +X N = 2X N ≠ X 2N –2 N+ 2 N = 2 N+1 Logarithms.

Slides:



Advertisements
Similar presentations
CMSC 341 Asymptotic Analysis. 2 Mileage Example Problem: John drives his car, how much gas does he use?
Advertisements

Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 2: Algorithm Analysis
Introduction to Analysis of Algorithms
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 1. 2 Big Oh and other notations Introduction Classifying functions by their asymptotic growth Theta, Little oh,
Cpt S 223 – Advanced Data Structures
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Algorithm/Running Time Analysis. Running Time Why do we need to analyze the running time of a program? Option 1: Run the program and time it –Why is this.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
CS2210 Data Structures and Algorithms Lecture 2:
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
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.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
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.
Iterative Algorithm Analysis & Asymptotic Notations
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
Asymptotic Analysis-Ch. 3
Introduction to Analysis of Algorithms COMP171 Fall 2005.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
CE 221 Data Structures and Algorithms Chapter 2: Algorithm Analysis - I Text: Read Weiss, §2.1 – Izmir University of Economics.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Algorithm Analysis Part of slides are borrowed from UST.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Big-Oh Notation. Agenda  What is Big-Oh Notation?  Example  Guidelines  Theorems.
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.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Data Structure and Algorithm Analysis 02: Algorithm Analysis Hongfei Yan School of EECS, Peking University 3/12/2014.
Asymptotic Complexity
Algorithm Analysis 1.
Analysis of Algorithms
Chapter 2 Algorithm Analysis
Analysis of Non – Recursive Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Non – Recursive Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
Complexity Analysis.
CS 3343: Analysis of Algorithms
Time Complexity Analysis Neil Tang 01/19/2010
Algorithm Analysis Neil Tang 01/22/2008
Analysis of Algorithms
Introduction to Algorithms Analysis
Analysis of Algorithms
BIG-OH AND OTHER NOTATIONS IN ALGORITHM ANALYSIS
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Analysis of Algorithms
CS 201 Fundamental Structures of Computer Science
Chapter 2.
Analysis of Algorithms
CSE 2010: Algorithms and Data Structures Algorithms
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Algorithm/Running Time Analysis
Algorithm Course Dr. Aref Rashad
Analysis of Algorithms
Presentation transcript:

Algorithm Analysis

Math Review – 1.2 Exponents –X A X B = X A+B –X A /X B =X A-B –(X A ) B = X AB –X N +X N = 2X N ≠ X 2N –2 N+ 2 N = 2 N+1 Logarithms –X A =B iff log X B=A –log A B =log C B/log C A; A,B,C > 0, A ≠ 1 –logAB = logA + logB; A, B > 0 Series N –∑ 2 i = 2 N+1 -1 i=0 N –∑ A i = A N+1 -1/A-1 i=0 N –∑ i = N(N+1)/2 i=0

Running Time Why do we need to analyze the running time of a program? Option 1: Run the program and time it –Why is this option bad? –What can we do about it?

Pseudo-Code Used to specify algorithms Part English, part code Algorithm (arrayMax(A, n)) curMax = A[0] for i=1 i<n i++ if curMax < A[i] curMax = A[i] return curMax

Counting Operations Algorithm (arrayMax(A, n)) curMax = A[0] //2 for i=1 i<n i++ //1+n if curMax < A[i] //4(n-1) 6(n-1) curMax = A[i] return curMax //1 Best case – 5n Worst case – 7n-2 Average case – hard to analyze

Asymptotic Notation 7n-2 n=5 -> 33 n=100 -> 698 n=1,000,000 -> 6,999,998 Running time grows proportionally to n What happens as n gets large?

Big-Oh T(N) is O(f(N)) if there are positive constants c and n 0 such that T(N) =n 0 7n 2 -2 is O(n 2 ) n 0 >=1 c=8 Upper bound

Omega T(N) is Ω(g(N)) if there are positive constants c and n 0 such that T(N) >= cg(N) when N>=n 0 7n 2 -2 is Ω(n 2 ) n 0 >=1 c=1 Lower bound

Theta T(N) is Θ(h(N)) if and only if T(N) = Oh(N) and T(N) = Ωh(N) 7n 2 -2 is Θ(n 2 ) Tightest result possible

Little-Oh T(N) is o(p(N)) if T(N) = O(p(N)) and T(N) ≠ Θ(p(N)) 7n 2 -2 is o(n 3 ) –O when n 0 >=8 c=1 Growth rate of T(N) is smaller than growth rate of p(N)

Rules Rule 1 –If T 1 (N) = O(f(N) and T 2 (N) = O(g(N)), then T 1 (N) + T 2 (N) = max(O(f(N)), O(g(N))) T 1 (N)* T 2 (N) = O(f(N)*g(N)) Rule 2 –If T(N) is a polynomial of degree k, then T(N) = Θ(N k ) Rule 3 –log k N = O(N) for any constant k. This tells us that logs grow very slowly.

Examples 87n 4 + 7n 3nlogn + 12logn 4n 4 + 7n 3 logn

Terminology Logarithmic – O(logn) Linear – O(n) Linearithmic – O(nlogn) Quadratic – O(n 2 ) Polynomial – O(n k ) k>=1 Exponential – O(a n ) a>1

Rule 1 – for loops The running time of a for loop is at most the running time of the statements inside the for loop (including tests) times the number of iterations. for(i=0; i<5; i++) cout << “hello\n”;

Rule 2 – nested loops Analyze these inside out. The total running time of a statement inside a group of nested loops is the running time of the statement multiplied by the product of the sizes if all of the loops. for(i=0; i<n; i++) for(j=0; j<n; j++) cout << “hello\n”;

Rule 3 – consecutive statements These just add (which means that the max is the one that counts) for(i=0; i<n; i++) a[i] = 0; for(i=0; i<n; i++) for(j=0; j<n; j++) a[i]+=a[j]+i+j;

Rule 4 – if/else The running time of an if/else statement is never more than the running time of the test plus the larger of the running times of the statements, if(condition) S1 else S2

Example Find maximum number in nxn matrix Algorithm: 64…2 123…9 ………… 58…1 0 n-1 0

Example What is the big-oh running time of this algorithm? Algorithm: Input: A, n curMax = A[0][0] for i=0 i<n i++ for j=0 j<n j++ if curMax < A[i][j] curMax = A[i][j] return curMax

Another Example Determine how many elements of array 1 match elements of array 2 Algorithm? 24…6 n-10 68…3 0

Another Example Algorithm: Input: A, B, n for i=0 i<n i++ for j=0 j<n j++ if A[i] == A[j] matches++ break What is the running time of the algorithm? 24…6 n-10 68…3 0

Logs in the Running Time An algorithm is O(logN) if it takes constant time to cut the problem size by a fraction. Binary search –Given an integer X and integers A 0, A 1, …, A N-1, which are presorted and already in memory, find i such that A i =X, or return i = -1 if X is not in the input.

Binary Search Algorithm? Running time is at most ceil(log(N-1))+2 which is O(logN)

Example 1 – (N-1) – (N-1)/ – (N-1)/ – (N-1)/8 - 8 i – (N-1)/2 i i-1 = N-1 –difference between high and low is 1 difference between high and low is 0

The Evil King King has N bottles of wine Exactly 1 bottle is poisoned How can the king figure out which bottle is poisoned and only kill at most logN of his testers?