%\newcommand{\ans}[1]{}
\documentclass[11pt]{article}
\usepackage{fullpage,pst-all,epsfig}

\usepackage{algorithmic}
\usepackage{algorithm}

\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{graphicx}

\usepackage{caption}
\usepackage{subcaption}

\newcommand{\comment}[1]{}
\newcommand{\Le}{\textbf{L}}

\newcommand{\ans}[1]{\emph{Solution: #1}}
%\newcommand{\ans}[1]{}

\newcommand{\seq}[1]{ \langle #1,\cdots \, \rangle}
\newcommand{\seqi}[1]{ \langle #1 \rangle}
	
\begin{document}
\thispagestyle{empty}
\begin{center}
\def\handout{Final Examination}
\vspace*{-.75in}
{\large University of New Mexico}\\
{\large Department of Computer Science}\\
\vspace*{0.5in}
{\LARGE {\bf \handout}}\\
\vspace*{0.1in}
{\large CS 561 Data Structures and Algorithms}\\
{\large Fall, 2016}\\ [0.3in]
\end{center}
 
\vfill

\makeatletter
\long\def\hint#1{({\em Hint\/}: #1)}
\let\@evenhead\@oddhead
\def\@oddfoot{}
\let\@evenfoot\@oddfoot

\def\problem#1{\def\problemheading{#1}\clearpage\item{\bf #1}}

% Comment out the above 'problem' def and use the one below to get
% all the problems on a single page, instead of page break each time.
%\def\problem#1{\def\problemheading{#1}\item{\bf #1}}

\def\extrapage{\addtocounter{enumi}{-1}\clearpage\item{\bf \problemheading, continued.}}

\let\part\item
\renewcommand{\theenumii}{\alph{enumii}}
\makeatother
\parindent 0pt

\vfill
\centerline{
\Large
\begin{tabular}{|l|}  \hline
Name: \hspace*{2in} \\ \hline
Email: \hspace*{2in}\\ \hline
\end{tabular}
}
\vfill

\hrule
\begin{itemize}

\item This exam lasts 2 hours.  It is closed book and closed notes with no electronic devices.  However, you are allowed 2 pages of cheat sheets.

\item {\em Show your work!}  You will not get full credit if we cannot figure out how you arrived at your answer.  

\item Write your solution in the space provided for the corresponding problem.

\item If any question is unclear, ask for clarification.

\end{itemize}
\hrule
\vfill
\centerline{
\Large
\begin{tabular}{|c|c|c|c|}  \hline
Question & Points & Score & Grader \\  \hline\hline
1 & 20 & & \\  \hline
2 & 20 & & \\  \hline
3 & 20 & & \\  \hline
4 & 20 & & \\  \hline
5 & 20 & & \\  \hline
\hline Total & 100 & & \\  \hline
\end{tabular}
}
\vfill

\newpage

\begin{enumerate}
 

\problem{Short Answer}
 

 
Answer the following questions using \emph{simplest possible} $\theta$ notation.  Draw a box around your final answer.  Briefly justify your answer where appropriate.%No need to justify answers for problems on this page.
 
 \begin{enumerate}

%\item ${n \choose 3} \frac{1}{n^{2}}$ \ans{$\theta(n)$}  \\ \ \\ \ \\ \ \\ \ \\ 


\item Solution to the recurrence $T(n) = T(n-1) + n$ \ans{$\theta(n^2)$} \\ \ \\ \ \\ \ \\ \ \\

\item Solution to the recurrence $T(n) = 2T(n/2) + \log n$ \ans{$\theta(n)$ by Master Method.}  \\ \ \\ \ \\ \ \\ \ \\

\item Solution to the recurrence: $f(n) = 3f(n-1) - 2f(n-2)$. \ans{$\theta(2^{n})$ or $\theta(c_{1}2^{n} + c_{3})$.  Annihilator is $L^2 -3L +2 = (L - 2)(L-1)$.} \\ \ \\ \ \\ \ \\ \ \\ 

\item Suppose you have a hash function that hashes $n$ items into an array of length $\sqrt{n}$. What is the expected number of items in the first bin? \ans{$\theta(\sqrt{n})$} \\ \ \\ \ \\ \ \\ \ \\ \ \\ 

\item Suppose you have a hash function that hashes $n$ items into an array of length $n$. What is the expected number of colliding pairs of items (asymptotically, as a function of $n$)? \ans{$\theta(n)$}  \ \\ \ \\ \ \\ \ \\ \ \\ 




\pagebreak

\item Runtime of fastest algorithm to solve the fractional knapsack problem over $n$ items if each item has a value independent distributed uniformly between $0$ and $1$, and a weight that is $1$? \ans{$\theta(n)$ (use bucket sort!)} \ \\ \ \\ \ \\ \ \\ \ \\ 

\item What is the expected number of nodes at the $(1/2)\log n$ level of a skip list \ans{$\theta(\sqrt{n})$} \ \\ \ \\ \ \\ \ \\ \ \\ 

\item There is a function on a data structure with amortized cost $O(1)$, but worst case cost $O(n)$.  If you call this function exactly $2$ times on a newly initialized data structure, what is the worst case cost of the sum of those two calls?  \ans{$O(1)$} \ \\ \ \\ \ \\ \ \\ \ \\ 

\item Your friend uses the Union-Find data structure to count the number of connected components in a graph $G = (V,E)$.  First, they call Make-Set on every node.  Then, they go through each edge $(u,v)$ in $E$, and if Find-Set(u) does not equal Find-Set(v), they call Union on Find-Set(u) and Find-Set(v).  Assume $|V|=n$ and $|E|=m$, that the amortized cost for all operations on the data structure is $O(\log^* n)$ and that the worst case cost of any operation is $O(\log n)$. Then what is the worst case runtime of your friend's algorithm? \ans{$\theta((m+n)\log^* n)$} \\ \ \\ \ \\ \ \\ \ \\

\item You have computed a max flow $f$ in a network $G$ with $n$ nodes and $m$ edges.  What is the cost of the most efficient algorithm to now find a min $s,t$ cut?
\ans{Just use BFS or DFS to find $S$ which equals all vertices reachable from $s$, and $T$ which is all remaining vertices.  This is the min $s,t$ cut} \\ \ \\ \ \\ \ \\ \ \\

\end{enumerate} 


\problem{Reductions and Induction}

\begin{enumerate}


Show that the next two problems are NP-Hard via a reduction from one of the following problems: 3-SAT, VERTEX-COVER, INDEPENDENT-SET, 3-COLORABLE, HAMILTONIAN-CYCLE, or CLIQUE	
\item (5 points) \textbf{Suspicious Group:} There is a set of $n$ attacks on a computer network.  For each attack, there is some subset of $m$ users who were active during that time.  You want to find a \emph{suspicious group} of $k$ users such that for each attack, there was some user in the suspicious group active during that attack. \ans{Reduce from Vertex Cover} \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\


\item (5 points) \textbf{Diverse Subset} There is a set of $n$ customers, and a set of $m$ items, where each customer has purchased some subset of the $m$ items.  For marketing purposes, you want to find a diverse subset of customers, $S$ such that no two customers in $S$ have bought the same product.  In the Diverse Subset Problem, you are given an integer $k \leq n$ and want to determine if there is a diverse subset of $k$ customers. \ans{Reduction from Independent Set}.

\pagebreak


\item (5 points) Prove by induction that any tree over $n$ nodes has $n-1$ edges for any $n \geq 1$.  Don't forget to include the base case, inductive hypothesis and inductive step.  Hint: For the inductive step, what do you get if you remove a leaf node from a tree over $n$ nodes? \ans{BC: n=1.  A tree with $1$ node has $0$ edges.  IH: For $0 \leq j <n$, all trees over $j$ nodes have $j-1$ edges.  IS: Consider some tree, $T$ with $n$ nodes.  Remove a leaf node to get a tree over $n-1$ nodes.  By the I.H., this tree has $n-2$ edges.  Hence $T$ has $n-1$ edges} \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\


\item (5 points) You have an unbounded supply of stamps with values $v_1, v_2, v_3$ and a target value $x$.  Give an algorithm to determine if it is possible to achieve the value $x$ with your stamps.  In particular, for any integer $i$, let $m(i) = 1$ if it is possible to achieve value $i$ with the stamps and $0$ otherwise, and show how to compute $m(x)$.  For example, if the stamps have values $3$, $6$, and $10$, then $m(17) = 0$ and $m(29) = 1$.

\ans{For all $j<0$, $m(j) = 0$, $m(0) = 1$.  For all $j > 0$,  $m(j) = \max (m(j-v_1), m(j-v_2, m(j-v_3) )$.}

\end{enumerate}




\problem{Graph Escape}


In the \textbf{graph escape problem}, you are given a directed graph $G = (V,E)$ along with a set of occupied vertices $X$, and a set of safe vertices $Y$.  You want to find paths from every vertex in $X$ to some vertex in $Y$ such that none of these paths share an edge.


\begin{enumerate}
	\item (5 points) Describe an algorithm to solve this problem. \ans{Create a flow network where there is an edge from $s$ to every node in $X$ with capacity $1$, an edge from every node in $Y$ to $t$ with capacity $\infty$ and all other edges in $G$ are given capacity $1$.  We then ask if there is a flow with value $|X|$ in this network.} \pagebreak
	\item (7 points) Now imagine the problem is changed so that none of the paths can share a \textbf{node}.  Give an algorithm to solve this new escape problem.\ans{Create the same network as above, but replace every node $v \in V$ with two nodes $v_i$ and $v_o$.  For all directed edges $u \rightarrow v \in E$, create an edge $u_o \rightarrow v_i$ with capacity $1$.} \pagebreak
	\item (8 points) Now imagine the escape starts at time $0$ with a person at each vertex in $X$, and in every time step, a person \textbf{must} traverse some edge.  You want to determine if there are paths for each person such that 1) no two paths traverse the same edge in the same time step; 2) all paths end at a safe vertex in $Y$; and 3) all paths end in at most $n = |V|$ time steps.  Concisely describe an algorithm for this problem. \ans{Create $n+1$ copies of $G$, $G_0, \ldots G_n$.  Add edges with capacity $1$ from $s$ to each vertex $X$ in $G_0$.  For each $u \rightarrow v$ in $G$, and for each $0 \leq i \leq n-1$, create edge of capacity $1$ from $u$ in $G_i$ to $v$ in $G_{i+1}$.  Finally,  create edges of infinite capacity from all vertices in $Y$ in each copy $G_i$ to $t$.}
\end{enumerate}



\problem{Parenthesis Puzzle}

Consider a puzzle where you are trying to place parenthesis in a math formula in order to maximize the value.  The formulas always contain positive numbers, and the two operators addition ($+$) and multiplication ($\cdot$).  For example, parenthesize $ 6 + 0 \cdot 6  $, has solution $(6 + 0) \cdot 6 = 36$.  Another example is that parenthesize: $.1 + .1 \cdot .1$ has solution $.1 + (.1 \cdot .1) = .11$.  

In general, the input to your problem is $x_0, o_0, x_1, o_1, \ldots o_{n-1}, x_n$, where the $x_i$ are positive numbers and the $o_i$ are operators that are either addition or multiplication.  

\begin{enumerate}
	\item (15 points) For $0 \leq i \leq j \leq n$, let $m(i,j)$ be the maximum value achievable by optimally parenthesizing the formula $x_i, o_i, \ldots, o_{j-1}, x_j$.  Write a recurrence relation for $m(i,j)$.  Don't forget the base case(s). \ans{$m(i,j) = \max_{i \leq k \leq j-1} m(i,k) o_k m(k+1,j)$, and $m(i,i) = x_i$} \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\
	\item (5 points) Briefly describe a dynamic program based on the above recurrence relation.  What is the runtime of your program? \ans{Use a table, proceed by increasing length of substring.  Keep pointers to value of $k$ that achieves each optimal value. Runtime is $\theta(n^3)$}
	
	
	
\end{enumerate}



\problem{Challenge Problem}

In the MAX-EDGE-COVER problem, you are given a graph $G = (V,E)$ and an integer $k$, and you want to find a set of $k$ vertices that \textbf{maximizes} the number of covered edges in $G$.   In this problem, you will develop an approximation algorithm for MAX-EDGE-COVER, based on a randomized rounding of a linear program (LP).

\begin{enumerate}

\item (6 points) Write an integer program for MAX-EDGE-COVER.  Hint: Create variables $x_i$ for every every vertex, whose value depends on whether or not the vertex is in a cover, and additional variables $z_j$ for every edge, whose value depends on whether or not the edge is covered. \ans{Maximize $\sum_{\ell} z_{\ell}$ subject to $x_i, z_{\ell} \in \{0,1\}$ for all vertices $i$ $x_i$, and $\sum_i x_i$  Finally, for all edges $\ell$ between vertices $i$ and $j$, there is a variable $z_{\ell}$ such that $z_{\ell} \leq x_i + x_j$ and $z_{\ell} \leq 1$.} \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\

\pagebreak
\item (8 points) Now consider a relaxation of your integer program above to a linear program(LP), where the $x_i$ variables can take on real numbers in the range $0$ to $1$.  Let $x^*_i$ and $z^*_{\ell}$ be the solution found by the LP.  For each vertex $i$, with probability $x^*_i$, include vertex $i$ in the cover.  Let OPT be the optimal max edge cover possible for $G$ using $k$ vertices.  Give a good lower bound on the expected number of edges covered by your rounding. Hint: Recall the geometric/arithmetic mean inequality: $\sqrt{xy} \leq (1/2)(x+y)$.  \ans{For each edge $j$, let $Y_{\ell}$ be a random variable that is $1$ if the edge is covered in the rounding and $0$ otherwise.  Let the edge $\ell$ be between vertex $i$ and vertex $j$.  Then $E(Y_{\ell}) = 1 - (1-x_i)(1-x_j) = x_i + x_j - x_i x_j \geq x_i + x_j - 1/4(x_i + x_j)^2$.  Where the last step holds by geometric/arithmetic inequality.  Then we have $x_i + x_j - 1/4(x_i + x_j)^2 \geq z_{\ell} - 1/4 z^2_{\ell} \geq 3/4 z_{\ell}$ where the last step holds since $z^2_{\ell} \leq z_{\ell}$.  Finally, by the linearity of expectation, the expected number of edges covered is $\sum_{\ell} (3/4) z_{\ell} = (3/4) OPT$.  }


\pagebreak

\item (6 points) Imagine that in the solution to you LP, $\sum_{\ell} z^*_{\ell} = |E|$.  After the randomized rounding described above, let $X$ be a random variable giving the number of vertices included in your cover, and let $Y$ be a random variable giving the number of edges covered.  Show that with constant probability, $X \leq 2k$  and $Y \geq |E|/3$. Hint: Markov's inequality and Union bound.\ans{Let $X$ be the number of vertices in the rounding.  Then $E(X) = k$, and by Markov's inequality, $Pr(X \geq 2E(X)) \leq 1/2$.  Next, let $Z$ be the number of edges that are *not* covered.  By the previous problem, $E(Z) = |E|/4$, and so by Markov's, $Pr(Z \geq (8/3)E(Z)) \leq 3/8$ (note that $(8/3)(|E|/4) = (2/3)|E|$).  Now by a Union bound, the probability that either of these bad events happen is no more than $1/2 + 3/8 \leq 7/8$.}
\end{enumerate}




 \end{enumerate} 
  
  \end{document}
 
 
   \problem{Recurrences and Asymptotics}
 
 Remember that when the base case for a recurrence is not explicitly given, assume that it is constant for inputs of constant size.
 
 \begin{itemize}
 
 \item Consider the recurrence $f(0) = 1$, $f(n) = \sum_{i=0}^{n-1} f(i)$.  Prove that the solution to this recurrence is $\theta(2^{n}$)
 
 \ans{This is a straightforward proof by induction.}
 \ \\ \ \\  \ \\ \ \\ \ \\  \ \\ \ \\ \ \\  \ \\ \ \\ \ \\  \ \\ \ \\ \ \\  \ \\ \ \\ \ \\  \ \\

\item Solve the following recurrence: $f(n) = 4f(n-1) - 4f(n-2) + 2^{n}$.  Do not solve for the constant coefficients
\ans{$(L^{2}-4L+4) = (L-2)^{2}$ annihilates the homogeneous part and $(L-2)$ annihilates the non-homogeneous part.  So the annihilator is $(L-2)^{3}$ and hence the solution is $(c_{1}n^{2} + c_{2} n + c_{3}) 2^{n}$}
 \ \\ \ \\  \ \\ \ \\ \ \\  \ \\ \ \\ \ \\  \ \\ \ \\ \ \\  \ \\

\pagebreak


\item Solve the following recurrence: $f(n) = f(n/2) + f(n/4)$.  Do not solve for the constant coefficients.  If an algorithms runtime is given by this recurrence, how would it compare with algorithms with runtimes of $\theta(2^{n})$, $\theta(n)$, $\theta(\sqrt{n})$?

 \ans{Let $2^{i}=n$ and $F(i) = f(2^{i})$.  Then we get a transformed recurrence: $F(i) = F(i-1) + F(i-2)$, whose solution is  $F(i) = c_{1} \phi^{i} + c_{2} \hat{\phi}^{i}$. Reverse transforming this solution, and using the fact that $i = \lg n$ we get  
 $f(n) = n^{\lg \phi} + n^{\lg \hat{\phi}}$.  This solution is approximately $\theta(n^{.694})$, so it is better than linear but worse than square root run time.}
 \ \\ \ \\  \ \\ \ \\ \ \\  \ \\ \ \\ \ \\  \ \\ \ \\ \ \\  \ \\
 
 \end{itemize}


 \problem{Probability}
 
 
 \item \emph{Closest Points:} Imagine $n$ points are distributed uniformly at random on a circle with circumference $1$.  Show that the expected number of pairs of points that are within distance $\theta(1/n^{2)}$ of each other is greater than $1$ (note that this implies that the smallest distance between two points is likely $O(1/n^{2})$).  Hint: Partition the circle into $n^{2}/k$ regions of size $k/n^{2}$ for some constant $k$; then use the Birthday paradox to solve for the necessary $k$.
 \ans{Think of the regions as bins and the points as balls.  The probability that two points fall in the same region is $k/n^{2}$.   Let $X_{i,j}$ equal $1$ if points $i$ and $j$ fall in the same region and $0$ otherwise and let $X$ be the sum over all $n \choose 2$ values $i$ and $j$ of $X_{i,j}$.  Using linearity of expectation, $E(X) = {n \choose 2}k/n^{2}$.  Thus, for some $k$, e.g. $k\geq 3$, we would expect at least one pair of balls to fall in the same bin.}

\end{itemize}


 \problem{Are you Smarter than a 561 Student?}
 
You are competing in the popular game show ``Let's Make a Dynamic Program'' with another player.  You and your opponent both start with $0$ dollars.  If you reach (or exceed) $n$ dollars before your opponent, you win $n$ dollars; if your opponent reaches (or exceeds) $n$ dollars before you, you win nothing; and if you both reach (or exceed) $n$ dollars at the same time, you both win $n$ dollars.  In each turn, you get to choose the level of difficulty of the next question asked, where this difficulty is represented by an integer $k$ between $1$ and $n$.  If you answer the question correctly, you get $k$ dollars, otherwise your opponent gets $k$ dollars.  Note that you are always in control throughout the entire game of the difficulty level of the question asked.
 
Through careful study of the game you have been able to determine the probability $p_{i}$ for $i$ between $1$ and $n$, which is the probability that you will answer a question of difficulty $i$ correctly.

\begin{itemize}

\item Consider the greedy algorithm where you always choose a question of difficulty level $i$ for $i$ maximizing $i*p_{i} - i*(1-p_{i}) = i(2p_{i}-1) $.  Is this an optimal algorithm?  Hint: Is it ever better to make a long shot bet because the probability of success from multiple short bets is small.  In particular,think about the case where your opponent has $n-1$ dollars and you have $0$.


\ans{Greedy is not optimal.  Consider the case where $p_{1} = 1/4$, $p_{n} = 1/5$ and for all other $i$, $p_{i} = 0$.  Then $1$ is the difficulty level maximizing the expected difference between you and your opponent, since $1p_{1} - 1q_{1} = -1/2$ and $np_{n} - nq_{n} = -(3/5)n$.  Assume further that your opponent has $n-1$ dollars and you have $0$ dollars.   If you choose a difficulty of $n$, you have the chance of jumping ahead of your opponent to victory, which happens with probability at least $1/5$.  In contrast, the probability of success by following the greedy strategy, where you just keep choosing difficulty of $1$ will only be $(1/4)^{n}$, which is exponentially small in $n$.}

\pagebreak

\item Let state $(i,j)$ be the state where you have $i$ dollars and your opponent has $j$ dollars.  Note that if you choose the difficulty level to be $k$ at that state, you have probability $p_{k}$ of going to state $(i+k,j)$ and probability $(1-p_{k})$ of going to state $(i,k+jj)$.  Now let $e(i,j)$ be your expected winnings if you have $i$ dollars and your opponent has $j$ dollars and you play optimally.  Write a recurrence relation for the value $e(i,j)$.  Note: You will find it useful to consider $i$ and $j$ values that range from $0$ to $2n-1$.  Hint: Use expected values for simpler subproblems and the probabilities $p_{i}$ described above to compute $e(i,j)$.  Don't forget the base case(s).

\ans{Base Cases: $e(i,j) = n$ for all $j \geq n$.  $e(i,j) = 0$ for all $i \geq n$ and $j < n$.  \\ Recurrence:  for other values of $i$ and $j$,
 $e(i,j) = max_{1 \leq k \leq n} p_{k} e(i+k,j) + (1-p_{k}) e(i,j+k)$}

\pagebreak


\item Give the pseudocode for computing the value $e(0,0)$, which gives you your expected winnings if you play this game optimally.
\ans{Basically, need to fill in the base case first of a n by n array and then fill in the remaining values from bottom up and right to left}
  
\pagebreak 

\item What if the game is changed as follows.  You still select the difficulty level $k$, but after your selection, both you and your opponent have the chance to write down the answer to the question.  Whoever gets the answer correct wins $k$ dollars (note that both of you may win now).  There is no penalty for a wrong answer.  The probability that you answer a question of difficulty $k$ correctly is $p_{k}$ and the probability that your opponent answers correctly is $q_{k}$.   Can you still solve this new problem using dynamic programming?  If so, give a recurrence and describe how to change the algorithm.  If not, describe why not.

\ans{It is still possible.  The base case for the recurrence is the same as before.  The new part of the recurrence is determined by following equation for fixed $k$:
$e(i,j) =  (p_{k}q_{k} e(i+k,j+k) + (1-p_{k})q_{k}e(i,j+k) + p_{k}(1-q_{k})e(i+k,j) + (1-p_{k})(1-q_{k}) e(i,j)$.  Note that $e(i,j)$ appears on both the left and right side of this equation, which at first seems to cause problems in formulating a recurrence; however, it is still possible to isolate $e(i,j)$ on the left side.  Then it is possible to get a proper recurrence by taking the maximum over all possible values of $k$.  The recurrence and pseudocode is left as an exercise.}
  
 \end{itemize}
  
  \end{enumerate}
  

 

 

 

 
 
\problem{Search Trees}

Consider a tree with the following properties:

\begin{itemize}
\item Each internal node has exactly three children
\item The heights of the subtrees rooted at each child differ by at most $1$.
\end{itemize}

What is the maximum height of such a tree containing $n$ nodes?

Hint: Write a recurrence relation for the maximum number of nodes as a function of the height and then solve for the height.  Show your work!

\ans{Let $T(h)$ be the maximum number of nodes in a tree of height $h$.  Then $T(h) = T(h-1) + 2 T(h-2) + 1$.  $(\Le^{2}-\Le - 2) = (\Le -2)(\Le +1)$ annihilates the homogeneous part and $\Le - 1$ annihilates the non-homogeneous part.  Thus, the solution is of the form $T(h) = c_{1}2^{h} + c_{2} + c_{3} (-1)^{h}$.  Let $n$ be the number of nodes in the tree.  We know that $n \geq T(h)$ and so $n \geq c_{1}2^{h} + c_{2} + c_{3} (-1)^{h}$.  If we let $c = c_{2} - c_{3}$, we can say that, $n \geq c_{1} 2^{h} + c$.  Taking logs of both sides, we have that $\log n \geq h \log (2c_{1}) + \log c$.  This implies that $h \leq 1/(log(2c_{1}))(\log n - \log c)$.  The right hand side is $O(\log n)$.  Thus, $h$ is $O(\log n)$.}

\problem{Hash Tables and Probability}

Assume we hash $n$ items into a hash table with $n$ bins using a good hash function i.e. each item is hashed to a bin chosen independently and uniformly at random.  Give a good upper bound on the number of empty bins.  Solve for the constants in your upper bound i.e. do not use asymptotic notation.

Hint: Use the fact that $1-x \leq e^{-x}$ for all $x$.

\ans{Let $X_{i}$ be an indicator random variable which is $1$ if the $i$-th bin is empty and is $0$ otherwise.   Then note that $E(X_{i})$ equals the probability that the $i$-th bin is empty.  This is the probability that the $n$ items do not fall in bin $i$.  The probability that a single item does not fall in bin $i$ is exactly $1 - 1/n$.  Since the items are all hashed independently, the probability that \emph{no} item hashes into bin $i$ is exactly $(1-1/n)^{n}$.  Using the hint, note that  $(1-1/n)^{n} \leq (e^{-1/n})^{n} = e^{-1}.$  Let $X$ be a random variable giving the number of empty bins and note that $X = \sum_{i=1}^{n} X_{i}$.  Using linearity of expectation, we see that $E(X) = E(\sum_{i=1}^{n} X_{i}) = \sum_{i=1}^{n} E(X_{i}) \leq n/e$.  Thus the expected expected number of empty bins is at most $n/e$.  This is actually a pretty tight upper bound as $n$ gets large.}

\problem{Divide and Conquer}

Imagine that after graduating from UNM, you start your new job at the exciting investment banking firm SELLOUT, Inc.  The firm if faced with the following problem: they have an array of the predicted prices of a stock over $n$ days and they want to determine, using this array, exactly one day to buy the stock and one day to sell the stock in order to maximize their profit.

The problem can be formally stated as follows.  You are given an array $A$ of $n$ numbers.  You want to choose indices $1 \leq i < j \leq n$ such that $A[j] - A[i]$ is maximized over all such indices.  Give an $o(n^{2})$ algorithm to solve this problem.

\ans{Use Recursion!  Recursively find the pair of indices $i_{l}$ and $j_{l}$ on the left half of the array such that $i_{l} < j_{j}$ and $A[j]-A[i]$ is maximized over all such pairs.  Find a similar pair $i_{r}$ and $j_{r}$ on the right half of the array.  Now find $x$, the index of the element with smallest value on the left half and $y$, the index of the largest element on the right half.  Finally, return 
max($A[j_{l}] - A[i_{l}]$, $A[j_{r}] - A[i_{r}]$, $A[y] - A[x]$).  The run time of this algorithm is given by the same recurrence as for merge sort $T(n) = 2T(n/2) + n$, whose solution is $n \log n$.  Note that we can get an even faster algorithm than this using dynamic programming. Also there is another $O(n \log n)$ solution that makes use of a heap or BST.}

\end{enumerate}





% \item {\bf True or False}:  In a max-heap, the element with smallest key is always at the
% rightmost leaf node of the heap? \ans{F: it's always at a leaf node
% but not necessarily the rightmost leaf node}
% \item {\bf True or False}:  Bubblesort requires $O (n)$ extra space (not counting the space
% to store the array to be sorted)? \ans{F: it takes $O (1)$ extra space}
% \item {\bf True or False}:  $1/\log n$ is $o (1)$? \ans{T}
% \item {\bf True or False}:  $\log \sqrt{n}$ is $\Theta (\log n)$?
% \ans{T: since $\log \sqrt{n} = 1/2\log n$}
% \item {\bf True or False}:  $2^{n-1}$ is $o (2^{n})$?\ans{F: since $2^{n-1}=1/2*2^{n}$}
% \problem{Annihilators}\\

% Consider the recurrence $T (n) = 2T (n-1) - T (n-2) + 4$, $T (0)=0$, $T (1)=0$.
% Solve this recurrence \emph{exactly} using annihilators.  Don't forget
% to check your answer.

% \ans{Consider the homogeneous part first.  Let $T_{n} = 2T (n-1) - T
% (n-2)$, and $T = \seqi{T_{n}}$.  Then
% \begin{eqnarray}
% T & = & \seqi{T_{n}}\\
% \Le T & = & \seqi{T_{n+1}}\\
% \Le^{2} T & = & \seqi{T_{n+2}}
% \end{eqnarray}
% Since $\seqi{T_{n+2}} = \seqi{2T_{n+1}-T_{n}}$, we know that
% $\Le^{2}T-2\Le T + T = \seqi{0}$, and thus $\Le^{2}-2\Le +1 = (\Le -1)
% (\Le -1)$ annihilates $T$.  Further we know that $(\Le -1)$
% annihilates the non-homogeneous part.  Thus the annihilator of the
% whole sequence is $(\Le -1)^{3}$.  Thus $T (n)$ is of the form:
% \[
% T (n) = c_{1}n^{2}+c_{2}n+c_{3}
% \]
% We know:
% \begin{eqnarray}
% T (0) = 0 & = & c_{3}\\
% T (1) = 0 & = & c_{1} + c_{2}\\
% T (2) = 4 & = & 4 c_{1} + 2c_{2}
% \end{eqnarray}
% so $c_{1}=2$, $c_{2}=-2$, $c_{3}=0$ and thus
% \[
% T (n) = 2n^{2}-2n
% \]
% Check: $T (3) = 2*4-0+4=12$ and $2*9-6=12$.}




% \problem{Substitution Method}\\
% Consider the following recurrence: $$T(n) = T(n-1) + T(n-2) - n + 3,$$
% where $T(1) = 1,T(2)=2$.\\ \ \\ Show that $T(n) = n$ by induction.
% Include the following in your proof: 1)the base case(s) 2)the
% inductive hypothesis and 3)the inductive step.

