Professional Documents
Culture Documents
© 2013 by Greg Ozbirn, UT-Dallas, For Use With Data Structures Book by Mark Allen Weiss 1 Summer 2013
© 2013 by Greg Ozbirn, UT-Dallas, For Use With Data Structures Book by Mark Allen Weiss 1 Summer 2013
© 2013 by Greg Ozbirn, UT-Dallas, For Use With Data Structures Book by Mark Allen Weiss 1 Summer 2013
Introduction
2
Mathematics Review
Mathematics review
Exponents
Logarithms
Series
Proof techniques
Counterexample
Contradiction
Induction
3
Exponents
Rule Example
4
Logarithms
XA = B if and only if logXB = A
23 = 8 so log28 = 3
5
Logarithms
logX AB = logX A + logX B; A,B > 0
log10 (3*4) = log10 3 + log10 4
= .477 + .602
= 1.079
7
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
9
Sum of Ai of first N integers if 0<A<1:
N
Ai 1 / (1-A)
i=0
11
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
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
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 print(6371);
function: print(637)
print(63)
public static void print(int n) print(6)
{ printDigit(6%10);
if (n >= 10) printDigit(63%10);
print( n / 10); printDigit(637%10);
printDigit( n % 10); printDigit(6371%10);
}
19
Induction proof of previous function for n>=0:
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
public class MemoryCell
{
public Object read()
{ return storedValue; }
24
public class TestMemoryCell
{
public static void main(String [] args)
{
MemoryCell m = new MemoryCell ( );
m.write(new Integer(5));
Integer wval = (Integer) m.read();
int val = wval.intValue();
System.out.println(Cell contents: + val );
}
}
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
Using Java Generics
Java 5 added support for Generic types.
This allows the compiler to perform type-
checking at compile time.
Consider the new MemoryCell class using
Java Generics:
27
public class GenericMemoryCell<AnyType>
{
public AnyType read()
{ return storedValue; }
m.write( 37 );
int val = m.read();
System.out.println(Cell contents: + val );
}
}
29
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> ( );
can be written as:
GenericMemoryCell<Integer> m =
new GenericMemoryCell< > ( );
This is convenient since Integer has already been
specified, so it does not need typing again.
Note that it is not the same as omitting the < >.
31
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
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)
If Shape implements Comparable<Shape>, it is
accepted, but if Square extends Shape and
implements Comparable<Shape>, it does not.
37
Type Bounds
Consider this method header:
public static <AnyType extends Comparable<? super AnyType>>
AnyType findMax(AnyType [] arr)
38
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