CSC 124: Solutions to homework problems C.G.Koay February 12, 2009
1
Problem 1 of Homeowork 1 N=number of columns
(2,1)
(2,2)
... ...
...
(1,N)
(2,N-1)
f ...
...
N=number of rows
(1,1) (1,2)
(1,1)
...
...
(1,2) 1
2
(1,N) (2,1)
...
N
N+1
...
(2,N-1) ...
(N,1)
...
(N-1,1) (N-1,2) (N,1)
FIGURE 1a
FIGURE 1b
Figure 1: Mapping of two indices into one index according to the rule, f. The role of the function, f, is to take each pair of (i,j) of Figure 1a and map it to k as in Figure 2 or Figure 1b. Please ˛nd the expression for k in terms of i, j, N or other parameters that you think may be needed to fully specify k.
(i,j) k
Figure 2: De˛nition of k.
1
Solution. In one of the lectures, we worked out an example similar to this ˛rst homework problem. In that example, our goal was to map two indices of a ‘˛xed’ rectangular array in a row-wise fashion (I hope you know what I meant here) onto a one-dimensional array. We came to the conclusion that the index k for this example can be expressed as k = i˜N +j, where N is the number of columns.
The number of elements in a row
(2,1)
(2,2)
N
N
1 (N - 0/2)
N-1
2N-1
2 (N - 1/2)
N-(N-2) N-(N-1)
(N-1)(N - (N-2)/2)
...
FIGURE 3a
...
(2,N-1)
(1,N)
...
(N,1)
...
...
...
...
(N-1,1) (N-1,2)
...
...
(1,2)
...
(1,1)
...
N=number of rows
N=number of columns
The total number of elements beginning from the first element in the first row to Simplified formulae the last element of the second column of the m-th row
N (N - (N-1)/2)
FIGURE 3b
Figure 3: Discovering patterns in the formulae. In this homework problem, we know that the number of columns changes (or decreases by one unit) as we move down the rows. Let’s do some simple counting of the number of elements. Figure 3b above has three columns. Each entry in the ˛rst column is simply the number of elements in the corresponding row. Each entry in the second column is the total number of elements starting from the ˛rst element in the ˛rst row to the last element in the corresponding row. For example, the ˛rst entry in the second column should be N because the total number of elements starting from the ˛rst element in the ˛rst row to the last element in the corresponding (or the same) row is N. Similarly, the second entry in the second column should be 2N ` 1 because the total number of elements starting from the ˛rst element in the ˛rst row to the last element in the corresponding (or the second)row is 2N ` 1. In brief, if we continue in the same way, we will have the following a sequence of numbers, fN; 2N ` 1; 3N ` 3; 4N ` 6; 5N ` 10; 6N ` 15; ´ ´ ´ g. Note that the third column is a simple but important rearrangement of this sequence of numbers. The importance of this rearrangement lies in the fact that we can better predict what the next entry would
2
be. In short, we have found a simple pattern in this sequence of numbers. This pattern tells us that the total number of elements starting from the ˛rst element in the ˛rst row to the last element in the i-th row is i(N ` (i ` 1)=2). Once we have found the pattern in the numbers, we have to consider the index k as a ‘dynamical’ object. If we use k = j, the formula works only for the ˛rst row because the total number of elements starting from the ˛rst element of the ˛rst row to the last element of the corresponding row does not get ‘carried over’ as we move to the next row. Let’s look into this ‘incorrect’ but informative formula, k = i(N ` (i ` 1)=2) + j. Why is the formula incorrect? (Hint: when i = 1 and j = 1, k is N + 1). Incidentally, we are one row ahead. Can we ˛x the formula? Yes, we can! We simply replace i with i ` 1 in the expression of k. Namely, k
=
(i ` 1)(N ` (i ` 1 ` 1)=2) + j
=
(i ` 1)(N ` (i ` 2)=2) + j:
Discovering patterns in numbers or in research problems is fun but it is a path that must be taken by you. The explanations above are likely to be boring if you have not discovered it yourself. Let me end this problem with a saying from a famous mathematical pedagogue: Where do the good questions, the research problems, come from? They probably come from the same hidden cave where authors ˛nd their plots and composers their tunes - and no one knows where that is or can even remember where it was after luckily stumbling into it once or twice. One thing is sure: they do not come from a vague desire to generalize. Almost the opposite is true: the source of all great mathematics is the special case, the concrete example. - Paul R. Halmos
2
Problem 2 of Homework 1
Suppose the triangular matrix is ‚ipped upside down as shown in Figure 4a, please ˛nd the expression for k in terms of i, j, N according to the rule of rearrangement as shown in Figure 4b. Solution. Using the same technique as in the ˛rst problem, we can show that the total number of elements starting from the ˛rst 3
(2,1)
(2,2)
f
...
...
...
N=number of rows
(1,1)
(1,1)
(2,1)
(N-1,1)
(N-1,2)
...
(N-1,N-1)
(N,1)
(N,2)
...
...
...
(2,2) 3
2
1
(N,N) n(n+1)/2
(N,N)
N=number of rows FIGURE 4a
FIGURE 4b
Figure 4: Another mapping of a pair of indices to a single index.
. . . . . . . . i-5 i-4
S
0
1
2
S
i
i-1
i-2 i-3 i-4 i-5 . . . . . . . .
5
4
ST
i
i
i
.... ....
i
i
3
i
4
i
5
i
i-3 i-2
i-1
i
3
2
1
0
i
i
i
i
(i+1) terms
S T = i (i+1) ST = S + S = 2 S S = i (i+1)/2 Figure 5: Arithmetic progession. Pi row to the last elements of the i-th is j=1 j = 1 + 2 + ::: + i. In one of the lectures, I mentioned that Gauss 1 was able to sum 1 Johann Carl Friedrich Gauss was born on April 30, 1777 in Braunsweig, Germany. His father worked as a gardener, canal tender, and bricklayer. He was harsh with his sons and tried to thwart every opportunity for advancement that came their way. On the other hand, his mother, Dorothea Benz, expected great things of Carl and used her own sharp intellect and humorous good sense to help him realize his dreams. She lived with her son for the last 22 years of her life, and he would allow no one other than himself to wait on her after she went blind. She lived to be 97. It would be di‹cult to ˛nd a child more precocious than Gauss. He began showing signs of his genius before he was three. He amazed his early teachers when they learned he could sum the integers from 1 to 100 instantly by seeing that the sum was 50 pairs of numbers, each pair adding up to 101. He quickly went beyond the scope of his teacher. It was the schoolmaster’s assistant, Johann Martin Bartels, who developed a friendship with Gauss and led him into the mysteries of algebra. When Gauss
4
P100 from 1 to 100, i.e., j=1 j = 1 + 2 + ::: + 99 + 100 instantly at a very young age. In the same lecture, I showed how he did it. Pi Based on this same technique of summation, we can show that j=1 j = 1 + 2 + ::: + i = i(i + 1)=2. The basic idea of this type of summation, which is also known P as an arithmetic progression, is i shown in Figure 5. The sum S = j=1 j can be arranged in two di¸erent ways as shown in the ˛rst two rows in the ˛gure. If we add the top and bottom entries column by column, each sum turns out to be i, which is shown in the third row. The sum of all the elements of the third row, denoted by ST , is (i + 1)i because there are the number of i in the third row is i + 1. It is also clear that ST = S + S = 2S. Therefore, S = i(i + 1)=2. Coming back to the homework problem, we can de˛ne k = i(i + 1)=2+j. But, as before, we are one row ahead. To ˛x this problem, we replace i with i ` 1 in the expression for k. Therefore, k = i(i ` 1)=2 + j
3
Problem 1 of Homework 2
Please ˛nd the expression for k in terms of i, j, N according to the rule of rearrangement as shown in Figure 6b. Solution. The key observations of this problem are: 1. The number of elements along the diagonal is i + j ` 1,see Figure 7. 2. The total number of elements starting from the ˛rst element of the ˛rst diagonal array to the last element of the corresponding diagonal array whose row number is i is m(m + 1)=2 where m = i + j ` 1. 3. The total number of elements starting from the ˛rst element of the ˛rst diagonal array to the ˛rst element of the corresponding diagonal array whose row number is i is m(m + 1)=2 ` (m ` 1) where m = i + j ` 1. was 14, Bartels introduced him to Carl Wilhelm Ferdinand, the Duke of Brunswick. The Duke was so taken with this shy, awkward boy that he agreed to pay for his education. At the age of 18, Gauss entered the University of G ottingen and could not decide whether to pursue his love of languages or mathematics. His discovery of the polygon of 17 sides was the impetus that pushed him into mathematics.[1]
5
N=number of columns
(2,1) (2,2)
... ...
(1,N)
... (2,N-1)
f ...
...
N=number of rows
(1,1) (1,2)
... ...
(1,1) (1,2) (2,1) (1,3) (2,2) (3,1)
...
1
2
3
4
5
(N,1)
6
(N-1,1) (N-1,2) (N,1)
FIGURE 6a
FIGURE 6b
Figure 6: Another mapping of two indices into a single index. The number of elements in each diagonal array. 1 (1,1)
(1,2)
(2,1)
(2,2)
2
3
...
...
...
(1,N)
(2,N-1)
...
...
...
(N-1,1) (N-1,2) (N,1) FIGURE 7
Figure 7: Another mapping of two indices into a single index. To ˛nd the expression for k, what we need to accomplish is to be able to iterate (or move a step by step) from Observation 3 to Observation 2. In other words, we want k, (1)
k=
(i + j)(i + j ` 1) ` (i + j ` 2) 2
to approach (2)
k=
(i + j)(i + j ` 1) 2
as (i; j) moves ‘along the diagonal array’ from the top right element to the bottom left element, e.g., from (1; 4) to (4; 1). Notice that i increases as we move diagonally from the top right element to the bottom left element. Therefore, we must add the
6
term, (i ` 1), onto the expression of k given in Eq.(1): k
= =
(i + j)(i + j ` 1) ` (i + j ` 2) + (i ` 1) 2 (i + j)(i + j ` 1) ` j + 1: 2
Interestingly, a variant of this problem was listed as the 4th example on the ˛rst page of a famous mathematics textbook by G. H. Hardy [2]. For your convenience, I have included this example in the footnote2 . The signi˛cance of this particular example is the fact that the set of all positive rational numbers can be mapped into the set of positive integers. Therefore, the set of rational numbers is countable even though it is in˛nite.
4
Homework 3
Construct a method called removeDuplicates. What this method does is to remove redundant data. For example, if one has the following array: ` ´ 2 1 2 6 6 7 2 1 8 ; then into:
removeDuplicates `
2
will transform the above array of data 1
6
7
´ 8 :
Note that order is not important. Below is a template for you to ˛ll in the implementation.
public static int[] removeDuplicates(int [] a){ }//end removeDuplicates Solution. 2 The
positive rational numbers may be arranged in the form of a simple series as
follows: 1 2 1 3 2 1 4 3 2 1 ; ; ; ; ; ; ; ; ; ;´´´ 1 1 2 1 2 3 1 2 3 4 Show that
p q
is the [ 12 (p + q ` 1)(p + q ` 2) + q]th term of the series.
7
public static int[] removeDuplicates(int[] a){ if( a==null || a.length==0 ) { throw new IllegalArgumentException("a is either null or has zero length"); } int len = a.length; int[] in = new int[len]; //assume everything is distinct for(int i=0; i
other sophisticated functions or methods except multiple for loops, a number of equality tests (using ==) and a simple summation. Note that the keyword ‘continue’ may be removed and the method will still work. Do you why it is placed there?
References [1] Meyer CD. Matrix Analysis and Applied Linear Algebra. SIAM; Philadelphia: 2000. [2] Hardy GH. A Course of Pure Mathematics. 10th Ed. Cambridge University Press; Cambridge: 1952
9