today lecture 26
play

Today, Lecture 26: Recursion By Elsamuko, Creative Commons - PowerPoint PPT Presentation

Previous Lecture: OOP: Access modifiers & inheritance Today, Lecture 26: Recursion By Elsamuko, Creative Commons Attribution-Share Alike 2.0 Generic license Announcements: Last discussion section today/tomorrow work


  1. ◼ Previous Lecture: ◼ OOP: Access modifiers & inheritance ◼ Today, Lecture 26: ◼ Recursion By Elsamuko, Creative Commons Attribution-Share Alike 2.0 Generic license ◼ Announcements: ◼ Last discussion section today/tomorrow – work together to optimize an algorithm ◼ Test 2B released today 4:30pm EDT; submit by Thurs, May 7, 4:30pm EDT ◼ Project 6 due Tue, May 12, 11pm EDT. Part B to be released this evening.

  2. Recursion A method of problem solving by breaking a problem into smaller and smaller instances of the same problem until an instance is so small that it’s trivial to solve

  3. Recursion ◼ The Fibonacci sequence is defined recursively: F(1)=1, F(2)=1, F(3)= F(1) + F(2) = 2 F(k) = F(k-2) + F(k-1) F(4)= F(2) + F(3) = 3 It is defined in terms of itself; its definition invokes itself. ◼ Algorithms, and functions, can be recursive as well. I.e., a function can call itself. ◼ Example: remove all occurrences of a character from a string ‘ gc aatc gga c ’ → ‘ gcaatcggac ’

  4. Example: removing all occurrences of a character ◼ Can solve using iteration — check one character (one component of the vector) at a time … … 1 2 k s ‘c’ ‘s’ ‘ ’ ‘1’ ‘1’ ‘1’ ‘2’ Subproblem 1: Iteration: Keep or discard s(1) Divide problem Subproblem 2: into sequence of Keep or discard s(2) equal-sized, Subproblem k: identical Keep or discard s(k) subproblems See RemoveChar_loop.m

  5. Example: removing all occurrences of a character ◼ Can solve using recursion ◼ Original problem: remove all the blanks in string s ◼ Decompose into two parts: 1 . remove blank in s(1) 2 . remove blanks in s(2:length(s)) Original problem Decompose into 2 parts Decompose Decompose Decompose Decompose ‘ ’

  6. function s = removeChar(c, s) % Return string s with character c removed if length(s)==0 % Base case: nothing to do return else end

  7. function s = removeChar(c, s) % Return string s with character c removed if length(s)==0 % Base case: nothing to do return else if s(1)~=c else end end

  8. function s = removeChar(c, s) % Return string s with character c removed if length(s)==0 % Base case: nothing to do return else if s(1)~=c % return string is % s(1) and remaining s with char c removed else end end

  9. function s = removeChar(c, s) % Return string s with character c removed if length(s)==0 % Base case: nothing to do return else if s(1)~=c % return string is % s(1) and remaining s with char c removed else % return string is just % the remaining s with char c removed end end

  10. function s = removeChar(c, s) % Return string s with character c removed if length(s)==0 % Base case: nothing to do return else if s(1)~=c % return string is % s(1) and remaining s with char c removed s= [s(1) removeChar(c, s(2:length(s)))]; else % return string is just % the remaining s with char c removed end end

  11. function s = removeChar(c, s) % Return string s with character c removed if length(s)==0 % Base case: nothing to do return else if s(1)~=c % return string is % s(1) and remaining s with char c removed s= [s(1) removeChar(c, s(2:length(s)))]; else % return string is just % the remaining s with char c removed s= removeChar(c, s(2:length(s))); end end

  12. function s = removeChar(c, s) % Return string s with character c removed if length(s)==0 % Base case: nothing to do return else if s(1)~=c % return string is % s(1) and remaining s with char c removed s= [s(1) removeChar(c, s(2:length(s)))]; else % return string is just % the remaining s with char c removed s= removeChar(c, s(2:length(s))); end end

  13. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; else s= removeChar(c, s(2:length(s))); end end removeChar – 1 st call c _ s d _ o g _ [ ] d

  14. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; 1 else s= removeChar(c, s(2:length(s))); end end removeChar – 2 nd call removeChar – 1 st call c _ c _ s s d _ o _ o g g _ _ [ ] [ ] d

  15. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; 1 else s= removeChar(c, s(2:length(s))); 2 end end removeChar – 2 nd call removeChar – 3 rd call removeChar – 1 st call c _ c _ c _ s s s d _ o _ o g g o g _ _ _ [ ] [ ] [ ] d o

  16. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; 3 1 else s= removeChar(c, s(2:length(s))); 2 end end removeChar – 2 nd call removeChar – 3 rd call removeChar – 1 st call removeChar – 4 th call c _ c _ c _ c _ s s s d _ o _ o s _ g g o g g _ _ _ [ ] [ ] [ ] [ ] d o

  17. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; 3 1 else s= removeChar(c, s(2:length(s))); 4 2 end end removeChar – 2 nd call removeChar – 3 rd call removeChar – 1 st call removeChar – 4 th call c _ c _ c _ c _ s s s d _ o _ o s _ g g o g g _ _ _ [ ] [ ] [ ] [ ] d o removeChar – 5 th call c _ s g [ ] g

  18. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; 5 3 1 else s= removeChar(c, s(2:length(s))); 4 2 end end removeChar – 2 nd call removeChar – 3 rd call removeChar – 1 st call removeChar – 4 th call c _ c _ c _ c _ s s s d _ o _ o s _ g g o g g _ _ _ [ ] [ ] [ ] [ ] d o removeChar – 6 th call removeChar – 5 th call c _ c _ s ‘’ s g [ ] g

  19. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; 5 3 1 else s= removeChar(c, s(2:length(s))); 4 2 end end removeChar – 2 nd call removeChar – 3 rd call removeChar – 1 st call removeChar – 4 th call c _ c _ c _ c _ s s s d _ o _ o s _ g g o g g _ _ _ [ ] [ ] [ ] [ ] d o removeChar – 6 th call removeChar – 5 th call c _ c _ s ‘’ s g [ ] ‘’ g

  20. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; 3 1 else s= removeChar(c, s(2:length(s))); 4 2 end end removeChar – 2 nd call removeChar – 3 rd call removeChar – 1 st call removeChar – 4 th call c _ c _ c _ c _ s s s d _ o _ o s _ g g o g g _ _ _ [ ] [ ] [ ] [ ] d o g removeChar – 6 th call removeChar – 5 th call c _ c _ s ‘’ s g [ ] ‘’ g

  21. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; 3 1 else s= removeChar(c, s(2:length(s))); 2 end end removeChar – 2 nd call removeChar – 3 rd call removeChar – 1 st call removeChar – 4 th call c _ c _ c _ c _ s s s d _ o _ o s _ g g o g g _ _ _ [ ] [ ] [ ] [ ] d o g g removeChar – 6 th call removeChar – 5 th call c _ c _ s ‘’ s g [ ] ‘’ g

  22. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; 1 else s= removeChar(c, s(2:length(s))); 2 end end removeChar – 2 nd call removeChar – 3 rd call removeChar – 1 st call removeChar – 4 th call c _ c _ c _ c _ s s s d _ o _ o s _ g g o g g _ _ _ [ ] [ ] [ ] [ ] d o g o g g removeChar – 6 th call removeChar – 5 th call c _ c _ s ‘’ s g [ ] ‘’ g

  23. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c removeChar('_', 'd_o_g') s= [s(1) removeChar(c, s(2:length(s)))]; 1 else s= removeChar(c, s(2:length(s))); end end removeChar – 2 nd call removeChar – 3 rd call removeChar – 1 st call removeChar – 4 th call c _ c _ c _ c _ s s s d _ o _ o s _ g g o g g _ _ _ [ ] [ ] [ ] [ ] d o g o g o g g removeChar – 6 th call removeChar – 5 th call c _ c _ d o g s ‘’ s g [ ] ‘’ g

  24. Key to recursion ◼ Must identify (at least) one base case , the “trivially simple” case ◼ no recursion is done in this case ◼ The recursive case(s) must reflect progress towards the base case ◼ E.g., give a shorter vector as the argument to the recursive call – see removeChar

  25. function s = removeChar(c, s) if length(s)==0 return else if s(1)~=c s= [s(1) removeChar(c, s(2:length(s)))]; else s= removeChar(c, s(2:length(s))); end end How many call frames are opened (used) in executing each of the following statements? >> st= removeChar('t', 'Matlab'); >> sx= removeChar('x', 'Matlab'); 3, 0 4, 1 3, 6 6, 6 7, 7 A B C D E

  26. Divide-and-conquer methods, such as recursion, is useful in geometric situations Chop a region up into triangles with smaller triangles in “areas of interest ” 3D Graphics: Level of Detail Recursive mesh generation

Recommend


More recommend