quiz4

CS 35 content builds throughout the semester, so you should make sure you are also reviewing previous study guides!

Data Structures and Invariants

You should be prepared to discuss the data structures we have created in class, the fields in their implementations, and the invariants we rely upon in order to write algorithms for those data structures.

  1. What is an ADT? What is the difference between an ADT and a data structure? Give two examples of each.

    An ADT is a description of how data can be accessed and manipulated. In other words, an ADT is an interface allowing the caller to store and retrieve data. A data structure is a concrete realization of an ADT; data structures give specific algorithms to implement the interface that the ADT describes.

    Two examples of ADTs are lists and stacks. Lists and stacks are both defined by a collection of routines that allow a user to store and manipulate data (e.g. insertFirst, getLast, etc. for list; pop, push, etc. for stack). Two examples of data structures are linked lists and array lists. Both linked lists and array lists are concrete implementations of the list ADT; each of them give implementations for the interface that the list ADT describes.

  2. What is the difference between an interface and an implementation?

    An interface is the publicly-accessible methods of a class definition, and it describes what parts of the class users are allowed to access. For example, we have seen many data structures with methods like getSize() and isEmpty() in their interfaces. The interface presents users wiht a clean way to use the class, without worrying about the private details of how the class is implemented.

    An implementation is all the private details of how a class methods and data fields are defined.

Inductive Proofs

You should be prepared to give inductive proofs for simple mathematical summations.

  1. Prove by induction that the following equality holds for all \(n\geq 1\): \(\sum\limits_{i=1}^{n} i \cdot 2^i = 2^{n+1}(n-1) + 2\).

    First, define \(P(n) \equiv \sum\limits_{i=1}^{n} i \cdot 2^i = 2^{n+1}(n-1) + 2\). We claim that \(P(n)\) is true for all \(n\geq 1\). The proof is by induction.

    The base case is \(P(1)\), that is to say \(\sum\limits_{i=1}^{1} i \cdot 2^i = 2^{1+1}(1-1) + 2\). Consider the left- and right-hand sides of this equality individually:

    On one hand, \(\sum_{i=1}^{1} i \cdot 2^i = 1\cdot 2 = 2\)

    On the other hand, \(2^{1+1}(1-1) + 2 = 2^2\cdot 0 + 2 = 2\)

    In this way, the left hand side and right hand side of the equation in \(P(1)\) are equal, so \(P(1)\) holds and the base case is proven.

    For the induction hypothesis, assume \(P(m)\) holds for all \(m \lt n\); that is, we assume for all \(m \lt n\) that \(\sum\limits_{i=1}^{m} i \cdot 2^i = 2^{m+1}(m-1) + 2\).

    For the induction step, we want to show that \(P(n)\) holds as well. Note that

    \(\sum_{i=1}^{n} i \cdot 2^i\)

    =

    \(n\cdot 2^n + \sum_{i=1}^{n-1} i \cdot 2^i\)

    =

    \(n\cdot 2^n + 2^{n-1+1}(n-1-1) + 2\)

    =

    \(n\cdot 2^n + 2^n(n-2) + 2\)

    =

    \(2n\cdot 2^n -2\cdot 2^n + 2\)

    =

    \(n\cdot 2^{n+1} -2\cdot 2^n + 2 \)

    =

    \(n\cdot 2^{n+1} - 2^{n+1} + 2\)

    =

    \(2^{n+1}(n-1) + 2 ,\)

    where the first equality comes from pulling out the last term in the summation, the second equality uses the induction hypothesis with \(m=n-1\), and the remaining inequalities come from simplifying the math. We’ve shown that \(P(n)\) also holds, hence by induction \(P(n)\) holds for all \(n\geq 1\).

  2. Prove by induction that for all \(n \geq 1\), we have \(\sum\limits_{i=1}^{n} \frac{3}{4^i} = 1 - (\frac{1}{4})^n\).

    Let \(P(n)\) be the statement \(\sum\limits_{i=1}^{n} \frac{3}{4^i} = 1 - (\frac{1}{4})^n\). We prove that \(P(n)\) holds for all \(n \geq 1\) by induction.

    The base case is \(P(1)\), that is to say \(\sum\limits_{i=1}^{1} \frac{3}{4^i} = 1 - (\frac{1}{4})^1\).

    Consider the left hand side and right hand side of this equality separately:

    On one hand, \(\sum\limits_{i=1}^{1} \frac{3}{4^i} = \frac{3}{4}\)

    On the other hand, \(1 - (\frac{1}{4})^1 = 1 - \frac{1}{4} = \frac{3}{4}\ .\)

    The left hand side and right hand side of the equation in \(P(1)\) are equal, so \(P(1)\) holds and the base case is proven.

    For the induction hypothesis, assume that \(P(m)\) holds for all \(m \lt n\).

    For the induction step, we want to show that \(P(n)\) holds as well. Note that

    \(\sum\limits_{i=1}^{n} \frac{3}{4^i}\)

    =

    \(\frac{3}{4^n} + \sum\limits_{i=1}^{n-1} \frac{3}{4^i}\)

    =

    \(\frac{3}{4^n} + 1 - \frac{1^{n-1}}{4^{n-1}}\)

    =

    \(\frac{3}{4^n} + 1 - \frac{1}{4^{n-1}}\)

    =

    \(1 + \frac{3}{4^n} - \frac{4}{4^{n}}\)

    =

    \(1 - \frac{1}{4^n}\) ,

    where the first equality comes from pulling out the last term in the summation, the second equality comes from using the induction hypothesis with \(m=n-1\), and the remaining equalities come from simplifying the math. We’ve shown that \(P(n)\) also holds, hence by induction \(P(n)\) holds for all \(n\geq 1\).