% \ans{Base Case: T(1) = 1.\\Inductive Hypothesis: For all $j<n$, T(j) =
% j\\Inductive Step: We must show that $T(n)=n$, assuming the inductive
% hypothesis.  
% \begin{eqnarray}
% T(n) & = & T(n-1) + T(n-2) - n + 3\\
% T(n) & = & (n-1) + (n-2) - n + 3\\
% T(n) & = & n
% \end{eqnarray}
% where the inductive hypothesis allows us to make the replacements in
% the second step.}

% \item {\bf True or False}: If $X$ and $Y$ are sequences that both
% begin with the character $a$, then some longest common subsequence of
% $X$ and $Y$ begins with the character $a$. \ans{True}
 \pagebreak

\item (5 points) Your boss wants to create the following data structure in the comparison model and to name it after himself, the \emph{Merkle}.  A Merkle has the following operations and properties on it.  BuildMerkle takes an arbitrary array and builds a Merkle from it in $O(n)$ time.  The resulting Merkle will provide the following operations.  FindMin (resp. FindMax) will return the minimum (resp. maximum) element and run in $O(\log n)$ time.  Successor(x) (resp. Predecessor(x)) return the next largest (resp. smallest) element in the Merkle after the element $x$, and both of these operations run in $O(1)$ time.  Intuitively, your boss wants you to combine the nice properties of the heap with the nice properties of a data structure like skip lists.  Can you immortalize your boss's name in CS textbooks by creating this data structure?

