Download presentation
Presentation is loading. Please wait.
Published byRosamond Foster Modified over 9 years ago
1
Recursion When to use it and when not to use it
2
Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made to the same method Within that method a call is made to the same method Somewhere within the method, the loop must be exited Somewhere within the method, the loop must be exited
3
Disadvantages 3 major disadvantages 3 major disadvantages Slow algorithms Slow algorithms e.g. recursive Fibonacci: e.g. recursive Fibonacci: int fib (int n) { if (n <= 2) return 1; if (n <= 2) return 1; else return fib (n – 1) + fib (n – 2); else return fib (n – 1) + fib (n – 2);}
4
Disadvantages Cont. Procedure call overheads: 30% slower Procedure call overheads: 30% slower Not much time lost though Not much time lost though Data on stack (stack overflow) Data on stack (stack overflow) Not much of a problem these days Not much of a problem these days Watch out for using more than the limit Watch out for using more than the limit
5
Disadvantages Summary The first one is the one to watch out for (slower algorithms) The first one is the one to watch out for (slower algorithms) The other two can almost be ignored (procedure call overheads and data on stack) The other two can almost be ignored (procedure call overheads and data on stack)
6
What to Use Instead If iteration is easy to program, use it If iteration is easy to program, use it If dynamic programming works, use it instead If dynamic programming works, use it instead
7
Why Use Recursion? Easy to program Easy to program Easy to debug Easy to debug Shorter than dynamic programming Shorter than dynamic programming
8
n Queens Problem Place n queens on an n x n chess board so that no queen is attacked by another queen Place n queens on an n x n chess board so that no queen is attacked by another queen Find out the total number of configurations Find out the total number of configurations
9
n Queens Solution The most obvious solution is to recursively add queens, trying all possible placements The most obvious solution is to recursively add queens, trying all possible placements It is easy to see that there can only be one queen per column It is easy to see that there can only be one queen per column At each step, you know the position of the queens for the first i columns At each step, you know the position of the queens for the first i columns
10
n Queens Solution Cont. You try to place a queen in the i + 1 column You try to place a queen in the i + 1 column If successful, you move on to the next step, trying to place a queen in the i + 2 column If successful, you move on to the next step, trying to place a queen in the i + 2 column
11
n Queens Solution Cont. QQXQXQX X X X QQXX XQ QXQXQXQX Q QX X X XXX QQXQXX
12
XQQXX QX Q QX QX XQXX QQQXQX XXQQX QX X X XQQQX XQ
13
QQXQXX Q X X X QQXQXQX QQXXX XQQXQX X X X XQQX QQQQX
14
QXXXX FINISHED X Q QX X XXX QXQQXQX
15
void search (int col) void search (int col){ if (col == n) if (col == n) { count++; count++; return; return; } for (int row = 0; row < n; row++) for (int row = 0; row < n; row++) if (not_attacked (row, col)) if (not_attacked (row, col)) { board [col] = row; board [col] = row; search (col + 1); search (col + 1); }}
16
n Queens Solution Cont. The not_attacked method simply checks to see if the queen can be placed in that particular block The not_attacked method simply checks to see if the queen can be placed in that particular block
17
n Queens Analysis Since the number of possibilities is small, recursion is quick enough in this case Since the number of possibilities is small, recursion is quick enough in this case For a 10 x 10 board it runs for 0.11 s For a 10 x 10 board it runs for 0.11 s This solution is also an example of depth first search This solution is also an example of depth first search In questions similar to this, use recursion, since it is easier to program In questions similar to this, use recursion, since it is easier to program
18
Little Flower Shop IOI ’99 You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind and at least as many vases (V) ordered in a row. You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind and at least as many vases (V) ordered in a row. The bunches are moveable and are uniquely identified by integers between 1 and F. These id-numbers have a significance. They determine the required order of appearance of the flower bunches in the row of vases. The bunch i must be on the left of bunch j whenever i < j. The bunches are moveable and are uniquely identified by integers between 1 and F. These id-numbers have a significance. They determine the required order of appearance of the flower bunches in the row of vases. The bunch i must be on the left of bunch j whenever i < j. Excess vases will be left empty. A vase can only hold one bunch of flowers. Excess vases will be left empty. A vase can only hold one bunch of flowers.
19
Little Flower Shop Cont. Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value. These values are presented in the following table: Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value. These values are presented in the following table: VASES 12345 FLOWERS 1723-52416 2521-41023 3-215-4-2020
20
Flower Solution 1 We could use recursion: We could use recursion: int flo (int f, int v) int flo (int f, int v){ if (f == F) if (f == F) return 0; return 0; int m = 0; int m = 0; for (; v <= V – F + f ; v++) for (; v <= V – F + f ; v++) m = max (m, grid [f][v] + m = max (m, grid [f][v] + flo (f + 1, v)); flo (f + 1, v)); return m; return m;}
21
Flower Solution 1 Analysis In the initial problem, the vase numbers must also be output In the initial problem, the vase numbers must also be output It is far too slow It is far too slow It tries every solution to find the maximum It tries every solution to find the maximum
22
Flower Solution 2 The first solution was too slow The first solution was too slow This time we’ll try dynamic programming This time we’ll try dynamic programming Start from the second-last row Start from the second-last row Scan each row from left to right Scan each row from left to right Add the maximum value in each block Add the maximum value in each block
23
Flower Solution 2 Cont. int m; int m; for (int i = F - 2; i >= 0; i--) { m = -100; m = -100; for (int j = V – F + i; j >= i;j--) for (int j = V – F + i; j >= i;j--) { m = max (m, grid [i + 1][j + 1]); m = max (m, grid [i + 1][j + 1]); grid [i][j] += m; grid [i][j] += m; }
24
Flower Solution 2 Cont. int answer = -1000000; int answer = -1000000; for (int i = 0; i < F; i++) ans = max (ans, grid [0][i]); ans = max (ans, grid [0][i]);
25
Flower Solution 2 Cont. The solution is illustrated in the tables on the right The solution is illustrated in the tables on the right The top table is before The top table is before The bottom table is after The bottom table is after Yellow numbers were changed Yellow numbers were changed 12345 1723-52416 2521-41023 3-215-4-2020 1234514153252416 2541163023 3-215-4-2020
26
Flower Solution 2 Analysis Only the necessary calculations are done Only the necessary calculations are done Much faster algorithm (0.05 s, quicker than 2 s allowed) Much faster algorithm (0.05 s, quicker than 2 s allowed) To determine the vase numbers is easy To determine the vase numbers is easy
27
Flower Analysis In this case, recursion is not fast enough In this case, recursion is not fast enough As always, if dynamic programming works, use it As always, if dynamic programming works, use it Not all problems can be solved with dynamic programming, since it uses more memory Not all problems can be solved with dynamic programming, since it uses more memory
28
Conclusion When recursion runs within the time limit, use it When recursion runs within the time limit, use it If it doesn’t, try using dynamic programming or another technique If it doesn’t, try using dynamic programming or another technique If you can’t do it any other way, use recursion to get partial marks If you can’t do it any other way, use recursion to get partial marks
29
Conclusion Cont. Remember, many other techniques use recursion, so it is important to know well Remember, many other techniques use recursion, so it is important to know well Most graph theory (shortest paths, etc.) uses recursion Most graph theory (shortest paths, etc.) uses recursion
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.