
\documentclass[12pt]{article}

%\setlength{\parindent}{2em}
%\setlength{\parskip}{0ex}
\usepackage[letterpaper,hmargin=1in,vmargin=1in]{geometry}

\newcommand{\comment}[1]{}
\newcommand{\var}[1]{\mbox{\em #1}}
%\pagestyle{empty}

\begin{document}

\bibliographystyle{plain}
\setcounter{page}{1}
%\pagestyle{myheadings}
%\markright{\protect\small DRAFT \hfill\today\hfill page \protect\arabic{page}}

\normalsize

\begin{center}
\LARGE
Homework 1\\
\normalsize 
\ \\
CS 3343 -- Fall 2006 \hfill assigned August 24, 2006 \\
Tom Bylander, Instructor \hfill due September 1, 2006
\end{center}

Your solutions must be submitted as a document to WebCT.  Part of what
you should learn this semester is being able to write mathematical
expressions using some word processing software.  For example,
both Microsoft Word and OpenOffice have equation editors.

I prefer to use Latex, which admittedly has a higher learning curve
than Word or OpenOffice.  Applying the command {\tt pdflatex} to this
file {\tt hw1.tex} (which you can download from the web site), results
in the PDF file {\tt hw1.pdf}.

\begin{enumerate}

\item (20 pts.)
Prove the equality $\mbox{gcd}(m, n) = \mbox{gcd}(n, m - n)$ for
positive integers $m$ and $n$, assuming $m \geq n$.

\item (20 pts.)  Do Exercise 1.2.9, making at least one improvement.

Consider the following algorithm for finding the distance between the
two closest elements in an array of numbers.

\begin{tabbing}
\ \ \ \ \=\ \ \ \ \=\ \ \ \ \=\ \ \ \ \=\kill
{\bf algorithm} $\var{MinDistance}(A[0..n-1])$ \+\\
// Input: Array $A[0..n-1]$ of numbers \\
// Output: Minimum distance between two of its elements \\
\var{dmin} $\leftarrow$ $\infty$ \\
{\bf for} $i$ $\leftarrow$ $0$ to $n-1$ {\bf do} \\
\> {\bf for} $j$ $\leftarrow$ $0$ to $n-1$ {\bf do} \\
\>\> {\bf if} $i \neq j$ {\bf and} $|A[i]-A[j]| < \var{dmin}$ {\bf
  then} \\
\>\>\> \var{dmin} $\leftarrow$ $|A[i]-A[j]|$ \\
{\bf return} \var{dmin}
\end{tabbing}

Make at least one improvement in this algorithm.


\item (20 pts.)  Do Exercise 1.3.1, but, for part a, sort the list
$47, 14, 98, 81, 35, 60$ instead and be sure to show the values in the
\var{Count} array.

Consider the following algorithm for the sorting problem that sorts
an array by counting, for each of its elements, the number of smaller
elements and then uses this information to put the element in its
appropriate position in the sorted array:

\newpage

\begin{tabbing}
\ \ \ \ \=\ \ \ \ \=\ \ \ \ \=\ \ \ \ \=\kill
{\bf algorithm} $\var{ComparisonCountingSort}(A[0..n-1])$ \+\\
// Sorts an array by comparison counting \\
// Input: Array $A[0..n-1]$ of orderable values \\
// Output: Array $S[0..n-1]$ of $A$'s elements sorted in nondecreasing
order \\
{\bf for} $i$ $\leftarrow$ $0$ to $n-1$ {\bf do} \\
\> $\var{Count}[i]$ $\leftarrow$ $0$ \\
{\bf for} $i$ $\leftarrow$ $0$ to $n-2$ {\bf do} \\
\> {\bf for} $j$ $\leftarrow$ $i+1$ to $n-1$ {\bf do} \\
\>\> {\bf if} $A[i] < A[j]$ {\bf then} \\
\>\>\> $\var{Count}[j]$ $\leftarrow$ $\var{Count}[j] + 1$ \\
\>\> {\bf else} $\var{Count}[i]$ $\leftarrow$ $\var{Count}[i] + 1$ \\
{\bf for} $i$ $\leftarrow$ $0$ to $n-1$ {\bf do} \\
\> $S[\var{Count}[i]]$ $\leftarrow$ $A[i]$
{\bf return} $S$
\end{tabbing}

\begin{enumerate}
\item Apply this algorithm to the list $47, 14, 98, 81, 35, 60$.
\item Is this algorithm stable?
\item Is it in place?
\end{enumerate}

\item (20 pts.) Do Exercise 1.4.4.

\begin{enumerate}
\item Let $A$ be an adjacency matrix of an undirected graph.  Explain
  what property of the matrix indicates that
\begin{enumerate}
\item the graph is complete.
\item the graph has a loop, i.e., an edge connecting a vertex to
  itself.
\item the graph has an isolated vertex, i.e., a vertex with no edges
  incident to it.
\end{enumerate}
\item Answer the same questions for the adjacency list representation.
\end{enumerate}

\item (20 pts.) Do Exercise 1.4.6.

Prove the inequalities that bound the height of a binary tree with $n$
vertices:
\[\lfloor \log_2 n \rfloor \leq h \leq n-1\]

To show the lower bound, show that a binary tree of height $h$ can
  have up to $2^{h+1}-1$ vertices (use summation formula 5 on p.\ 470,
  which is also the 4th summation formula on slide 13 of my Chapter 2
  notes).  This implies that one more vertex requires a binary tree of
  height $h+1$.  Show how the lower bound corresponds to this
  property.

\end{enumerate}

\end{document}