\ans{No.  This would allow sorting in $O(n)$ time.  To show this, first build the Merkle from an unsorted array, then find the minimum element and keep calling successor}



\pagebreak

In this problem, you will modify count-min sketches so that they handle negative counts.  As in class, assume you are presented with a stream of tuples of the form $(i_{t},c_{t})$, except now $c_{t}$ may be either a negative or positive integer.  The data structure you will use will consist of two count-min sketches, a positive count-min sketch for positive counts and a negative count-min sketch for negative counts.  In particular, each of the two sketches will use $m$ counters and $k$ hash functions, where all hash functions can be assumed to be independent.  If $c_{t}$ is positive, in the positive count-min sketch (positive sketch for short),  for each $1 \leq a \leq k$, $C_{a,h_{a}(i)}$ will be incremented by $c_{t}$.  If $c_{t}$ is negative, in the negative sketch, for each $1 \leq a \leq k$, $C_{a,h_{a}(i)}$ will be incremented by $-c_{t}$.   The estimate of the count of an item, $i$ at time $T$ is $m^{+}(i,T) - m^{-}(i,T$, where $m^{+}(i,T)$ is the value of the smallest counter associated with $i$ in the positive sketch and $m^{-}(i,T)$ is the value of the smallest counter associated with $i$ in the negative sketch.  As in class, let $Count(i,T)$ be the true count of item $i$ in the stream up to time $T$.  Also assume that $k = m \epsilon/e$ for the positive sketch and for the negative sketch.
 
 \item (7 points) Give a good bound on the probability that the following holds:\\
 $$ Count(i,T) - \epsilon \sum_{i=1}^{T} |c_{i}| \leq m(i,T) \leq Count(i,T) + \epsilon \sum_{i=1}^{T} |c_{i}| $$
 Please prove your bound.
 
 
 \ans{Let $S^{+}_{T}$ be the sum of all the positive counts in the stream up to time $T$.  For the positive sketch, we know that $Pr(Z_{j}> \epsilon S^{+}_{T}) \leq e^{m\epsilon/e}$, where $Z_{j}$ is the amount the min counter associated with $i$ in the positive sketch is incremented by items other than $i$.  Let $S^{-}_{T}$ be the sum of the absolute values of the negative counts in the stream up to time $T$.  Then we also know using the analysis in class that for the negative sketch, $Pr(Z'_{j}> \epsilon S^{-}_{T}) \leq e^{m\epsilon/e}$, where $Z'_{j}$ is the amount the min counter associated with $i$ in the negative sketch is incremented by items other than $i$.   A simple union bound on these two inequalities shows that $Pr(Z_{j}> \epsilon S^{+}_{T} \textrm{ or } Z'_{j}> \epsilon S^{-}_{T}) \leq 2 e^{m\epsilon/e}$.  Thus, we can see that $Pr(Z_{j} + Z'_{j} > \epsilon \sum_{i=1}^{T} |c_{i}|) \leq  2 e^{m\epsilon/e}$.  Thus the probability the approximation bound given holds is at least $1 - 2 e^{m\epsilon/e}$. }

\pagebreak

\item  (7 points) Now imagine you are given a constant number of data streams $D_{1}, D_{2}, \ldots, D_{c}$ and weights associated with them $w_{1}, w_{2}, \ldots w_{c}$ that may be positive or negative real numbers.  For each item $i$, at time $T$, define $Count(i,T)$ to be the weighted sum of the count values seen in all data streams up to time $T$, where a count value seen in stream $i$ is weighted by $w_{i}$.  Assume now that all count values seen are positive.  Describe a data structure based on count-min sketches that can approximate $Count(i,T)$.  How much memory does your data structure use? How closely can you approximate $Count(i,T)$ and with what probability?  Please justify your answers.  For consistency in notation, please let $S(i,j,T)$ be the sum of the counts of item $i$ in stream $j$ up to time $T$.

\ans{The basic idea is to use $c$ different count min-sketches and let $m(i,T)$ be the weighted sum of the min value in each of them associated with the item $i$.  The total memory used is $cm$.  A union bound over the $c$ different data streams can establish the following guarantee with probability $1-c e^{m\epsilon/e}$:
 $$ Count(i,T) - \epsilon \sum_{j=1}^{c} |w_{j}| S(i,j,T) \leq m(i,T) \leq Count(i,T) + \epsilon \sum_{j=1}^{c} |w_{j}| S(i,j,T) $$}


 
 Expected number of Cycles in Permutations
 http://web.engr.illinois.edu/~jeffe/teaching/algorithms/hwex/f10/midterm2.pdf


\end{enumerate}
 
 \item You are given a bipartite graph $G = (V,E)$, with nodes on the left side, $L$, representing interns and nodes on the right, $R$, representing hospitals.  There is an edge from an intern node, $x$, to a hospital node, $y$ iff the intern x is willing to be assigned to hospital $y$.  As in the homework, assume that for all $S \subseteq L$, $|N(S)| \geq |S|$.   

What is the runtime of the fastest algorithm to find a perfect matching in $G$ (in terms of $|V|$ and $|E|$)?  You should only use algorithms discussed in this class.

\ans{We create a new graph $G'$ with source $s$ and sink $t$.  There is an edge from $s$ to each node in $L$ with capacity $1$ and an edge from each node in $R$ to $t$ with capacity $1$.  Then for every edge $(x,y) \in E$, there is an edge from $x$ to $y$ with capacity $1$.  There are thus $O(E)$ edges and $O(|V|)$ nodes, all capacities are $O(1)$ and $f* = O(|V|)$.  Using the ``Short Pipe'' algorithm, we can find a max flow in $G'$ and thus a perfect matching in $G$ in time $O(|E|^{2} \log |E| \log |V|)$}

 A palindrome is any string that is exactly the same as its reversal, like I, or DEED, or HANNAH, or AMANAPLANACATACANALPANAMA. Describe and analyze an algorithm to find the length of the longest subsequence of a given string that is also a palindrome.
For example, the longest palindrome subsequence of MAHDYNAMICPROGRAMZLETMESHOWYOUTHEM is MHYMRORMYHM, so given that string as input, your algorithm should return the integer 11.

 
 
 
 Solution: The following dynamic program is the intended “correct” answer, though it ignores a
subtle issue detailed below (which only three students identified, and received bonus points for).
It is similar to the matrix-multiplication parenthesization dynamic program we saw in lecture, but
with a different recurrence.
1. For subproblems, we use substrings xi
, oi
, . . . , oj 1, xj − , for each 0 ≤ i ≤ j ≤ n. Thus there are
Θ(n
2
) subproblems.
2. To solve DP[i, j], we guess which operation ok is outermost, where i ≤ k < j. There are
j − i = O(n) choices for this guess.
3. The resulting recurrence relation is
j
DP[i, j] = max −1

DP[i, k] ok DP[k + 1, j]
k=i

.
The base cases are
DP[i, i] = xi
.
The running time per subproblem is O(n).
4. The dynamic program uses either recursion plus memoization, or bottom-up table construction.
A suitable acyclic order is by increasing length ` of substring, i.e.,
for ` = 0, 1, . . . , n:
for i = 0, 1, . . . , n − `:
j = i + `
5. The value of the original problem is given by DP[0, n]. To actually reconstruct the parenthesization,
we can remember and follow parent pointers (the argmax in addition to each max). The
overall running time is
Θ(n
2
) · O(n) = O(n
3
).
6.006 Final Exam Solutions Name 20
The subtle issue is that this dynamic program assumes that, in order to maximize the sum or product
of two numbers, we aim to maximize the two arguments. This assumption is true if the numbers
are all nonnegative, as in the examples. If some numbers can be negative, however, then it is not so
easy to maximize the product of two numbers. If both of the numbers are negative, so the product is
negative, then the goal is to minimize both numbers (i.e., maximizing their absolute values); but if
exactly one of the numbers is negative, so the product is negative, then maximization is equivalent
to maximizing the negative number and minimizing the positive number (i.e., minimizing their
absolute values).
To deal with this issue, we can define two subproblems: DPmax[i, j] is the maximum possible
value for the substring xi
, . . . , xj
, as above, while DPmin[i, j] is the minimum possible value for
the same substring. Instead of working out which of the two subproblems we need, we can simply
guess among the four possibilities, and choose the best. The recurrence relation thus becomes
j−1
DPm[i, j] = m m 
DPm1
[i, k] ok DPm2
[k + 1, j] .
k=i m1,m2∈{max,min}

The running time remains the same, up to constant factors.



----Short Answer Problems


What is the fastest algorithm discussed in class for each of the following problems? Give a short description
or citation (no more than two sentences each). What is the run time of the fastest algorithm?

1. Determining if an undirected graph with n vertices and m edges is bipartite.
Use Breadth First Search to divide the graphs into two sets of vertices. O(n + m).
2. Computing the longest common subsequence of a pair of strings each of length n.
Dynamic Programming. O(n
2
).
3. Solving the single source shortest paths problem on a graph with n vertices and m edges.
Bellman-Ford algorithm. O(nm) time. (Note that nothing is said about the edges being
non-negative.).
5. Checking whether or not an undirected graph with n vertices and m edges has a cycle.
Use depth first search, and report a cycle if non-tree edges are detected. O(n + m) time.

7. Solving the knapsack problem with n items, and a bound of K on the size of the knapsack.
Dynamic programming. O(nK) time.
8. Find a maximum cardinality matching in a bipartite graph with n vertices and m edges.
Convert to network flow, and use the Ford-Fulkerson algorithm. O(nm) time.
9. Finding a minimum cut of a graph, after a flow algorithm has found the maximum
flow?
Construct the residual graph, and then determine which vertices are reachable in the residual
graph by edges of non-zero capacity. This gives one side of the cut. The edges leaving this
set are all filled to capacity.  Takes O(n+m) time

10. Assume you have a function with amortized cost $O(1)$, but worst case cost $O(n)$.  If you call this function $2$ times on a newly initialized data structure, what is the worst case cost of the sum of those calls?  \ans{$O(1)$}


How can you:

a) Find a maximum weight spanning tree using an algorithm which computes a minimum weight
spanning tree.
Negate all edges, find a minimum spanning tree.
b) Find a shortest path in an undirected graph, using an algorithm which finds a shortest path
in a directed graph.
Replace all edges by pairs of anti-parallel edges.
d) Find a maximum flow in a graph with capacities on the vertices (meaning a bound on the
flow that can go into each vertex), using an algorithm for maximum flow where the capacities
are on the edges.
Split each vertex into two vertices, with an edge between them giving the vertix capacity.
 
