Tutorial 11 of CSCI2110 Number of Sequence & Recursion

24
Tutorial 11 of CSCI2110 Number of Sequence & Recursion Tutor: Zhou Hong ( 周周 ) [email protected]

description

Tutorial 11 of CSCI2110 Number of Sequence & Recursion. Tutor: Zhou Hong ( 周宏 ) [email protected]. Announcement. HW2 & HW3 has been marked, please pick them up. HW4 will be returned in next Monday . Announcement. - PowerPoint PPT Presentation

Transcript of Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Page 1: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Tutorial 11 of CSCI2110Number of Sequence & Recursion

Tutor: Zhou Hong (周宏 )[email protected]

Page 2: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

• HW2 & HW3 has been marked, please pick them up.

• HW4 will be returned in next Monday.

Announcement

Page 3: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

• In your homework, you should declare your cooperators or cite references, this will not affect your grades.– Note: write your OWN solutions, do NOT just copy!

• Fail to do so may be considered as plagiarism.– The same notation or the same term (e.g. “imaginary

women”) are clues.

Announcement

Page 4: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Outline

• Number of Sequence

• Setup Recurrence Relations

Page 5: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

NUMBER OF SEQUENCE

Page 6: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Warm Up Exercise

What’s the next number?• a1 = -3, a2 = -1, a3 = 1, a4 = 3, …

Answer: a5 = 5• a1 = 3, a2 = 6, a3 = 12, a4 = 24, …

Answer: a5 = 48

What about this one?

• a1=1, a2=11, a3=21, a4=1211, a5=111221, a6=312211,

a7=13112221, …

Answer: a8=1113213211

• This is just for fun = )

Page 7: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Warm Up Exercise (Cont.)

Write down the general formula of ai

• a1 = -3, a2 = -1, a3 = 1, a4 = 3, …Answer: ai = a1 + (i-1)d = -3 + 2(i-1) = 2i - 5

• a1 = 3, a2 = 6, a3 = 12, a4 = 24, …Answer: ai = a1 q(i-1) = 3 x 2(i-1)

Write down the closed form of the following partial sum ()• a1 = -3, a2 = -1, a3 = 1, a4 = 3, …, ai = 2i – 5, …

Arithmetic Sequence: • a1 = 3, a2 = 6, a3 = 12, a4 = 24, …, ai = 3 x 2(i-1), …

Geometric Sequence:

Page 8: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Warm Up Exercise (Cont.)

Evaluate the following summation (Hint: Telescoping Sum)

Answer:

Page 9: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Future Value

Given bank rate b unchanged, if we deposit $X now, how much will we have after n years?

Now After 1 Year

After 2 Years … After n Years

$X $X * b $X * b2 … $X * bn

Future Value: 1 dollar today will be worth bn dollars after n years.

Page 10: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Current Value

Given bank rate b unchanged, if we want to have $X after n years, how much should we deposit now?

Now … After n-2 Years After n-1 Years

After n Years

$X / bn … $X / b2 $X / b $X

Current Value: 1 dollar after n years is only worth 1 / bn dollars today.

Page 11: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Mortgage Loan

Suppose the bank rate is b and keeps unchanged. Now, we rise a mortgage loan of $X dollars to buy a house. We have to repay the loan in n years. What is the annual repayment $p. (assume repayment is made at the end of each year)

Page 12: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Solution 1: Consider Future Value

Year Amount to Be RepaidNow $X1 year $X * b - $p

2 years ($X * b – $p) * b – $p = $X * b2 – $p * b – $p

… …n years $X * bn – $p* bn-1 – … – $p * b – $p

$ 𝑋 ⋅𝑏𝑛−$𝑝∑𝑖=0

𝑛− 1

𝑏𝑖=0

In order to repay the loan in n years, we must have

Therefore, in each year, we have to repay

$ 𝑋 ⋅𝑏𝑛−$𝑝 1−𝑏𝑛

1−𝑏 =0⇔

$𝑝=$ 𝑋 𝑏𝑛(1−𝑏)1−𝑏𝑛

