
\documentclass[10pt]{article}
\usepackage{amsmath,amssymb,amsfonts,fancyhdr}
\usepackage{graphicx}
%
\setlength{\textheight}{9in}
\setlength{\topmargin}{-0.75in}
\setlength{\textwidth}{7in}
\setlength{\oddsidemargin}{-.2in}
\setlength{\parskip}{6pt}
\setlength{\parindent}{0pt}
%\usepackage{fullpage}
\renewcommand{\headrulewidth}{1pt}
\setlength{\headsep}{1cm}
% \usepackage{txfonts}
\usepackage{newcent}
\usepackage{fancyhdr}
\usepackage{multirow}   % for table formatting
\usepackage{hyperref}  % for weblinks in pdf
\usepackage{color}


\newcommand{\ds}{\displaystyle} 
\newcommand{\QQ}{{\mathbb{Q}}} % blackboard Q
\newcommand{\Qbar}{{\overline{\QQ}}} % blackboard Qbar
\newcommand{\ZZ}{{\mathbb{Z}}} % blackboard Z
\newcommand{\NN}{{\mathbb{N}}} % blackboard N
\newcommand{\CC}{{\mathbb{C}}} % blackboard C
\newcommand{\Zplus}{{\mathbb{Z^+}}} % blackboard Z^+
\newcommand{\FF}{{\mathbb{F}}} % blackboard F
\newcommand{\RR}{{\mathbb R}} % blackboard R
\newcommand{\Rtwo}{{\mathbb R^2}} % blackboard R^2
\newcommand{\Rthree}{{\mathbb R^3}} % blackboard R^3
\newcommand{\RRn}{{\mathbb R^n}} % blackboard R^n
\newcommand{\D}{{\cdot}} % dot
\def\mod#1{{\ifmmode\text{\rm\ (mod~$#1$)}
\else\discretionary{}{}{\hbox{ }}\rm(mod~$#1$)\fi}}

\def\tra{70}   %text rotation angle in table header
\newcommand{\tablehead}[1]{\rotatebox{\tra}{\scriptsize{#1}}}


%----------- colours ------------------------------------------------------
\definecolor{SFUred}{rgb}{0.60547,0.140625,0.17969}
\definecolor{SFUblue}{rgb}{.1602, .2734, .51172}
\definecolor{SFUgrey}{rgb}{.28515,.28125,.25}

\definecolor{DarkOlive}{rgb}{0.1047,0.2412,0.0064}
\definecolor{FireBrick}{rgb}{0.5812,0.0074,0.0083}
\definecolor{RoyalBlue}{rgb}{0.0236,0.0894,0.6179}
\definecolor{RoyalGreen}{rgb}{0.0236,0.6179,0.0894}
\definecolor{RoyalRed}{rgb}{0.6179,0.0236,0.0894}
\definecolor{LightBlue}{rgb}{0.8544,0.9511,1.0000}
\definecolor{Black}{rgb}{0.0,0.0,0.0}
\definecolor{FuncColor}{rgb}{1.0,0.0,0.0}
%------------------------------------------------------------------------------
%amps stuff

\usepackage{enumitem}
\setlist[enumerate,1]{label=}
\setlist[enumerate,2]{label=\Alph*.}
\setlist[enumerate,3]{label=\arabic*.}
\setlist[enumerate,4]{label=(\alph*)}



% check boxes for after instructors questions
\newcommand{\insertcheckboxes}{
\begin{center}
\fbox{
\begin{tabular}{ccccc}
done & checked & corrected & study MT & study final \\
$\Box$ &  $\Box$ &  $\Box$ &  $\Box$ &  $\Box$
\end{tabular}
}
\end{center}
}


% standard basis vectors
\def\ihat{{\bf \hat{i}}}
\def\jhat{{\bf \hat{j}}}
\def\khat{{\bf \hat{k}}}

% vector arrow notation
\newcommand{\bvec}[1]{\vec{ \bf #1}}  % absolute value


\newcommand{\twovec}[2]{\left[\begin{array}{c}
#1\\ #2\\ \end{array}\right] }

\newcommand{\closethegap}{\vspace{-2mm} }

\newcommand{\threevec}[3]{\left[\begin{array}{c}
#1\\ #2\\ #3\\ \end{array}\right] }

\newenvironment{soln}[1]{ \textit{Solution #1:} }{\medskip}

\pagestyle{plain}

%------------------------------------------------
% different types of labels for lists
% 1) type of label
%\renewcommand{\theenumi}{\arabic{enumi}}   default
%\renewcommand{\theenumi}{\alph{enumi}}     alphabetic   (use capital A for capitals)
%\renewcommand{\theenumi}{\roman{enumi}}    roman numerals (use capital R for capitals)
%
% 2) formatting of label
%\renewcommand{\labelenumi}{(\theenumi)}   default
%\renewcommand{\labelenumi}{\theenumi.}    remove brackets
%------------------------------------------------


%------------ Course Information --------------------
\newcommand{\coursename}{Discrete Mathematics II}
\newcommand{\coursenum}{MACM 201 - D100 and D200}
\newcommand{\semester}{Fall 2020}
\newcommand{\instructor}{Jamie Mulholland and Michael Monagan}

%------------ Assignment Information -------------
\newcommand{\anum}{4}
\newcommand{\duedate}{Wednesday October 21, 2020}
\newcommand{\duetime}{11:59pm}

\pagestyle{fancy}
\fancyhf{} 

\lhead{\includegraphics[height=1cm]{logo.png}}
\rhead{\large\sc \coursenum ~~~~Assignment\ \#\anum}
\lfoot{ {\color{blue} \sc Copyright \instructor}}
\rfoot{{\small \thepage}}



%--------- Now for the document ----------------------------------

\begin{document}

\hfill{{\bf Due Date}:     \duedate, \duetime}

%\vspace{-0.75cm}
\subsection*{Instructions}


Answer all questions on paper or a tablet using your own handwriting.
Put your name, student ID number and page number at the top of each page.
If you use paper make a photo of each page and upload your solutions to crowdmark.
If you use a tablet, export your assignment to .pdf and upload the .pdf to crowdmark.

\begin{enumerate}
\item { \color{SFUred} \textbf{Textbook Reading} }  

	\begin{itemize}
	\item[$\bullet$] Sections: 10.2, 10.3
        \item[$\bullet$] DivAndConq.pdf notes (under Lecture Notes on Canvas)
        \end{itemize}

%=====================================
% Definitions, Concepts & Keywords
%=====================================
\item {\color{SFUred}\textbf{Definitions, Concepts \& Keywords}}

   \begin{itemize}
   \item[$\bullet$] Solve first order non-homogenous recurrences.
   \item[$\bullet$] Solve some second order non-homogenous recurrences.
   \item[$\bullet$] Understand the Mergesort algorithm.
   \item[$\bullet$] Solve recurrences arising from divide-and-conquer algorithms.
   \end{itemize}

\item {\color{SFUred}\textbf{Exercises}}

   \begin{enumerate}

   %=====================================
   % Textbook Questions
   %=====================================	
   \item  \textbf{Textbook Questions} 

        \begin{itemize}
        \item[]  Section 10.2 Exercises 1(abd), 4, 24.
                 % Exercise 24 is a_n = a_{n-1} + 2 a_{n-2} which exercises 
                 % the characteristic polynomial two real roots case
        \item[]  Section 10.3 Exercises 1(b+d), 8.
        \end{itemize}


   \item \textbf{Questions}

   \begin{enumerate}

  \item  Second order recurrence.

  \item  John takes out a bank loan for \$20,000 to buy a car.
         If the bank charges interest at 10\% per year, and John
         has to pay back the loan over 5 years, how much does he
         have to pay every year?  Use a recurrence.  See example 10.29.

  \item  Consider the complete bipartite graph $K_{n,n}$ with $2n$ vertices.
         Let $k_n$ be the number of edges in $K_{n,n}$.  
         Draw $K_{1,1}$, $K_{2,2}$ and $K_{3,3}$ and determine $k_1$, $k_2$, $k_3$.
         Give a recurrence relation for $k_n$ and solve it using an initial value.
         % k_n = k_n-1 + 2n - 1, k_1 = 1, k_2 = 4, k_3 = 9, k_n = n^2

  \item  Calculate a formula for $\sum_{i=0}^{n-1} 3i^2$ and $\sum_{k=0}^{n-1} k 2^k.$
         % (n-1) n (2n-1)/2 and (n-2) 2^n+2

  \item  Solve the recurrence $a_n = 2a_{n-1} + 4^{n-1}$ with $a_1=3$.

  \item Below is C code and Python code for an algorithm.

\medskip
\begin{minipage}{8cm}
C code:
\begin{verbatim}
1: long crazy(long A[],long n) {
2:    long i,s,t;
3:    if( n<=1 ) return A[0];
4:    s = crazy(A,n-1);
5:    for( i=1; i<n; i++ )
6:       A[i-1] += A[i];
7:    t = crazy(A,n-1);
8:    return s+t;
9: }
\end{verbatim}
\end{minipage}
\begin{minipage}{5cm}
Python code:
\begin{verbatim}
1: def crazy(A,n):
2:      if n<=1:
3:          return A[0]
4:      s = crazy(A,n-1)
5:      for i in range(1,n):
6:          A[i-1] += A[i]
7:      t = crazy(A,n-1)
8:      return s+t

\end{verbatim}
\end{minipage}

\medskip

    \noindent
    Let $c_n$ be the number of times line 6 is executed.
    Assume $n = 2^k$ and $k \ge 0$.
    Give a recurrence equation and an initial value for $c_n$ and solve it.
    Check that $c_4 = 13.$
    Note, you do not need to know what the code is doing to answer this question.
    % c_n = 2 c_n-1 + n-1, c_1 = 0, c_n = 2^n-n-1

  \item  Consider the recurrence $a_n - 3 a_{n-1} + 2 a_{n-2} = n$.
         \begin{itemize}
         \item[(a)] Find the general solution to the associated homogenous recurrence.
         \item[(b)]  Find a particular solution.
         \item[(c)] Find the solution for $a_0 = a_1 = 1$.
         \end{itemize}

  \item  Consider the recurrence $a_n - a_{n-1} - 6 a_{n-2} = 5 \cdot 3^n$.
         \begin{itemize}
         \item[(a)] Find the general solution to the associated homogenous recurrence.
         \item[(b)]  Find a particular solution.
         \item[(c)] Find the solution for $a_0=7$ and $a_1 = 5$.
         \end{itemize}

  \item The recurrence for number of comparisons that the Mergesort 
        algorithm does, assuming $n=2^k$, is $C(n) \le 2 \, C(n/2)+ n-1$ and $C(1)=0$.
        If the input array $A$ is already sorted, give a recurrence for the number of
        comparisons Mergesort does and solve it.  Hint: how many comparisons does
        the merging algorithm do?
        % C(n) = 2C(n/2) + n/2, C(1)=0, C(n) = n/2 log_2 n$.

  \item Below is C code and Python code for an algorithm.

\begin{minipage}[t]{8.5cm}
C code
\begin{verbatim}
1: int locate(int A[],int s,int n,int x)
2: {  if( n==1 )
3:       if( A[s]==x ) return s;
4:       else return -1;
5:    int j,k,m = n/2;
6:    j = locate(A,s,m,x);
7:    k = locate(A,s+m,n-m,x);
8:    return j>=0 ? j : k;
9: }
\end{verbatim}
\end{minipage}
\quad
\begin{minipage}[t]{6cm}
Python code
\begin{verbatim}
1: def locate(A,s,n,x):
2:   if n==1:
3:      if A[s]==x: return s 
4:      else: return -1
5:   m = n/2;
6:   j = locate(A,s,m,x);
7:   k = locate(A,s+m,n-m,x);
8:   if j>=0: return j
9:   else: return k
\end{verbatim}
\end{minipage}

\medskip

    \noindent
    Let $a_n$ be the number of times the comparison {\tt A[s]==x} is 
    executed in line 3.  For $n=2^k$ with $k\ge 0$ give
    a recurrence equation and initial value for $a_n$ and solve it.
    Note, you do not need to know what the code is doing to answer this question.
    % a_n = 2 a_n-1, a_1 = 1, a_n = 2^{n-1}

   \item For $n=2^k$ the recurrence 
         for a divide-and-conquer algorithm is $$T_n = 4T_{n/2} + 2n ~~{\rm for}~ n>1.$$  
         Solve it for the initial value $T_1=1$.  Check that $T_4 = 40$.

\end{enumerate}
\end{enumerate}

%\item {\color{SFUred}\textbf{ Selected Hints \& Answers}}
%
%Some final answers are given below. This is to provide you instant feedback on the validity of your solution - i.e. a quick check if your line of reasoning is correct. A complete solution requires full justification as to why these are the answers. 
%
\end{enumerate}

\bigskip

\begin{table}[h!]
\begin{center}
\begin{tabular}{ c c c }
$\ds \sum_{i=1}^n i^2 = \ds \frac{n(n+1)(2n+1)}{6}$ & 
\quad  $\ds \sum_{k=1}^n k {n \choose k} = \ds \frac{n}{2} 2^n$ &
\quad $\ds \sum_{k=1}^n k 2^{k-1} = (n-1)2^n + 1.$
\end{tabular}
\end{center}
\caption{Some useful sums}
\end{table}

\end{document}
