Recursive Programming and Time Complexity in Discrete Mathematical Structures (CS 2233), Assignments of Discrete Mathematics

Prof. Winsborough's lecture nineteen from april 14, 2009, covers recursive programming, its advantages, and time complexity analysis using the power(n) algorithm as an example. The lecture also discusses improving recursive programs and solving recurrences using induction.

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-vaw
koofers-user-vaw 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Discrete Mathematical Structures
CS 2233 Lecture Nineteen
Prof. William Winsborough
April 14, 2009
14 April 2009 Winsborough CS 2233 Lecture 19 2
Business
Recall: Homework 9 due Thursday 4/16
4.3: 4, 6, 12, 22
Read
4.4, 7.3
14 April 2009 Winsborough CS 2233 Lecture 19 3
Recursive Programming
Can write the same programs using
iteration
Often algorithms are easier to design and
understand using a recursive formulation
Classic programming paradigm:
Divide and Conquer
Decompose problem into subproblems
Solve each subproblem
Combine the results
14 April 2009 Winsborough CS 2233 Lecture 19 4
A Simple Iterative Program
Consider the following algorithm:
power(n) takes a non-negative integer and returns 3
n
power(n)
power = 1
for i = 1 to n
power = power * 3
return power
Cost (time complexity): O(n)
14 April 2009 Winsborough CS 2233 Lecture 19 5
Example Recursive Program
Consider the following algorithm:
power(n) takes a non-negative integer and r eturns 3
n
power(n)
if (n 0) return 1
return 3*power(n-1)
Cost (time complexity): T(n)
Defined by the following recurrence (basica lly a recursive
definition):
T(0) = 1
T(n) = T(n-1) + 3 (depending on which ops you count)
Closed form solution: T(n) = 3n+1 = O(n)
14 April 2009 Winsborough CS 2233 Lecture 19 6
Improving the Example Program
A faster version of power(n):
power(n)
if (n 0) return 1
temp = n/2
if 2*temp = n return (power(temp))
2
return 3*(power(temp))
2
Cost:
T(0) = 1
T(n) = T(
n/2
) + d, in which d is a small constant
Claim: closed form solution is T(n) = O(lg n)
pf2

Partial preview of the text

Download Recursive Programming and Time Complexity in Discrete Mathematical Structures (CS 2233) and more Assignments Discrete Mathematics in PDF only on Docsity!

Discrete Mathematical Structures

CS 2233 Lecture Nineteen

Prof. William Winsborough April 14, 2009

14 April 2009 Winsborough CS 2233 Lecture 19 2

Business

  • Recall: Homework 9 due Thursday 4/
    • 4.3: 4, 6, 12, 22
  • Read
    • 4.4, 7.

14 April 2009 Winsborough CS 2233 Lecture 19 3

Recursive Programming

  • Can write the same programs using iteration - Often algorithms are easier to design and understand using a recursive formulation
  • Classic programming paradigm: Divide and Conquer - Decompose problem into subproblems - Solve each subproblem - Combine the results

14 April 2009 Winsborough CS 2233 Lecture 19 4

A Simple Iterative Program

  • Consider the following algorithm:
    • power(n) takes a non-negative integer and returns 3n power(n) power = 1 for i = 1 to n power = power * 3 return power
  • Cost (time complexity): O(n)

14 April 2009 Winsborough CS 2233 Lecture 19 5

Example Recursive Program

  • Consider the following algorithm:
    • power(n) takes a non-negative integer and returns 3n power(n) if (n ≤ 0) return 1 return 3*power(n-1)
  • Cost (time complexity): T(n)
    • Defined by the following recurrence (basically a recursive definition): T(0) = 1 T(n) = T(n-1) + 3 (depending on which ops you count)
  • Closed form solution: T(n) = 3n+1 = O(n) 14 April 2009 Winsborough CS 2233 Lecture 19 6

Improving the Example Program

  • A faster version of power(n): power(n) if (n ≤ 0) return 1 temp = ⌊n/2⌋ if 2temp = n return (power(temp))^2 return 3(power(temp))^2
  • Cost: T(0) = 1 T(n) = T(⌊n/2⌋) + d, in which d is a small constant
  • Claim: closed form solution is T(n) = O(lg n)

14 April 2009 Winsborough CS 2233 Lecture 19 7

Solving Recurrences

  • Two steps
    • Find a good guess
    • Prove it is correct
  • Guess
    • Expansion method T(n) = T(n-1) + 3 = T(n-2) + 3 + 3 = …= 1+3+…+3 (with n) copies of “+ 3”
    • Expand each occurrence of T(n) until you get to T(0) = 1

14 April 2009 Winsborough CS 2233 Lecture 19 8

Proving Recurrence

  • Recall 1st^ recurrence: T(0) = 1 and T(n) = T(n-1) + d
  • Want to prove T(n) = O(n)
    • We’re not so worried about exactly which function in O(n) T(n) may happen to be
  • Suppose T(n) ≤ en + h, for some constants e and h - That would prove T(n) = O(n), since en + h = O(n) - Use induction to prove that for some e and h, T(n) ≤ en + h satisfies the definition of T(n)

14 April 2009 Winsborough CS 2233 Lecture 19 9

Induction Proof

  • Show T(n) ≤ en + h in which T(n) is given by T(0) = 1 and T(n) = T(n-1) + d
  • Basis: T(0) ≤ h if h ≥ 1
  • Step: T(n) = T(n – 1) + d, by recurrence ≤ e(n-1) + h + d, by induction assumption = en + h – (e – d) ≤ en + h, as long as e ≥ d
  • It now follows that T(n) = O(n)

14 April 2009 Winsborough CS 2233 Lecture 19 10

Proving Recurrence

  • Recall 2nd^ recurrence: T(0) = 1 and T(n) = T(⌊n/2⌋) + d
  • Guess: How many times can you divide n in half before reaching 1? Answer: lg n
  • Want to prove T(n) = O(lg n)
  • Suppose T(n) ≤ e*lg n + h, for some constants e and h

14 April 2009 Winsborough CS 2233 Lecture 19 11

Induction Proof

  • Show T(n) ≤ e*lg n + h in which T(n) is given by T(0) = 1 and T(n) = T(⌊n/2⌋) + d
  • Basis: T(0) ≤ h if h ≥ 1
  • Step: T(n) = T(⌊n/2⌋) + d, by recurrence ≤ elg (n/2) + h + d, by induction assumption = e(lg n – lg 2) + h + d = elg n + h – (e – d) ≤ elg n + h, as long as d ≤ e
  • It now follows that T(n) = O(lg n)