Practice in Algorithm Analysis - PowerPoint PPT Presentation

About This Presentation
Title:

Practice in Algorithm Analysis

Description:

Practice in Algorithm Analysis * * Solving Recurrences Write recursive expression for time taken Keep substituting and see a pattern Find a value for k for which you ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 16
Provided by: fsu109
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Practice in Algorithm Analysis


1
Practice inAlgorithm Analysis
2
Solving Recurrences
  • Write recursive expression for time taken
  • Keep substituting and see a pattern
  • Find a value for k for which you know the
    solution (base case)
  • You may need to evaluate a series to get your
    final answer

2
3
Recursion Example 1
  • long f(int n)
  • if( n lt 1 )
  • return 1
  • else
  • return 1 f(n-1)

In terms of big-Oh t(1) 1 t(n) 1 t(n-1)
1 1 t(n-2) 1 1 1 t(n-3) ... k
t(n-k)
In terms of big-Oh t(1) 1 t(n) 1 t(n-1)
If n-k 1, we know the time Choose k n-1 t(n)
n-1 t(1) n-1 1 O(n)
Note t(n-1) 1 t(n-2) t(n-2) 1
t(n-3) t(n-3) 1 t(n-4)
4
Recursion Example 2
  • f(1) 5
  • f(n) 5 f(n-1)

In terms of big-Oh t(1) ? t(n) ?
4
5
Recursion Example 2
  • f(1) 5
  • f(n) 5 f(n-1)

In terms of big-Oh t(1) 1 t(n) 1 t(n-1)
5
6
Recursion Example 3
  • f(1) 5
  • f(n) 5 5f(n-1)

In terms of big-Oh t(1) ? t(n) ?
6
7
Recursion Example 3
  • f(1) 5
  • f(n) 5 5f(n-1)

In terms of big-Oh t(1) 1 t(n) 1 t(n-1)
7
8
Recursion Example 4
  • f(1) 5
  • f(n) 5 5f(n-1) 2f(n-1)

In terms of big-Oh t(1) ? t(n) ?
8
9
Recursion Example 4
  • f(1) 5
  • f(n) 5 5f(n-1) 2f(n-1)

In terms of big-Oh t(1) 1 t(n) 1 2t(n-1)
9
10
Recursion Example 5
  • f(1) 5
  • f(n) 5 5f(n/3) 2f(n/3)

In terms of big-Oh t(1) ? t(n) ?
10
11
Recursion Example 5
  • f(1) 5
  • f(n) 5 5f(n/3) 2f(n/3)

In terms of big-Oh t(1) 1 t(n) 1 2t(n/3)
t(n/3) ? t(n/9) ? t(n/27) ?
11
12
Recursion Example 5
  • f(1) 5
  • f(n) 5 5f(n/3) 2f(n/3)

In terms of big-Oh t(1) 1 t(n) 1 2t(n/3)
t(n/3) 1 2t(n/9) t(n/9) 1
2t(n/27) t(n/27) 1 2t(n/81)
Find the general pattern for t(n) by repeated
substitution
12
13
Recursion Example 5
  • f(1) 5
  • f(n) 5 5f(n/3) 2f(n/3)

In terms of big-Oh t(1) 1 t(n) 1 2t(n/3)
t(n) 1 2t(n/3) 1 212t(n/9)
1 2 4t(n/9) 1 2 41
2t(n/27) 1 2 4 8t(n/27)
1 2 4 ... 2k-1 2kt(n/3k) 2k-1
2kt(n/3k)
t(n/3) 1 2t(n/9) t(n/9) 1
2t(n/27) t(n/27) 1 2t(n/81)
What value of k should we choose?
13
14
Recursion Example 5
  • f(1) 5
  • f(n) 5 5f(n/3) 2f(n/3)

In terms of big-Oh t(1) 1 t(n) 1 2t(n/3)
t(n) 1 2t(n/3) 1 212t(n/9)
1 2 4t(n/9) 1 2 41
2t(n/27) 1 2 4 8t(n/27)
1 2 4 ... 2k-1 2kt(n/3k) 2k-1
2kt(n/3k)
t(n/3) 1 2t(n/9) t(n/9) 1
2t(n/27) t(n/27) 1 2t(n/81)
n/3k 1, so n 3k so k log3n
Substitute for k
14
15
Recursion Example 5
  • f(1) 5
  • f(n) 5 5f(n/3) 2f(n/3)

In terms of big-Oh t(1) 1 t(n) 1 2t(n/3)
t(n) 1 2t(n/3) 1 212t(n/9)
1 2 4t(n/9) 1 2 41
2t(n/27) 1 2 4 8t(n/27)
1 2 4 ... 2k-1 2kt(n/3k) 2k-1
2kt(n/3k)
t(n/3) 1 2t(n/9) t(n/9) 1
2t(n/27) t(n/27) 1 2t(n/81)
n/3k 1, so n 3k so k log3n t(n) 2log3n -
1 2log3n t(1) O(2log3n) O(2 log2n
log32) O(nlog32)
15
Write a Comment
User Comments (0)
About PowerShow.com