© 2013 by Greg Ozbirn, UT-Dallas, For Use With Data Structures Book by Mark Allen Weiss 1 Summer 2013

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 42

Chapter 1

Introduction

2013 by Greg Ozbirn, UT-Dallas, for use with Data 1


Summer 2013 Structures book by Mark Allen Weiss
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.

2
Mathematics Review
Mathematics review
Exponents
Logarithms
Series
Proof techniques
Counterexample
Contradiction
Induction
3
Exponents
Rule Example

XAXB = XA+B 22 * 23 = 22+3 = 25


XA / XB = XA-B 25 / 23 = 25 3 = 22
(XA)B = XAB (22)3 = 26
XN + XN = 2XN 32 + 32 = 2(32)
2N + 2N = 2N+1 22 + 22 = 23

4
Logarithms
XA = B if and only if logXB = A
23 = 8 so log28 = 3

logAB = logCB / logCA; A,B,C > 0, A 1


log216 = log1016 / log102
= 1.204 / .301
=4

5
Logarithms
logX AB = logX A + logX B; A,B > 0
log10 (3*4) = log10 3 + log10 4
= .477 + .602
= 1.079

logX A/B = logX A logX B


log10 4/3 = log10 4 log10 3
= .602 - .477
= .125
6
Logarithms
logX AB = B logX A
log10 34 = 4 log10 3 = 4(.477) = 1.908

log2 X < X for all X > 0

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

Sum of Ai of first N integers:


N
Ai = (AN+1 - 1) / A-1
i=0

9
Sum of Ai of first N integers if 0<A<1:
N
Ai 1 / (1-A)
i=0

As N , the sum approaches 1/(1-A).


Proof:
S = 1 + A + A2 + A3 + A4 + A5 +
Multiply through by A:
AS = A + A2 + A3 + A4 + A5 +
Subtract AS from S (permitted if convergent):
S AS = 1
So S = 1/(1-A) 10
Series
Sum of first N integers:
N
i = N(N+1)/2 N2/2
i=1

To find the sum of 2 + 5 + 8 + + 3k-1


Write as: 3(1+2+3++k) (1+1+1++1)
= 3(k(k+1)/2) k

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

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
n
Prove i = 1+2+3++n = n(n+1)/2 for all n >= 1
i=1

Base case: n=1, the sum is 1, and 1(1+1)/2 = 1(2)/2 = 1


So it is true for n=1
Inductive Step:
Assume true for k.
k

i = k(k+1)/2
i=1

Show true for k+1:


k+1 k

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

Conclusion: by induction the statement holds true for all n >= 1 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 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:

Base case: if n is one digit, it is printed correctly.


Inductive hypothesis: assume function prints correctly for
numbers of k or fewer digits.
For k+1 digits, there are k digits and the least significant
digit. The first k digits are exactly n/10 which are
correctly printed according to the inductive hypothesis,
and the least significant digit is n%10, which again prints
correctly according to the inductive hypothesis, so any k+1
digit number prints correctly.
Therefore the function is correct.

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; }

public void write(Object x)


{ storedValue = x; }

private Object 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; }

public void write(AnyType x)


{ storedValue = x; }

private AnyType storedValue;


}
28
public class TestGenericMemoryCell
{
public static void main(String [] args)
{
GenericMemoryCell<Integer> m =
new GenericMemoryCell<Integer> ( );

m.write( 37 );
int val = m.read();
System.out.println(Cell contents: + val );
}
}

Note: the cast on the value read() returns is not


needed since the compiler knows it is an Integer.

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)

Now if Square extends Shape and


implements Comparable<Shape>, it is
accepted, since Shape is a superclass of
Square.

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

You might also like