%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[12pt]{article}
\pdfoutput=1

\usepackage{fullpage}
\usepackage{graphicx}   % need for figures
\usepackage{subfig}  % use for side-by-side figures
\usepackage{hyperref}   % use for hypertext links, including those to external documents and URLs
\usepackage{mdwlist}
\usepackage{xspace}
\usepackage[usenames,dvipsnames]{color}
\usepackage{amsmath, amsthm,amssymb}

\usepackage{mathpazo} % math & rm
%\linespread{1.05}        % Palatino needs more leading (space between lines)
\usepackage[scaled]{helvet} % ss
\usepackage{courier} % tt
%\normalfont
\usepackage[T1]{fontenc}


\begin{document}

%%%%%%%%%% TITLE %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{center}
{\LARGE Homework 2} \\ \vspace{3mm}

{\large Due on October 29}
\end{center}


\vspace{10mm}
\noindent {\large \textsc{A: Size Bounds for Joins} {\bf[25\%]}}

\noindent \rule[3mm]{13.9cm}{0.5mm}

\begin{enumerate}

\item {\bf [15\%]}  Compute the maximum possible output size for the following join queries, when all relations have size at most $N$.

%
\begin{enumerate}
\item  $q(x,y,z) :- R(x), T(y), U(z), S(x,y,z). $ 
%
\item $q(x,y,z,w,t) :- R(x,y), S(y,z), T(z,w), U(w,x), V(x,t). $ 
%
\item $q(x,y,z,w) :- R(x,y,z), S(x,z,w), T(x,y,w), U(y,z,w).$ 
\end{enumerate}



\item {\bf [10\%]} Suppose that relation $R_i$ has size $N_i$ (in tuples). Compute the maximum output size for the following query. (Hint: there will be different cases depending on the given $N_i$).
%
$$ q(x_1, x_2, x_3, x_4, x_5):- R_1(x_1,x_2), R_2(x_2,x_3), R_3(x_3,x_4), R_4(x_4,x_5).$$

\end{enumerate}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{10mm}
\noindent {\large \textsc{B: Datalog} {\bf[75\%]}}

\noindent \rule[3mm]{12.5cm}{0.5mm}


\begin{enumerate}
 
\item {\bf [15\%]} Is the following Datalog program equivalent to a UCQ query? If so, write the query. If not, prove why it is not the case.
%
\begin{verbatim} 
B(X, Y) : - L(X, Y).
B(X, Y) : - T(X), B(Z, Y). 
\end{verbatim} 

\item {\bf [20\%]}   A Datalog program $P$ with a singe recursive predicate is said to be {\em bounded} if there is a positive integer $n_0$ such that, on every database instance $I$, the bottom-up evaluation of $P$
terminates within at most $n_0$ steps. Otherwise, we say that $P$ is unbounded.
\begin{enumerate}
\item Prove that transitive closure is unbounded.
\item Give an example of a Datalog program that is bounded and has at least one recursive predicate.
\end{enumerate}
 
\item {\bf [10\%]}   Consider the following Datalog program:
%
\begin{verbatim}
T(x,y) :- R(x,y).
T(x,t) :- T(x,y),T(y,z),T(z,w),R(w,t).
\end{verbatim}
%
Can you write an equivalent {\em linear} Datalog program? If yes, provide the program; otherwise, explain why this is not the case.  
 
%
\item {\bf [20\%]} Perform the magic set transformation for the following Datalog program:
%
\begin{verbatim}
T(x,y) :- F(x,y).
T(x,y) :- up(x,z1),T(z1,z2),F(z2,z3),T(z3, z4),down(z4,y).
q(y)   :- T(a,y).
\end{verbatim}

\item {\bf [10\%]} Find all possible stratifications for the following Datalog program with negation:
%
 \begin{verbatim}
T(x)   :- S(x), not R(x).
S(x)   :- T(x), not R(x). 
U(x)   :- R(x), not T(x), not S(x).
V(x,y) :- V(x,y), not U(x), not U(y).
\end{verbatim}
 
 
\end{enumerate}

\vspace{10mm}
\noindent {\large \textsc{Deliverables}}

\noindent \rule[3mm]{4.3cm}{0.5mm}

Submit a single PDF document using \texttt{Canvas} (Homework 2). It is strongly suggested to use \LaTeX\ to write your solution.


\end{document}
