7 Recursion
7 Recursion
Yves Lucet
CC BY-SA 3.0
https://en.wikipedia.org/wiki/Skip_list#/media/File:Skip_list_add_element-en.gif
"Hash table 3 1 1 0 1 0 0 SP" by Jorge Stolfi - Own work. Licensed under CC BY-SA 3.0 via Commons -
https://commons.wikimedia.org/wiki/File:Hash_table_3_1_1_0_1_0_0_SP.svg#/media/File:Hash_table_3_1_1_0_1_0_0_SP.svg
Wk Class Date Activity Reading/Prep given Peer
1 1 Sep 06 Syllabus, TBL, Java review 2
2 Sep 08 Git, testing Java, Generics, Testing
2 3 Sep 13 RAT1: generics; unit testing Complexity
4 Sep 15 Lect: Complexity, streams Lists
3 5 Sep 20 Build & Critic (training) iMAT1
6 Sep 22 tMAT1 Recursion
4 7 Sep 27 RAT2 Stack, Queue P eer 1
8 Sep 29 Build & Critic Iterators
5 9 Oct 04 mini-lecture+exercises iMAT2
10 Oct 06 tMAT2 BST, PQ, heap
6 11 Oct 11 RAT3 Hash, skip list P eer 2
12 Oct 13 Hash table, Skiplist, bottom-up
14.6heap
Shortest
construction
path
7 13 Oct 18 Dijsktra+adaptable PQ Union-find
14 Oct 20 Union-find/Disjoint sets iMAT3
8 15 Oct 25 tMAT3 Search Trees AVL/RB
16 Oct 27 Lecture BST, AVL, (2,4), RB B-Trees P eer 3
9 17 Nov 01 B-trees iMAT4
18 Nov 03 tMAT4
10 19 Nov 08 Midterm review
20 Nov 10 Midterm
11 Nov 15 Reading week
Nov 17 Reading week Text processing
12 21 Nov 22 Pattern matching KMP, BM, Trie
22 Nov 24 RAT4 Huffman coding
13 23 Nov 29 Huffman coding iMAT5
24 Dec 01 tMAT5
14 25 Dec 06 Review/Course Evaluation P eer 4
iPeer evaluation
This Photo by Unknown Author is licensed under
CC BY-SA
• Deadline in iPeer
• Penalty
– 1 day: 20%;
– 2 days: 50%;
– 3+ days: 100%.
3
4
Remember
• It is your job to check your grades weekly; any
mark you do not understand, should be brought to
my attention (or your TA for the lab) asap
• You may receive a 0 for a lab for a number of
reasons: does not compile, not submitted, not
submitted as per the instructions, etc. Check, ask
your TA, and follow instructions.
• Do NOT wait till Christmas!
• And be patient while I go through the backlog of
questions; all emails are stored and will be looked
at.
5
RAT
• iRAT2
– 2023: 52%; 2022: 53%; 2018: 59%; 2016: 52%;
2015: 53%
• tRAT2 (to be checked)
– 2023: 80%; 2022: 86%; 2018: 92%; 2016: 89%;
2015: 93%
7
Module 1: Labs
Practicing
https://cmps-people.ok.ubc.ca/ylucet/DS/Alg
orithms.html
10
Using Recursion
Using Recursion
11
Time Complexity
public static int factorial(int n)
if (n == 0)
1. Write recursive equation return 1;
T(n)=T(n-1)+1 else
return n * factorial(n – 1);
T(0)=1 }
2. Solve recursive equation
1. Maple
2. Master’s Theorem
3. Manually (Mathematical proof usually by induction)
24
Master’s Theorem
25
26
• In the future
– Compute the complexity of recursive
algorithms, at least in the known cases
27
• Example trace:
0, 8
0, 4 4, 4
0, 2 2, 2 4, 2 6, 2
0, 1 1, 1 2, 1 3, 1 4, 1 5, 1 6, 1 7, 1
Careful
static int[] A = IntStream.rangeClosed(1, 100).toArray();
Recursive Iterative
static int sumRecursive() { static int sumIterative() {
return int sum = 0;
sumRecursiveFromIndexToEnd(0); for (int i=0; i<A.length; i++) {
} sum += A[i];
}
return sum;
static int
}
sumRecursiveFromIndexToEnd(int
i) {
if (i==A.length - 1) return A[i];
else return A[i] + static int sumStream() {
sumRecursiveFromIndexToEnd(i+1); return Arrays.stream(A).sum();
} }
Depth-first search
• Visit all nodes in a graph
DFS-recursive(G, s):
mark s as visited
for all neighbours w of s in Graph G:
if w is not visited:
DFS-recursive(G, w)
https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/tutorial/
https://cmps-people.ok.ubc.ca/ylucet/DS/DFS.html
30
APPLICATION EXERCISE
31
Application:
inteDashboard
• Application exercise: Recursion
1. Write your code
2. Look at other people code
32
eGallery
34
Solution
Official solution
35
Readings
Do not forget
• your readings
• iPeer
37
Questions?