32
Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer

Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Data Structures in JavaLecture 5: Sequences and Series, Proofs

9/23/2015

1

Daniel Bauer

Page 2: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Algorithms• An algorithm is a clearly specified set of simple

instructions to be followed to solve a problem.

• Algorithm Analysis — Questions:

• Does the algorithm terminate?

• Does the algorithm solve the problem? (correctness)

• What resources does the algorithm use?

• Time / Space

2

Page 3: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Contents

1. Sequences and Series

2. Proofs

3

Page 4: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Sequences

• What are these sequences?

• 0, 2, 4, 6, 8, 10, …

• 2, 4, 8, 16, 32, 64, …

• 1, 1/2, 1/4, 1/8, 1/16, …

4

Page 5: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Sequences

• What are these sequences?

• 0, 2, 4, 6, 8, 10, …

• 2, 4, 8, 16, 32, 64, …

• 1, 1/2, 1/4, 1/8, 1/16, …

Arithmetic Sequence

4

Page 6: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Sequences

• What are these sequences?

• 0, 2, 4, 6, 8, 10, …

• 2, 4, 8, 16, 32, 64, …

• 1, 1/2, 1/4, 1/8, 1/16, …

Arithmetic Sequence

Geometric Sequence

4

Page 7: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Arithmetic Series

5

Page 8: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Arithmetic Series• Arithmetic Sequence of length N, with start term

a and common difference d.

5

Page 9: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Arithmetic Series• Arithmetic Sequence of length N, with start term

a and common difference d.

• Series: The sum of all elements of a sequence.

5

Page 10: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Sum-Formulas for Arithmetic Series

• In particular (for a=1 and d=1):

6

Page 11: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Geometric Series

7

Page 12: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Geometric Series• Geometric Sequence with start term s and

common ratio A.

7

Page 13: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Geometric Series• Geometric Sequence with start term s and

common ratio A.

• Geometric Series:

7

Page 14: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Geometric Series• Geometric Sequence with start term s and

common ratio A.

• Geometric Series:

• Often 0 < A < 1 or A = 2

7

Page 15: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Sum-Formulas for Finite Geometric Series

8

Page 16: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Sum-Formulas for Finite Geometric Series

• In particular, if s=1

8

Page 17: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Sum-Formulas for Finite Geometric Series

• In particular, if s=1

• In Computer Science we often have A = 2

8

Page 18: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Sum-Formulas for Infinite Geometric Series

only if 0<A<1

9

Page 19: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Sum-Formulas for Infinite Geometric Series

• In particular, if s=1

and

only if 0<A<1

9

Page 20: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Sum-Formulas for Infinite Geometric Series

• In particular, if s=1

• For instance,

and

only if 0<A<1

9

Page 21: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Analyzing the Towers of Hanoi Recurrence

10

base case: geometric series

Page 22: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

The End of The World

11

Legend says that, at the beginning of time, priests were given a puzzle with 64 golden disks. Once they finish moving all the

disks according to the rules, the wold is said to end.

If the priests move the disks at a rate of 1 disk/second, how long will it take to solve the puzzle?

Page 23: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

The End of The World

11

Legend says that, at the beginning of time, priests were given a puzzle with 64 golden disks. Once they finish moving all the

disks according to the rules, the wold is said to end.

If the priests move the disks at a rate of 1 disk/second, how long will it take to solve the puzzle?

Page 24: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

The End of The World

11

Legend says that, at the beginning of time, priests were given a puzzle with 64 golden disks. Once they finish moving all the

disks according to the rules, the wold is said to end.

If the priests move the disks at a rate of 1 disk/second, how long will it take to solve the puzzle?

secondsminutesdaysyears

Page 25: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Contents

1. Sequences and Series

2. Proofs

12

Page 26: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Types of Proofs

• Proof by Induction

• Proof by Contradiction

• Proof by Counterexample

13

Page 27: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Proofs by Induction• We are proving a theorem T. (“this property holds for all

cases.”).

• Step 1: Base case. We know that T holds (trivially) for some small value.

• Step 2: Inductive step:

• Inductive Hypothesis: Assume T holds for all cases up to some limit k.

• Show that T also holds for k+1.

• This proves that T holds for any k.

14

Page 28: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

• For the Fibonacci numbers, we prove that

• Base case:

• Inductive step:

• Assume the theorem holds for i = 1,2,…,k • We need to show that

Proof by Induction - Example

15

Page 29: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Proof by Induction - Inductive Step

• We know that and by the inductive hypothesis:

16

Page 30: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Proof by Counter-Example• We are proving that theorem T is false. (“this

property does not hold for all cases.”).

• It is sufficient to show that there is a case for which T does not hold.

• Example:

• Show that is false.

• There are i for which ,e.g.

17

Page 31: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Proof by Contradiction

• We want to proof that T is true.

• Step 1: Assume T is false.

• Step 2: Show that this assumption leads to a contradiction.

18

Page 32: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134... · Data Structures in Java Lecture 5: Sequences and Series, Proofs 9/23/2015 1 Daniel Bauer. Algorithms

Proof by Contradiction - Example

• We want to proof that there is an infinite number of primes.

• Assume the number of primes is finite and the largest prime it Pk .

• Let the sequence of all primes be P1, P2,,…,Pk

• Since N>Pk, N cannot be prime, so it must have a factorization into primes. Such a factorization cannot exist: dividing N by any P1, P2,,…,Pk will always leave a remainder of 1.

19