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 teams in a round-robin tournament? 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 be the number of matches for teams. We know that
Can we now express in terms of ? Yes, we can say that with teams, # of matches involving all n teams = # of matches involving teams 1… (n-1) + # of matches involving team n Team plays precisely matches (one with every other team). I.e, . 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 permutations of objects? In other words, what is ? Answer
Let us write a recurrence (in the spirit of this lecture). To obtain a permutation of objects (numbered 1…n), let us do the following:
Step 1 yields possible permutations (we pretend not to know what it may be :-) ) Once we have fixed a permutation of the first objects, there are possible places where the object #n can be inserted. Therefore the recurrence is
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 represent the answer to this problem. Simple cases:
Recurrence. We want to count the number of solutions for the general case where . We have two cases:
Any solution to the system either falls under case-1 or case-2. Therefore, we conclude that: . Exercise Knowing secretly that , can we verify that the recurrence holds? Example-4We wish to roll a dice times to obtain a sum of . Each roll of the dice can give us a number from to . Q Let be the number of ways to obtain a sum of from rolls of a dice. Write a recurrence relation for . Answer
Let us get rid of the base cases.
Now for the generic case. Suppose we wish to roll the dice times and arrive at a sum of exactly . Let us split cases on the last roll:
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. |