Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
 The running time of an algorithm as input size approaches infinity is called the asymptotic running time  We study different notations for asymptotic.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
The Growth of Functions
Asymptotic Growth Rate
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation.
Cmpt-225 Algorithm Efficiency.
The Efficiency of Algorithms
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
The Efficiency of Algorithms
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 3: Growth of Functions (slides enhanced by N. Adlai A. DePano)
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
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.
Design and Analysis Algorithm Drs. Achmad Ridok M.Kom Fitra A. Bachtiar, S.T., M. Eng Imam Cholissodin, S.Si., M.Kom Aryo Pinandito, MT Pertemuan 04.
CS 3343: Analysis of Algorithms
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
CSCI 3160 Design and Analysis of Algorithms Tutorial 1
Asymptotic Analysis-Ch. 3
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithms Growth of Functions. Some Notation NNatural numbers RReal numbers N + Positive natural numbers R + Positive real numbers R * Non-negative real.
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
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
CMSC 341 Asymptotic Analysis. 2 Complexity How many resources will it take to solve a problem of a given size? –time –space Expressed as a function of.
CSC – 332 Data Structures Generics Analysis of Algorithms Dr. Curry Guinn.
COP 3530 Spring2012 Data Structures & Algorithms Discussion Session Week 5.
Asymptotic Notations By Er. Devdutt Baresary. Introduction In mathematics, computer science, and related fields, big O notation describes the limiting.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
13 February 2016 Asymptotic Notation, Review of Functions & Summations.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
Asymptotic Notation Faculty Name: Ruhi Fatima
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Algorithms Lecture #05 Uzair Ishtiaq. Asymptotic Notation.
DR. Gatot F. Hertono, MSc. Design and Analysis of ALGORITHM (Session 2)
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Mathematical Foundations (Growth Functions) Neelima Gupta Department of Computer Science University of Delhi people.du.ac.in/~ngupta.
COMP108 Algorithmic Foundations Algorithm efficiency
Chapter 3: Growth of Functions
Introduction to Algorithms
Lecture 06: Linked Lists (2), Big O Notation
Analysis of Algorithms
CS 3343: Analysis of Algorithms
Chapter 3: Growth of Functions
O-notation (upper bound)
Asymptotic Notations Algorithms Lecture 9.
Asymptotic Growth Rate
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
CS 3343: Analysis of Algorithms
Advanced Analysis of Algorithms
Chapter 2.
Time Complexity Lecture 14 Sec 10.4 Thu, Feb 22, 2007.
Intro to Data Structures
Performance Evaluation
Time Complexity Lecture 15 Mon, Feb 27, 2006.
Estimating Algorithm Performance
Presentation transcript:

Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper Collins Publishers)

Data Structures and Algorithms2 Review: Logarithms, Powers and Exponentials Let b be any real greater than 1 and let x be any real greater than 0. The logarithm to the base b of x, denoted log b x is defined to be the number y such that b y = x log b 1 = 0 log b x > 0 if x > 1 log b b = 1 log b x < 0 if 0 < x < 1 Any logarithmic function is a monotone increasing function of its argument, that is log b x 1 > log b x 2 provided that x 1 > x 2

Data Structures and Algorithms3 Doubling the argument increases the base 2 logarithm by 1. That is, log 2 2x = (log 2 x) + 1 Why? 2 (log 2 x) + 1 = 2 log 2 x  2 = 2  x 2 log 2 2x = 2x

Data Structures and Algorithms4 Review: Logarithms log b (x 1  x 2 ) = log b x 1 + log b (x 2 ) log b (x 1 / x 2 ) = log b x 1 - log b x 2 log b x c = c  log b x

Data Structures and Algorithms5 Suppose a and b are both greater than 1, what is the relation of log a x to log b x? Since x = a log a x log b x = log b (a log a x ) = log a x  log b a This is a constant. So, any two logarithmic functions differ only by a constant factor. For example, suppose we know that an algorithm executes lg x instructions, where x is the size of the input. lg x = lg(10 log 10 x ) = log 10 x  lg 10 ~ 3.32 * log 10 x

Data Structures and Algorithms6 The number of bits in the usual binary notation for the positive integer N is  LgN  + 1. For example, how many bits are required to represent bits  Lg41  + 1 = 5+1 = bits  Lg3  + 1 = 1+1 =

Data Structures and Algorithms7 The number of digits in the usual base 10 notation for the positive integer N is  Log 10 N  + 1. For example, how many digits are required to represent 31? digits  Log  + 1 = 1+1 =

Data Structures and Algorithms8 Any function from reals to reals of the form g(x) = x  for some constant  > 0 is called a simple power. Any simple power is an increasing function of its argument. Examples: x 2, x 3 and x 1/3 are simple powers

Data Structures and Algorithms9 An exponential function is one of the form h(x) = c x for some constant c > 1. Examples: 2 x and 100 x are exponential functions of x

Data Structures and Algorithms10 Let f and g be functions from reals to reals. f dominates g if the ratio f(n) / g(n) increases without bound as n increases without bound. In other words, if for any c > 0 there is an n 0 > 0 such that f(n) > c  g(n) for all n > n 0. Examples: f(n) = n 2 dominates g(n) = 2n since for any c n 2 > c  2n whenever n > 2c. f(n) = 10n does not dominate g(n) = 2n since the ratio of f(n) / g(n) is never larger than 5. Dominates

