\documentclass[11pt]{article}
\usepackage{epsfig}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amstext}
\usepackage{amsmath}
\usepackage{xspace}
\usepackage{theorem}
\usepackage{hyperref}
\usepackage{fullpage}
%\usepackage[]{algorithm2e}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{enumitem}
\newif\ifrubric
\rubrictrue
\rubricfalse
% This is the stuff for normal spacing
%\makeatletter
% \setlength{\textwidth}{6.5in}
% \setlength{\oddsidemargin}{0in}
% \setlength{\evensidemargin}{0in}
% \setlength{\topmargin}{0.25in}
% \setlength{\textheight}{8.25in}
% \setlength{\headheight}{0pt}
% \setlength{\headsep}{0pt}
% \setlength{\marginparwidth}{59pt}
%
% \setlength{\parindent}{0pt}
% \setlength{\parskip}{5pt plus 1pt}
% \setlength{\theorempreskipamount}{5pt plus 1pt}
% \setlength{\theorempostskipamount}{0pt}
% \setlength{\abovedisplayskip}{8pt plus 3pt minus 6pt}
\usepackage{titlesec}
\titleformat*{\section}{\bfseries}
\titleformat*{\subsection}{\bfseries}
\titleformat*{\subsubsection}{\bfseries}
\titleformat*{\paragraph}{\bfseries}
\titleformat*{\subparagraph}{\bfseries}
% \renewcommand{\section}{\@startsection{section}{1}{0mm}%
% {2ex plus -1ex minus -.2ex}%
% {1.3ex plus .2ex}%
% {\normalfont\Large\bfseries}}%
% \renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
% {1ex plus -1ex minus -.2ex}%
% {1ex plus .2ex}%
% {\normalfont\large\bfseries}}%
% \renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
% {1ex plus -1ex minus -.2ex}%
% {1ex plus .2ex}%
% {\normalfont\normalsize\bfseries}}
% \renewcommand\paragraph{\@startsection{paragraph}{4}{0mm}%
% {1ex \@plus1ex \@minus.2ex}%
% {-1em}%
% {\normalfont\normalsize\bfseries}}
% \renewcommand\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
% {2.0ex \@plus1ex \@minus .2ex}%
% {-1em}%
% {\normalfont\normalsize\bfseries}}
%\makeatother
\newenvironment{proof}{{\bf Proof: }}{\hfill\rule{2mm}{2mm}}
\newenvironment{proofof}[1]{{\bf Proof of #1: }}{\hfill\rule{2mm}{2mm}}
\newenvironment{proofofnobox}[1]{{\bf#1: }}{}
\newenvironment{example}{{\bf Example: }}{\hfill\rule{2mm}{2mm}}
%\renewcommand{\thesection}{\lecnum.\arabic{section}}
%\renewcommand{\theequation}{\thesection.\arabic{equation}}
%\renewcommand{\thefigure}{\thesection.\arabic{figure}}
%\renewcommand{\theequation}{\lecnum.\arabic{equation}}
%\renewcommand{\thefigure}{\lecnum.\arabic{figure}}
%\newcounter{LecNum}
%\setcounter{LecNum}{1}
%\newtheorem{fact}{Fact}[LecNum]
\newtheorem{fact}{Fact}
\newtheorem{lemma}[fact]{Lemma}
\newtheorem{theorem}[fact]{Theorem}
\newtheorem{definition}[fact]{Definition}
\newtheorem{corollary}[fact]{Corollary}
\newtheorem{proposition}[fact]{Proposition}
\newtheorem{claim}[fact]{Claim}
\newtheorem{exercise}[fact]{Exercise}
% math notation
\newcommand{\R}{\ensuremath{\mathbb R}}
\newcommand{\Z}{\ensuremath{\mathbb Z}}
\newcommand{\N}{\ensuremath{\mathbb N}}
\newcommand{\F}{\ensuremath{\mathcal F}}
\newcommand{\SymGrp}{\ensuremath{\mathfrak S}}
\newcommand{\size}[1]{\ensuremath{\left|#1\right|}}
\newcommand{\ceil}[1]{\ensuremath{\left\lceil#1\right\rceil}}
\newcommand{\floor}[1]{\ensuremath{\left\lfloor#1\right\rfloor}}
\newcommand{\poly}{\operatorname{poly}}
\newcommand{\polylog}{\operatorname{polylog}}
% anupam's abbreviations
\newcommand{\e}{\epsilon}
\newcommand{\half}{\ensuremath{\frac{1}{2}}}
\newcommand{\junk}[1]{}
\newcommand{\sse}{\subseteq}
\newcommand{\union}{\cup}
\newcommand{\meet}{\wedge}
\newcommand{\prob}[1]{\ensuremath{\text{{\bf Pr}$\left[#1\right]$}}}
\newcommand{\expct}[1]{\ensuremath{\text{{\bf E}$\left[#1\right]$}}}
\newcommand{\Event}{{\mathcal E}}
\newcommand{\mnote}[1]{\normalmarginpar \marginpar{\tiny #1}}
\setenumerate[0]{label=(\alph*)}
\usepackage{alltt}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{shapes}
\tikzstyle{code} = [black!90, draw=black!30, fill=black!5, very thick,
rectangle, dashed, inner xsep=10pt, inner ysep=7pt]
\newenvironment{codebox}{
\hspace{.05\textwidth}
\begin{tikzpicture}
\node[code] \bgroup
\begin{minipage}{.80\textwidth}
\begin{alltt}}
{\end{alltt}
\end{minipage}
\egroup;
\end{tikzpicture}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document begins here %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\noindent {\large {\bf 601.433/633 Introduction to Algorithms} \hfill {{\bf Fall 2024}}}\\
{{\bf Homework \#2}} \hfill {{\bf Due:} September 12, 2024, 9am} \\
\rule[0.1in]{\textwidth}{0.4pt}
Remember: you may work in groups of up to three people, but must write up your solution entirely on your own. Collaboration is limited to discussing the problems -- you may not look at, compare, reuse, etc.~any text from anyone else in the class. Please include your list of collaborators on the first page of your submission. You may use the internet to look up formulas, definitions, etc., but may not simply look up the answers online.
Please include proofs with all of your answers, unless stated otherwise.
\noindent \rule[0.1in]{\textwidth}{0.4pt}
\section{Reversals (33 pts)}
Given an array $[a_1, a_2, \dots, a_n]$, a reversal is a pair $(i,j)$ such that $i < j$ but $a_i > a_j$. For example, in the array $[5, 3, 2, 10]$ there are are three reversals ($(1,2), (1, 3), (2,3)$). Note that the array has no reversals if and only if it is sorted, so the number of reversals can be thought of as a measure of how well-sorted an array is.
\begin{enumerate}
\item (11 points) What is the expected number of reversals in a random array? More formally, consider a uniformly random permutation of $n$ distinct elements $a_1, \dots, a_n$: what is the expected number of reversals? Give your answer precisely, without asymptotic notation. Prove your answer.
\item (11 points) Recall the insertion sort algorithm:
\begin{verbatim}
for i = 1 to n
j = i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j = j - 1
\end{verbatim}
Suppose that our array has $d$ reversals. Prove that the (worst-case) running time of insertion sort is $\Theta(n + d)$.
\item (11 points) What does this imply about the \emph{average-case} running time of insertion sort as a function only of $n$? That is, if we draw a permutation uniformly at random, what is the expected running time of insertion sort (in asymptotic notation)? Note that this is \emph{not} a randomized algorithm; this is a deterministic algorithm on a random input.
\end{enumerate}
\section{Pitcher Matching (34 points)}
Suppose that you are given $n$ red and $n$ blue water pitchers, all of different shapes and sizes. All red pitchers hold different amounts of water, as do all the blue ones. Moreover, for every red pitcher there is a blue pitcher that holds exactly the same amount of water (and vice versa).
Your job is to find a matching between red pitchers and blue pitchers that hold the same amount of water. In other words, you want to identify for every red pitcher which blue pitcher has the same volume, and similarly for every blue pitcher which red pitcher has the same volume. To do this, you are only allowed to use the following operation: pick a red pitcher and a blue pitcher, fill the red pitcher, and pour it into the blue pitcher. This will tell you whether the volume of the red pitcher is less than, equal to, or greater than the volume of the blue pitcher. In other words, you can compare any red pitcher and any blue pitcher. But you \emph{cannot} compare two red pitchers or two blue pitchers.
\begin{enumerate}
\item (16 points) Give a randomized algorithm that uses $O(n \log n)$ comparisons in expectation.
\item (9 points) Prove that your algorithm is correct (i.e., that it always returns the correct matching).
\item (9 points) Prove that your algorithm uses $O(n \log n)$ comparisons in expectation.
\end{enumerate}
\section{Searching an Array Randomly (33 points)}
Let $A$ be an unsorted array of length $n$, where each entry of $A$ is an integer. Suppose that we are looking for some integer $x$ in $A$, i.e., we want to find an index $i$ such that $A[i] = x$ if such an index exists. If no such index exists, we should return False. Consider the following randomized algorithm.
\begin{itemize}
\item Initially, all indices are unmarked.
\item While not all indices are marked:
\begin{itemize}
\item Pick an index $i \in [n]$ uniformly at random.
\item If $A[i] = x$ return $i$.
\item Else mark index $i$.
\end{itemize}
\item Return false
\end{itemize}
Note that in each iteration we pick an index $i$ uniformly at random from $[n]$, not from the set of unmarked indices. So we might examine a given index more than once.
\begin{enumerate}
\item (16 points) Suppose that $x$ appears in $k \geq 1$ places in $A$, i.e., $|\{i : A[i] = x\}| = k$. What is the expected running time of this algorithm, as a function of $n$ and $k$?
Hint: if you are unfamiliar with Bernoulli and Geometric distributions, consider reading CLRS (3rd edition) Appendix C.4.
\item (17 points) Suppose that $x$ does not appear in $A$. What is the expected running time of the algorithm?
\end{enumerate}
\end{document}