Recursion and Recurrence
PhD Student
Spring 2018
Mostly practice problems and homework help.
But I want to review some linear data structures first.
Each element of the list is a container. Each container has a
\(head\)
\(null\)
\(tail\)
192
42
36
\(head\)
\(null\)
\(tail\)
192
42
36
We call this a "linked list" because it's list of "things" where each "thing" is linked to the next.
Is the better than an array? When would you use this instead of an array?
Each element of the list is a container. But now each container has a
\(head\)
\(null\)
\(tail\)
192
42
36
\(null\)
What does this allow you to do that Linked lists don't?
\(head\)
\(null\)
\(tail\)
192
42
36
\(null\)
What does this allow you to do that Linked lists don't?
What is the sacrifice?
Put a plate on (push), take a plate from the top (pop). Both are "easy" operations i.e. O(1).
How do we make a data structure for this?
\(top\)
\(null\)
This is just a linked list (without a tail)
\(top\)
\(null\)
How do you push?
How do you pop?
Easy to enter the queue, and easy to exit when it's your turn. Cutting in line is problematic, so is removing yourself from the middle.
How do we make a data structure for this?
\(front\)
\(back\)
This is just a linked list...
\(null\)
How do we add?
\(front\)
\(back\)
How do we remove?
When solving recurrence relations, you have a few options.
With guess and check, it's a good idea to look at some values for \(T(n)\), then guess an appropriate \(g(n)\) such that \(T(n) = O(g(n))\).
You then have to prove that this is true, which is often easy to do by induction.