Top Banner
© 2015 by Greg Ozbirn, UT-Dallas, for use with Data Structures book by Mark Allen Weiss 1 Chapter 1 Introduction Spring 2015
42

chap1

Jul 17, 2016

Download

Documents

maria_luz

Chapter 1 sldes ecs 3361
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: chap1

© 2015 by Greg Ozbirn, UT-Dallas, for use with Data Structures book by Mark Allen Weiss

1

Chapter 1

Introduction

Spring 2015

Page 2: chap1

2

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.

Page 3: chap1

3

Mathematics Review

• Mathematics review– Exponents– Logarithms– Series

• Proof techniques– Counterexample– Contradiction– Induction

Page 4: chap1

4

Exponents

Rule

• XAXB = XA+B

• XA / XB = XA-B

• (XA)B = XAB

• XN + XN = 2XN

• 2N + 2N = 2N+1

Example

• 22 * 23 = 22+3 = 25

• 25 / 23 = 25 – 3 = 22

• (22)3 = 26

• 32 + 32 = 2(32)• 22 + 22 = 23

Page 5: chap1

5

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

Page 6: chap1

6

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

Page 7: chap1

7

Logarithms

• logX AB = B logX A log10 34 = 4 log10 3 = 4(.477) = 1.908

• log2 X < X for all X > 0

Page 8: chap1

8

Logarithms

• Normally ln is base e, and log is base 10.• In our textbook, log is always base 2.

log 1 = 0log 2 = 1log 1024 = 10log 1048576 = 20log 1073741824 = 30

Page 9: chap1

9

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

Page 10: chap1

10

• 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)

Page 11: chap1

11

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

Page 12: chap1

12

Proof by Counterexample• For proving a statement is false.• Simply show a case where it isn’t 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.

Page 13: chap1

13

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.

Page 14: chap1

14

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+13. Conclusion: by induction, the theorem holds for

all cases, i.e., the minimal case and all its successors.

Page 15: chap1

15

nProve Σ 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 = 1So 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

Page 16: chap1

16

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.

Page 17: chap1

17

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}

Page 18: chap1

18

Recursionpublic 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

Page 19: chap1

19

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

Page 20: chap1

• 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

Page 21: chap1

21

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: don’t duplicate work

already performed in a previous call.

Page 22: chap1

22

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.

Page 23: chap1

23

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:

Page 24: chap1

24

public class MemoryCell{ public Object read() { return storedValue; }

public void write(Object x) { storedValue = x; }

private Object storedValue;}

Page 25: chap1

25

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

Page 26: chap1

26

Generic Objects

• When retrieving an object from type Object, it is necessary to use casting before using the object’s methods.

• However, the compiler can’t know whether the cast is valid until runtime.

Page 27: chap1

27

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:

Page 28: chap1

28

public class GenericMemoryCell<AnyType>{ public AnyType read() { return storedValue; }

public void write(AnyType x) { storedValue = x; }

private AnyType storedValue;}

Page 29: chap1

29

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.

Page 30: chap1

30

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.

Page 31: chap1

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

Page 32: chap1

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

Page 33: chap1

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 doesn’t catch this since arr’s type is Person and Student is a Person.

• But at runtime, this is an error since Student is not an Employee.

33

Page 34: chap1

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

Page 35: chap1

35

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.

Page 36: chap1

36

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.

Page 37: chap1

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)

• If Shape implements Comparable<Shape>, it is accepted, but if Square extends Shape and implements Comparable<Shape>, it does not.

37

Page 38: chap1

38

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.

Page 39: chap1

39

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.

Page 40: chap1

40

Restrictions

• GenericMemoryCell<int> is illegal.• instanceof and typecasts work only with raw

types.• Cannot use class’s 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

Page 41: chap1

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

Page 42: chap1

42

End of Slides