\documentclass[10pt]{article}
\usepackage{times,graphicx,epstopdf,fancyhdr,amsfonts,amsthm,amsmath,url,xspace}
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage[left=.75in,top=.75in,right=.75in,bottom=.75in]{geometry}
\pagestyle{fancy}
\begin{document}
\newtheorem{claim}{Claim}
\newtheorem{definition}{Definition}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{observation}{Observation}
\newtheorem{question}{Question}
\lhead{Williams College}
\chead{Homework 4}
\rhead{Brent Heeringa}
\cfoot{Algorithm Design and Analysis - CS 256}
\lfoot{Due: 11.00, 08 March 2013}
\rfoot{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\headwidth}{\textwidth}
\renewcommand{\footrulewidth}{0.4pt}
\begin{question}[JE]
Describe and analyze a data structure to support the following operations on an array $A[1 \ldots n]$ as quickly as possible. Initially, $A[i] = 0$ for all $i$.
\begin{itemize}
\item Given an index $i$ such that $A[i] = 0$, set $A[i]$ to $1$.
\item Given an index $i$, return $A[i]$.
\item Given an index $i$, return the smallest index $j \geq i$ such that $A[ j] = 0$, or report that no such index exists.
\end{itemize}
For full credit, the first two operations should run in worst-case constant time, and the amortized cost of the third operation should be equivalent to the amortized cost of the {\sc Union-Find} data structure.
\end{question}
\begin{question}[DPV]
Suppose you are choosing between the following three algorithms:
\begin{enumerate}
\item Algorithm A solves problems by dividing them into five subproblems of half the size, recursively solving each subproblem, and then combining the solutions in linear time.
\item Algorithm B solves problems of size $n$ by recursively solving two subproblems of size $n-1$ and then combining the solutions in constant time.
\item Algorithm C solves problems of size $n$ by dividing them into nine subproblems of size $n/3$, recursively solving each subproblem, and then combining the solutions in $O(n^{2})$ time.
\end{enumerate}
What are the running times of each of these algorithms (in asymptotic notation) and which would you choose?
\end{question}
\begin{question}[DPV] \label{q:maj}
An array $A[1\ldots n]$ is said to have a {\em majority element} if more than half of its entries are the same. Given an array , the task is to design an efficient algorithm to tell whether the array has a majority element, and, if so, to find that element. The elements of the array are not necessarily from some ordered domain like the integers, and so there can be no comparisons of the form $A[i] > A[j]$. You should think of the array elements as, say, JPEG files. However, you {\em can} answer questions of the form: $A[i] = A[j]$ in $O(1)$ time.
\begin{enumerate}
\item [(a)] Show how to solve this problem in $O(n \log n)$ time. Make sure to prove that your algorithm is correct (via induction) and give a recurrence relation for the running time of your algorithm. A hint for this problem is available at the end of the homework.
\item [(b)] Can you give a linear-time algorithm? A hint for this problem is available at the end of the homework.
\end{enumerate}
\end{question}
\begin{question}[JE]
An inversion in an array $A[1 \ldots n]$ is a pair of indices $(i, j)$ such that $i < j$ and $A[i] > A[j]$. The number of inversions in an $n$-element array is between 0 (if the array is sorted) and $n\choose 2$ (if the array is sorted backward). Describe and analyze an algorithm to count the number of inversions in an n-element array in $O(n \log n)$ time.
\end{question}
\begin{question}
I found this homework (a) challenging, but fun; (b) challenging, but not fun; (c) fun, but not challenging; (d) not fun and not challenging.
\end{question}
\subsection*{Extra Credit}
This question adapted from the Design and Analysis of Algorithms course at the University of Konstanz by Dr. Ulrik Brandes and Dr. Sabine Cornelsen
\begin{question}[Least Common Ancestor]
Let $T =(V,E)$ be an oriented tree with root $r \in V$ and let $P \subseteq \{ \{u,v\} \,|\, u,v \in V \}$ be a set of unordered pairs of vertices. For each $v \in V$ let $\Pi_{v} = \{r, . . . , v\} \subseteq V$ denote the sequence of vertices along the path from $r$ to $v$ and let $d(v) = |\Pi_{v}| - 1$ denote the depth of $v \in T$. The {\em least common ancestor} of pair $\{u,v\} \in P$ is defined as $\overline{w} \in V$ with $\overline{w} \in \Pi_{v} \cap \Pi_{u}$ and $d(\overline{w}) > d(w)$ for all $w \in \Pi_{v} \cap \Pi_{u}$.
LCA(r) traverses $T$ to determine the least common ancestors of all pairs $\{u, v\} \in P$ . At the beginning, all vertices are unmarked.
\begin{algorithm}[H]
\SetKwFunction{ms}{Makeset}
\SetKwFunction{union}{Union}
\SetKwFunction{find}{Find}
\SetKwData{anc}{ancestor}
\ms(u) \\
$\anc[\find(u)] \longleftarrow u$\\
\ForEach{child $v$ of $u$ in $T$} {
LCA(v) \\
$\union(\find(u), \find(v))$ \\
$\anc[\find(u)] \longleftarrow u$}
mark u \\
\ForEach{$v$ with $\{u,v\} \in P$}{
\If{$v$ is marked}{
print ``LCA('' + u + ``,'' + v ``) is'' + $\anc[\find(v)]$
}
}
\caption{LCA(u)}
\end{algorithm}
\begin{itemize}
\item[(a)] Show that, when Line 8 in LCA(u) is executed, the set {\sc Find(u)} contains all vertices of the subtree $T_{u} \subseteq T$ with root $u$. \\ {\bf Hint:} Do an induction on the height $h(u)$ of $u$. ($h(u) = 0$ if $u$ is a leaf)
\item[(b)] Show that the number of sets in the Union-Find data structure at the time of call LCA(v) equals $d(v)$.\\
{\bf Hint:} The recursive calls of LCA specify a traversing order of $T$ which implies an order on $V$ . Do an induction on the position of $v$ in this order.
\item[(c)] Prove that LCA(r) determines the least common ancestors of all $\{u, v\} \in P$ correctly.\\
{\bf Hint:} Differentiate the two cases: 1. w.l.o.g. $v \in T_{u}$ and 2. $v \not\in T_{u}$ and $u \not\in T_{v}$.
\item[(d)] Analyze the running time of LCA(r).
\end{itemize}
\end{question}
\subsection*{Hints}
\begin{description}
\item [ Hint for~\ref{q:maj} (a)] Split the array $A$ into two arrays $A_{1}$ and $A_{2}$ of half the size. Does knowing the majority elements of $A_{1}$ and $A_{2}$ help you figure out the majority element of $A$? If so, can you use a divide-and-conquer approach?
\item [ Hint for~\ref{q:maj} (b)] Arbitrarily pair up the elements of the array. For each pair, if the two elements are different, discard both of them; if they are the same, keep just one of them. Show that after this procedure executes, there are at most $n/2$ elements left, and that they have a majority element if $A$ does.
\end{description}
\end{document}