\documentclass{article}

\usepackage[noend]{algpseudocode}
\usepackage[margin=1in]{geometry}
\usepackage{enumitem}
\usepackage{cprotect}

\setlength{\headsep}{0.5 in}
\setlength{\parindent}{0 in}
\setlength{\parskip}{0.1 in}
\addtolength\topmargin{-5mm}
\textheight  9in

\usepackage{amsmath,amsfonts,graphicx,multicol, color}
%\usepackage{ulem}

\newcommand{\todo}{\textcolor{red}}
\newcommand{\red}{\textcolor{red}}
\newcommand{\eat}[1]{#1}

\usepackage{url}

%\def\IsSolution{1}   % Uncomment to debug solutions. Note: Makefile generates both discussion and solution automatically.

%
% Define two macros
%   \soln{}     --- contents appear only in solution
%   \notsoln{}  --- contents appear only if not solution

\usepackage{etoolbox}
\ifx\IsSolution\undefined
 \newcommand{\notsoln}[1]{#1}
 \newcommand{\soln}[1]{}
 \newcommand{\thesoln}[1]{}
\else
  \newcommand{\notsoln}[1]{}
  \newcommand{\soln}[1]{#1}
  \newcommand{\thesoln}[1]{{\color{red}\textbf{Solution:} #1}}
\fi


%
% The following macro is used to generate the header.
%
\newcommand{\header}[1]{
   \pagestyle{myheadings}
   \thispagestyle{plain}
   \newpage
   \setcounter{page}{1}
   \noindent
   \begin{center}
   \framebox{
      \vbox{\vspace{2mm}
    \hbox to 6.28in { {\bf COMPSCI 311: Introduction to Algorithms
		\hfill Spring 2020} }
       \vspace{4mm}
       \hbox to 6.28in { {\Large \hfill Homework #1 \soln{Solutions} \hfill } }
       \vspace{2mm}
       \hbox to 6.28in { Released 1/26/2020 \hfill Due 2/10/2020 11:59pm in Gradescope}
      \vspace{2mm}}
   }
   \end{center}
   \markboth{Homework #1}{Homework #1}

   \vspace*{4mm}
}

\newcounter{problem}
\newcommand{\prob}{\addtocounter{problem}{1}\noindent {\bf Problem \theproblem.} ~}

\begin{document}
\header{1}

\textbf{Instructions.} You may work in groups, but you must write solutions yourself.
List collaborators on your submission.   Also list any sources of help (including online
sources) other than the textbook and course staff.

If you are asked to design an algorithm, please provide: (a) the pseudocode or precise description in words of the algorithm, (b) an explanation of the intuition for the algorithm, (c) a proof of correctness, (d) the running time of your algorithm and (e) justification for your running time analysis. 

\textbf{Submissions.} Please submit a PDF file. You may submit a scanned handwritten document, but a typed submission is preferred. Please assign pages to questions in Gradescope.

\begin{enumerate}[topsep=0pt]
  
\item \textbf{(30+5 points) Variations on Stable Matching}

\begin{enumerate}
\item (10 points) Consider a variant of stable matching in which at every point, either a \emph{free} college \emph{or} a \emph{free} student can propose. As in the Gale-Shapley algorithm, proposals are done going down in the preference list, so that a proposer cannot repeat a proposal to the same partner. 
Show that this algorithm always terminates with a perfect matching,
but not necessarily a stable one.\footnote{We are no longer confident that the procedure with only free parties proposing always yields a perfect matching.  We have also failed to find an example where it doesn't.  You will get full credit for exhibiting an example where this procedure fails to find a stable matching.  There may be extra credit available if a student or students resolve the question of whether the matching from the procedure is always perfect.}

\item (5 points extra credit)
Consider colleges A B, C and students 1, 2, 3, with preference lists:
\begin{tabular}{l}
1: A B C\\
2: B C A\\
3: C A B
\end{tabular}  
\begin{tabular}{l}
A: 2 3 1\\
B: 3 1 2\\
C: 1 2 3
\end{tabular}

How many of the six perfect matchings are stable? How many stable matchings can be obtained by a version of part (a) where colleges and students alternate proposing (any party can start)?

\item (10 points) Now allow proposals also from matched parties. That is, on any turn, any college or student may propose to the next partner on its preference list if this is better than the current match (if any), and if the proposal is accepted, both the proposer's and the acceptor's former partner (if any) become free. Does the algorithm always terminate? Will it always produce a perfect matching? Will it always produce a stable matching? (Hint: a college $c$ might not get a student $s$ when $s$ already has a better match, but be offered by $s$ later if $s$ becomes unmatched. Look at $n=3$ first).
  
\item (10 points) Consider now an algorithm that starts with an arbitrary perfect matching of colleges to students. As long as the matching is not stable, choose an instability $(c, s)$ and eliminate it, by matching $c$ with $s$, and the former partners of $c$ and $s$ with one another. Show that this algorithm does not always terminate with a stable matching. (Hint: an example with $n = 3$ suffices).
\end{enumerate}   

\thesoln{
}
    
\item \textbf{(15 points) Big-O.} For each function $f(n)$ below, find (1) the smallest \emph{integer} constant $H$ such that $f(n) = O(n^H)$, and (2) the largest \emph{positive real} constant $L$ such that $f(n) = \Omega(n^L)$. Otherwise, state that $H$ or $L$ do not exist. All logarithms have base $2$. Your answer should consist of: (1) the correct value of $H$, (2) a proof that $f(n)$ is $O(n^H)$, (3) the correct value of $L$, (4) a proof that $f(n)$ is $\Omega(n^L)$.
  \begin{enumerate}
    \item $f(n) = \frac {n\log n}{\log (\log n)}$
    \item $f(n) = \sum_{k=1}^n \sqrt{k}\cdot\sqrt{n-k}$
    \item $f(n) = \sum_{k=1}^n k\cdot 2^{-k}$
    \item $f(n) = \sum_{k=1}^n k \log k$
    \item $f(n) = \sum_{k=1}^n \frac{n-k}{k}$
  \end{enumerate}

  \thesoln{
  }

\item \textbf{(15 points) Asymptotics.}
  Let $n = k^2$ for some positive integer $k$.  A Sudoku solution is an $n \times n$ array of numbers, each in the set $\{1,...,n\}$, such that the same number does not occur  (1) twice in a row, (2) twice in a column, and (3) twice in a $k \times k$ square ``box'', where the whole array is divided into $n$ such boxes.  Given an array as input, we want to determine whether it is a Sudoku solution.
\begin{enumerate}
  \item (5 points) Give an $\Theta(n^3)$ time algorithm to solve this problem.
  \item (5 points) Give an $\Theta(n^2)$ time algorithm to solve this problem.
  \item (5 points) Give an $\Omega(n^2)$ lower bound for this problem, by showing that
any algorithm that makes fewer than $n^2$ queries to entries of the input array
cannot be correct for all possible inputs.
\end{enumerate}

\thesoln{

}

\ifx\IsSolution\undefined\else\newpage\fi 

\item \textbf{(20 points) Paths of Particular Lengths}
  Let $G$ be an undirected graph, and $s$ and $t$ be arbitrary vertices of $G$. We are interested in the possible lengths of (non necessarily) simple paths from $s$ to $t$.
  \begin{enumerate}
  \item (10 points). Design an algorithm that determines whether there is a path from $s$ to $t$ with an odd number of edges. Do the same for an even number of edges. Argue that your algorithm is correct and analyze its running time.

   \item (10 points). Design an algorithm that determines whether there is a path from $s$ to $t$ whose length is divisible by 3. Argue that your algorithm is correct and analyze its running time. (Hint: It might be useful to also consider paths whose length is 1 or 2 modulo 3).
\end{enumerate}    

\thesoln{
}

\item  \textbf{(20 points) (Non)overlapping Intervals}. We must schedule $n$ jobs, each with a starting time $s_i$ and a finishing time $f_i$. We have a set of constraints of the following form, each about a pair of jobs:
  \begin{itemize}
  \item job $i$ finishes before job $j$ starts
  \item jobs $i$ and $j$ at least partially overlap.
  \end{itemize}
We want to assign each of the $n$ starting and finishing times a \emph{distinct} integer value from 1 to $2n$, satisfying these constraints. Design an algorithm that produces such an assignment or reports that this is not possible.  Argue that your algorithm is correct and analyze its running time.
\thesoln{
  
}

\item \textbf{(0 points).} How long did it take you to complete this assignment?

\end{enumerate}


\end{document}
