Data Structures and Algorithms -- CSCI 230
Discrete Mathematics Overview

Introduction


During the first three weeks of the semester we will cover some techniques from discrete mathematics relevant to computer science in general and algorithm analysis in particular. This material is somewhat more extensive than offered in Chapter 1 of the Weiss text. It does not even begin to substitute for a course in discrete mathematics, however. Students, especially those interested in graduate school in computer science, should consider taking a separate discrete mathematics course during their undergraduate careers.

Sets


In-class problems


Work on these problems briefly. Most should be straightforward. We will discuss the solutions in class.

1.
Given $S = \{ a, b\}$ and $T = \{0, a, \{1,2\} \}$.
(a)
List the elements of T.
(b)
List all subsets of S.
(c)
What is |S|? What is |T|?
(d)
What are $ S \cap T $ and S - T?

2.
Let S be the set of items on your shopping list and let T be the set of items you actually bought during a trip to the store. Describe each of the following in English.
(a)
$ S \cap T $
(b)
T-S
(c)
S-T

Functions


In-Class Problems on Functions


1.
What are
(a)
5!
(b)
$\lceil 2 \rceil$, $\lceil 2.1 \rceil$, $\lceil -2.1 \rceil$,
(c)
$\lfloor 2 \rfloor$, $\lfloor 2.1 \rfloor$, $\lfloor -2.1
 \rfloor$?

2.
What is the derivative of $\log_b x$? (Hint: recall that the derivative of the natural logarithm is 1/x.)

Summations


Problems on summations


1.
Write each of the following as a summation.
(a)
$5 + 10 + 15 + 20 + \ldots + 1000$
(b)
$3 + 8 + 13 + 18 + \ldots + 98$
(c)
$4 + 8 + 16 + 32 + \ldots + 1024$

2.
Derive a formula for each of the following that does not involve a summation and does not involve i. Use the rules for manipulation and the special formulas.
(a)
$\displaystyle \sum_{i=1}^n 4i$.
(b)
$\displaystyle \sum_{i=3}^n (5 i + 4) $.
(c)
$\displaystyle \sum_{j=0}^k (4^j + k)$.
(d)
$\displaystyle \sum_{i=0}^N (2 + \sum_{j=0}^i j )$

3.
(Extra challenge!) Derive a formula for the following that does not involve a summation:

\begin{displaymath}
\sum_{i=1}^N \frac{i}{2^i}.\end{displaymath}

Hint: use the technique given in class for proving that

\begin{displaymath}
\sum_{i=0}^n a^i = \frac{a^{n+1} - 1}{a - 1}. \end{displaymath}

Proofs


In-Class Exercises on Proofs


1.
Is it true that n3 > 2n for all integers n>1? Prove your result.
2.
Prove that 2 is the only even prime number.

Proof by Induction


Class Exercises on Induction


1.
Prove each of the following by mathematical induction.
(a)
$\displaystyle \sum_{i=1}^n 2i-1 = n^2$ for $n\geq 1$.
(b)
n < 2n for $n \geq 0$.

(c)
Any set containing n elements has 2n different possible subsets. (Hint: in the inductive step, consider a particular element a of S and count all the subsets that do contain a and all those that do not.)

2.
What is wrong with the following inductive proof showing that all horses are the same color? (Or, more specifically, in any set H of n horses, each horse in H has the same color as every other horse in H.)

Basis:
n=1. One horse is trivially the same color as itself.
Induction Step:
We have to prove for each $n \geq 2$ that if in all sets containing fewer than n horses all horses are the same color, then in any set of n horses all horses are the same color. Let H be any set containing n horses, and label the horses $h_1,
h_2, \ldots, h_{n}$. Let $H_1 = H - \{ h_{n} \}$ and let $H_2 = H -
\{ h_{1} \}$. By the inductive hypothesis, since |H1| = n-1, all horses in H1 are the same color; call it c1. Also by the inductive hypothesis, since |H2| = n-1, all horses in H2 are the same color; call it c2. Also, since $H_1 \cap H_2 = \{ h_2,
\ldots, h_{n-1} \}$, horses $h_2, \ldots, h_{n-1} $ are each both colors c1 and c2. Hence, c1=c2. Therefore, all horses in H are the same color.