Page 13: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Solution 2: Consider Current Value

Year Current Value of Each Year’s Payment

1 year $p / b2

years $p / b2

… …n

years $p / bnThe key observation is that the total current value should equal to $X.

$ 𝑋=∑𝑖=1

𝑛

$𝑝⋅𝑟 𝑖=$𝑝 𝑟 (1−𝑟 𝑛)1−𝑟

Let ,

Therefore, in each year, we have to repay

$𝑝=$ 𝑋 1−𝑟𝑟 (1−𝑟𝑛)

=$ 𝑋 𝑏𝑛(1−𝑏)1−𝑏𝑛

Page 14: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

RECURSION

Disclaimer: Some of the slides in this section are taken from the slides by Chow Chi Wang, last year’s Tutor of CSCI2110.

Page 15: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Recursion

• Recursion is an important technique in computer science. The key idea is to reduce a problem into the similar problems in simpler cases.

• In this tutorial, we will focus on how to setup the recurrence relations. We will discuss solving recurrence relations in next week’s tutorial.

• Tip: After setting up a recurrence relation, remember to test it’s correctness by trying some base cases.

Page 16: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Fibonacci Variant

We have a single pair of rabbits (male and female) initially. Assume that:

– (a) the rabbit pairs are not fertile during their first month of life, but thereafter give birth to four new male/female pairs at the end of every month;

– (b) the rabbits will never die.

Let be the number of pairs of rabbits alive in the -th month. Find a recurrence relation for

Page 17: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Fibonacci Variant

Initiation

Month 1

Month 2

Month 3

Month 4

Key:Baby rabbit pair

Fertile rabbit pair

()

()

()

()

()

Page 18: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Fibonacci Variant

• Let be the number of baby rabbit pairs in the -th month.

• Let be the number of fertile rabbit pairs in the -th month.

Note that we have:,

where,,

Therefore: for , and .

Page 19: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

The Josephus Problem

Flavius Josephus is a Jewish historian living in the 1st century.

During the Jewish-Roman war, he and his 40 soldiers were trapped in a cave, the exit of which was blocked by Romans.

The soldiers chose suicide over capture and decided that they would form a circle and proceeding around it, to kill every third remaining person until no one was left.

However, Josephus wanted none of this suicide nonsense.

His task is to choose a place in the initial circle so that he is the last one remaining and so survive.

Page 20: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

The Josephus Problem in General Form

There are n people numbered 1 to n around a circle. Start with people #1, eliminate every j-th remaining people until only one survives. The task is to find the survivor’s initial number.

A simple case with n=10 and j=2:

The elimination order: 2, 4, 6, 8, 10, 3, 7, 1, 9.So, people #5 is the survivor.

Page 21: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

The Case of j=2

Let J(n) be the survivor’s number when start with n people.

If n is a even number, we haveJ(2k) = 2J(k) – 1, for k >= 1.

If n is a odd number, we haveJ(2k+1) = 2J(k) + 1, for k >= 1.

Base case, J(1) = 1.

Page 22: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

The General Value of j

Note that, the position of the i-th people in the initial circle is((i-1) mod n) + 1 , starting with people #1.

Consider eliminating people one by one. In the very beginning, the j-th people is eliminated, then we start the next count with the j+1-th people starting with people #1.

J(n-1) is the survivor’s number in the remaining circle of n-1 people, starting with the j+1-th people in the initial circle.

Then, the survivor is the (J(n-1)+j)-th people starting with people #1 in the initial circle.

Therefore, we have the following recurrence relationJ(1) = 1,J(n) = ((J(n-1)+j-1) mod n) + 1, for n >= 2.

Page 23: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Comparison of The Two Recurrence Relations

J(1) = 1,J(n) = ((J(n-1)+j-1) mod n) + 1, for n >= 2.

J(1) = 1,J(2k) = 2J(k) – 1, for k >= 1,J(2k+1) = 2J(k) + 1, for k >= 1.

V. S.

Page 24: Tutorial 11 of CSCI2110 Number of Sequence & Recursion

Thank You!

Q & A ?