\documentclass[a4paper,11pt]{article}
\usepackage{graphicx}
\usepackage{enumerate}
\usepackage{amsmath}

\begin{document}

\begin{flushright}

\vspace{1.1cm}

{\bf\Huge Problem Set 1}

\rule{0.25\linewidth}{0.5pt}

\vspace{0.5cm}
%Put Authors
Justin Ely
\linebreak
\newline
%Put Author's affiliations
\footnotesize{Foundations of Algorithms \\}
% Date here below
June 06, 2016 \\
612-240-0924
\end{flushright}

\noindent\rule{\linewidth}{1.0pt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{1)}
We wish to prove by induction that $\sum_{i=1}^n i^3 = (\frac{n(n+1)}{2})^2$.  We start this by proving a trivial case of n=1.

\begin{equation}
1 = (\frac{1(1 + 1)}{2})^2
\end{equation}

Now we prove that the solution works for n+1.

\begin{eqnarray}
\sum_{i = 1}^{k+1} i^3 &=& \sum_{i=1}^k i^3 + (k+1)^3 \\
&=&(\frac{k (k+1)}{2})^2 + (k+1)^3 \\
&=& \frac{k^2 (k+1)^2}{2^2} + \frac{2^2 (k+1)^3}{2^2} \\
&=& \frac{k^2 (k+1)^2 + 2^2 (k+1)^3}{2^2} \\
&=& k^2(k^2 + 2k + 1) + 2^2(k^2 + 2k + 1)(k+1) \\
&=& k^4 + 2k^3 + k^2 + (4k^2 + 8k + 4)(k+1) \\
&=& k^4 + 2k^3 + k^2 + 4k^3 + 12k^2 + 12k + 4 \\
&=& k^4 + 6k^3 + 13k^2 + 12k + 4 \\
&=& \frac{(k + 1)^2 (k+2)^2}{2^2} \\
&=& (\frac{(k + 1) (k + 1 + 1)}{2})^2
\end{eqnarray}

Which proves the induction hypothesis. \\

Using:
\begin{eqnarray}
(k + 1)(k + 1) &=& k^2 + 2k + 1 \\
(k + 2)(k + 2) &=& k^2 + 4k + 4 \\
(k + 1)^2 (k + 2)^2 &=& k^4 + 6k^3 + 13k^2 + 12k + 4
\end{eqnarray}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{2a)}
\begin{verbatim}
[40, 17, 45, 82, 62, 32, 30, 44, 93, 10]
[17, 40, 45, 82, 62, 32, 30, 44, 93, 10]
[17, 40, 45, 82, 62, 32, 30, 44, 93, 10]
[17, 40, 45, 82, 62, 32, 30, 44, 93, 10]
[17, 40, 45, 62, 82, 32, 30, 44, 93, 10]
[17, 32, 40, 45, 62, 82, 30, 44, 93, 10]
[17, 30, 32, 40, 45, 62, 82, 44, 93, 10]
[17, 30, 32, 40, 44, 45, 62, 82, 93, 10]
[17, 30, 32, 40, 44, 45, 62, 82, 93, 10]
[10, 17, 30, 32, 40, 44, 45, 62, 82, 93]
\end{verbatim}

\section*{2b)}
\begin{verbatim}
Split:  [75, 56, 85, 90, 49, 26, 12, 48, 40, 47]  ->
        [75, 56, 85, 90, 49] & [26, 12, 48, 40, 47]
Split:  [75, 56, 85, 90, 49]  ->
        [75, 56] & [85, 90, 49]
Split:  [75, 56]  ->
        [75] & [56]
Merge: [75] with [56] -> [56, 75]
Split:  [85, 90, 49]  ->
        [85] & [90, 49]
Split:  [90, 49]  ->
        [90] & [49]
Merge: [90] with [49] -> [49, 90]
Merge: [85] with [49, 90] -> [49, 85, 90]
Merge: [56, 75] with [49, 85, 90] -> [49, 56, 75, 85, 90]
Split:  [26, 12, 48, 40, 47]  ->
        [26, 12] & [48, 40, 47]
Split:  [26, 12]  ->
        [26] & [12]
Merge: [26] with [12] -> [12, 26]
Split:  [48, 40, 47]  ->
        [48] & [40, 47]
Split:  [40, 47]  ->
        [40] & [47]
Merge: [40] with [47] -> [40, 47]
Merge: [48] with [40, 47] -> [40, 47, 48]
Merge: [12, 26] with [40, 47, 48] -> [12, 26, 40, 47, 48]
Merge: [49, 56, 75, 85, 90] with [12, 26, 40, 47, 48] ->
       [12, 26, 40, 47, 48, 49, 56, 75, 85, 90]
\end{verbatim}

\section*{2c)}
With $\frac{n}{k}$ sublists, each of length $k$ elements, and insertion sort running at order $\Theta (n^{2})$; then $\frac{n}{k} k^2 = nk $ to sort all lists.

\section*{2d)}
Each merge operation takes on order n, operating $log(\frac{n}{k})$ times, thus merging all the sublists takes: $\Theta(n log(\frac{n}{k}))$.

\section*{2e)}
The largest value of k will come when the modified merge sort is of the same order as the standard merge sort, which happens when:

\begin{equation}
\Theta(n log(n)) = \Theta(nk + nlog\frac{n}{k})
\end{equation}

We know that k cannot be a higher order than $log(n)$, as this would cause the modified algorithm to have a higher order
worst-case growth rate than the standard merge sort.  We can thus make the assumption that $k=log(n)$ is the valid solution,
and test.

\begin{eqnarray}
\Theta(n log(n)) &=& \Theta(nk + nlog(\frac{n}{k})) \\
&=& \Theta(nk + nlog(n) - nlog(k)) \\
&=& \Theta(nlog(n) + nlog(n)- nlog(log(n))) \\
&=& \Theta(2nlog(n) - nlog(log(n))) \\
&=& \Theta(nlog(n))
\end{eqnarray}

\section*{2f)}
In practice, a modified merge sort is chosen to execute in a faster time than a standard merge sort.  Therefore, k should be
chosen to to be the maximum size for which insertion sort is faster than merge sort.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{3)}
My implementation is in python; as I find it very close to pseudo-code in simple cases - with the added benefit that i can run it
on sample cases.  Please let me know if this is an issue and I can modify my approach in the future.

The algorithm loops over the nodes and adjacent nodes a maximum of only 1 time each; thus running in $\Theta(n + m)$.
The out-degree is a simple count of the adjacency list, while the in-degree must be tracked and summed from all adjacency lists
as the nodes are iterated.

\begin{verbatim}
def compute_degree(adj_list):
    """Computer the in-degree and out-degree of a given node"""
    out_counts = {}
    in_counts = {}

    for node in adj_list:
        out_counts[node] = len(adj_list[node])

        for other_node in adj_list[node]:
            if not other_node in in_counts:
                in_counts[other_node] = 0
            in_counts[other_node] += 1

    return out_counts, in_counts
\end{verbatim}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{4}
For a path to be a valid binary search tree, each left subtree must be strictly smaller than it's root, and every right subtree must
be strictly larger than it's root.  For any of the paths given, if either of these conditions are broken then the path is not a
valid binary search tree.

\section*{4.1)}
This path is valid.

\section*{4.2)}
This path is valid.

\section*{4.3)}
This path is invalid.  The node 912 appears on the left subtree of node 911, which violates the binary search conditions.

\section*{4.4)}
This path is valid.

\section*{4.5)}
This path is invalid.  The node 299 appears on the right subtree of node 347, which violates the binary search conditions.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{5)}
Lets assume that the number of executions takes the form $2n + 1$.  Solving the simple base case of $n=0$:

\begin{equation}
2(0) + 1 = 1
\end{equation}

Which agrees with a tree of 0 nodes, for which only the initial conditional will be executed a single time.

For $n > 0$, execution will happen once on the root, pluse $2n+1$ for the left subtree and $2n+1$ for the right subtree:

\begin{eqnarray}
N_{tot} &=& 1 + (2n_l + 1) + (2n_r + 1) \\
&=& 1 + (2n_l + 2n_r + 2) \\
&=& 1 + 2(n_l + n_r + 1)
\end{eqnarray}

Using the total number of nodes in the tree as $n = n_l + n_r + 1$:

\begin{equation}
N_{tot} = 1 + 2n
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}


\documentclass[11pt]{article}