Recursion


Here is an outline of five steps I find useful in writing and debugging recursive functions:

1.
Handle the base case(s) first, at the start of the function.
2.
Define the problem solution in terms of smaller instances of the problem. This defines the necessary recursive calls.

3.
Figure out what work needs to be done before making the recursive call(s).

4.
Figure out what work needs to be done after the recursive call(s) complete(s) to finish the solution.

5.
Assume the recursive calls work correctly, but make sure they are progressing toward the base case(s)!

Here are two example recursive functions: factorial and mergesort.

    int Fact( int n )
    {
      if ( n == 0 || n == 1 ) 
        return 1;
      else
        return n * Fact( n-1 );
    }

    template <class T>
    void MergeSort( T * pts, int n )
    {
      MergeSort( pts, 0, n-1 );
    }

    templage <class T>
    void MergeSort( T * pts, int low, int high )
    {
      if ( low == high ) return;

      int mid = (low + high) / 2;
      MergeSort( T, low, mid );
      MergeSort( T, mid+1, high );

      //  At this point the lower and upper halves
      //  of "pts" are sorted. All that remains is
      //  to merge them into a single sorted list.
      //  We will discuss the details of this later.
    }

Class Exercises on Recursion


1.
Assume the following structure for a node in a binary tree:
    struct TreeNode {
      int value;
      TreeNode * left_child; 
      TreeNode * right_child;
    };
Write a recursive function to count the number of nodes in the binary tree whose root is pointed to be T. Note the structural similarity between your solution and the inductive proof about complete binary trees.
2.
The following is a recursive version of binary search. Will it work correctly? Why or why not?
    template <class T>
    int RBinSearch ( T * pts, int low, int high, T point, int& loc )
    {
      if ( high == low ) {
        loc = low;
        return pts[loc] == point;
      }
      int mid = (low + high) / 2;
      if ( pts[mid] <= point ) 
        return RBinSearch( pts, mid, high, point, loc );
      else
        return RBinSearch( pts, low, mid-1, point, loc );
    }

3.
The Fibonacci numbers are defined recursively as
\begin{align*}
f_0 &= 0 \\ f_1 &= 1 \\ f_n &= f_{n-1} + f_{n-2}, \qquad n \geq 2\end{align*}
(The notation fn means the same thing as f(n), i.e. f is a function of n.)
(a)
Write a recursive function to calculate fn.
(b)
How many recursive calls will your function make to calculate f3, f4, f5, etc? Can you write a recursive expression similar to the recursive formula for the Fibonacci numbers to count the number of recursive calls?

Review Problems


Here are a few review problems which have appeared on homeworks or tests in previous semesters. Practice writing solutions carefully and then compare to solutions provided on-line. If you can solve these problems and the problems we worked on in class then you are ready for the chapter quiz!

1.
Give an inductive proof showing that for all integers $n \geq 5$,4 n + 4 < n2.
2.
Give an inductive proof showing that for all positive integers n,

\begin{displaymath}
\sum_{i=1}^n \frac{1}{(2i-1)(2i+1)} = \frac{n}{2n+1}.\end{displaymath}

3.
Give an inductive proof showing that for all $n\geq 1$,

\begin{displaymath}
\sum_{i=1}^n i(i!) = (n+1)! - 1\end{displaymath}

4.
Evaluate the following summations.

(a)
$\displaystyle \sum_{i=0}^{100} -1^i$
(b)
$\displaystyle \sum_{i=5}^{2n} i$

(c)
$\displaystyle \sum_{i=0}^n ( a^i - 2 i + n ) $

(d)
$\displaystyle \sum_{i=0}^{n-1}\bigl( c + \sum_{j=0}^{i-1} (j+2) \bigr)$

5.
Prove using mathematical induction that fn > (3/2)n, for $n \geq 5$. Here, fn is the nth Fibonacci number. Use the definition of the Fibonacci numbers given in the text. Study the example inductive proof on page 6 carefully.

6.
Write the code necessary to accomplish the merging step in MergeSort. This code should follow the comments in the main MergeSort function.



 

Charles Stewart
8/27/1998