Foundations of Data Structures

13
Foundations of Data Structures Practical Session #3 Recurrence 08/04/2013 Amihai Savir & Ilya Mirsky - 2013

description

Foundations of Data Structures. Practical Session #3 Recurrence. Solution Methods. Substitution method Guess the form of the solution and prove it by induction. Iteration method Convert the recurrence into a summation and solve it Master method Next practical session…. - PowerPoint PPT Presentation

Transcript of Foundations of Data Structures

Page 1: Foundations of Data Structures

Amihai Savir & Ilya Mirsky - 2013

Foundations of Data Structures

Practical Session #3Recurrence

08/04/2013

Page 2: Foundations of Data Structures

2

Solution Methods

• Substitution methodGuess the form of the solution and prove it by induction.

• Iteration methodConvert the recurrence into a summation and solve it

• Master methodNext practical session…

Page 3: Foundations of Data Structures

3

Question 0- warm up

Find the most tight asymptotic relation between the following pairs:

1. )

Θ

ΘO

ΘΘ

ΩΩOΘ

Ω

Page 4: Foundations of Data Structures

4

Question 1

Prove that using the substitution method.

SolutionThe form of the solution is already given, so we don't have to guess it. We only need to prove that , i.e., that .

Page 5: Foundations of Data Structures

5

Question 1 cont’d

Page 6: Foundations of Data Structures

6

Question 2

Page 7: Foundations of Data Structures

7

Question 3

fact(n) {if (n == 0) return 1return n * fact(n – 1)

}

Page 8: Foundations of Data Structures

8

Question 4

Fibonacci series is defined as follows:

Find an iterative algorithm and a recursive one for computing element number in Fibonacci series, i.e., Fibonacci(n).Analyze the running-time of each of the algorithms.

Page 9: Foundations of Data Structures

9

Question 4 cont’d

recFib(n) { if (n ≤ 1) return n else return recFib(n-1) + recFib(n-2)}

Page 10: Foundations of Data Structures

10

Question 4 cont’d

In the same manner:

The series ends when

To summarize:

Page 11: Foundations of Data Structures

11

Question 4 cont’d

iterFib (n) { allocate f[0..n] f[0] = 0 f[1] = 1 for (i=2 ; i ≤ n ; i++) f[i] = f[i-1] + f[i-2] return f[n] }

Page 12: Foundations of Data Structures

12

Question 5public static int findMax(int [] arr) }

return FindMax(arr,0, arr.length-1);{public static int findMax( int a[], int left, int right )}

int middle;int max_l, max_r;if ( left == right )

return a[left];else }

middle = (left + right) / 2;max_l = FindMax( a, left, middle); max_r = FindMax( a, middle+1, right); return Math.max(max_l,max_r);

{{

Page 13: Foundations of Data Structures

13

Question 5 cont’d