CS3381 Des & Anal of Alg (2001-2002 SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.

Slides:



Advertisements
Similar presentations
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
Advertisements

A Basic Study on the Algorithm Analysis Chapter 2. Getting Started 한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님 1.
the fourth iteration of this loop is shown here
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Chapter 3 Growth of Functions
Introduction to Analysis of Algorithms
CS421 - Course Information Website Syllabus Schedule The Book:
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Data Structures Types of Data Structures Algorithms
Introduction to Algorithm design and analysis
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Instructor Neelima Gupta
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Week 2 CS 361: Advanced Data Structures and Algorithms
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
A Lecture /24/2015 COSC3101A: Design and Analysis of Algorithms Tianying Ji Lecture 1.
Data Structures for Media Introduction. Dr. Minming Li Department of Computer Science Room :Y6426 Phone :
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
MS 101: Algorithms Instructor Neelima Gupta
MS 101: Algorithms Instructor Neelima Gupta
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Analysis of Algorithms1 O-notation (upper bound) Asymptotic running times of algorithms are usually defined by functions whose domain are N={0, 1, 2, …}
David Luebke 1 11/29/2015 CS 332: Algorithms Introduction Proof By Induction Asymptotic notation.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Introduction to Analysis of Algorithms CS342 S2004.
Time Complexity of Algorithms (Asymptotic Notations)
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
Algorithm Analysis Algorithm Analysis Lectures 3 & 4 Resources Data Structures & Algorithms Analysis in C++ (MAW): Chap. 2 Introduction to Algorithms (Cormen,
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 5: Algorithms.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
CSC 413/513: Intro to Algorithms Introduction Proof By Induction Asymptotic notation.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Lecture 2 Algorithm Analysis
CMPT 438 Algorithms.
Analysis of Algorithms
Introduction to Algorithms
CS 583 Fall 2006 Analysis of Algorithms
CS 3343: Analysis of Algorithms
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
Asymptotic Notations Algorithms Lecture 9.
CSC 413/513: Intro to Algorithms
Introduction to Algorithms Analysis
Ch. 2: Getting Started.
Analysis of Algorithms
Estimating Algorithm Performance
Analysis of Algorithms
Presentation transcript:

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Analysis of Algorithms

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Analysis of Algorithms Coming up Asymptotic performance, Insertion Sort A formal introduction to asymptotic notation (Chap , Chap 3.1)

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Asymptotic performance “How does the algorithm behave as the problem size gets very large?” Running time Memory/storage requirements Bandwidth/power requirements/logic gates/etc. In analysis of algorithms, we care most about asymptotic performance

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Asymptotic performance Assume: an algorithm can solve a problem of size n in f(n) microseconds (10 -6 seconds). f(n)n=20n=40n=60 Log 2 n Sqrt(n) n n log 2 n n2n2 n4n4 2n2n n! 5.32 * sec4.32 * sec5.91 * sec 6.32 * sec4.47 * sec7.75 * sec 40 * sec20 * sec60 * sec 213 * sec86 * sec354 * sec 1600 * sec400 * sec3600 * sec 2.56 sec0.16 sec12.96 sec

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Asymptotic performance Assume: an algorithm can solve a problem of size n in f(n) microseconds (10 -6 seconds).

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Input Size Time and space complexity is generally a function of the input size E.g., sorting, multiplication How we characterize input size depends: Sorting: number of input items Multiplication: total number of bits Graph algorithms: number of nodes & edges

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Insertion Sort

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Insertion Sort To sort A[1..n] in place: Steps: Pick element A[j] Move A[j-1..1] to the right until proper position for A[j] is found. Currently sorted part Currently unsorted part j1…j+1..n

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Insertion Sort

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Correctness of Insertion Sort To prove Insertion Sort is correct, we state the loop invariant: “At start of each iteration of the for loop, A[1..j-1] consists of the elements originally in A[1..j-1] but in sorted order.” We observe 3 properties: Initialization: It is true prior to the first iteration of the loop Maintenance: If it is true before an iteration, it remains true before next iteration. Termination: When the loop terminates, the invariant gives a useful property that helps show the algorithm is correct. Can you state these properties for Insertion Sort?

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Correctness of Insertion Sort loop invariant “At start of each iteration of the for loop, A[1..j-1] consists of the elements originally in A[1..j-1] but in sorted order.” Initialization Before the first iteration, j=2. => A[1.. j-1] contains only A[1]. => Loop invariant holds prior to the first iteration. Maintenance The outer loop moves A[j-1],A[j-2],A[j-3].. to the right until the proper position for A[j] is founded. Then A[j] is inserted. => if the loop invariant is true before an iteration, it remains true before next iteration. Termination The outer loop ends with j=n+1. Substituting n+1 for j in the loop invariant, we get “A[1..n] consists of the n sorted elements.”

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Analyzing Insertion Sort times n n-1  j=2..n t j  j=2..n (t j -1) n-1 Cost c 1 c 2 0 c 4 c 5 c 6 c 7 c 8 The running time T(n) = c 1 *n+c 2 *(n-1)+c 4 *(n-1)+c 5 *(  j=2..n t j )+c 6 *(  j=2..n (t j -1))+c 7 *(  j=2..n (t j -1))+c 8 *(n-1) t j = no. of times that line 5 is executed, for each j.

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Analyzing Insertion Sort Noting :  j=2..n j = __________  j=2..n (j-1) = _______ T(n) = c 1 *n+c 2 *(n-1)+c 4 *(n-1)+c 5 *(  j=2..n t j )+ c 6 *(  j=2..n (t j -1))+c 7 *(  j=2..n (t j -1))+c 8 *(n-1) Worse case: Reverse sorted  inner loop body executed for all previous elements.  t j =j.  T(n) = c 1 *n+c 2 *(n-1)+c 4 *(n-1)+c 5 *(  j=2..n j)+ c 6 *(  j=2..n (j-1))+c 7 *(  j=2..n (j-1))+c 8 *(n-1)  T(n) = An 2 +Bn+C  T(n) = c 1 *n+c 2 *(n-1)+c 4 *(n-1)+c 5 *(  j=2..n j)+ c 6 *(  j=2..n (j-1))+c 7 *(  j=2..n (j-1))+c 8 *(n-1)

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Analyzing Insertion Sort T(n)=c 1 *n+c 2 *(n-1)+c 4 *(n-1)+c 5 *(  j=2..n t j )+c 6 *(  j=2..n (t j -1))+c 7 *(  j=2..n (t j -1))+c 8 *(n-1) Worst caseReverse sorted => inner loop body executed for all previous elements. So, t j =j. => T(n) is quadratic: T(n)=An 2 +Bn+C Average caseHalf elements in A[1..j-1] are less than A[j]. So, t j = j/2 => T(n) is also quadratic: T(n)=An 2 +Bn+C Best case Already sorted => inner loop body never executed. So, t j =1. => T(n) is linear: T(n)=An+B

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Kinds of Analysis (Usually) Worst case: T(n) = max time on any input of size n Knowing it gives us a guarantee about the upper bound. In some cases, worst case occurs fairly often Average case is often as bad as worst case. (Sometimes) Average case: T(n) = average time over all inputs of size n (Rarely) Best case: Cheat with slow algorithm that works fast on some input. Good only for showing bad lower bound.

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Random-Access Machine Analysis is performed with respect to a computational model We usually use a generic uniprocessor random-access machine (RAM) All memory equally expensive to access No concurrent operations All reasonable instructions take unit time (Except, of course, function calls) Constant word size

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Order of Growth  Ignore machine-dependent constants  Look at growth of T(n) as n ->   Drop low-order terms, Ignore leading constants Eg. worse case of insertion sort, T(n) = An 2 +Bn+C Order of Growth = n 2 “T(n) is in  (n 2 )” For convenience, we usually say “T(n) is  (n 2 )” and “T(n) =  (n 2 )” An algorithm is more efficient if its worst-case running time has a lower order of growth.  (n 2 ) is a set of functions that relates to n 2 in some way. (We’ll define later)

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Asymptotic Notations 3 major notations for describing algorithm complexities: Asymptotic Tight Bound:  Intuitively like “=” Asymptotic Upper Bound:  Intuitively like “  ” Asymptotic Lower Bound:  Intuitively like “  ” Other notations: o,  Intuitively like “ ” Eg., Insertion Sort’s worse case running time is  (n 2 ). Insertion Sort’s best case running time is  (n). Insertion Sort’s running time is  (n). Insertion Sort’s running time is O(n 2 )... running time is in  (n 2 ).

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Asymptotic Notations Very often the algorithm complexity can be observed directly from simple algorithms: The levels of nested loops. O(n 2 )  (n)

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Definitions of O, , and  O(g(n)) = {f(n): there exist positive constants c and n 0 such that 0  f(n)  cg(n) for all n  n 0 } f(n) c 1 g(n) c 2 g(n) n0n0 f(n) is  (g(n)) f(n) cg(n) n0n0 f(n) is O(g(n)) f(n) cg(n) n0n0 f(n) is  (g(n))  (g(n)) = {f(n): there exist positive constants c and n 0 such that 0  cg(n)  f(n) for all n  n 0 }  (g(n)) = {f(n): there exist positive constants c 1, c 2, n 0 such that 0  c 1 g(n)  f(n)  c 2 g(n) for all n  n 0 }  (g(n)) O(g(n)) and  (g(n))

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Example To do so, we must determine positive constants c 1, c 2, n 0 such that c 1 n 2  0.5n 2 -3n  c 2 n 2, for all n  n 0. Dividing by n 2 yields: c 1  /n  c 2. c 1  0.5-3/n holds for any value of n  7 by choosing c 1  1/ /n  c 2 holds for any value n  1 by choosing c 2  0.5 Hence by choosing c 1 =1/14, c 2 =0.5, and n 0 =7, we can verify that 0.5n 2 - 3n =  (n 2 ). According to the formal definition of , prove that 0.5n 2 - 3n =  (n 2 )

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Example (cont’d) We have shown that 0.5n 2 - 3n =  (n 2 ). Similar prove can be applied to show An 2 +Bn+C =  (n 2 ) and An+B =  (n). For insertion sort: Worse case running time: T(n) = An 2 +Bn+C => T(n) =  (n 2 ) Average case running time: T(n) = An 2 +Bn+C => T(n) =  (n 2 ) Best case running time: T(n) = An+B => T(n) =  (n) Recall that, informally, the  () term can be obtained by observing the highest order term and drop the leading constant.

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Example According to the formal definition of O, prove that 6n 3  O(n 2 ) Prove by contradiction: Suppose c, n 0 exist such that 6n 3  cn 2, for all n  n 0. => n  c 2 /6, which is impossible for arbitrarily large n, since c is a constant. => 6n 3  cn 2 is not correct => 6n 3  O(n 2 )

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Points to Note We can write 2n 2 +3n+1 = 2n 2 +  (n). f(n) = n 2 + O(n) means f(n) = n 2 + h(n) for some h(n)  O(n) O-notation is an upper-bound notation. It makes no sense to say f(n) is at least O(n). Why? It is also correct to write n 2 =O(n 3 ), and n 2 =  (n). Proof?

CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms Analysis of Algorithms Summary Algorithm, input, output, instance, “Correct Algorithm” (recall Introduction) Asymptotic Performance, Input Size Insertion Sort Proof: Insertion Sort is correct (Loop Invariant + 3 properties) Analysis of Insertion Sort, Worse/Average/Best case Order of Growth Asymptotic Notations