CSCI 2824: Lecture 27We have two topics left over in counting/combinatorics that we will look at over the rest of this week.
Recursive CountingThe idea behind recursive counting is to set up a recurrence that expresses what you wish to count. This is especially useful when what we wish to count does not neatly fit into any of the categories studied thus far. Example-1Q How many matches need to be played between You should know the answer to this problem by now and be able to reason it out in many different ways. Answer
Let us look at it recursively. Let
Can we now express Yes, we can say that with Team Therefore, the answer to the problem is governed by the recurrence relation:
The closed form solution is indeed Example-2Q What is the number of In other words, what is Answer
Let us write a recurrence (in the spirit of this lecture). To obtain a permutation of
Step 1 yields Once we have fixed a permutation of the first
Once again, we know by eyeballing the recurrence that Example-3How many solutions are there to the equation:
Again, let us file under the category of things we already know how to solve by different methods :-) Answer
Let Simple cases:
Recurrence. We want to count the number of solutions
Any solution to the system either falls under case-1 or case-2. Therefore, we conclude that: Exercise Knowing secretly that Example-4We wish to roll a dice Q Let Answer
Let us get rid of the base cases.
Now for the generic case. Suppose we wish to roll the dice
Therefore, can we now write a recurrence to express the sum?
A recurrence should directly allow you to write an efficient program to compute the answer using dynamic programming. Example-1Let us take the following recurrence:
How do we solve it? We can write a program to compute Program to compute T
int T(int n, int r){ assert( n > 1); assert( r > 1); /*-- Base Case --*/ if ( r == 1) return 1; if ( n == 1) return 1; /* Recurrence */ return T(n-1,r) + T(n,r-1); } I implemented this in C and ran it. Here are the running times and some results:
We will explain how this recurrence can be computed faster in class. |