340 likes | 509 Views
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.
E N D
Stuff • Check out GUI for assn 5! CISC121 - Prof. McLeod
Last Time • Traditional example – Towers of Hanoi! • Backtracking, using another tradition – the NQueen’s problem. CISC121 - Prof. McLeod
Today • Quick look at NQueen’s backtracking example code. • Analyzing the complexity of recursive methods. CISC121 - Prof. McLeod
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
The “NQueen’s” Problem - Cont. • See “Board.java” and “NQueen.java” for the other methods. CISC121 - Prof. McLeod
Complexity of Recursion • Start with simple recursive methods and then analyze recursive sorts. CISC121 - Prof. McLeod
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
Example #1 - Cont. • If called as in “r1(10)”: 1 2 3 4 5 6 7 8 9 10 CISC121 - Prof. McLeod
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
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
Example #1 - Cont. • As a recursive definition: • What is t(n-1)? • i is the number of “unrolling’s”. CISC121 - Prof. McLeod
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
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
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
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
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
Example #2 - Cont. • Unrolling: CISC121 - Prof. McLeod
Example #2 - Cont. • What is the pattern for the “b” coefficient?: • Remember?: CISC121 - Prof. McLeod
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
Example #2 - Cont. • At the anchor case: • Which is O(n2) Lots of Fun! CISC121 - Prof. McLeod
Example #3 • You can imagine the code for this kind of definition: CISC121 - Prof. McLeod
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
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
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
Example #3 - Cont. • One more “unrolling” to give t(n/4): • Which, for i = 3, gives the following for t(n): CISC121 - Prof. McLeod
Example #3 - Cont. • Search for a pattern: CISC121 - Prof. McLeod
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
Example #3 - Cont. • Note that: • So: • Or: • Which means that t(n) is O(n). CISC121 - Prof. McLeod
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
One More Example! • (From a summer course final exam.) • Any guesses? CISC121 - Prof. McLeod
One More Example! - Cont. • Unrolling’s: • Series: CISC121 - Prof. McLeod
One More Example! - Cont. • Remember the “Geometric Sum”?: • For our series, a = 4 and m = i - 1. CISC121 - Prof. McLeod
One More Example! - Cont. • Anchor case t(0) = 20 when n=1, implies: CISC121 - Prof. McLeod
One More Example! - Cont. • Which proves that t(n) is O(n). CISC121 - Prof. McLeod