\usepackage{alltt,fullpage,graphics,color,epsfig,amsmath, amssymb}
\usepackage{hyperref}
\usepackage{boxedminipage}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{amsmath}
\usepackage{commath}
\usepackage{graphicx}
\graphicspath{ {.} }
\newcommand{\floor}[1]{\lfloor #1 \rfloor}
\newcommand{\ceil}[1]{\lceil #1 \rceil}

\title{CS 512 Assignment 1}
\author{Daniel Campos}
\date{March 5th,2021}
\begin{document}

\maketitle
\section{Problem 1}
\subsection{List two differences between Frequent Pattern Growth algorithm and Apriori
algorithm
}
Apriori requires K+1 scans of the database while FPGrowth requires 2. The apriori algorithm is a breath first search while FPGrowth is divide and conquer.  Apriori requires a large amount of memory and FPGrowth does not.
\subsection{Prove all nonempty subsets of a frequent itemset must also be frequent and prove the support of any nonempty subset s' of itemset s must be at least as large as the support of s.}
\subsubsection{Answer Prove all nonempty subsets of a frequent item list must be frequent}
\begin{enumerate}
    \item Let $S = {i_0, i_1,..., i_n}$ be a set of items.
    \item Let $R = {r_0, r_1, ...,r_m}$ be a set of records where each record $r_j$ is a set of items where $\forall k \in r_j$ $r_{jk} \in S$.
    \item Let $F = {i_0, i_1, ...,i_k}$ where each $\forall k \in F$ $F_k \in S$
    \item A frequent item set contains items where their occurrence is $>= minsup$ where minsup represents a minimum support threshold where only items that occur at or more than this threshold are in $F$.
    \item Let $ss$ represent possible non empty subsets of $F$
    \item Since every possible $ss \in F$ all items in $ss$ occur $>= minsup$ and therefore all items $\in ss$ must all be frequent as well.
    \item As a result, all non empty subsets of a frequent itemset must be frequent.
\end{enumerate}
\subsubsection{Prove any nonempty subset s must be at least as large as the support of s}
\begin{enumerate}
    \item Let $S = {i_0, i_1,..., i_n}$ be a set of items.
    \item Let $R = {r_0, r_1, ...,r_m}$ be a set of records where each record $r_j$ is a set of items where $\forall k \in r_j$ $r_{jk} \in S$.
    \item Given all non-empty subsets of $S$ where $s' = {i_1, i_k, ...}$ where $s' \in S$.
    \item Since the support of a itemset is driven by how often its items occur in $R$ the support of $S$ is that of its least common item.
    \item Since all items $i_k \in ss$ are also in $S$ the support for $i_k \in s'$ > = support($S$).
    \item Since $\forall i_k \in s'> support(S)$ then we know $support(s') \ge support(s)$
    \item Thus all non-empty subsets $s'$ must be at least as large as the support of $S$
\end{enumerate}
\subsection{What problem might lift and $\chi^2$ have? Explain why Jaccard function is null-invariant}
Lift and $X^2$
\subsubsection{What problems do $Lift$ and $\chi^2$ have}
Neither deal way with datasets that have large amounts of null transaction for the variables being compared. In other words if we are studying the correlation between variable A and B and both A and B do not have a balanced occurrence(in other words $\neg A > A$ and $\neg B > B$ both $\chi^2$ and $Lift$ will not be good measures.
\subsubsection{Why is Jaccard function null-invariant}
The Jaccard function is null invariant because it is only looking at when variables occur and does not use information from where the variables do not occur. To build on our example from part a: Jaccard function looks at $A$ and $B$ and ignores $\neg A$ and $\neg B$. This makes Jaccard null-invariant.
\subsection{Under what circumstances will PrefixSpan also have poor performance}
Since the major cost in prefixspan is Constructing projected dbs and Suffixes are largely repeating, if the target records include a large quantity of unique items then storing the physical DB in memory can be prohibitive and require projections. In short when the transaction dataset is large and there are a large amount of prefixes(say items on amazon).
\section{Problem 2: Apriori vs Frequent Pattern Growth}
\subsection{Present the intermediate results of the first three scans and the final derived frequent itemsets.}
\subsubsection{First Scan}
See results in table \ref{tab:1st}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
        Itemset & Support \\ \hline
        A & 4 \\ \hline
        B & 3 \\ \hline
        C & 5 \\ \hline
        F & 4 \\ \hline
        M & 3 \\ \hline
        P & 3 \\ \hline
    \end{tabular}
    \caption{Apriori first scan}
    \label{tab:1st}
\end{table}
\subsubsection{Second Scan}
See results in table \ref{tab:2nd}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
        Itemset & Support \\ \hline
        A,C & 4 \\ \hline
        A,M & 3 \\ \hline
        A,P & 3 \\ \hline
        C, F & 3 \\ \hline
        C, M & 3 \\ \hline
        C, P & 3 \\ \hline
    \end{tabular}
    \caption{Apriori second scan}
    \label{tab:2nd}
\end{table}
\subsubsection{Third Scan}
See results in table \ref{tab:3rd}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
        Itemset & Support \\ \hline
        A, C, M & 3  \\ \hline
        A, C, P & 3  \\ \hline
    \end{tabular}
    \caption{Apriori third scan}
    \label{tab:3rd}
\end{table}
\subsubsection{Final derived frequent itemsets}
See results in table \ref{tab:final}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
        Itemset & Support \\ \hline
        A & 4 \\ \hline
        B & 3 \\ \hline
        C & 5 \\ \hline
        F & 4 \\ \hline
        M & 3 \\ \hline
        P & 3 \\ \hline
        A,C & 4 \\ \hline
        A,M & 3 \\ \hline
        A,P & 3 \\ \hline
        C, F & 3 \\ \hline
        C, M & 3 \\ \hline
        C, P & 3 \\ \hline
        A, C, M & 3  \\ \hline
        A, C, P & 3  \\ \hline
    \end{tabular}
    \caption{Apriori Derived frequent itemsets}
    \label{tab:final}
\end{table}
\subsection{Frequent Pattern}
First off we go ahead and sort our items by frequency giving us the ordering: C(5), A(4), F(4), B(3), M(3), P(3).
Using the updated frequency sorted ordering the updated transactional database is found in table \ref{tab:sorted}.
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
        TID & ITEMS \\ \hline
        1 & {C, A, B, P} \\ \hline
        2 & {C, A, B, M} \\ \hline
        3 & {F, B} \\ \hline
        4 & {C, A, F, M, P} \\ \hline
        5 & {C, A, F, M, P} \\ \hline
        6 & {C, F} \\ \hline
    \end{tabular}
    \caption{FPGrowth updated and sorted transaction database}
    \label{tab:sorted}
\end{table}
Using this updated transaction database we create the pattern tree found in figure \ref{fig:fptree}.
\begin{figure}[]
\centering1
\includegraphics[width=8cm]{Assignments/Assignment1/FPtree.png}
\caption{Frequent Pattern Tree}
\label{fig:fptree}
\end{figure}
\subsection{Association rules}
Top 3 association rules and their confidence. \\
$M \rightarrow A = 1.0$ \\
$A \rightarrow C = 1.0$ \\
$P \rightarrow C = 1.0$ \\
\section{Problem 3: Frequent Pattern Mining Implementation}
\subsection{Data statistics}
\begin{table}[]
    \centering
    \resizebox{\textwidth}{!}{%
    \begin{tabular}{|c|c|} \hline
       Product Types  & 169  \\ \hline
       Transactions & 9835 \\ \hline
       Average Products per transactions & 4.41 \\ \hline
       Top 5 most popular products  & Whole milk(2513), other vegetables(1903), rolls/buns(1809), soda(1715), yogurt(1372) \\ \hline
       Items with single purchase & baby food, sound storage medium  \\ \hline
    \end{tabular}}
    \caption{Grocery dataset statistics}
    \label{tab:stats}
\end{table}
\subsection{Results }
Using the minimum support values of 0.05 and confidence of 0.25 the top 10 frequent item sets:
\begin{enumerate}
    \item whole milk: 0.26
    \item other vegetables:0.19
    \item rolls/buns:0.184
    \item soda:0.174
    \item yogurt:0.140
    \item bottled water: 0.11
    \item root vegetables: 0.109
    \item tropical fruit: 0.11
    \item shopping bags: 0.10
    \item sausage:0.09
\end{enumerate}
Using the same thresholds the inferred rules are:
\begin{enumerate}
    \item (whole milk) $\rightarrow$ (other vegetables) : $0.29$
    \item (rolls/buns) $\rightarrow$ (whole milk) : $0.29$
    \item (other vegetables) $\rightarrow$ (whole milk) : $0.29$
    \item (yogurt) $\rightarrow$ (whole milk) : $0.40$
\end{enumerate}
\subsection{Contingency Table: Yogurt and Whole milk}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|c|c| } \hline
       Item & Yogurt & $\neg$ Yogurt & Total  \\ \hline
       Whole Milk & 551 & 1962 & 2513 \\ \hline
       $\neg$ Whole Milk & 821  & 6501&  7322\\ \hline
       Total &1372 & 8463 &9835 \\ \hline
    \end{tabular}
    \caption{Caption}
    \label{tab:my_label}
\end{table}
\subsection{Interestingness using Lift}
Let A represent Whole milk and B represent yogurt. \\
\begin{equation}
lift(A,B) = \frac{c(A \rightarrow B)} {s(B)} = \frac{s(A \cup B)}{s(A)*s(B)} =  \frac{551/9836}{(1372/9835)*(2513/9835)}
\end{equation}
\begin{equation}
lift(A, B) = \frac{0.05601871}{(0.13950178)(0.25551601)} = \frac{0.05601871}{0.0356449382134978} = 1.5715754552433814 = 1.57
\end{equation}
Since the Lift of A and B is over one they are positively correlated. \\
\begin{equation}
lift(A,\neg B) = \frac{c(A \rightarrow B)} {s(B)} = \frac{s(A \cup B)}{s(A)*s(B)} =  \frac{1962/9836}{(8463/9835)*(2513/9835)}
\end{equation}
\begin{equation}
lift(A, \neg B) =  \frac{0.1994713298088654}{(0.8604982206405694)(0.25551601)} = \frac{0.1994713298088654}{0.21987107195017794} = 0.9072195266054142 = 0.91
\end{equation}
Since the lift of A and $\neg B$ is under one they are negatively correlated.\\
\subsection{Run time}
My run times comparing our implementation in of Apriori in python vs FP-Growth(via pyfpgrowth)
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|c|} \hline
       Transactions  & Apriori(ms) & FP-Growth(ms)  \\ \hline
       500 & 3.13e-5 & 1.58e-4 \\ \hline
       1000 & 6.37e-5 & 5.98e-5 \\ \hline
       2000 & 1.41e-4 & 7.90e-5 \\ \hline
       5000 & 1.74e-4 & 6.35e-5\\ \hline
       Full & 2.51e-4 & 1.09e-4 \\ \hline
    \end{tabular}
    \caption{FP-Growth vs Apriori execution time}
    \label{tab:my_label}
\end{table}
\section{Problem 4: Sequential and Graph Pattern Mining}
\subsection{Sequential database}
\subsubsection{Projections }
for $\langle ab \rangle$ see table \ref{tab:ab}, for $\langle (ab) \rangle$ see table \ref{tab:(ab)}, for $\langle de \rangle$ see table \ref{tab:de}, for $\langle abc \rangle$ see table \ref{tab:abc}, for $\langle acb \rangle$ see table \ref{tab:acb},for $\langle abca \rangle$ see table \ref{tab:acba}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
       SID  & Sequence  \\ \hline
       1 & $\langle (_cf)(ac)b \rangle$ \\ \hline
       2 & $\langle (_)c(bc)ab(ce) \rangle$ \\ \hline
       3 & $\langle _(acf)acd \rangle$ \\ \hline
       4 & $\langle (_c)(ae) \rangle$ \\ \hline
       5 & $\langle (_d)df(ab) \rangle$\\ \hline
       6 & $\langle (_c)eab(cd)f(abd) \rangle$ \\ \hline
    \end{tabular}
    \caption{Projection for $\langle ab \rangle$}
    \label{tab:ab}
\end{table}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
       SID  & Sequence  \\ \hline
       1 & $\langle  \rangle$ \\ \hline
       2 & $\langle c(bc)ab(ce) \rangle$ \\ \hline
       3 & $\langle  \rangle$ \\ \hline
       4 & $\langle (_c)(ae) \rangle$ \\ \hline
       5 & $\langle (_d)df(ab) \rangle$\\ \hline
       6 & $\langle (_c)eab(cd)f(abd) \rangle$ \\ \hline
    \end{tabular}
    \caption{Projection for $\langle (ab) \rangle$}
    \label{tab:(ab)}
\end{table}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
       SID  & Sequence  \\ \hline
       1 & $\langle  \rangle$\\ \hline
       2 & $\langle  \rangle$ \\ \hline
       3 & $\langle _(af)acb(acf)acd \rangle$ \\ \hline
       4 & $\langle _(abc)(ae) \rangle$ \\ \hline
       5 & $\langle  \rangle$\\ \hline
       6 & $\langle  \rangle$ \\ \hline
    \end{tabular}
    \caption{Projection for $\langle de \rangle$}
    \label{tab:de}
\end{table}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
       SID  & Sequence  \\ \hline
       1 & $\langle (_f)(ac)b \rangle$\\ \hline
       2 & $\langle _(bc)ab(ce) \rangle$ \\ \hline
       3 & $\langle (_f)acd \rangle$ \\ \hline
       4 & $\langle (_)(ae) \rangle$ \\ \hline
       5 & $\langle  \rangle$\\ \hline
       6 & $\langle eab(cd)f(abd) \rangle$ \\ \hline
    \end{tabular}
    \caption{Projection for $\langle abc\rangle$}
    \label{tab:abc}
\end{table}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
       SID  & Sequence  \\ \hline
       1 & $\langle  \rangle$\\ \hline
       2 & $\langle (_c)ab(ce) \rangle$ \\ \hline
       3 & $\langle (acf)acd \rangle$ \\ \hline
       4 & $\langle  \rangle$ \\ \hline
       5 & $\langle (_d)df(ab) \rangle$\\ \hline
       6 & $\langle (cd)f(abd) \rangle$ \\ \hline
    \end{tabular}
    \caption{Projection for $\langle acb \rangle$}
    \label{tab:acb}
\end{table}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|} \hline
       SID  & Sequence  \\ \hline
       1 & $\langle _ \rangle$\\ \hline
       2 & $\langle _b(ce) \rangle$ \\ \hline
       3 & $\langle (_cf)acd \rangle$ \\ \hline
       4 & $\langle  \rangle$ \\ \hline
       5 & $\langle (_b) \rangle$\\ \hline
       6 & $\langle (_bd)  \rangle$ \\ \hline
    \end{tabular}
    \caption{Projection for $\langle acba\rangle$}
    \label{tab:acba}
