CPSC 331: Tutorial 4

Recursion and Recurrence

PhD Student

Spring 2018

Today

Mostly practice problems and homework help.

But I want to review some linear data structures first.

Linked List

Each element of the list is a container. Each container has a

  • Reference to some data
  • Reference to the next (or perhaps previous) element

\(head\)

\(null\)

\(tail\)

192

42

36

Linked List

\(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?

Doubly Linked List

Each element of the list is a container. But now each  container has a

  • Reference to some data
  • Reference to the next AND previous element

\(head\)

\(null\)

\(tail\)

192

42

36

\(null\)

What does this allow you to do that Linked lists don't?

Doubly Linked List

\(head\)

\(null\)

\(tail\)

192

42

36

\(null\)

What does this allow you to do that Linked lists don't?

What is the sacrifice?

Stacks

Put a plate on (push), take a plate from the top (pop). Both are "easy" operations i.e. O(1). 

Stacks

How do we make a data structure for this?

  • I need to know what's on the top of the stack...
  • I need to have easy access to the element after the top
  • And this is true for all elements of the stack

\(top\)

\(null\)

This is just a linked list (without a tail)

Stacks

\(top\)

\(null\)

How do you push?

How do you pop?

Queues

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.

Queues

How do we make a data structure for this?

  • Need to keep track of the front and back of the queue (easy insertion and deletion)
  • If I remove someone, I need to know who the next person in line is

\(front\)

\(back\)

This is just a linked list...

\(null\)

Queues

How do we add?

\(front\)

\(back\)

How do we remove?

Recursion

When solving recurrence relations, you have a few options.

  • Substitution
  • Guess n' check

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.

CPSC 331: Tutorial 4

By Joshua Horacsek

CPSC 331: Tutorial 4

  • 1,026