1 / 24

Tutorial 11 of CSCI2110 Number of Sequence & Recursion

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.

joie
Download Presentation

Tutorial 11 of CSCI2110 Number of Sequence & Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial 11 of CSCI2110Number of Sequence & Recursion Tutor: Zhou Hong (周宏) hzhou@cse.cuhk.edu.hk

  2. Announcement • HW2 & HW3 has been marked, please pick them up. • HW4 will be returned in next Monday.

  3. 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.

  4. Outline • Number of Sequence • Setup Recurrence Relations

  5. Number of Sequence

  6. 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 = )

  7. 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:

  8. Warm Up Exercise (Cont.) • Evaluate the following summation (Hint: Telescoping Sum) • Answer:

  9. 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.

  10. 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.

  11. 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)

  12. 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

  13. 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

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

  15. 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.

  16. 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

  17. Fibonacci Variant Initiation Month 1 Month 2 Month 3 Month 4 () () () () () Key: Baby rabbit pair Fertile rabbit pair

  18. 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 .

  19. 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.

  20. 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.

  21. 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.

  22. 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.

  23. 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.

  24. Thank You! Q & A ?

More Related