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 \(\sum\limits_{i=1}^{n} i \cdot 2^i = 2^{n+1}(n-1) + 2\).

    We have \(P(n) \equiv \sum\limits_{i=1}^{n} i \cdot 2^i = 2^{n+1}(n-1) + 2\).

    The base case, \(P(1)\), that is to say \(\sum\limits_{i=1}^{1} i \cdot 2^i = 2^{1+1}(1-1) + 2\). We reduce this as follows:

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

    =

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

    \(1 \cdot 2^1\)

    =

    \(2^{2}(0) + 2\)

    \(2\)

    =

    \(0 + 2\)

    For the inductive hypothesis, we assume \(P(k)\), that is, we assume that \(\sum\limits_{i=1}^{k} i \cdot 2^i = 2^{k+1}(k-1) + 2\).

    For the inductive case, we show that \(P(k)\) implies \(P(k+1)\) for positive \(k\).

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

    =

    \(\left(\sum_{i=1}^{k} i \cdot 2^i\right) + (k+1)\cdot 2^{k+1}\)

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

    =

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

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

    =

    \(2^{k+1}(k-1) + 2^{k+1}(k+1) + 2\)

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

    =

    \(2^{k+1}(k-1+k+1) + 2\)

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

    =

    \(2^{k+1}(2k) + 2\)

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

    =

    \(2^{k+2}(k) + 2\)

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

    =

    \(2^{(k+1)+1}((k+1)-1) + 2\)

    As we have shown both the base case and the inductive case, \(P(n)\) must be true for all integers \(n>1\).

  2. Prove by induction that \(\sum\limits_{i=1}^{n} \frac{3}{4^i} = 1 - (\frac{1}{4})^n\).

    We have \(P(n) \equiv \sum\limits_{i=1}^{n} \frac{3}{4^i} = 1 - (\frac{1}{4})^n\).

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

    \(\sum_{i=1}^{1} \frac{3}{4^i}\)

    =

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

    \(\frac{3}{4^1}\)

    =

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

    \(\frac{3}{4}\)

    =

    \(1 - \frac{1}{4}\)

    \(\frac{3}{4}\)

    =

    \(\frac{3}{4}\)

    For the inductive hypothesis, we assume \(P(k)\), that is, we assume that \(\sum\limits_{i=1}^{k} \frac{3}{4^i} = 1 - (\frac{1}{4})^k\).

    For the inductive case, we show that \(P(k)\) implies \(P(k+1)\) for positive \(k\).

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

    \(\left(\sum_{i=1}^{k} \frac{3}{4^i}\right) + \frac{3}{4^{k+1}}\)

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

    \(\left(\sum_{i=1}^{k} \frac{3}{4^i}\right) + 1 - (\frac{1}{4})^k\)

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

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

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

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

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

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

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

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

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

    \(1 - \frac{1}{4^{k+1}}\)

    As we have shown both the base case and the inductive case, \(P(n)\) must be true for all integers \(n>1\).

  3. Prove by induction that \(\sum\limits_{i=1}^{n} i \cdot 2^i = 2^{n+1}(n-1) + 2\).

    We have \(P(n) \equiv \sum\limits_{i=1}^{n} i \cdot 2^i = 2^{n+1}(n-1) + 2\).

    The base case, \(P(1)\), that is to say \(\sum\limits_{i=1}^{1} i \cdot 2^i = 2^{1+1}(1-1) + 2\). We reduce this as follows:

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

    =

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

    \(1 \cdot 2^1\)

    =

    \(2^{2}(0) + 2\)

    \(2\)

    =

    \(0 + 2\)

    For the inductive hypothesis, we assume \(P(k)\), that is, we assume \(\sum\limits_{i=1}^{k} i \cdot 2^i = 2^{k+1}(k-1) + 2\).

    For the inductive case, we show that \(P(k)\) implies \(P(k+1)\) for positive \(k\).

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

    =

    \(\left(\sum_{i=1}^{k} i \cdot 2^i\right) + (k+1)\cdot 2^{k+1}\)

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

    =

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

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

    =

    \(2^{k+1}(k-1) + 2^{k+1}(k+1) + 2\)

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

    =

    \(2^{k+1}(k-1+k+1) + 2\)

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

    =

    \(2^{k+1}(2k) + 2\)

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

    =

    \(2^{k+2}(k) + 2\)

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

    =

    \(2^{(k+1)+1}((k+1)-1) + 2\)

    As we have shown both the base case and the inductive case, \(P(n)\) must be true for all integers \(n>1\).

  4. Prove by induction that \(\sum\limits_{i=1}^{n} \frac{3}{4^i} = 1 - (\frac{1}{4})^n\).

    We have \(P(n) \equiv \sum\limits_{i=1}^{n} \frac{3}{4^i} = 1 - (\frac{1}{4})^n\).

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

    \(\sum_{i=1}^{1} \frac{3}{4^i}\)

    =

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

    \(\frac{3}{4^1}\)

    =

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

    \(\frac{3}{4}\)

    =

    \(1 - \frac{1}{4}\)

    \(\frac{3}{4}\)

    =

    \(\frac{3}{4}\)

    For the inductive hypothesis, we assume \(P(k)\), that is, we assume \(\sum\limits_{i=1}^{k} \frac{3}{4^i} = 1 - (\frac{1}{4})^k\).

    For the inductive case, we show that \(P(k)\) implies \(P(k+1)\) for positive \(k\).

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

    \(\left(\sum_{i=1}^{k} \frac{3}{4^i}\right) + \frac{3}{4^{k+1}}\)

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

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

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

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

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

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

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

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

    \(\sum_{i=1}^{k+1} \frac{3}{4^i}\)

    =

    \(1 - \frac{1}{4^{k+1}}\)

    As we have shown both the base case and the inductive case, \(P(n)\) must be true for all integers \(n>1\).