1 / 34

Stuff

Stuff. Check out GUI for assn 5!. Last Time. Traditional example – Towers of Hanoi! Backtracking, using another tradition – the NQueen’s problem. Today. Quick look at NQueen’s backtracking example code. Analyzing the complexity of recursive methods.

fern
Download Presentation

Stuff

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. Stuff • Check out GUI for assn 5! CISC121 - Prof. McLeod

  2. Last Time • Traditional example – Towers of Hanoi! • Backtracking, using another tradition – the NQueen’s problem. CISC121 - Prof. McLeod

  3. Today • Quick look at NQueen’s backtracking example code. • Analyzing the complexity of recursive methods. CISC121 - Prof. McLeod

  4. static boolean solveNQ(Board B, int col) { // anchor case: we have succeeded! if (col >= B.getSize()) return true; // try putting a queen in each row of this column for (int row = 0; row < B.getSize(); row++) { if (B.safePosition(row, col)) { B.putQueen(row, col); if (solveNQ(B, col+1)) return true; else B.removeQueen(row, col); } // end if } // end for // anchor case: there is no solution return false; } // end solveNQ CISC121 - Prof. McLeod

  5. The “NQueen’s” Problem - Cont. • See “Board.java” and “NQueen.java” for the other methods. CISC121 - Prof. McLeod

  6. Complexity of Recursion • Start with simple recursive methods and then analyze recursive sorts. CISC121 - Prof. McLeod

  7. Complexity of a Recursive Program - Example #1 • For example: public static void r1 (int n) { if (n <= 0) System.out.println(); else { r1(n-1); System.out.println(n); } // end else } // end r1 • First of all, what does this do? CISC121 - Prof. McLeod

  8. Example #1 - Cont. • If called as in “r1(10)”: 1 2 3 4 5 6 7 8 9 10 CISC121 - Prof. McLeod

  9. Example #1 - Cont. • What is the complexity of the method? public static void r1 (int n) { if (n <= 0) System.out.println(); else { r1(n-1); System.out.println(n); } // end else } // end r1 • O(n), but how to prove this? CISC121 - Prof. McLeod

  10. Example #1 - Cont. • Count primitive operations: public static void r1 (int n) { if (n <= 0) System.out.println(); else { r1(n-1); System.out.println(n); } // end else } // end r1 • Introduce “t(n)” which is time as a function of n: • t(n) = a + t(n-1), where a is a constant. CISC121 - Prof. McLeod

  11. Example #1 - Cont. • As a recursive definition: • What is t(n-1)? • i is the number of “unrolling’s”. CISC121 - Prof. McLeod

  12. Example #1 - Cont. • What is t(n-2)? • Easy to see a pattern emerging: • When n-i equals 0, t(0) equals b, at anchor case. CISC121 - Prof. McLeod

  13. Example #1 - Cont. • When the method is complete (at the anchor case!): • Now t(n) has been reduced to a non-recursive equation that easily shows that t(n) is O(n). CISC121 - Prof. McLeod

  14. Example #2 • How about: public static void r2 (int n) { if (n >= 0) { for (int i = 0; i <= n; i++) System.out.print(i + " "); System.out.println(); r2(n-1); } // end if } // end r2 • What does it do? CISC121 - Prof. McLeod

  15. Example #2 - “r2(10)” 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 0 1 2 3 4 5 0 1 2 3 4 0 1 2 3 0 1 2 0 1 0 CISC121 - Prof. McLeod

  16. Example #2 - Cont. • What is the complexity of this method? public static void r2 (int n) { if (n >= 0) { for (int i = 0; i <= n; i++) System.out.print(i + " "); System.out.println(); r2(n-1); } // end if } // end r2 • t(n) = a + bn + t(n-1) CISC121 - Prof. McLeod

  17. Example #2 - Cont. • Unrolling: CISC121 - Prof. McLeod

  18. Example #2 - Cont. • What is the pattern for the “b” coefficient?: • Remember?: CISC121 - Prof. McLeod

  19. Example #2 - Cont. • Substitute i-1 for m: • As for example #1, t(0) = c when n-i=0, therefore i=n when the anchor case is encountered. • Substituting this back into the above equation and simplifying: CISC121 - Prof. McLeod

  20. Example #2 - Cont. • At the anchor case: • Which is O(n2) Lots of Fun! CISC121 - Prof. McLeod

  21. Example #3 • You can imagine the code for this kind of definition: CISC121 - Prof. McLeod

  22. Example #3 - Cont. public static void r3 (int n) { if (n > 1) { r3(n/2); for (int i = 0; i <= n; i = i+4) System.out.print(i + " "); System.out.println(); } // end if } // end r3 CISC121 - Prof. McLeod

  23. Example #3 - “r3(64)” 0 0 4 0 4 8 0 4 8 12 16 0 4 8 12 16 20 24 28 32 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 CISC121 - Prof. McLeod

  24. Example #3 - Cont. • What is t(n/2)? • When this is put back in the t(n) part of the definition: • Now, i = 2. CISC121 - Prof. McLeod

  25. Example #3 - Cont. • One more “unrolling” to give t(n/4): • Which, for i = 3, gives the following for t(n): CISC121 - Prof. McLeod

  26. Example #3 - Cont. • Search for a pattern: CISC121 - Prof. McLeod

  27. Example #3 - Cont. • Note that at the anchor case: • So, unrolling is complete when n=2i, or i = log(n) • Put this back into the equation at the bottom of slide 26: CISC121 - Prof. McLeod

  28. Example #3 - Cont. • Note that: • So: • Or: • Which means that t(n) is O(n). CISC121 - Prof. McLeod

  29. Complexity of Recursion – Summary • Create a recursive definition from the code. • Unroll the definition until you see a pattern emerging. • Write the definition as a function of the number of “unrollings”. • Simplify in order to remove the recursive part of the definition. • Simplify further to obtain the big O complexity of the equation. CISC121 - Prof. McLeod

  30. One More Example! • (From a summer course final exam.) • Any guesses? CISC121 - Prof. McLeod

  31. One More Example! - Cont. • Unrolling’s: • Series: CISC121 - Prof. McLeod

  32. One More Example! - Cont. • Remember the “Geometric Sum”?: • For our series, a = 4 and m = i - 1. CISC121 - Prof. McLeod

  33. One More Example! - Cont. • Anchor case t(0) = 20 when n=1, implies: CISC121 - Prof. McLeod

  34. One More Example! - Cont. • Which proves that t(n) is O(n). CISC121 - Prof. McLeod

More Related