© 2015 by Greg Ozbirn, UT-Dallas, For Use With Data Structures Book by Mark Allen Weiss 1 Spring 2015
© 2015 by Greg Ozbirn, UT-Dallas, For Use With Data Structures Book by Mark Allen Weiss 1 Spring 2015
Introduction
Spring 2015
Objectives
This chapter reviews some pre-requisite
material we will need.
It is assumed you have already learned
much of this material in previous courses.
Mathematics Review
Mathematics review
Exponents
Logarithms
Series
Proof techniques
Counterexample
Contradiction
Induction
3
Exponents
Rule
Example
XAXB = XA+B
XA / XB = XA-B
(XA)B = XAB
XN + XN = 2XN
2N + 2N = 2N+1
22 * 23 = 22+3 = 25
25 / 23 = 25 3 = 22
(22)3 = 26
32 + 32 = 2(32)
22 + 22 = 23
4
Logarithms
XA = B if and only if logXB = A
23 = 8
so log28 = 3
Logarithms
logX AB = logX A + logX B; A,B > 0
log10 (3*4) = log10 3 + log10 4
= .477 + .602
= 1.079
Logarithms
logX AB = B logX A
log10 34 = 4 log10 3 = 4(.477) = 1.908
Logarithms
Normally ln is base e, and log is base 10.
In our textbook, log is always base 2.
log 1 = 0
log 2 = 1
log 1024 = 10
log 1048576 = 20
log 1073741824 = 30
8
Series
Sum of 2i of first N integers:
N
2i
= 2N+1 - 1
i=0
Ai
= (AN+1 - 1) / A-1
i=0
Ai 1 / (1-A)
i=0
Series
Sum of first N integers:
N
= N(N+1)/2 N2/2
i=1
Proof by Counterexample
For proving a statement is false.
Simply show a case where it isnt true.
For example, consider the Fibonacci numbers.
Suppose it was stated that F(k) <= k2 . To prove
this is false, we need only consider one case, such
as F(11), which is 144, which is > 112 .
Here, we demonstrate the original statement is
false by giving an example where it is false (a
counterexample).
One such example is all that is needed to prove
this statement is false.
12
Proof by Contradiction
Assume theorem is false, then show how this assumption
leads to a conclusion that something which is known to be
true is false, hence the original false assumption cannot be
true, proving the theorem is true.
For example, prove that the sum of two even numbers is
always even.
Assume it is false: given even x and y, then x+y is odd.
If x+y is odd, then x+y = 2c+1.
But x=2a and y=2b means 2a+2b = 2c+1
So 2(a+b) = 2c + 1, but this says an even number equals
an odd number, which is impossible.
Therefore, the sum of two even numbers is even.
13
Induction
Steps:
1. Base case: Prove for the minimal case.
2. Inductive step:
a. Inductive hypothesis: Assume the theorem
holds for all cases up to some limit k.
b. Prove the next case: for example, k+1
3. Conclusion: by induction, the theorem holds for
all cases, i.e., the minimal case and all its
successors.
14
i = k(k+1)/2
i=1
i = i + (k+1)
i=1
i=1
=
=
=
=
=
k(k+1)/2 + k+1
k(k+1)/2 + 2(k+1)/2
(k(k+1)+2(k+1))/2
(k+1)(k+2)/2
(k+1)(k+1 + 1) / 2
15
Recursion
A function defined in terms of itself is
recursive.
Many math problems can be expressed this
way.
For example: xn = x * xn-1
x! = x * (x-1)!
We can see that these examples are
naturally recursive.
16
Recursion
Write a recursive function to compute powers of x.
public static int power(int x, int n)
{
if (n = = 0)
// base case
return 1;
else
return x * power(x, n-1); // recursive call
}
17
Recursion
public static int power(int x, int n)
{
if (n = = 0)
return 1;
else
return x * power(x, n-1);
}
power(5,3) = 5*power(5,2)
= 5 * (5 * power(5,1))
= 5 * (5 * (5 * power(5,0)))
= 5 * 5 * 5 * 1 = 125
18
Recursion
Consider this recursive
function:
public static void print(int n)
{
if (n >= 10)
print( n / 10);
printDigit( n % 10);
}
print(6371);
print(637)
print(63)
print(6)
printDigit(6%10);
printDigit(63%10);
printDigit(637%10);
printDigit(6371%10);
19
20
Recursion
Four basic rules when writing recursive routines:
Base case: which can be solved without recursion.
Making progress: each recursive call makes
progress towards the base case.
Design rule: assume recursive calls work without
tracing it all out.
Compound interest rule: dont duplicate work
already performed in a previous call.
21
Java Review
Most students already know how to
program in Java.
Prior to Java version 5, generic objects
simply used type Object, using casts as
appropriate.
Java 5 introduced Java Generics which
allows a type to be a parameter.
22
Generic Objects
Suppose we want to write a class that can
store some data, but we may wish to use it
with different types.
Prior to Java 5, we might do something like
the following slide shows:
23
24
25
Generic Objects
When retrieving an object from type Object,
it is necessary to use casting before using
the objects methods.
However, the compiler cant know whether
the cast is valid until runtime.
26
27
Autoboxing
Note in the previous example, wrapper
types (like Integer for int) were not
required.
This is a feature of Java 5 called
autoboxing which automatically inserts a
wrapper.
30
Diamond Operator
In Java 7, the declaration:
GenericMemoryCell<Integer> m =
new GenericMemoryCell<Integer> ( );
Array Types
If a Person class has a subclass of
Employee, then it is possible to write:
Person x = new Employee();
But what about arrays?
Person x[] = new Employee[5];
Arrays in Java are covariant, so the above
assignment works.
32
Array Types
Now consider this code if both Employee and
Student are subclasses of Person:
Person[] arr = new Employee[5];
arr[0] = new Student();
The compiler doesnt catch this since arrs type
is Person and Student is a Person.
But at runtime, this is an error since Student is
not an Employee.
33
Collections
What about a generic Collection, is it also
covariant?
For example, if a method has a parameter
which accepts collections of type Person,
could you pass it a collection of type Student?
No, because collections are not covariant.
The solution is to use wildcards.
34
Wildcards
Consider the following method header which accepts
collections of type Shape:
public static double area(Collection<Shape> arr)
This will only accept arguments that are collections of
Shape, but not Collections of subclasses of Shape.
Consider this method header:
public static double area(Collection<? extends Shape> arr)
This accepts arguments that are collections of Shape or
collections of any subclass of Shape.
For example, it would accept Collection<Circle> and
Collection<Square> if Circle and Square are subclasses of
Shape.
35
Generic Methods
Methods can have their own type parameters:
public static <AnyType> boolean
contains( AnyType [] arr, AnyType x )
This says that a method named contains has a
type parameter of AnyType. Its two parameters
use this type.
The data type for AnyType is inferred from the
arguments passed to it.
36
Type Bounds
Square implements Comparable<Shape>
In Java 5, the Comparable interface is now generic:
public interface Comparable<T>
Consider this method header:
public static <AnyType extends Comparable<AnyType>>
AnyType findMax(AnyType [] arr)
Type Bounds
Consider this method header:
public static <AnyType extends Comparable<? super AnyType>>
AnyType findMax(AnyType [] arr)
Type Erasure
Generic classes are seen by the compiler but
are converted to regular classes (called raw
classes) during compilation.
This process is known as type erasure.
There will only be one class for all generic
invocations.
One consequence of this is that static variables
of a generic class are shared by all instances,
regardless of the type parameter.
39
Restrictions
GenericMemoryCell<int> is illegal.
instanceof and typecasts work only with raw
types.
Cannot use classs type variable in static fields or
methods.
T obj = new T(); // illegal
T obj[] = new T[]; // also illegal
GenericMemoryCell<String> arr[] = new
GenericMemoryCell<String>[10]; // illegal
40
Comparators
A Comparator allows the comparison rule to
be separate from the object.
For example, rectangles can be compared in
many different ways.
Different comparators can be defined and
passed as arguments to routines that need to
compare them.
41
End of Slides
42