Data Structures and Algorithms11 Theorem: Any exponential function dominates any simple power, any simple power dominates any logarithmic function.

Data Structures and Algorithms12 Let N be the set of nonnegative integers {0,1,…}. Let R be the set of real numbers and let R* be the set of nonnegative real numbers. Let g be a function from N to R*. Then O(g) is the set of all functions f from N to R* such that, for some constants c > 0 and N 0  0. f(N)  c  g(N) for all N  N 0. In other words, f  O(g) if the value of f is bounded from above by a fixed multiple of the value of g for all sufficiently large values of the argument.

Data Structures and Algorithms13 Examples: For any f it is the case that f  O(f). Any constant multiple of f is in O(f). F(n) = 13n + 7 is in O(n). Why? 13n + 7  14n for n  7 So the definition is satisfied with c = 14, n 0 = n  O(.0001n 2 ) Why? Let c = 10 7 and n 0 = 0 in the definition of O(). f(n) = n f(n)  O(n 2 )

Data Structures and Algorithms14 Little o For any function g, o(g) is the set of all functions that are dominated by g. That is, the set of all functions f such that for each constant c > 0 there is an n 0 > 0 such that f(n) n 0. Examples: Let f(n) = n and g(n) = n 2 then f(n)  o(g(n)) Let f(n) = n 2 and g(n) = 2 n then f(n)  o(g(n))

Data Structures and Algorithms15 Theorem: Growth Rates 1. The power n  is in O(n  ) if and only if    ( ,  > 0); and n  is in o(n  ) if and only if  < . Examples: Intuitively n  O(n 3 ) n <= n 3 n  o(n 3 ) n < n 3

Data Structures and Algorithms16 2. log b n  o(n  ) for any b and . log 10 n  o(n) log 2 n  o(n 1/2 ) Examples:

Data Structures and Algorithms17 3. n   o(c n ) for any  > 0 and c > 1. Examples: n 2  o(4 n ) n 100  o(2 n )

Data Structures and Algorithms18 4. log a n  O(log b n) for any a and b. log 2 n  O(log 10 n) log 10 n  O(log 2 n)

Data Structures and Algorithms19 5. c n  O(d n ) if and only if c  d, and c n  o(d n ) if and only if c < d. Examples: 3 n  O(4 n ) 3 n  o(4 n )

Data Structures and Algorithms20 6. Any constant function f(n) = c is in O(1). For example: A 32-bit add instruction O(1).

Data Structures and Algorithms21 Big-O only provides an upper bound. For example: 17n 2  O(n 2 ) but 17n 2  O(n 37 ) 17n 2  O(2 n )

Data Structures and Algorithms22 Big Omega (Big-  ): Big-  notation is exactly the converse of Big-O notation; f   (g) if and only if g  O(f). f  O(g) implies that f grows at most as quickly as g. f   (g) implies that f grows at least as quickly as g. Examples: let f(n) = n f(n)  O(n 2 ) n 2   (n)

Data Structures and Algorithms23 Big theta (Big  ):  (f) = O(f)   (f) Example: Let f(n) = 4n then f(n)  O(n) f(n)   (n) so f(n)   (n) The set of functions  (f) is the order of f.

Data Structures and Algorithms24 A Quiz Suppose we use a phone book to look up a number in the standard way. Let T(n) be the number of operations (comparisons) required. In the worst case T(n)  O(Log N). True or False: Also, in the worst case, T(n)  O(n) ______ T(n)   (n) ______ T(n)   (n) ______

Data Structures and Algorithms25 A Quiz Suppose we use a phone book to look up a number in the standard way. Let T(n) be the number of operations (comparisons) required. In the worst case T(n)  O(Log N). True or False: Also, in the worst case, T(n)  O(n) True T(n)   (n) False T(n)   (n) False

Data Structures and Algorithms26 A Quiz Suppose we use a phone book to look up a number. Let T(n) be the number of operations (comparisons) required. In the best case T(n)  O(1). True or False: Also, in the best case, T(n)  O(n) _____ T(n)   (n) _____ T(n)   (n) _____

Data Structures and Algorithms27 A Quiz Suppose we use a phone book to look up a number. Let T(n) be the number of operations (comparisons) required. In the best case T(n)  O(1). True or False: Also, in the best case, T(n)  O(n) True T(n)   (n) False T(n)   (n) False

Data Structures and Algorithms28 A Quiz Suppose we want to delete the last item on a singly linked list. Let T(n) be the number of operations (comparisons) required. There are no cases to consider: T(n)  O(n). True or False: T(n)  O(Lg n) T(n)   (n) T(n)   (Lg n)

Data Structures and Algorithms29 A Quiz Suppose we want to delete the last item on a singly linked list. Let T(n) be the number of operations (comparisons) required. There are no cases to consider: T(n)  O(n). True or False: T(n)  O(Lg n) False T(n)   (n) True T(n)   (Lg n) True

Remember Data Structures and Algorithms30 When working with Big O, Big  and Big , be sure to always consider only large n. In addition, pin the case down first and then consider Big O, Big , and Big . Lastly, remember that some times “case” does not apply.

Algorithms and Problems Data Structures and Algorithms31 In this class, we will mostly be analyzing algorithms (counting operations) in terms of Big O, Big  and Big . Problems may also be analyzed. The lower bound for a particular problem is the worst case running time of the fastest algorithm that solves that problem. Later, we will look at an argument that comparison based sorting is  (n Log n). What does that mean?