1 / 13

Foundations of Data Structures

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….

nailah
Download Presentation

Foundations of Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Foundations of Data Structures Practical Session #3 Recurrence Amihai Savir & Ilya Mirsky - 2013

  2. 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 • Mastermethod • Next practical session…

  3. Question 0- warm up • Find the most tight asymptotic relation between the following pairs: • )

  4. Question 1 Prove that using the substitution method. Solution The 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 1 return 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 5 • publicstaticintfindMax(int[]arr) } • returnFindMax(arr,0, arr.length-1); • { • public staticintfindMax(inta[],intleft, intright)} • intmiddle; • intmax_l, max_r; • if( left == right ) • returna[left]; • else } • middle = (left + right) / 2; • max_l= FindMax( a, left, middle); • max_r= FindMax( a, middle+1, right); • returnMath.max(max_l,max_r); • { • {

  13. Question 5 cont’d

More Related