lecture-8

# for a given function gn we denote by ogn the set of

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: O(f(n)) if constants k and n0 exist such that A requires no more than k · f(n) time units to solve a problem of size n ≥ n0 Alternative deﬁnition of big O analysis [Cormen et al.] For a given function g(n), we denote by O(g(n)) the set of functions O(g(n)) = {f(n) | there exist positive constants k and n0 such that 0 ≤ f(n) ≤ k · g(n) for all n ≥ n0} •  Thus, big O gives an upper bound on a function f(n) to within a constant factor CPSC 223, 2009 7 Growth Rates Fairly typical growthrate functions •  Ordered from least to most expensive [Carrano, 2007] CPSC 223, 2009 8 4 9/23/10 Asymptotic Upper Bounds Big O notation can sometimes be misleading –  We are considering asymptotic upper bounds Any algorithm that is O(n) is also O(n2), since every function f(n) in O(n) is also in O(n2) … (n ≤ k1·n ≤ k2·n2) –  Are we analyzing the algorithm or the problem? The algorithm is a particular computational approach for solving the problem … e.g., selection sort is O(n2) The problem is the general task … e.g., what is the smallest upper bound for the complexity of sorting CPSC 223, 2009 9 CPSC 223  ­ ­ Fall 2010 10 Binary Search 5 9/23/10 Recurring Themes … •  What are common List ADT operations? –  Insert items into the collection (add) –  Remove items from the collection (delete) –  Search for elements of a collection (retrieve, lookup) –  … plus checking if empty, printing, etc. •  When analyzing data structures, we often are comparing the cost of each of these operations •  So how does “sorting” ﬁt in? CPSC 223, 2009 11 Why Sorting … •  We often want to output elements in order •  Lookup (search) is more efﬁcient if items are sorted –  for an unordered list O(n) … why? –  for a sorted list O(log n) … we’ll disc...
View Full Document

Ask a homework question - tutors are online