Foundations of Data Structures
description
Transcript of Foundations of Data Structures
Amihai Savir & Ilya Mirsky - 2013
Foundations of Data Structures
Practical Session #3Recurrence
08/04/2013
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…
3
Question 0- warm up
Find the most tight asymptotic relation between the following pairs:
1. )
Θ
ΘO
ΘΘ
ΩΩOΘ
Ω
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 .
5
Question 1 cont’d
6
Question 2
7
Question 3
fact(n) {if (n == 0) return 1return n * fact(n – 1)
}
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.
9
Question 4 cont’d
recFib(n) { if (n ≤ 1) return n else return recFib(n-1) + recFib(n-2)}
10
Question 4 cont’d
In the same manner:
The series ends when
To summarize:
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] }
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);
{{
13
Question 5 cont’d