\end{table}
\subsubsection{Sequential Patterns}
Sequential patterns where support = 3 and length $\ge 3$
aca, abc, acd, dea, eab, bca, abd, afa, bce, add
\subsection{Graph mining}
\subsubsection{Support}
P1 support is $\frac{4}{5}$ and is supported by G2, G3, G4, and G5. \\
P2 support is $\frac{3}{5}$ and is supported by G1, G2, and G5. \\
P3 support is $\frac{3}{5}$ and is supported by G1, G3 and G5.
\subsubsection{Apriori-based}
see figure \ref{fig:graph-apri}
\begin{figure}[]
\centering
\includegraphics[width=8cm]{Assignments/Assignment1/graphapri.png}
\caption{Graph sequence apriori mining}
\label{fig:graph-apri}
\end{figure}
\section{Problem 5: SVM, RBF Kernel}
\subsection{Prove all eigenvalues are non negative}
We know from other work that every semidefinite matrix only has non negative eigenvalues.
For a matrix to be semi-definite $x^TAx \ge 0$ for all $x \in \mathbf{R}^N $ . Since we know that $\text{k}(\bold{x}, \bold{x}') = \bold{x}^\top \bold{x}'$ to ensure semi-definite we ensure A > 0. Since A is the transform its always > 0.
\subsection{Prove RBF Kernel is the sum of infinite polynomial kernels}
A kernel is any function in the form $K(x, y) = \langle \psi(x), \psi(y)\rangle$ where $\psi$ projects $x$ into a new vector space. \\
We are trying to prove that $\psi_{RBF}: \mathbf{R}^N \rightarrow \mathbf{R}^\infty$. \\
WLOG we set $\gamma = 1$
\begin{equation}
\begin{split}
K_{RBF}(x, y)  &= exp [-1 \abs{x -y}^2] \\
& = exp [-1 \langle x -y, x-y \rangle] \\
& = exp [-1 (\langle x, x -y \rangle - \langle y, x-y \rangle)] \\
& = exp [-1 (\langle x, x \rangle - \langle x,y \rangle -\langle y, x \rangle + \langle y,y \rangle)] \\
& = exp [-1 (\abs{x}^2 + \abs{y}^2 - 2 \langle x, y \rangle)] \\
& = exp[-1\abs{x}^2-1\abs{y}^2 ]exp[-1 -2 \langle x, y \rangle] \\ \\
c :&= exp[-1\abs{x}^2-1\abs{y}^2 ] \\ \\
K_{RBF}(x, y) &= c*e^{\lange x, y \rangle} \\
&= c \sum_{n=0}^{\infty} \frac{(\langle x, y \rangle)^n}{n!}  \text{via taylor expansion of $e^x$}  \\
&= c \sum_{n=0}^{\infty} \frac{K_{poly(n)}(x,y)}{n!} \\
\end{split}
\end{equation}
Thus we see that RBF is an infinite sum over poly kernels.
\subsection{Decision Boundary}
If we were to use a large $\gamma$ we would likely be able to separate the training data nearly perfectly. We would be over fitting to training but could likely produce results seen in figure \ref{fig:bound}
\begin{figure}[]
\centering
\includegraphics[width=8cm]{Assignments/Assignment1/decisionbound.png}
\caption{Decision Boundary using high $\gamma$}
\label{fig:bound}
\end{figure}
\subsection{Effect of large $\gamma$}
If we are to make the $\gamma$ infinitely large we will completely fit the SVM to the training data and overfit the model. Our training accuracy may reach 100\% but our transfer accuracy will suffer. This is because larger RBF kernel bandwidths(which are smaller $\gamma$ values produce a smoother feature space mapping. As we scale our $\gamma$ value we produce a less smooth kernel which will allow us to learn harder decision boundaries but will overfit to the decision boundary of the training corpus.
\section{Problem 6:Feature Selection}
\subsection{Classification Results}
On the test portion accuracy:0.68, precision:0.76, recall:0.38, F1 score:0.51. More detailed breakdown can be found in table \ref{tab:Accuracy}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|c|} \hline
       Reference Label  & Predicted 0 & Predict 1   \\ \hline
       0 & 60 & 6 \\ \hline
       1 & 31 & 19 \\ \hline
    \end{tabular}
    \caption{SVM}
    \label{tab:Accuracy}
\end{table}
\subsection{Fisher Score Feature Selection}
Using Fisher Score calculation and a threshold of $\ge$ 1 we find that only columns [8, 11, 18, 26] pass the threshold. We then remove all the other indexes and retrain our svm. Performance on train goes from 0.81 to 0.94 and performance on test goes from 0.68 to 0.84!  On the test portion accuracy:0.84, precision:0.88, recall:0.72, F1 score:0.79 . More detailed breakdown can be found in table \ref{tab:Accuracy2}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|c|} \hline
       Reference Label  & Predicted 0 & Predict 1   \\ \hline
       0 & 61 & 5 \\ \hline
       1 & 36 & 14 \\ \hline
    \end{tabular}
    \caption{Fisher Score Augmented SVM}
    \label{tab:Accuracy2}
\end{table}
\section{Problem 7:Self Training}
Using self training we first train an svm based on the regular training corpus(non fisher score adjusted). Then, we label the unlabeled corpus and only keep data where confidence is $\ge$ 0.6. By that we mean there is over a 0.6 difference between the confidence scores and we set the winning label to the self-trained label. Using this threshold of training we include 322 inferred label samples in our training data. \\
Using this amplified training data we find performance improvements on non Fisher-score optimized models and fisher score optimized models. Without using fisher-score optimization the introduction of self-trained data improves F1 score from 0.51 to 0.55, recall goes from 0.38 to 0.42, precision goes from 0.76 to 0.78. More details available on self training model performance are available in table \ref{tab:Accuracy3}. Using fisher score optimization the amplified training data improves F1 from 0.79 to 0.80, recall goes from 0.72 to 0.74, precision goes from 0.88 to 0.86. More details about fisher+self training model performance are available in table \ref{tab:Accuracy4}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|c|} \hline
       Reference Label  & Predicted 0 & Predict 1   \\ \hline
       0 & 60 & 6 \\ \hline
       1 & 29 & 21 \\ \hline
    \end{tabular}
    \caption{Self-Training Augmented SVM}
    \label{tab:Accuracy3}
\end{table}
\begin{table}[]
    \centering
    \begin{tabular}{|c|c|c|} \hline
       Reference Label  & Predicted 0 & Predict 1   \\ \hline
       0 & 60 & 6 \\ \hline
       1 & 13 & 37 \\ \hline
    \end{tabular}
    \caption{Fisher Score and Self Training Augmented SVM}
    \label{tab:Accuracy4}
\end{table}
\section{Random Walk with Restart}
\subsection{Capture multiple weighted relationships}
Starting with the formula $r c*A*r +(1-c)*e$ from the textbook(chapter 8 page 449) we can see that the RWR leverages information in the adjacency matrix but since the $e$ term is kept constant this modifier allows consistent steering of $r$. In doing so we can ensure that we keep tweaking $r$ to have the initial value for the query seed which allows for multiple path explorations.
\subsection{RWR implemented}
Top 10 nodes sorted by importance starting with a seed node of 42: 160, 121, 82, 107, 62, 434, 249, 183, 86, 166
\end{document}


\documentclass[9pt, a4paper, oneside, reqno]{amsart}

\usepackage[final]{pdfpages}
\usepackage{wrapfig}

\usepackage{enumitem}
\usepackage{parskip}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{multicol}
\renewcommand{\thefootnote}{\fnsymbol{footnote}}

