$ $
Reminder: “recurrence relations” are recursive functions.
E.g.:
Finding closed-form solutions requires that we provide initial conditions:
Draw a recursion trace for split(3)
:
Express the number of activation records as a recurrence:
More typically, we want to count the number of steps executed by a recursive algorithm:
public static int split1(int n) { for (int i = 0; i < n + 10; i++) { //BASIC OPERATION HERE. } if (n == 0) { return 1; } else { return split1(n - 1) + split1(n - 1); } }
Step one is to develop a recurrence.
Typically:
Initial Conditions
Recurrence Relation
Develop a recurrence that describes the number of multiplication operations performed by the following code block:
public static int fun(int n) {
if (n == 1) {
return 5;
} else {
int sum = 1;
for (int i = 0; i < 7; i++) {
sum *= 2;
}
return sum * fun(n - 1);
}
}
public static int fun(int n) { if (n == 1) { return 5; } else { int sum = 1; for (int i = 0; i < 7; i++) { sum *= 2; // Seven here. } return sum * fun(n - 1); // One here. } }
Write a recurrence relation that describes the number of additions performed by this method.
public static int fun1(int n) {
if (n == 0) {
return 20;
} else {
int result = 2 * n;
return result - (fun1(n - 1) + fun1(n - 1));
}
}
Write a recurrence relation that describes the number of assignments to sum performed by this method.
public static int fun(int n) {
if (n <= 1) {
return 8;
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += fun(n - 1) + fun(n - 2);
}
return sum;
}
Write a recurrence relation that describes the number of assignments to sum performed by this method.
public static int fun(int n) {
if (n <= 1) {
return 8;
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += fun(n - 1) + fun(n - 2);
}
return sum;
}
We need two initial conditions!
Next time…
Space, Right Arrow or swipe left to move to next slide, click help below for more details