Tutorial 1 C++ Programming

advertisement
Tutorial 6
Recursion
Beautiful Recursion: Fractal
• Koch Snowflake
• Sierpinski Triangle
http://en.wikipedia.org/wiki/Fractal
• Recursive Tree
More Examples
• Droste Effect
http://en.wikipedia.org/wiki/Droste_effect
• Russian Doll
http://en.wikipedia.org/wiki/Matryoshka_doll
• Recursive Acronyms
– PHP: PHP Hypertext Preprocessor
– GNU: GNU’s Not Unix
– VISA: VISA International Service Association
http://en.wikipedia.org/wiki/Recursive_acronym
• A “Definition” in an English-English dictionary
– Recursion
• See “Recursion”.
• If you are tired, stop.
Recursion: Basic Idea
• Template for ANY recursion:
Recursive_Function_Name(Parameter_List)
// you will always see “selection statement” (if, switch, etc)
if (Base Case)
do something simple
else // Recursive Case
Recursive_Function_Name(Modified Parameter_List);
• Modified Parameter_List must bring the recursive function closer to
the base case (simplified problem).
Example
How many ways to choose k items out of n items?
•
How many ways to choose when k < 0, e.g. (k=-1) out of (n=3)?
–
•
How many ways to choose when k > n, e.g. (k=4) out of (n=3)?
–
•
1, do not take anything
How many ways to choose in general case, e.g. k < n, k > 0: (k=2) out of (n=3)?
–
–
•
1, take all
How many ways to choose when k = 0, e.g. (k=0) out of (n=3)?
–
•
0, also undefined, impossible…
How many ways to choose when k == n, e.g. (k=3) out of (n=3)?
–
•
0, actually this problem is undefined for k < 0…, such case will never exist.
Either I take one item X,
• My problem becomes choosing k-1 (k=1) out of n-1 (n=2)
Plus if I do not take that one item X
• My problem becomes choosing k (k=2) out of n-1 (n=2)
Code it:
int c(int n,int k) {
if (k>n) // assume k<0 is undefined
return 0;
if (k == n || k == 0)
return 1;
return c(n-1,k-1) + c(n-1,k);
}
Recursion is Powerful
• Divide and Conquer
• Dynamic Programming
• Used in future lectures in CS1102:
– Sorting: Mergesort and Quicksort are recursive
– Tree/Heap/Graph: these data structures and operations are naturally recursive
• Can also be used for past CS1102 materials:
– Linked List data structure and operations are also naturally recursive
Student Presentation
•
Gr3 Main
1.
2.
3.
4.
•
Cao Hoang Dang
Chng Jiajie
Chen Tianni Rebecca
Huang Chuanxian
Gr4 Main
1.
2.
3.
4.
Tan Peck Luan
Li Yawen
Wang Kang
Wong Suet Teng, M
Backup
•
Cai Jingfang, Du Xin
Ding Ying Shiaun
Jashan Deep Kaur
Leow Wei Jie, Lim Wei Hong
Backup
Tan Shu Yu Cynthia
Tan Miang Yeow
Ahmed Shafeeq
Chong Tze Koi
Gr5 Main
1.
2.
3.
4.
•
Wu Shujun
Teo Sim Yee, Stephanie
Wang Ruohan
Liu Na
Gr6 Main
1.
2.
3.
4.
Rasheilla Bte Rajah
Lou Wei Chen Gerard J
Zhang Chao
Gan Zhi Wei James
Backup
Joyeeta Biswas
Tan Yan Hao
Zheng Yang
Zhang Denan
Backup
Tan Ping Yang
Koh Jye Yiing
Wong Shiang Ker
Kuganeswari D/O K
Overview of the questions:
1. Trace IsEven/IsOdd
2. Recursive counting
3. Recursive sort
4. Reversing BasicLinkedList
The rabbit question is your homework…
7
Q1: Trace
boolean isEven(int number) {
if (number == 0)
return true;
else
return isOdd(number-1);
}
• Trace: isEven(5)!
• Trace: isOdd(1), isEven(2)!
–
–
• Are these two functions
recursive?
–
boolean isOdd(int number) {
if (number == 0)
return false;
else
return isEven(number-1);
}
There are repeated sub-problems!
You will learn how to optimize this situation in
more advanced modules!
http://en.wikipedia.org/wiki/Mutual_recursion
• In what situation(s) these two
functions may fail?
–
Negative numbers
• How to improve these codes to
address the situation above?
–
If input is negative, multiply it with -1 first.
Q2: Recursive Counting
• Adam can climb:
– 1 stair per step, or
– 2 stairs per step
• Given n stairs,
Count how many ways
Adam can go up
From stair 0 (ground) to stair n!
• n=4
1.
2.
3.
4.
5.
1+1+1+1
1+1+2
1+2+1
2+1+1
2+2
• Thinking steps:
– Let steps(n): the number of ways
– Start from small cases
•
•
•
•
•
steps(0), not defined, assume n>0
steps(1), 1 way (1 step)
steps(2), 2 ways (1+1 or 2)
steps(3), 3 ways
(1+1+1, 2+1, or
1+2)
steps(4), 5 ways
(1+1+1+1, 2+1+1, 1+2+1, or
1+1+2, 2+2)
– Feel that this problem is recursive!
– Go backwards!
•
•
•
•
There are two ways to reach stair n
From stair n-2, then Adam +2 stairs, or
From stair n-1, then Adam +1 stair
Naturally:
– steps(n) = steps(n-1)+steps(n-2)
Q3: Recursive Sort
• Many options
• We will learn this again in
details in Lecture 6 (Sorting)
• Possible answers:
–
–
–
–
Recursive bubble sort
Recursive selection sort
Recursive merge sort
Recursive quick sort
• Recursive selection sort:
sort(array, size)
if (size == 1)
return; // done (base case)
pass through the array one time to
get the maximum item M;
swap M with array[size-1];
call sort(array, size-1); // recursive
Q4: Reversing BLL Recursively
• Original BLL:
– ABCD
– Head is A
• Possible answer:
node reverse(node n) {
node newhead;
if (n.next != null) {
newhead = reverse(n.next);
n.next.next = n; // change arrow!
n.next = NULL;
}
else // base case, this is old tail!
newhead = n; // now: new head
return newhead;
• Reversed BLL:
– ABCD
– Head is D
• Given a BLL,
reverse it, recursively!
}
Call with:
head = reverse(head);
Food For Thought
•
•
•
•
•
If you are asked to design a recursive function to model your life while
taking CS1102, what kind of function that you will design?
void CS1102_Life(int week_ID) {
do_tutorial_and_lab(week_ID);
if (next_week)
CS1102_Life(week_ID+1);
}
Wrong, this is infinite…
void CS1102_Life(int week_ID) {
do_tutorial_and_lab(week_ID);
if (week_ID != end of semester)
CS1102_Life(week_ID+1);
}
It is going to be over 
Midterm Test
• If you want to know the truth…
• Visit my website
• I have scanned my answers and upload the PDF there.
Download