\newcommand{\hint}[1]{\footnote{\raggedleft\rotatebox{180}{Hint: #1\hfill}}}

\pagestyle{fancy}

\newlist{questions}{enumerate}{1}
\setlist[questions, 1]{label = \bf Q.\arabic*., itemsep=1em}

\lhead{\scshape Apurva Nakade}
\rhead{\scshape Honors Single Variable Calculus}
\renewcommand*{\thepage}{\small\arabic{page}}
\title{Problem Set 10}

\begin{document}

\maketitle
\thispagestyle{fancy}

\section*{Part 1 - Taylor series}
\begin{questions}
	\item For the polynomial $ p(x) = x^3$ compute the Taylor polynomial at $x=1$ of degree 3. Simplify the Taylor polynomial and verify that it equals the original polynomial.

	\item Compute the Taylor polynomial of the following functions:
	\begin{multicols}{2}
		\begin{enumerate}
			\item $ e^{\sin x}$ at $ x=0$, degree 3
			\item $ \dfrac{1}{x+1}$ at $ x=0$, degree $ n$
		\end{enumerate}
	\end{multicols}

	\item In this exercise we'll compute the \textbf{remainder term} of the Taylor polynomial. Assume that the function $ f$ is differentiable enough number of times. Consider the Taylor polynomial of the $ f(x)$ at $ x=0$ of degree $ n$.
	\begin{align*}
		P_{n}(x) = a_0 + a_1 x + a_2 x^2 + \cdots + a_n x^n &   & a_i = \dfrac{f^{(i)}(0)}{i!}
	\end{align*}
	\begin{quote}
		\textbf{Theorem.} The remainder term $ R_{n}(x) = f(x) - P_{n}(x)$ equals
		\begin{align*}
			R_{n}(x)  = \int \limits_0^x \dfrac{f^{(n+1)}(t)}{n!} \cdot (x-t)^n \: dt
		\end{align*}
	\end{quote}
	\begin{enumerate}
		\item Prove the theorem directly for $ n = 0$. (Note: $ 0! = 1$.)
		\item Find the Taylor polynomial of $ f'(x)$ at $ x=a$ of degree $ n-1$.
		\item Assume the following Leibniz's identity:
		      \begin{align*}
		      	\mbox{ If } g(x) = \int \limits_0^x f(x,t) \: dt \mbox{ then } g'(x) = f(x,x) + \int \limits_0^x \dfrac{\partial f(x,t)}{\partial x} \: dt
		      \end{align*}
		      (This very useful trick is called \emph{differentiating under the integral sign}, unfortunately we do not have enough language to prove this.)

		      Use part (2) and induction on the degree $ n$ to prove the Theorem.
	\end{enumerate}

	\item This is a very difficult problem to write down rigorously, for this one it's ok to be vague in your arguments. In this problem $ n,k$ are positive integers and all the Taylor series are at $ x=0$. \hint{It is very crucial here that you're only asked to find the derivatives at $ x=0$.}
	\begin{enumerate}
		\item If $ f(x) = x^ng(x)$. Find the Taylor series of $ f(x)$ in terms of Taylor series of $g(x)$ (Don't think too hard). Hence find $ f^{(k)}(0)$ in terms of the derivatives of $ g$.
		\item If $ f(x) = g(x^2)$ find $ f^{(k)}(0)$ in terms of the derivatives of $ g$. Hence find the Taylor series for $ f$ in terms of $ g$.
		\item Find the Taylor series for $ e^{(x^2)}$.
		\item If $ f(x) = g(x^n)$, find $ f^{(k)}(0)$ in terms of the derivatives of $ g$. Hence find the Taylor series of $ f$ in terms of $ g$.
		\item Find the Taylor series for $ \sin (x^4)$.
	\end{enumerate}

\end{questions}

\section*{Part 2 - Numerical Computations}
For this week's HW you're allowed (and required) to use wolframalpha (or calculator) to do the computations/simplify large fractions.

A table of Taylor series can be found on Pg. 426 in the book.
\begin{questions}[resume]
	\item
	\begin{enumerate}
		\item Suppose the coefficients of Taylor series of $ f$ are $ c_i$ and of $ g$ are $ d_i$ at $ x=a$. Find the Taylor series of $ fg$ at $ x=a$ in terms of $ c_i, d_i$.
		\item Find the general formula for the $ n^{th}$ derivative of the product ${ (fg)^{(n)}}(a) $ in terms of the derivatives of $ f$, $ g$ at $ a$.
	\end{enumerate}

	\item Use estimates on the remainder term of the appropriate Taylor series to compute the following quantities within the prescribed error.
	\begin{multicols}{2}
		\begin{enumerate}
			\item $ \cos 1$, error $ < 10^{-2}$
			\item $ \sin 0.01$, error $ < 10^{-10}$
			\item $ e$, error $ < 10^{-2}$
			\item $ \log 1.1$, error $ < 10^{-4}$
		\end{enumerate}
	\end{multicols}

	\item Show that the remainder terms for the Taylor series of $ \log(1+x)$ and $ \arctan(x)$ at $ x=0$ grows with $ n$ when $ x > 1$. (Hence the standard Taylor series cannot be used to approximate these numbers.)

	\item Taylor series are useful in theory but not so great for numerical approximations in practice.
	\begin{enumerate}
		\item Find the value of $ \pi/4$ correct up to 1 decimal place using Taylor series of $ \arctan(x)$. How many terms of the Taylor series will you need, to determine the value of $ \pi/4$ correct up to 2 decimal places?

		\item Prove the following identities:
		      \begin{align*}
		      	\tan(x+y)               & = \dfrac{\tan x + \tan y}{1 - \tan x \tan y}    \\
		      	\arctan(u) + \arctan(v) & = \arctan \left( \dfrac{u + v}{1 - u v} \right) \\
		      \end{align*}
		\item Show that
		      \begin{align*}
		      	\dfrac{\pi}{4} & = \arctan \dfrac{1}{2} + \arctan \dfrac{1}{3}    \\
		      	\dfrac{\pi}{4} & = 4\arctan \dfrac{1}{5} - \arctan \dfrac{1}{239}
		      \end{align*}
		\item Use the second formula in part (3) to compute $ \pi$ correct up to 4 decimal places.
	\end{enumerate}

\end{questions}

\newpage
\section*{Part 3 - Integral Computations}
\begin{questions}[resume]
	\item For this week do Q.3, and Q.4 problems - i) to v) on Pg. 378-379 from Ch.19. \\\\

\end{questions}

\end{document}


\begin{frame}
\titlepage % Print the title page as the first slide
\end{frame}
\section{Part I: Foundations}
\begin{frame}{Welcome!}
\begin{itemize}
 \item This is a 60-hour, PhD-level course on Bayesian inference.
 \item We have 11 planned weeks. Reading material is posted at~\url{https://github.com/maxbiostat/BayesianStatisticsCourse/}
 \item Assessment will be done via a written exam (70\%) and an assignment ($30\%$);
 \item Tenets:
 \begin{itemize}
  \item Respect the instructor and your classmates;
  \item Read before class;
  \item Engage in the discussion;
  \item Don't be afraid to ask/disagree.
 \end{itemize}
 \item Books are
 \begin{itemize}
  \item  \cite{Robert2007};
  \item \cite{Hoff2009};
  \item \cite{Bernardo2000}.
 \end{itemize}
\end{itemize}
\end{frame}

\begin{frame}{Bayes's Theorem}
What do
\begin{equation}
 \label{eq:BT_1}
 \pr(A \mid B) = \frac{\pr(B \mid A)\pr(A)}{\pr(B)},
\end{equation}
and
\begin{equation}
 \label{eq:BT_2}
 \pr(A_i \mid B) = \frac{\pr(B \mid A)\pr(A)}{\sum_{i=1}^n \pr(B \mid A_i)\pr(A_i)},
\end{equation}
and
\begin{equation}
 \label{eq:BT_3}
  p(\theta \mid \boldsymbol{y}) = \frac{l(\boldsymbol{y} \mid \theta)\pi(\theta)}{\int_{\boldsymbol{\Theta}} l(\boldsymbol{y} \mid t)\pi(t) \, dt},
\end{equation}
and
\begin{equation}
 \label{eq:BT_4}
  p(\theta \mid \boldsymbol{y}) = \frac{l(\boldsymbol{y} \mid \theta)\pi(\theta)}{m(\boldsymbol{y})},
\end{equation}
all have in common?
In this course, we will find out how to use Bayes's rule in order to draw statistical inferences in a coherent and mathematically sound way.
\end{frame}
\begin{frame}{Bayesian Statistics is a complete approach}
Our whole paradigm revolves around the posterior:
$$ p(\theta \mid \boldsymbol{x}) \propto l(\theta \mid \boldsymbol{x})\pi(\theta).$$
Within the Bayesian paradigm, you are able to
\begin{itemize}
 \item Perform point and interval inference about unknown quantities;
 \begin{align*}
  \delta(\boldsymbol{x}) &= E_p[\theta] := \int_{\boldsymbol{\Theta}} t p(t \mid \boldsymbol{x} )\,dt,\\
\pr( a \leq \theta \leq b) &= 0.95 = \int_{a}^{b} p(t \mid \boldsymbol{x} )\,dt;
 \end{align*}
\item Compare models:
$$\operatorname{BF}_{12} = \frac{\pr(M_1 \mid \boldsymbol{x})}{\pr(M_2 \mid \boldsymbol{x})} = \frac{\pr(\boldsymbol{x} \mid M_1)\pr(M_1)}{\pr(\boldsymbol{x} \mid M_2)\pr(M_2)};$$
 \item Make predictions: $g(\tilde{x} \mid \boldsymbol{x}) := \int_{\boldsymbol{\Theta}} f(\tilde{x} \mid t)p(t\mid \boldsymbol{x})\,dt$;
 \item Make decisions: $E_p[U(r)]$.
\end{itemize}
\end{frame}
\begin{frame}{Statistical model: informal definition}
Stuff you say at the bar:
\begin{defn}[Statistical model: informal]
\label{def:statistical_model_informal}
DeGroot, def 7.1.1, pp. 377
A statistical model consists in identifying the random variables of interest (observable and potentially observable), the specification of the joint distribution of these variables and the identification of parameters ($\theta$) that index this joint distribution.
Sometimes it is also convenient to assum that the parameters are themselves random variables, but then one needs to specify a joint distribution for $\theta$ also.
\end{defn}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Statistical model: formal definition}
Stuff you say in a Lecture:
\begin{defn}[Statistical model: formal]
\label{def:statistical_model_formal}
\href{https://projecteuclid.org/download/pdf_1/euclid.aos/1035844977}{McCullagh, 2002}.
Let $\mathcal{X}$ be an arbitrary sample space, $\Theta$ a non-empty set and $\mathcal{P}(\mathcal{X})$ the set of all probability distributions on $\mathcal{X}$, i.e. $P : \Theta \to [0, \infty)$, $P \in \mathcal{P}$.
 A \underline{parametric} statistical model is a function $P : \Theta \to \mathcal{P}(\mathcal{X})$, that associates each point $\theta \in \Theta$ to a probability distribution $P_\theta$ over $\mathcal{X}$.
\end{defn}
\textbf{Examples}:
\begin{itemize}
 \item Put $\mathcal{X} = \mathbb{R}$ and $\Theta = (-\infty, \infty)\times (0, \infty)$.
 We say $P$ is a \textit{normal} (or \textit{Gaussian}) statistical model\footnote{Note the abuse of notation: striclty speaking, $P_\theta$  is a probability~\textbf{measure} and not a ~\textit{density} as we have presented it here.} if for every $\theta = \{\mu, \sigma^2\} \in \Theta$,
 $$P_{\theta}(x) \equiv \frac{1}{\sqrt{2\pi}\sigma}\exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right), \: x \in \mathbb{R}.$$
 \item Put $\mathcal{X} = \mathbb{N}\cup \{0\}$ and $\Theta = (0, \infty)$.
 $P$ is a Poisson statistical model if, for $\lambda \in \Theta$,
 $$P_{\lambda}(k) \equiv \frac{e^{-\lambda}\lambda^k}{k!}, \: k = 0, 1, \ldots$$
\end{itemize}
\end{frame}
% \begin{frame}
% Theorem
% $$ \int_{\mathcal{X}} f_X(t)\,dt$$
%  \begin{theo}[b]
%  a
% \end{theo}
% \end{frame}
% \begin{frame}{Overview}
% \tableofcontents
% \end{frame}


\documentclass[12pt]{amsart}
\usepackage{geometry} % see geometry.pdf on how to lay out the page. There's lots.
\geometry{a4paper} % or letter or a5paper or ... etc
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{amsmath}
\usepackage{amsaddr}
\usepackage{hyperref}
\usepackage{dirtytalk}
\usepackage{float}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{enumerate}

\usepackage{color}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{stringcolor}{rgb}{0.7,0.23,0.36}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{keycolor}{rgb}{0.007,0.01,1.0}
\definecolor{itemcolor}{rgb}{0.01,0.0,0.49}

\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=blue,
    urlcolor=blue,
}

\lstdefinestyle{mystyle}{
    %backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{keycolor},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{stringcolor},
    basicstyle=\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}

\lstset{style=mystyle}

\lstdefinelanguage{Swift}{
  keywords={associatedtype, class, deinit, enum, extension, func, import, init, inout, internal, let, operator, private, protocol, public, static, struct, subscript, typealias, var, break, case, continue, default, defer, do, else, fallthrough, for, guard, if, in, repeat, return, switch, where, while, as, catch, dynamicType, false, is, nil, rethrows, super, self, Self, throw, throws, true, try, associativity, convenience, dynamic, didSet, final, get, infix, indirect, lazy, left, mutating, none, nonmutating, optional, override, postfix, precedence, prefix, Protocol, required, right, set, Type, unowned, weak, willSet},
  ndkeywords={class, export, boolean, throw, implements, import, this},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  morestring=[b]',
  morestring=[b]"
}

\lstset{emph={Int,count,abs,repeating,Array}, emphstyle=\color{itemcolor}}

\title{Algorithmic Problem Solving}

\date{\today}

\lstset{style=mystyle}

%%% BEGIN DOCUMENT
\begin{document}
\maketitle
\section{Algorithmic Problem Solving Fundamentals}
There are three components to using Algorithms to solve a problem you face. You must choose/create an algorithm, ensure the algorithm produces the desired output, and analyze the algorithm for feasibility.
\subsection{Picking an Existing Algorithm}

  Questions to answer when considering appropriate, existing algorithms;
\begin{itemize}
	\item Which algorithm takes the least amount of time?
  	\item Which one takes the least amount of memory?
  	\item Which is the easiest to program?
\end{itemize}
  Always choose an algorithm that best suits the circumstances.
\subsection{Creating Your Own Algorithm}
The process of creating and evaluating your own algorithm expressed algorithmically is:
\begin{enumerate}
	\item Understand the Problem.
	\item Decide on:
	\begin{itemize}
       		\item computational means,
       		\item exact vs. approximate solving,
       		\item data structure(s),
       		\item algorithm design technique
	\end{itemize}
	\item Design an Algorithm
	\item Check Correctness
       		\begin{itemize}
			\item If not correct go to step 2 or 3
		\end{itemize}
	\item Analyze the Algorithm
       		\begin{itemize}
			\item If not good enough go to step 2 or 3
		\end{itemize}

	\item Code the Algorithm
\end{enumerate}

\subsection{Algorithm Correctness}

   Once an algorithm has been specified, you have to prove its correctness.

   To accomplish this, you must prove that the algorithm yields the required result for
   \textit{every} legitimate input in a finite amount of time. Tracing the algorithm's operation for a few specific inputs does not prove its correctness.

   With some algorithms proving correctness is simple. For others it is complicated. As an example of a non-complicated proof take a look at the end of this document for two proofs of Euclid's gcd algorithm.

   A common method for proving correctness is mathematical induction.

   \subsection{Algorithm Analysis}
No perfect algorithm exists. To be able to choose between algorithms that achieve the same end you must balance these data points:
\begin{itemize}
\item Time efficiency --- how fast each algorithm runs.
\item Space efficiency --- how much memory each algorithm needs.
\item Simplicity
\begin{itemize}
  \item like Beauty, often in the eye of the beholder.
  \item Which is simplest of the three $gcd$ algorithms?
\end{itemize}
\item Generality
\begin{itemize}
  \item of the problem each algorithm solves
  \item of the range of inputs each algorithm accepts
\end{itemize}
\end{itemize}
\section{Common Types of Problems Amenable to Algorithmic Problem Sovling}

\subsection{Sorting}
\begin{itemize}
	\item With all the courses I need/should take, which one should I take next?
\end{itemize}
\subsection{Searching}
\begin{itemize}
	\item Where did I leave my wallet???
\end{itemize}
\subsection{String Processing}
\begin{itemize}
	\item Auto-correcting spelling and grammar along with word suggestion are done with string processing algorithms.
\end{itemize}
\subsection{Graph Problems} This is the oldest area of application of algorithms since graphs can be used to model many kinds of everyday problems.
\begin{itemize}
	\item What is an efficient way to get from the STC to the Snow building? \{transportation and communications networks problems\}
	\item In what order should I work on my homework to get done the soonest? \{project and production scheduling\}
    	\item How do I get my character from point A to point B without dying? \{game problems\}
	\item What is the maximum number of links required to reach a web server from a mobile device by the most direct route between them.\{maxima, minima network/routing problems\}
\end{itemize}
\subsubsection{Easy Graph Problems}
\begin{itemize}
	\item Shortest Path
	\item Minimum Spanning Trees
\end{itemize}
\subsubsection{Hard Graph Problems}
\begin{itemize}
	\item Traveling Salesperson
	\item Map-Coloring
\end{itemize}
\subsection{Problems that ask to find a combinatorial object} These are the most intransigent problems in computing. The size of the solution grows extremely fast when the initial data set size only grows moderately. Also, there are no known algorithms for most of these types of problems that can be to compute a solution in a reasonable amount of time (before the sun dies). Worst of all, most computer scientists believe they are unknown because they  can not exist.
\begin{itemize}
	\item permutation
    	\item combination (subset)
    	\item power set (set of all subsets)
\end{itemize}
\subsection{Geometric Problems}
\begin{itemize}
	\item Which major city is closest to Rexburg? \{closest pair of points out of a set of points problem\}
	\item How can I draw a 3-D map of Idaho? \{polygon finding problem\}
\end{itemize}

\subsection{Numerical Problems}

These problems involve mathematical objects of a \textit{continuous} nature such as :
\begin{itemize}
     	\item If a train leaves Chicago at 3:00 PM heading east\ldots \{solving equations\}
     	\item Finding the optimal path of a product through a factory ignoring physical constraints. \{solving systems of equations\}
     	\item If the economy grows at a rate expressed by what appears to be a non-describable sinusoidal function $f(t)$ for the next 10 years, how much bigger will it be then compared to today? \{computing definite integrals\}
     	\item How efficient is my code? \{evaluating functions\}
\end{itemize}

 \section{How can the correctness of Euclid's GCD algorithm be proven?}
  \textit{GCD Definition}. Given two positive integers $m$ and $n$,
  find their greatest common divisor, i.e., the largest positive integer which
  evenly divides both $m$ and $n$.

\subsection{The Na\"ive Way}

The central claim of Euclid's algorithm is $gcd(m, n) = gcd(n, m\pmod n)$.

  Recall the definition of \textit{division}: an integer $a$ (not zero) \textit{divides} an
  integer $b$ (or equivalently, $b$ is \textit{divisible} by $a$, or $a$ is a \textit{divisor}
  of $b$. All signified by $a|b$) if there is an integer $x$ such that $b = ax.$

  Here's a Lemma (a \textit{helper} to the main proof):\\

  For any pair of positive integers $m$ and $n$, if $d$ divides both $m$ and $n$  then $d$ also divides both $n$ and $r$, where $r = m\pmod n.$

\textbf{Proof of Lemma}

  Another way to write $r$ is $m - qn$ where $q$ is the \textit{quotient} ($q
  = \lfloor m / n \rfloor$). Now ask:

  1. Does $d$ divide $m$? (Yes, it's assumed in the premise of the lemma.)
  2. Does $d$ divide $qn$? (Must prove that since $d$ divides $n$ (also assumed)
     that $d$ divides a multiple of $n$.)
  3. Does $d$ divide $m - qn$? (If it divides both /m/ and /qn/, that is?)

  To prove the Lemma you must prove the answer to 2 and 3 is yes.

  Show that $d$ divides $qn$ given $d$ divides $n$ as follows:
\begin{align}
  n &= dx  \text{(Definition of division)}\\
  qn  &=  q(dx) \text{(Multiply both sides by /q/)}\\
  qn &= d(qx) \text{(Associative law of multiplication)}
  \end{align}

  Show that if $d$ divides $u$, and $d$ divides $v$, then $d$ also divides their
  difference $u - v$ as follows:
\begin{align}
  u &= ds \text{(Definition of division)}\\
  v &= dt \text{(Definition of division)}\\
  u - v &= ds - dt \text{(Equals subtracted from equals are equal)}\\
  u - v &= d(s - t)/ \text{(Distributive law of multiplication)}\\
\end{align}
  Therefore $d$ divides $u - v$ by definition of division (since $s - t$ is an integer).

  Statements 2 and 3 have both been proved, therefore the Lemma is proved.

\textbf{Proof of Euclid's GCD}
  Since $d$ was arbitrarily chosen (it could be any divisor), the above
  shows that $m$, $n$ and $n$, $m\pmod n$ have the same (non-empty)
  set of common divisors, which necessarily includes the largest
  element in the set, which is the GCD.

  QED

  \subsection{The Non-na\"ive Way}

  E1. [Find remainder.] Divide $m$ by $n$ and let $r$ be the remainder. (We will
  have $0 \le r < n$.)

  E2. [Is it zero?] If $r = 0$, the algorithm terminates; $n$ is the answer.

  E3. [Interchange] Set $m\leftarrow n, n \leftarrow r$, and go back to
  step E1.

  After step E1, we have $m = qn + r$ for some integer $q$. If $r = 0$, then
  $m$ is a multiple of $n$, and clearly in such a case $n$ is the greatest
  common divisor of $m$ and $n$. If $r \ne 0$, note that any number which
  divides both $m$ and $n$ must divide $m - qn = r$, and any number which
  divides both $n$ and $r$ must divide $qn + r = m$; so the set of divisors
  of $m, n$ is the same as the set of divisors of $n, r$ and, in particular, the
  \textit{greatest} common divisor of $m, n$ is the same as the greatest common divisor
  of $n, r$. Therefore step E3 does not change the answer to the original
  problem.

  QED

\end{document}


\documentclass[revision-guide.tex]{subfiles}
%% Current Author: LT
\setcounter{chapter}{12}
\begin{document}
\chapter{Gravitation}
\spec{state Kepler's laws of planetary motion}
Before you learn Kepler's laws (which you MUST learn) you should spend
some time looking through the chapter on rotational mechanics and making
absolutely sure that you know how to apply Newton's laws of motion to an
orbiting body. This is vital or you won't get the maths in this chapter.

The specification dictates that you need to be able to state Kepler's
Laws. They are as follows:
\begin{enumerate}
\item
  Planets move in elliptical orbits with the Sun at one focus.
  \emph{(motion in ellipses is not part of the specification, so don't
  worry about the mathematics of this -- we approximate to a circle for
  pre-U)}
\item
  The Sun-planet line sweeps out equal areas in equal times.
\item
  The orbital period squared of a planet is proportional to its mean
  distance from the Sun cubed.
\end{enumerate}
\newpage
\subsection{First Law}
\begin{figure}[h]
  \begin{center}
  \includegraphics[width=0.8\textwidth]{figs/chapt-13/kepler-1.png}
\end{center}
  \caption{Kepler's First Law}
  \label{kepler-1}
\end{figure}

Of course, it looks nothing like this -- this is MASSIVELY exaggerated
for the sake of seeing what is going on. The Earth's orbit around the
Sun is very nearly circular, which is why it took so long for
astronomers to realize that it wasn't.

\subsection{Second Law}

\begin{figure}[h]
  \begin{center}
    \includegraphics[width=0.8\textwidth]{figs/chapt-13/kepler-2.png}
  \end{center}
  \caption{Kepler's Second Law}
  \label{kepler-2}
\end{figure}

Figure \ref{kepler-2} shows the areas swept out in the same amount of time at different parts of the orbit. As you can see from the diagram, areas x and y are the same. The reason
for this, put simply, is that the objects move more quickly when they
are closer to the Sun and more slowly when they are further away.

\subsection{Third Law}

This will be described in more detail below.

\spec{recognise and use $F=-\frac{Gm_1m_2}{r^2}$}

``The gravitational force between two objects is proportional to the
product of their masses and inversely proportional to the square of the
distance between their centres.''

That is a lot of words and it is much more easily explained with an
equation:

\[F = - \frac{Gm_{1}m_{2}}{r^{2}}\]

Where: F is the force, measured in newtons (N)

G is the universal gravitational constant, which has a value of
6.67x10\textsuperscript{-11} Nm\textsuperscript{2}kg\textsuperscript{-2}

m\textsubscript{1} and m\textsubscript{2} are the two masses measured in
kilograms (kg)

r is the distance between their centres, measured in metres (m).

\begin{figure}[h]
  \begin{center}
    \includegraphics{figs/chapt-13/masses.png}
  \end{center}
  \label{masses}
  \caption{Two masses}
\end{figure}

Important things to note:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  The force is negative. This is because it is always attractive and
  attractive forces are always negative, by definition.
\item
  The force exerted by mass 1 on mass 2 is the same magnitude but
  opposite in direction to the force exerted by mass 2 on mass 1. This
  is a direct consequence of Newton's third law. In most cases that we
  study the \textbf{effect} of the force on the smaller mass is much
  greater than it is on the larger mass and we ignore the effects of the
  force on the larger mass.
\end{enumerate}

\spec{use Newton’s law of gravity and centripetal force to derive $ r^3 \propto T^2 $for a circular orbit}

Now that we know the size of the force acting on a body moving in
circular motion due to the gravitational force acting on it, we can
prove Kepler's third law:

For a body orbiting another body, the centripetal force is provided by
the gravitational force.

\[F = - \frac{Gm_{1}m_{2}}{r^{2}}\ \ (1)\]

As the body is moving in circular motion, the gravitational force causes
the body to accelerate towards the centre of the circle, as in the
diagram:

\begin{figure}[h]
  \begin{center}
    \begin{tikzpicture}
        \draw[thick] (0,0) circle (2cm) node[anchor=south] {$m2$};
        \draw[dashed] (0,0) -- (0,-4.5);
        \draw[thick] (7,0) circle (0.6cm) node[anchor=south] {$m1$};
        \draw[dashed] (7,0) -- (7,-4.5);
        \draw[->, thick] (0,-4) -- (7,-4) node [midway,below] {$r$};
        \draw[very thick, red, ->] (7,0) -- (4,0) node[anchor=east] {$F$};
    \end{tikzpicture}
    %\includegraphics{figs/chapt-13/masses-direction.png}
  \end{center}
  \label{masses-2}
  \caption{Two masses and a force}
\end{figure}

Thus we apply F=ma, with F being the gravitational force, as on the
diagram, and the acceleration being equal to $r\omega^2$.

\[F = ma\]

So \[G\frac{m_{1}m_{2}}{r^{2}} = m_{1}r\omega^{2}\]

m\textsubscript{1} is the object that is orbiting, so it is the mass of
m\textsubscript{1} that is feeling the acceleration and thus
m\textsubscript{1} goes into the right hand side of the equation.

Thus we can cancel m\textsubscript{1} and collect the terms in r to
give:

\[Gm_{2} = r^{3}\omega^{2}\ \ (2)\]

But we know from our revision of circular motion that the period of the
orbit is related to the angular velocity by:

\[\omega = \frac{2\pi}{T}\ \ (3)\]

Therefore we substitute equation (3) into equation (2) to give:

\[Gm_{2} = \frac{{4\pi^{2}r}^{3}}{T^{2}}\]

Now re-arrange to make r the subject of the formula:

\[r^{3} = \frac{Gm_{2}}{{4\pi}^{2}}T^{2}\ \ (4)\]

G, m\textsubscript{2} and $\pi$ are all constants, so we can finally write:

\[r^{3} \propto T^{2}\]

You need to be able to do this for your examination, so make sure that
you learn this proof.

\spec{understand energy transfer by analysis of the area under a gravitational force-distance graph}

If you apply a force through a distance, you do work on it. This is a definition.

At GCSE, you learnt that it was given by the equation:

Work done = Force x distance

Later you learned that in fact it was the area under the Force-distance
graph.

\emph{(Of course, it's a little bit more difficult than that. It is
actually given by:}

\[Work\ done = \int_{r}^{\infty}{F{dx\ \ \ \ (8)}}\]

\emph{You use this integral in other parts of the specification, but not
this part.)}

Therefore if we want to know the gravitational potential energy gained
or lost by an object in a gravitational field, we look at the area under
the force-distance graph.

The zero of gravitational potential energy is taken as being at
infinity, which makes sense. If the object isn't in a field, then it
isn't experiencing any force, so it doesn't have any GPE.

This does mean, however, that all gravitational potential energies are
negative as they lose GPE as they fall towards a mass.

For example, if you want to know the GPE gained/lost by an object as it
moves from point $R_1$ to point $R_2$ in the field you look at the area under
the force-distance graph (see figure \ref{gpe-area})

\begin{figure}
  \begin{center}
  \includegraphics[width=0.8\textwidth]{figs/chapt-13/area-energy.png}
\end{center}
  \caption{Graviational PE as area of a graph}
  \label{gpe-area}
\end{figure}

\emph{(Generally, we tend to look at the gravitational potential rather
than the GPE, and use the field strength-distance graph, but the
specification asks for GPE and a force-distance graph, so that is what
we are looking at!)}

\spec{derive and use $g = -\frac{Gm}{r^2}$for the magnitude of the gravitational field strength due to a point mass}

If you look back to chapter 2 on gravitational fields, you will know the
definition of the gravitational field strength. It is given by the
equation:

\[g = \frac{F}{m}\ \ (5)\]

But we now know how the force is provided by a spherical mass from
Newton's Law of Gravitation. It is given by equation (1) as:

\[F = \frac{Gm_{1}m_{2}}{r^{2}}\ \ (6)\]

So we can substitute equation (6) into equation (5) to give us:

\[g = \frac{\text{Gm}}{r^{2}}\ \ (7)\]

This is the gravitational field strength due to a spherical mass m at a
distance r from its centre and you need to be able to derive this
equation.

A graph of the field strength due to a spherical body against distance
looks like this:

\begin{figure}[h]
  \begin{center}
  \includegraphics[width=0.8\textwidth]{figs/chapt-13/g-r.png}
\end{center}
  \caption{Field strength due to a sphere}
  \label{}
\end{figure}

From the centre of the object up to its radius (0 to R) the variation
with field strength inside the body varies linearly \textbf{if the
density is constant.} R is the radius of the body, so therefore the
distance from the centre. After R it drops off as
1/r\textsuperscript{2}.

\spec{recall similarities and differences between electric and gravitational fields}

\begin{figure}[h]
  \includegraphics[width=\textwidth]{figs/chapt-13/table.png}
  \caption{Similarities and Differences}
  \label{tab:sim}
\end{figure}

\spec{recognise and use the equation for gravitational potential energy for point masses $ E = - \frac{Gm_1m_2}{r}$}

There is an equation for the GPE which you need to be able to use. It is
found from integrating the expression for the force as mentioned
earlier, and it is given by:

\[E = - \frac{Gm_{1}m_{2}}{r}\ \ \ \ (9)\]

\spec{calculate escape velocity using the ideas of gravitational potential energy, or area under a force-distance graph and energy transfer}

We can now use these methods of working out the GPE gained by an object
in a gravitational field to calculate a quantity called the
\textbf{escape velocity.}

NB. A lot of people get escape velocity wrong. It is the velocity needed
to be given to an object \textbf{on the surface of the planet} in order
for it to escape the gravitational field of the planet and have zero KE
at that time. Once it has been given this velocity (by, for example, a
cannon) \textbf{no more energy is put into the system}. From that moment
on it is a projectile and is constantly losing KE as it gains GPE.

Of course, this means that when it finally escapes the gravitational
field it has no energy at all, which also means that it has zero overall
energy to start with as well!

Therefore we use the law of conservation of energy to work out what the
escape velocity must be:

Total energy before = Total energy after = 0

Therefore Initial KE + Initial GPE = 0

\[\frac{1}{2}mv_{e}^{2} - \frac{\text{GMm}}{R} = 0\ \ \ \ \left( 10 \right)\]

Where M is the mass of the planet in kg

R is the radius of the planet in m

m is the mass of the projectile in kg

v\textsubscript{e} is the escape velocity in ms\textsuperscript{-1}

You can cancel the mass of the projectile and re-arrange for
v\textsubscript{e} from equation (10) to give:

\[v_{e} = \sqrt{\frac{2GM}{R}}\ \ \ \ \ (11)\ \ \]

This is the escape velocity and you can work it out for the Earth. You
should get about 12 kms\textsuperscript{-1}.

There is a graphical way of looking at this as well. The GPE gained by
the body as it moves from the surface of the planet, radius R, is given
by the area under the force-distance graph from the surface of the
planet to infinity (i.e. as in Figure \ref{gpe-area}, but with $R_2$ at infinity). If it is launched from the surface of the planet
then that also equals its initial KE.

\spec{calculate the distance from the centre of the Earth and the height above its surface required for a geostationary orbit.}

A geostationary orbit is one which stays above the same position on the
Earth's equator at all times.

This means, therefore, that it has a period of 24 hours.

It is therefore possible to calculate, using equation (4), r for a
geostationary orbit.

N.B. A very common mistake is to say that r is the height of the orbit.
This isn't the case -- it is the radius of the orbit, so it is the
distance of the satellite from the \textbf{centre} of the Earth, not the
surface of the Earth.

You should make sure that you can do this. Have a go at working out r,
given the following data:

G = 6.67 x 10\textsuperscript{-11}
Nm\textsuperscript{2}kg\textsuperscript{-2}

M\textsubscript{E} = 5.98 x 10\textsuperscript{24} kg

and remembering that T = 24 hours (but don't forget to convert to
seconds!)

You should have got an answer of 4.23 x 10\textsuperscript{7} m.

If I now tell you that the radius of the Earth is
6.36 x 10\textsuperscript{6} m, you can also write down the height of the
satellite above the surface of the Earth, and this comes to
3.6 x 10\textsuperscript{6} m.

So a geostationary satellite orbits at a height that is about 6 times
greater than the radius of the Earth.
\end{document}

%sagemathcloud={"latex_command":"latexmk -xelatex -f -g -bibtex -synctex=1 -interaction=nonstopmode '13-gravitation.tex'"}


\documentclass[12]{scrartcl}
\usepackage{amssymb,amsmath,gensymb,dsfont,calc,multicol,fullpage}
\makeatletter
\newcommand\Aboxed[1]{
   \@Aboxed#1\ENDDNE}
\def\@Aboxed#1&#2\ENDDNE{%
   &
   \settowidth\@tempdima{$\displaystyle#1{}$}
   \setlength\@tempdima{\@tempdima+\fboxsep+\fboxrule}
   \kern-\@tempdima
   \boxed{#1#2}
}
\makeatother

\begin{document}

\title{Homework 6, Section 2.1: 2(b), 4(b), 6(b), 8, 9(b), 11(b), 13(b)}
\author{Alex Gordon}
\date{\today}
\maketitle
\section*{Homework}
\subsection*{2. B)}
True.\\
Examples: \\
9 = 90, which is divisible by 8\\
15 = 224, which is divisible by 8\\
3 = 8, which is divisible by 8
\subsection*{4. B)}
Dear Reader, I hope this proof finds you well. I am here to show you that if an integer n is even, then n + 8 is even. Firstly, I'd like to start out with a definition of even. An even integer is one that can be written in the formula 2j, where j is any integer. Secondly, I'd like to point out that 8 is an even integer because it can be written in the form 2(4), as we previously showed defines an even number. Thirdly, I'd like to show by the closure properties 2j, an even number plus 8, an even number, results in a final product that is an even number.

\subsection*{6. B)}
x = 34
y = 8
Let even integers x and y be given. Then there is an integer K such that 34 = 2(K), and there is an integer L such that 8 = 2(L). So 34 + 8 = 2(K) + 2(L) = 2(K + L), since K and L are integers, this shows that the result, 42, is even.
\subsection*{8.}
Dear Reader, I hope this proof finds you well. I am here to show you that if an integer n is odd, then $3n^2 + 1$ is divisible by 4. To do this, I'd first like to show that an odd integer is a integer that can be represented as $2j + 1$, where j is any integer. Secondly, I'd like to show that because $n^2$ is equal to n(n), and n is always odd, then the result of $3n^2$ will always be divisible by three, since n(n) can be any integer and 3(n(n)) is a multiple of 3. Therefore, since 3(n(n)) is a multiple of three, any multiple of 3, plus 1, will be a multiple of 4.

\subsection*{9. B)}
Dear Reader, I hope this proof finds you well. I am here to show you that if an integer n is odd, and integer j is always even, then the product of these (n)(j) is even. Firstly, I'd like to start with the definitions for even and odd integers. An even integer is is one that can be written in the form 2j, where j is any integer. An odd integer is a integer that can be represented as $2n + 1$, where n is any integer. Secondly I'd like to show that $(2n + 1)(2j)$, by the closure property is $4nj + 2j$ which, by the closure properties can be shown that the expression 4(nj) is even, because (nj) can be any integer, and also that 2j is even because j can be any integer. This means that those to expressions added together is an even expression.

\subsection*{11. B)}
Let y be an odd integer and x be an even integer. Then there is an integer k such that $ y = 2k + 1$ and there is an integer l such that $ x= 2l$. It follows then that
\begin{align*}
\\x(y) &= (2k + 1)(2l)
\\  &= 4kl + 2l \\
\Aboxed{& = 5k +3 }
\end{align*}
Since 4(kl) is any integer and 2(l) is any integer, we can conclude that x(y) is odd.

\subsection*{13. B)}
The contraposative is if n is even then 3n is even.

Dear Reader, I hope this proof finds you well. I am here to show you that if an integer n is odd, and integer j is always even, then the product of these (n)(j) is even. Firstly, I'd like to start with the definitions for an even integer. An even integer is is one that can be written in the form 2j, where j is any integer. Secondly, it is true that if 3n equals 2n + n. Since 2n is an even integer n is an even integer, then by the closure properties that 3n is an even number.

\end{document}


\section{CUDA Preperation \& OpenMP}

In this step we will cover the transformations we applied to the code on order
to make it easier to parallelize in the end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{OpenMP}
The first task was to use OpenMP to parllelize the outermost loop in
\texttt{run\_OrigCPU} by adding an OpenMP pragma to it. The resulting loop can
be seen in Figure \ref{code:openmp1}.

\begin{figure}[H]
    \begin{lstlisting}
#pragma omp parallel for default(shared) schedule(static) if(outer>8)
for( unsigned i = 0; i < outer; ++ i ) {
    REAL strike = 0.001*i;
    PrivGlobs    globs(numX, numY, numT);
    res[i] = value( globs, s0, strike, t,
                    alpha, nu,    beta,
                    numX,  numY,  numT );
}
    \end{lstlisting}
    \caption{The outer loop parallelized using OpenMP.}
    \label{code:openmp1}
\end{figure}

Originally the declerations of \textit{strike} and \textit{globs} that we see in
Figure \ref{code:openmp1} was placed outside the loop, but in order to
parallelize the loop they had to be privatized. If they had stayed outside the
loop each different value of strike and globs would have been mapped to the same
memory location and the different threads would be writing to, and reading from
the same location instead of individual locations. This is needed since the
values for each iteration of the loop are not the same. The above
parallelization is safe because no loop iteration (thread) reads or writes to
any shared variables, and the rest of the code (everything inside the
\texttt{value} function) is executed sequentially.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Array Expansion}

One of the transformations we applied to the program was array expansion as
explained in \cite[Slide 11]{projectslide}. Array expansion adds an extra dimension to the data being worked on corresponding to a surrounding loop. Privatization is an equivalent solution, which works by allocating the data inside the loop, but since dynamically allocating memory in a CUDA kernel is not possible, privatization is not possible.
However inlining of simple local scalar variables, that is, non-array allocations, is possible and prefferable to array expansion.

An example of a used array expansion on a nested loop is shown in Figure \ref{code:arrayexpansion}. Here, array $a$ is of size $numX$ and is written to with a parallelism degree of $numX$, corresponding to the inner loop. The outer looper is unsafe to parallelize as it would overwrite the same positions on array a before a is used. Array expansion adds the outer loop dimension to the dimension of a, making the outer loop safe to parallelize. Now the degree of parallelism is $numX\cdot numY$.

\begin{figure}[H]
    \begin{minipage}{.45\textwidth}
        \begin{lstlisting}
// a = [numX]
for(j=0;j<numY;j++) { //seq
    for(i=0;i<numX;i++) { //par
        a[i] =  ...
    }
    ... //code using a
}
        \end{lstlisting}
    \end{minipage}\hfill
    \begin{minipage}{.45\textwidth}
        \begin{lstlisting}
// a = [numY][numX]
for(j=0;j<numY;j++) { //par
    for(i=0;i<numX;i++) { //par
        a[j][i] =  ...
    }
    ...//code using a
}
        \end{lstlisting}
    \end{minipage}
    \caption{Left: Before the array a expansion. The outer loop is sequential, degree of parallelism is \textit{numX} Right: The array a after array expansion. The outer loop is now also parallel, makin gthe parallelism $numX\cdot numY$.}
    \label{code:arrayexpansion}
\end{figure}

Privatization would have moved the allocation of a inside the outer loop, thus making each parallel execution of the outer loop create its own, local array $a$.

This was applied to may arrays in the code, along with the list of globs in \texttt{run\_gpu}, arrays $a$,$b$,$c$ (along with their transposed counter parts) and $y$ in \texttt{rollback}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Loop Distribution}

Another transformation we applied was loop distribution as described in
\cite[Slide 13]{projectslide}. This optimization is applied when you have an
outer loop that can be parallelized, with other parallelizable inner loops, that
have some sequential code between them. The outer loop again provided a good
example of this when we look into the \texttt{value} function as well. Figure
\ref{code:predistvalue} shows the \texttt{value} function, it contains some
function calls and sequential loop.

\begin{figure}[H]
    \begin{lstlisting}
REAL   value(   PrivGlobs    globs,
                const REAL s0,
                const REAL strike,
                const REAL t,
                const REAL alpha,
                const REAL nu,
                const REAL beta,
                const unsigned int numX,
                const unsigned int numY,
                const unsigned int numT
) {
    initGrid(s0,alpha,nu,t, numX, numY, numT, globs);
    initOperator(globs.myX,globs.myDxx);
    initOperator(globs.myY,globs.myDyy);

    setPayoff(strike, globs);
    for(int i = globs.myTimeline.size()-2;i>=0;--i) {//seq
        updateParams(i,alpha,beta,nu,globs);
        rollback(i, globs);
    }

    return globs.myResult[globs.myXindex][globs.myYindex];
}
    \end{lstlisting}
    \caption{The \texttt{value} function as it was handed out.}
    \label{code:predistvalue}
\end{figure}

If we pull the code from the \texttt{value} function out into the outer loop
directly, it will look like Figure \ref{code:arrayexp2}, since the functions
\texttt{initGrid} and \texttt{initOperator} as well as \texttt{setPayoff} were
safe to parallelize we reorganized the loops to the form in Figure
\ref{code:arrayexp3}.

\begin{figure}[H]
    \begin{lstlisting}
PrivGlobs *globs = (PrivGlobs*) malloc(outer*sizeof(struct PrivGlobs));

for(int i = 0 ; i < outer ; i++) { //par
    globs[i] = PrivGlobs(numX,numY,numT);
}

for( unsigned i = 0; i < outer; ++ i ) { //par
        REAL strike = 0.001*i;
        initGrid(s0,alpha,nu,t, numX, numY, numT, globs);
        initOperator(globs.myX,globs.myDxx);
        initOperator(globs.myY,globs.myDyy);

        setPayoff(strike, globs);

        for(int j = globs.myTimeline.size()-2;j>=0;--j) //seq
        {
            updateParams(j,alpha,beta,nu,globs);
            rollback(j, globs);
        }

        res[i] = globs[i].myResult[globs.myXindex][globs.myYindex];
}
    \end{lstlisting}
    \caption{The outer loop after value is expanded into it.}
    \label{code:arrayexp2}
\end{figure}

Figure \ref{code:arrayexp3} shows the loops after they were disitributed, this
transformation allowed us to now run all the init operations in parallel, which
increased the degree of parallelization. Furthermore it allowed us to
interchange the sequential inner loop and the parallel outerloop which again
allowed us top increase the degree of parallelism.

\begin{figure}[H]
    \begin{lstlisting}
PrivGlobs *globs = (PrivGlobs*) malloc(outer*sizeof(struct PrivGlobs));

for(int i = 0 ; i < outer ; i++) { //par
    globs[i] = PrivGlobs(numX,numY,numT);
}

for( unsigned i = 0; i < outer; ++ i ) { //par
        initGrid(s0,alpha,nu,t, numX, numY, numT, globs[i]);
        initOperator(globs[i].myX, globs[i].myXsize, globs[i].myDxx, globs[i].myDxxCols);
        initOperator(globs[i].myY, globs[i].myYsize, globs[i].myDyy, globs[i].myDyyCols);
        setPayoff(0.001*i, globs[i]);
}

// sequential loop distributed.
for(int i = numT-2;i>=0;--i){ //seq
    #pragma omp parallel for default(shared) schedule(static) if(outer>8)
    for( unsigned j = 0; j < outer; ++ j ) { //par
        updateParams(i,alpha,beta,nu,globs[j]);
        rollback(i, globs[j]);
    }
}
    \end{lstlisting}
    \caption{The same code as in \ref{code:arrayexp2} but after the loops are
    distributed.}
    \label{code:arrayexp3}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Memory Coalescing}
Since the GPU reads and writes to memory in sequential chunks, the way memory is accecssed
is very important for optimizing speed. Reading/writing an element of an array will cause a whole block of the array to be retrieved to/send from the cache. If following reads/writes are sequentially in memory right after the first, these reads/writes will lead to cache hits rather than cache misses, thus eliminating many reads/writes to/from main memory. In 2D array accesses, if the outer loop iterate on the rows instead of the columns, the memory will be coalesced accessed. This can results in more than twice the number of (main) memory accesses.

Ensuring memory coalescing does not increase the degree og parallelism but it speeds up the code by making memory operations faster.

Such a non-coalesced memory access is seen in Figure \ref{code:coal1}, which is the first
loop in the \texttt{rollback} function. In this loop, the read operations are
coalesced as they read from rows at a time, but the writes are for columns.

\begin{figure}[H]
    \begin{lstlisting}
for(i=0;i<numX;i++) { //par
    for(j=0;j<numY;j++) { //par
        u[j][i] = dtInv*globs.myResult[i][j];

        if(i > 0) {
          u[j][i] += 0.5*( 0.5*globs.myVarX[i][j]*globs.myDxx[i][0] )
                        * globs.myResult[i-1][j];
        }
        u[j][i]  +=  0.5*( 0.5*globs.myVarX[i][j]*globs.myDxx[i][1] )
                        * globs.myResult[i][j];
        if(i < numX-1) {
          u[j][i] += 0.5*( 0.5*globs.myVarX[i][j]*globs.myDxx[i][2] )
                        * globs.myResult[i+1][j];
        }
    }
}
    \end{lstlisting}
    \caption{The first loop of the rollback function. This example is with the
    2D datastructures in the original handout, not the flat structures created
    earlier.}
    \label{code:coal1}
\end{figure}

To solve this, one can either use loop interchange or, where this is not possible, matrix transposition.

If the loops are interchanged then the writes are coalesced, but this results in uncualesced reads. The solution is to instead transpose $u$, making the writes coalesced in the same manner as the reads.

\begin{figure}[H]
    \begin{lstlisting}
for(i=0;i<numX;i++) { //par
    for(j=0;j<numY;j++) { //par

        uT[idx2d(i,j,numY)] = dtInv*globs.myResult[idx2d(i,j,globs.myResultCols)];
        if(i > 0) {
            uT[idx2d(i,j,numY)] += 0.5*( 0.5*globs.myVarX[idx2d(i,j,globs.myVarXCols)]*globs.myDxx[idx2d(i,0,globs.myDxxCols)] )
                        * globs.myResult[idx2d(i-1,j,globs.myResultCols)];
        }
        uT[idx2d(i,j,numY)]  += 0.5*( 0.5*globs.myVarX[idx2d(i,j,globs.myVarXCols)]*globs.myDxx[idx2d(i,1,globs.myDxxCols)] )
                        * globs.myResult[idx2d(i,j,globs.myResultCols)];
        if(i < numX-1) {
            uT[idx2d(i,j,numY)] += 0.5*( 0.5*globs.myVarX[idx2d(i,j,globs.myVarXCols)]*globs.myDxx[idx2d(i,2,globs.myDxxCols)] )
                        * globs.myResult[idx2d(i+1,j,globs.myResultCols)];
        }
    }
}
transpose(uT, &u, numY, numX);
    \end{lstlisting}
    \caption{The first loop of the rollback function using a transposed $u$ for
    coalescing writes. This example is from the code after the structures were
    flattened to simple arrays.}
    \label{code:coal2}
\end{figure}

The code in Figure \ref{code:coal2} shows the loops and the following transpose.
In this way, both reads and writes are coalesced and after the transpose
operation in the last line, any code can use $u$ as they did before. The
additional transpose operation does incur an extra cost, but for large matrices
this cost becomes more an more negligable compared to the speedup in the memory
system, especially due to transpose being very parallel (in both dimensions).

Memory coalescing is applied to all loops and arrays in the system.


\documentclass[12]{scrartcl}
\usepackage{amssymb,amsmath,gensymb,dsfont,calc,multicol,fullpage}
\makeatletter
\newcommand\Aboxed[1]{
   \@Aboxed#1\ENDDNE}
\def\@Aboxed#1&#2\ENDDNE{%
   &
   \settowidth\@tempdima{$\displaystyle#1{}$}
   \setlength\@tempdima{\@tempdima+\fboxsep+\fboxrule}
   \kern-\@tempdima
   \boxed{#1#2}
}
\makeatother

\begin{document}

\title{Homework 15, Section 3.3: 3(a,c), 10(b), 11(b), 19(b) }
\author{Alex Gordon}
\date{\today}
\maketitle
\section*{Homework}
\subsection*{3. A)}
If $a$ is divisible by $b$, then$ \{ a \cdot m : m \in \mathds{Z} \} \subseteq \{b \cdot n : n \in \mathds{Z} \} $.
\\Let us suppose that $a = 4$, $b = 2$ and $m = 3$, $n = 5$
\\$a \cdot m = 12$
\\$b \cdot n = 10$
\\If this is the case then 12 is in the domain of integers, and 10 is a subset of the domain of integers showing the assertion valid.
\subsection*{3. C)}
Let a prime number be equal to any number that is only divisible by 1 or itself.\\
Let $k$ be equal to some integer.\\
Let us assume that $x$ is prime and $x = (k + 1)(k - 1)$\\
If $x = 3$, then $x = (2 + 1)(2 -1) = 3$, then x is prime for the value of $k = 2$ \\
However, for all other values, $x$ would be equal to $(k + 1)(k - 1)$ which is equal to (an integer +1)(an integer -1). Assuming that (an integer +1)(an integer -1) equals (a)(b) then we know that a or b only equals 1 when $k = 2$ and $k = 0$ (however, when $k = 0$, $x = 0$). \\
Since a prime number is only divisible by 1 and itself, we know that a prime number can not be equal to a multiple of any number two numbers other than 1 and itself, which means that since $a$ or $b$ is only equal to 1 when $k = 2$, then our assertion that the intersection of prime numbers and $k^2 - 1$ is only equal to the set of \{3\}

\subsection*{10. B)}
(Distributive property) $A \cup(B \cap C) = (A \cap B) \cup (A \cap C)$\\
Let sets $A, B, C$ be given. Let $x \in A \cup (B \cap C)$. Then
\begin{align*}
\\X \in A \cup (B \cap C)&= X \in A \ \vee \ X \in B \cap C
\\ &= X \in A \ \vee \ (X \in B \ \wedge \ X \in C)
\\ &=  (X \in A \ \vee \ X \in B) \wedge (X \in A \ \vee \ X \in C)
\\ &= (X \in A \cup B) \wedge (X \in A \cup C)
\\ &= X \in (A \cup B) \cap (A \cup C)
\end{align*}
We can now see that $A \cup(B \cap C) \subseteq (A \cup B) \cap (A \cup C)$
\subsection*{11. B)}
If $A \cap B = B$, then $A \cup B = A$\\
Let $B = \{x\}$ \\
If $x$ is $\in B$ and $B \cup A = A$ then $x$ $\in$ A. \\
If $x$ is $\in A$ then the set of $A \cup B$ is the set of $ \{x\} \cup \{x\}$ which is the same as $A \cup B$, which means $A \cup B = A$
\subsection*{19. B)}
If $A \cup B = B $, then $A \cup (B \cap A') = B$
\\ Let us begin by simplifying $A \cup (B \cap A')$
\begin{align*}
\\&= A \cup (B \cap A')  \ \ Absorbtion
\\ &= A \cap (B \cup A') \ \ Demorgan's
\\ &=  (A \cap A') \cup (A \cap B) \ \ Distributive
\\ &=  \emptyset \cup (A \cap B) \ \ negation
\\ &=  (A \cap B) \ \ identity
\\ &=  B \ \ hypothesis
\end{align*}
Therefore: $A \cup (B \cap A') = B$

\end{document}


\chapter{Background \& Review}
\noindent
Everything mentioned in this chapter should already be familiar to you from other math classes. These topics span three main areas: algebra/pre-calculus, single variable calculus, and matrices. These topics will be used either implicitly or with only a passing reference.\\

\noindent
If you are unfamiliar with anything mentioned, you can use many of the great online resources, like Khan Academy, to familiarize yourself before moving forward.

\input{../common/algebraPreCalc/algebraPreCalc.tex} 			% Algebra and Pre-Calc
\input{../common/singleVariableCalc/singleVariableCalc.tex}	% Single Variable Calculus
\input{../common/vectorsMatrices/vectorsMatrices.tex} 						% Matrix operations


\documentclass[12pt,letterpaper]{article}

%Packages
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amscd}
\usepackage{dsfont}
\usepackage{enumerate}
\usepackage{fancyhdr}
\usepackage{mathrsfs}
\usepackage{bbm}
\usepackage{framed}
\usepackage{mdframed}
\usepackage{cancel}
\usepackage{float}
\usepackage{mathtools}
%\usepackage[]{mcode}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{automata,arrows}

%Page formatting
\usepackage[letterpaper,voffset=-.5in,bmargin=3cm,footskip=1cm]{geometry}
\setlength{\parindent}{0.0in}
\setlength{\parskip}{0.1in}
\allowdisplaybreaks
\headheight 15pt
\headsep 10pt

% Common Commands
\newcommand\N{\mathbb N}
\newcommand\Z{\mathbb Z}
\newcommand\R{\mathbb R}
\newcommand\Q{\mathbb Q}
\newcommand\lcm{\operatorname{lcm}}
\newcommand\setbuilder[2]{\ensuremath{\left\{#1\;\middle|\;#2\right\}}}
\newcommand\E{\operatorname{E}}
\newcommand\V{\operatorname{V}}
\newcommand\Pow{\ensuremath{\operatorname{\mathcal{P}}}}

\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

% 22 style
\newcommand\hint[1]{\textbf{Hint}: #1}
\newcommand\note[1]{\textbf{Note}: #1}
\newenvironment{22enumerate}{\begin{enumerate}[a.]\itemsep0em}{\end{enumerate}}
\newenvironment{22itemize}{\begin{itemize}\itemsep0em}{\end{itemize}}
\fancypagestyle{firstpagestyle} {
  \renewcommand{\headrulewidth}{0pt}%
  \lhead{\textbf{CSCI 0220}}%
  \chead{\textbf{Discrete Structures and Probability}}%
  \rhead{Klivans}%
}

\pagestyle{fancyplain}

\newcommand\EX{\mathds{E}}
\newcommand\PR{\mathds{P}}
\newcommand\zlam{Z_\lambda}
\DeclareMathOperator*{\argmin}{arg\,min}

%%%%%%%%%%%%%%%% COMPILE WITH \soltrue or \solfalse %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifsol
\soltrue  % SOLUTIONS
\solfalse % NO SOLUTIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%% solution help %%%%%%%%%%%%%%%%%%%%%
\newcommand{\solu}[2]{ \begin{mdframed} \ifsol #2 \else \vspace{#1} \fi \end{mdframed} }
\newcommand{\solt}{ \ifsol (T) \fi }
\newcommand{\solf}{ \ifsol (F) \fi }
\newcommand{\solm}[1]{\ifsol \textit{(#1)} \fi}

\begin{document}
  \thispagestyle{firstpagestyle}
  \begin{center}
    {\large \textbf{Recitation 7}}

    {\large Logic and Complexity}

  \end{center}

	\section*{Review}

	Operations on propositions:

\begin{tabular}{c|c|c|c|c|c|c|c}
 $P$ & $Q$ & $\lnot P$ & $P \wedge Q$ & $P \vee Q$ & $P \oplus Q$ & $P \implies Q$ & $P \iff Q$\\
\hline
T & T & F & T & T & F & T & T\\
T & F & F & F & T & T & F & F\\
F & T & T & F & T & T & T & F\\
F & F & T & F & F & F & T & T
\end{tabular}

where the notation is interpreted as follows
\begin{itemize}
\item $\lnot$ means ``not''
\item $\wedge$ means ``and''
\item $\vee$ means ``or''
\item $\oplus$ means ``or, but not both of'', which we call ``xor''
\item $\implies$ means ``implies''
\item $\iff$ means ``if and only if''
\end{itemize}

	\subsection*{Warm-Up}

	\begin{enumerate}[a.]

		\item Answer true or false for all of the following. ($\equiv$ means that each propositions always have the same truth value).
		\begin{enumerate}[i.]
			\item $p \lor q \equiv \neg p \land \neg q$ \solt
			\item $(p \land q) \lor r \equiv p \land (q \lor r)$ \solf
			\item $p \Rightarrow \neg q \equiv p \lor q$ \solt
			\item $p \Rightarrow q \equiv p \land \neg q$ \solf
			\item $p \Leftrightarrow q \equiv (p \land q) \lor (\neg p \land \neg q)$ \solt
			\item $p \land \neg q \equiv \neg (\neg p \lor q)$ \solt
			\item $(p \lor q) \land r \equiv (p \land r) \lor (q \land r)$ \solt
	\end{enumerate}

		\item Give an assigment to the variables $x_1,x_2,x_3$ which makes the following logical expression evaluate to true.

		$$(x_1 \lor x_2)\land (\neg x_1 \lor \neg x_2 \lor \neg x_3) \land x_3$$
		\begin{mdframed}
		\vspace{2cm}
		\end{mdframed}

		\item Is the following logical expression is always true? Explain your answer.

		$$(x_1 \land x_2)\lor (\neg x_1 \land \neg x_2 \land \neg x_3) \lor (x_3 \land \neg x_3) $$

			\begin{mdframed}
			\vspace{4cm}
			\end{mdframed}

		\item Come up with a logical expression with three variables which has \textbf{only one} assignment to the variables which makes it true.

			\begin{mdframed}
			\vspace{4cm}
			\end{mdframed}

		\item Given inputs $p$ and $q$, create a circuit which outputs $p \oplus q$ using only \textbf{not gates}, \textbf{or gates}, and \textbf{and gates}.

			\begin{mdframed}
			\vspace{6cm}
			\end{mdframed}

		\item Given inputs $x_1$, $x_2$ and $x_3$, create a circuit which outputs true if and only if exactly one input is true.

			\begin{mdframed}
			\vspace{10cm}
			\end{mdframed}

	\end{enumerate}

	\section*{Section Lesson - P vs. NP}

	\textit{Everything past this point is outside the scope of CS22, though it will reinforce concepts from the class. These concepts are covered further in CS1010 and CS157.}

	An algorithm is simply a series of steps. It can be expressed as a computer program, instructions in a cookbook, directions on a map, as a plain sentence, etc. For example, consider the following \texttt{maximum algorithm} that finds the maximum element given \texttt{list} as input.
\begin{verbatim}
v = -infinity
for each element x in list:
    v = max(v, x)
output v
\end{verbatim}
It is often helpful to decribe the \textbf{run time} of an algorithm. The \textbf{run time} can be thought of as the number of steps that the algorithm takes, and is often expressed as a function as the size of the input to the algorithm

For example, if $n$ is the size of the input list to the \texttt{maximum algorithm} then the algorithm has a run time of $f(n) = n$ steps.

Give an informal inductive argument as to why this is true. (Hint: Start by considering the base case of a list with one item. The one item is clearly the maximum so we return it in 1 step.)

\begin{mdframed}
\vspace{8cm}
\end{mdframed}

An algorithm is considered \emph{fast} if its run time is a \textbf{polynomial}. This polynomial can be $n^2$, or even $n^{100} + 4n^{63}$, it doesn't matter. If the run time is polynomial then it is feasible for a computer. If the run time is instead exponential, like $2^n$, then it is infeasible for a computer if $n$ is large. The terms \textbf{polynomial time} and \textbf{fast} are used synonymously for the rest of this handout.

	Consider the following algorithm which determines if there is any assignment to a logical expression with variables $x_1,...,x_n$ that is ever true. (The problem of determining if a logical expression evaluates to true is called \texttt{SAT}).

	\texttt{Try \textbf{every} assignment to the variables.}

	\texttt{If the logical expression ever evaluates to true, return true.}

	\begin{enumerate}[a.]
		\setcounter{enumi}{9}
		\item What is the run time of this algorithm in terms of $n$, the number of variables.

		\hint How many assignments do you have to try? Think in terms of 0/1 strings.
		\begin{mdframed}
		\vspace{3cm}
		\end{mdframed}
	\end{enumerate}

	P is the set of problems that can be solved in \textbf{polynomial time}. In other words, a problem in P can be solved by an algorithm that has a polynomial run time.

	Finding the maximum element in a list is a P problem, as is sorting a list.

	Unfortunately, the algorithm given above is the best known algorithm for determining if a logical expression ever evaluates to true (solving SAT). Therefore it is not a P problem (as far as we know).

	\subsection*{NP}

A problem is in NP if, given a candidate solution to the problem, there exists a fast algorithm to determine whether the candidate is in fact a valid solution.

	For example, determining if a logical expression ever evaluates to true (SAT) is an NP problem. We don't know how to quickly find an assignment to the variables which makes the expression true, but given an assignment to the variables we can quickly check that this assignment makes the expression true by just constructing the corresponding circuit.

		\begin{enumerate}[a.]
		\setcounter{enumi}{10}
		\item \textit{Challenge:} Another NP problem is the following: Given a number $n$ where $n=pq$ for two large primes $p$ and $q$, can you find $p$ and $q$? Explain why this is an NP problem.

		\begin{mdframed}
		\vspace{3cm}
		\end{mdframed}
	\end{enumerate}
	At first it looks like this is a P problem as well because you can simply check all pairs of numbers between 0 and $\sqrt{n}$. However, when you encode a number using 0's and 1's in a computer, it takes $\log_2(n)$ bits. Therefore looping through $\sqrt{n}$ numbers is exponential in the size of your input (we understand that this is confusing so please call over a TA if you wish to discuss this further).

	Most computer scientists beleive that P $\not=$ NP but no one can come up with a proof. There is a million dollar reward for finding one. If you were to prove that P = NP then RSA would be useless.

	\subsection*{\textit{Challenge:} NP-Hard}

	It turns out that for any problem in NP, you can come up with a logic circuit for it where determining if the circuit ever evaluates true corresponds to solving the original problem.

	Therefore, if you can find a ``fast" algorithm for determining if a logical circuit ever outputs true then P = NP.

	Changing one problem into another problem is called a ``reduction".

	A problem $p$ is NP-Hard if there is a reduction from every problem in NP to $p$.

	Another example of a problem that is NP-Hard is determining if a graph is 3-colorable, which you will learn about soon.

	\subsection*{\textit{Challenge:} Not in NP}

	There are problems that we think are even harder then NP problems. For example, given a logical expression, is it always true?

\end{document}


\documentclass[12pt]{scrartcl}

\input{preamble}

\makeatletter
\title{Hack 4.0}\let\Title\@title
\subtitle{Computer Science I -- Honors\\
{\small
\vskip1cm
Department of Computer Science \& Engineering \\
University of Nebraska--Lincoln}
\vskip-1cm}
%\author{Dr.\ Chris Bourke}
\date{~}
\makeatother

\begin{document}

\maketitle

\hrule

\input{instructions.tex}

\section*{Problem Statement}

A 401(k) plan is a type of tax-qualified account for people to save
for retirement.  Employees typically have part of their pre-tax pay
check deposited into such an account and employers may match a certain
amount of those contributions.  As of 2018, there is a maximum
annual contribution limit of \$18,500.

The money contributed to a 401(k) account may be invested in many
different ways.  While the account may grow tax free, it is not
immune to inflation.  To account for inflation, you can use
the following formula which is the inflation-adjusted rate of
return.
  $$\frac{1 + \textrm{rate of return}}{1+\textrm{inflation rate}} - 1$$

Write a program that produces an \emph{amortization table} for a
401(k) account.  Your program will read the following inputs as
command line arguments.
\begin{itemize}
  \item An initial starting balance
  \item A monthly contribution amount (we'll assume its the same over the life of the savings plan)
  \item An (average) annual rate of return (on the scale $[0, 1]$)
  \item An (average) annual rate of inflation (on the scale $[0, 1]$)
  \item A number of years until retirement
\end{itemize}
Your program will then compute a monthly savings table detailing
the (inflation-adjusted) interest earned each month, contribution, and
new balance.  To get the monthly rate, simply divide by 12.  Each month,
interest is applied to the balance at this rate (prior to the monthly
deposit) and the monthly contribution is added.  Thus, the earnings
compound month to month.  Be sure to round appropriately, to the nearest
cent so that rounding errors do not compound.

Your program will need to validate the input and quit with an error
message on any potential bad input(s).  For inputs

\mintinline{text}{10000 500 0.09 0.012 10}

your output should look something like the following:

\begin{figure}[H]
\begin{minted}{text}
Month    Interest     Balance
    1 $     64.23 $  10564.23
    2 $     67.85 $  11132.08
    3 $     71.50 $  11703.58
    4 $     75.17 $  12278.75
    5 $     78.87 $  12857.62
    6 $     82.58 $  13440.20
    7 $     86.33 $  14026.53
    8 $     90.09 $  14616.62
    9 $     93.88 $  15210.50
...
  116 $    678.19 $ 106767.24
  117 $    685.76 $ 107953.00
  118 $    693.37 $ 109146.37
  119 $    701.04 $ 110347.41
  120 $    708.75 $ 111556.16
Total Interest Earned: $  41556.16
Total Nest Egg: $ 111556.16
\end{minted}
\end{figure}

\section*{Instructions}

\begin{itemize}
  \item You are encouraged to collaborate any number of students
  before, during, and after your scheduled hack session.
  \item Design at least 3 test cases \emph{before} you begin
  designing or implementing your program.  Test cases are
  input-output pairs that are known to be correct using means
  other than your program.
  \item Include the name(s) of everyone who worked together on
  this activity in your source file's header.
  \item Place your code in a \mintinline{java}{main()} method in
    a source file named \mintinline{text}{Retire.java} and turn it
    in via webhandin.  Make sure that
    it runs and executes correctly in the webgrader. Each individual
    student will need to hand in their own copy and will receive their
    own individual grade.

\end{itemize}

\end{document}