Docsity

Big O Notation and Complexity Analysis in Discrete Mathematics - CS 2233 Lecture 14 - Prof, Assignments of Discrete Mathematics

Toggle sidebar
bg1
1
Discrete Mathematical Structures
CS 2233 Lecture Fourteen
Prof. William Winsborough
March 5, 2009
5 March 2009 Winsborough CS 2233 Lecture 14 2
Business
•Turn in Homework 6 today
•Recall:
–Homework 7, due Thursday 3/19
• 3.2: 2, 4, 6, 8, 14, 20, 22
•3.3: 2
– Read Sections 3.2 and 3.3
• Questions???
5 March 2009 Winsborough CS 2233 Lecture 14 3
Example
• Let f(x) = x2+ 2x +1 and g(x) = x2
• f(x) is O(g(x))
– k = 1 and C = 4 (witnesses)
– k = 2 and C = 3
• g(x) is O(f(x))
– k = 1 and C = 1
5 March 2009 Winsborough CS 2233 Lecture 14 4
More Examples
• Sum of first n positive integers
1+2+…+n ≤n+n+…+n = n2
So Σi is O(n2)
• It follows that the complexity of both
bubble sort and insertion sort is O(n2)
n
i=1
n
5 March 2009 Winsborough CS 2233 Lecture 14 5
Still More Examples
• f(n) = n! is the product of the first n positive
integers
n! = 1·2·…·n ≤n·n·…·n = nn
So n! is O(nn)
n
5 March 2009 Winsborough CS 2233 Lecture 14 6
Big-O and Sums
• Def: If f1, f2are real-valued functions,
(f1+f2) is the function such that that
(f1+f2)(x) = f1(x)+f2(x) for all x
• Theorem:
If f1(x) is O(g1(x)) and f2(x) is O(g2(x)), then
(f1+f2)(x) is O(max(|g1(x)|, |g2(x)|)
• Corollary:
If f1(x) and f2(x) are each O(g(x)), then
(f1+f2)(x) is O(g(x))
pf2

Partial preview of the text

Download Big O Notation and Complexity Analysis in Discrete Mathematics - CS 2233 Lecture 14 - Prof and more Assignments Discrete Mathematics in PDF only on Docsity!

Discrete Mathematical Structures

CS 2233 Lecture Fourteen

Prof. William Winsborough March 5, 2009

5 March 2009 Winsborough CS 2233 Lecture 14 2

Business

  • Turn in Homework 6 today
  • Recall:
    • Homework 7 , due Thursday 3/
      • 3.2: 2, 4, 6, 8, 14, 20, 22
      • 3.3: 2
    • Read Sections 3.2 and 3.
  • Questions???

5 March 2009 Winsborough CS 2233 Lecture 14 3

Example

  • Let f(x) = x^2 + 2x +1 and g(x) = x^2
  • f(x) is O(g(x))
    • k = 1 and C = 4 (witnesses)
    • k = 2 and C = 3
  • g(x) is O(f(x))
    • k = 1 and C = 1

5 March 2009 Winsborough CS 2233 Lecture 14 4

More Examples

  • Sum of first n positive integers 1+2+…+n ≤ n+n+…+n = n^2

So Σ i is O(n^2 )

  • It follows that the complexity of both bubble sort and insertion sort is O(n^2 )

n i=

n

5 March 2009 Winsborough CS 2233 Lecture 14 5

Still More Examples

  • f(n) = n! is the product of the first n positive integers n! = 1· 2 ·…·n ≤ n·n·…·n = nn

So n! is O(nn)

n

5 March 2009 Winsborough CS 2233 Lecture 14 6

Big-O and Sums

  • Def: If f 1 , f 2 are real-valued functions, (f 1 +f 2 ) is the function such that that (f 1 +f 2 )(x) = f 1 (x)+f 2 (x) for all x
  • Theorem: If f 1 (x) is O(g 1 (x)) and f^2 (x) is O(g 2 (x)), then (f 1 +f 2 )(x) is O(max(|g 1 (x)|, |g 2 (x)|)
  • Corollary: If f 1 (x) and f 2 (x) are each O(g(x)), then (f 1 +f 2 )(x) is O(g(x))

5 March 2009 Winsborough CS 2233 Lecture 14 7

Big-O and Products

  • Def: If f 1 , f 2 are real-valued functions, (f 1 f 2 ) is the function such that that (f 1 f 2 )(x) = f 1 (x)·f 2 (x) for all x
  • Theorem: If f 1 (x) is O(g 1 (x)) and f 2 (x) is O(g 2 (x)), then (f 1 f 2 )(x) is O(g 1 (x) g 2 (x))

5 March 2009 Winsborough CS 2233 Lecture 14 8

Example

  • Claim: Σ i is Θ(n^2 )
    • We have already shown it is O(n 2 ),
    • We have to show it is Ω(n^2 )
      • Recall that Σ i = n(n+1)/
      • Taking C = ½ and k = 0, we have n^2 /2 + n/2 ≥ Cn^2 for all n>k

i=

n

i=

n

5 March 2009 Winsborough CS 2233 Lecture 14 9

Significance of Theorems on Sums

and Products

  • A polynomial of degree k is O(xk)

5 March 2009 Winsborough CS 2233 Lecture 14 10

Big-Omega and Big-Theta

  • Big-O provides an upper bound on function growth
  • Big-Omega gives a lower bound
    • Def: f(x) is Ω(g(x)) if there are positive constants C and k such that |f(x)| ≥ C|g(x)| whenever x>k
  • Big-Theta gives both
    • Def: f(x) is Θ(g(x)) if f(x) is O(g(x)) and f(x) is Ω(g(x))
    • In this case we say f(x) is of order g(x)

5 March 2009 Winsborough CS 2233 Lecture 14 11

Computational Complexity of

Algorithms

  • This is where Big-O, Big-Omega, and Big- Theta get used
  • Complexity is measured principally in terms of two resources - Time Complexity - Space Complexity - Discussed more in course on data structures
  • Worst-case complexity vs. Average-case

5 March 2009 Winsborough CS 2233 Lecture 14 12

Worst- versus Average-case Complexity

  • Possible inputs are partitioned into cases that elicit the same behavior ( input cases )
  • Worst-case complexity analyzes the input case that maximizes cost of executing the algorithm
  • Average-case complexity considers all input cases
    • The average-case complexity of executing an algorithm is:
      • The sum over all input cases of the cost of each case times its probability
      • Can be difficult to calculate
      • Assumes you know the probability of each input case
    • Also called expected-case complexity or, less formally, expected cost