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
and
.
- (a)
- List the elements of T.
- (b)
- List all subsets of S.
- (c)
- What is |S|? What is |T|?
- (d)
- What are
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)

- (b)
- T-S
- (c)
- S-T
Functions
- A function, f, is a mapping from one set, A, called the
domain , to another set, B, called the co-domain ,
that associates each element of A with a single
element of B.
We write this as

If
and f maps a to b, we write f(a) = b.
- The range of f is the set containing all
such that
f(a)=b for some
. - Special functions of interest
- Floor:
. This is the largest
integer less than or equal to x.
- Ceiling:
. This is the smallest
integer greater than or equal to x.
- Factorial: f(n) = n!. Note that the domain of this function is
the natural numbers.
- Exponentiation (base b): f(x) = bx. Rules for
manipulation:
- Logarithm (base b>0):
. This means,
if by = x. If b is unspecificed, 2 is assumed. Rules
for manipulation:
In-Class Problems on Functions
- 1.
- What are
- (a)
- 5!
- (b)
,
,
,
- (c)
,
,
?
- 2.
- What is the derivative of
? (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)

- (b)
- (c)

- 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)
.- (b)
. - (c)
. - (d)

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

Hint: use the technique given in class for proving that

Proofs
- A proof is a clear, logical demonstration of the truth of an
assertion based on previously known facts (established propositions,
lemmas and theorems).
- In writing a proof, the first step is to carefully determine
what the assertion says, including the meanings of all terms used.
For example, you can not prove that 2 is the only even prime number
without understanding what it means for a number to be ``even'' and to
be ``prime''.
Once you understand what is to be proved, the next step
is to write the actual proof. This is a bit of an art form, which we
will not have time to investigate thoroughly. But, ...
- Proof techniques we will explore:
- Direct proof. Starting from the assumptions -- e.g. n is
an even positive integer -- construct a series of deductions
leading to the claim -- e.g. n is not prime.
- Proof by example or counter-example. This is acceptable when
proving that there is a member of a set satisfying an assertion, or
when proving that not all members of a set satisify an assertion.
It is not acceptable when proving that all members of an infinite
set satisfy an assertion.
- Proof by contradiction. This is done by assuming that the
assertion to be proved is false and showing that the assumption
leads to a statement that must be both true and false (a
contradiction).
- Proof by induction. This technique is closely related to
data structure and algorithm analysis and is covered in detail below.
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
- Mathematical induction is used to prove statements true for all
integers greater than or equal a certain base value n0. Here are
three example statements we will prove in class using mathematical
induction:
-
for
and
. - n! < nn, for
. - A complete binary tree of height
contains exactly
2h+1 - 1 nodes.
- An inductive proof always has three main components:
- Basis case:
- Prove the statement is true for n=n0 (and
perhaps n=n0+1, etc.).
- Inductive hypothesis:
- Assume the statement is true for all
k,
.
- Induction step:
- Prove that for all
, the
inductive hypothesis implies that the statement is true for n.
- The principle of mathematical induction states that if the basis
case and induction step are both proved, then the statement holds for
all
. - Notes:
- At first proofs by induction appear to be circular, but in
fact they are not. Instead, think of a set of dominoes standing on
end: the basis case is the act of pushing over the first domino
(or two); the induction step is the relationship between the
positions of the dominoes showing that for every domino, if its
precedecessors fall over, it will fall over as well.
- Sometimes the inductive hypothesis is written
and the induction step proves that the inductive hypothesis shows
the statement to be true for n+1.
- At other times, the inductive step is to show for all
, if the statement is true for n then it is true for n+1.
Students more comfortable with this method of inductive proof
are welcome to use it here.
- Very often the inductive hypothesis is not written out explicitly.
- For completeness, here are written out versions of the three
example proofs by induction given in class.
-
for
and
.
- Basis case:
- When n=0, the summation reduces to a0 = 1,
and the right side of the equation is ( a0+1 - 1 ) / (a - 1) =
1. Since these are both 1, the basis case is proved.
- Induction step:
- For any given
, if the statement is true
for
, then
completing the induction step.
- n! < nn, for
.
- Basis case:
- For n=2, the left side of the inequality
evaluates to 2 and the right side evaluates 4. Since 2<4, the
basis case is established.
- Induction step:
- For n>2, if k! < kk for
then in particular
(n-1)! < (n-1)n-1.
Multiplying both sides of this inequality by n shows that

But, since n-1 < n (obviously),

So,
n! < nn.
- A complete binary tree of height
contains exactly
2h+1 - 1 nodes.
- Basis case:
- Any binary tree of height h=0 is complete and
has exactly one node. Since 20+1 - 1 = 2-1 = 1, the basis case
is established.
- Induction step:
- For h>0, assume any complete binary tree of
height k,
, contains 2k+1 - 1 nodes, and
consider any complete binary tree, T, of height h. Removing the
root of T creates two complete binary trees of height h-1, one
formed from the left subtree of T and the other formed from the
right. By the inductive hypothesis each of these has 2(h-1)+1 - 1
= 2h -1 nodes. Therefore, T had
(2h -1) + (2h-1) + 1
nodes to start with (the 1 at the end is for the root node). Adding
these values shows that T had
2h+1 - 1
nodes, which completes the proof.
Class Exercises on Induction
- 1.
- Prove each of the following by mathematical induction.
- (a)
for
.- (b)
- n < 2n for
.
- (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
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
. Let
and let
. 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
, horses
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
(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
,4 n + 4 < n2.
- 2.
- Give an inductive proof showing that for
all positive integers n,

- 3.
- Give an inductive proof showing that for
all
,

- 4.
- Evaluate the following summations.
- (a)

- (b)

- (c)

- (d)

- 5.
- Prove using mathematical induction that fn >
(3/2)n, for
. 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