240 likes | 406 Views
Tutorial 11 of CSCI2110 Number of Sequence & Recursion. Tutor: Zhou Hong ( 周宏 ) hzhou@cse.cuhk.edu.hk. Announcement. HW2 & HW3 has been marked, please pick them up. HW4 will be returned in next Monday . Announcement.
E N D
Tutorial 11 of CSCI2110Number of Sequence & Recursion Tutor: Zhou Hong (周宏) hzhou@cse.cuhk.edu.hk
Announcement • HW2 & HW3 has been marked, please pick them up. • HW4 will be returned in next Monday.
Announcement • In your homework, you should declare your cooperators or cite references, this will not affect your grades. • Note: write your OWNsolutions, do NOTjust copy! • Fail to do so may be considered as plagiarism. • The same notation or the same term (e.g. “imaginary women”) are clues.
Outline • Number of Sequence • Setup Recurrence Relations
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 = )
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:
Warm Up Exercise (Cont.) • Evaluate the following summation (Hint: Telescoping Sum) • Answer:
Future Value Given bank rate b unchanged, if we deposit $X now, how much will we have after n years? Future Value: 1 dollar today will be worth bn dollars after n years.
Current Value Given bank rate b unchanged, if we want to have $X after n years, how much should we deposit now? Current Value: 1 dollar after n years is only worth 1 / bn dollars today.
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)
Solution 1: Consider Future Value In order to repay the loan in n years, we must have Therefore, in each year, we have to repay
Solution 2: Consider Current Value The key observation is that the total current value should equal to $X. Let, Therefore, in each year, we have to repay
Recursion Disclaimer: Some of the slides in this section are taken from the slides by Chow Chi Wang, last year’s Tutor of CSCI2110.
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.
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
Fibonacci Variant Initiation Month 1 Month 2 Month 3 Month 4 () () () () () Key: Baby rabbit pair Fertile rabbit pair
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 .
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.
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.
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 have J(2k) = 2J(k) – 1, for k >= 1. If n is a odd number, we have J(2k+1) = 2J(k) + 1, for k >= 1. Base case, J(1) = 1.
The General Value of j 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. Note that, the position of the i-th people in the initial circle is ((i-1) mod n) + 1 , starting with people #1. Therefore, we have the following recurrence relation J(1) = 1, J(n) = ((J(n-1)+j-1) mod n) + 1, for n >= 2.
Comparison of The Two Recurrence Relations J(1) = 1, J(n) = ((J(n-1)+j-1) mod n) + 1, for n >= 2. V. S. J(1) = 1, J(2k) = 2J(k) – 1, for k >= 1, J(2k+1) = 2J(k) + 1, for k >= 1.
Thank You! Q & A ?