\documentclass[11pt]{article}

\usepackage[right=0.8in, top=1in, bottom=1.2in, left=0.8in]{geometry}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage[colorlinks,linkcolor=black]{hyperref}
\usepackage{cleveref}
\usepackage{tikz}
\usepackage{url}
\usepackage{fixme}
\usepackage[roman]{complexity}
\usepackage{setspace}
\spacing{1.06}

\newtheorem{theorem}{Theorem}
\newtheorem{example}[theorem]{Example}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}

\newcommand{\handout}[5]{
    \noindent
    \begin{center}
        \framebox{
            \vbox{\vspace{0.25cm}
                \hbox to 5.78in { {CS Theory (Spring '25)} \hfill #2 }
                \vspace{0.48cm}
                \hbox to 5.78in { {\Large \hfill #5  \hfill} }
                \vspace{0.42cm}
                \hbox to 5.78in { {#3 \hfill #4} }\vspace{0.25cm}
            }
        }
    \end{center}
    \vspace*{4mm}
}

\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{#4}{#1}}

\begin{document}

\lecture{Lecture Note: Streaming Lower Bounds}{February 25, 2025}{Instructor:\hspace{0.08cm}\emph{Josh Alman }}{}

Recall that last time we learnt that every regular language can be recognized by streaming algorithms using $O(1)$ space, and $O(1)$-space streaming algorithms only recognize regular languages. 

We also studied two examples. We showed that
\[L_1 \coloneq \{w \in \{0, 1\}^* \mid \text{$w$ has more $0$'s than $1$'s}\}\]
has an $O(\log n)$-space streaming algorithm, and 
\[L_2 \coloneq \{w \in \{0, 1\}^* \mid \text{$w$ is a palindrome}\}\]
has an $O(n)$-space algorithm.

This time, we will prove lower bounds on the space usage of streaming algorithms. In particular, we will prove that there is no possible streaming algorithm for $L_1$ that uses fewer than $O(\log n)$ space, and there is no possible streaming algorithm for $L_2$ that uses fewer than $O(n)$ space.

\section{Streaming lower bounds}

Let's first recall algorithm for $L_1$:
\begin{itemize}
    \item Variable: $a$.
    \item Initialization: set $a = 0$.
    \item Update rule: on input $\sigma \in \{0, 1\}$, if $\sigma = 0$ then set $a \coloneq a + 1$, if $\sigma = 1$ then set $a \coloneq a - 1$.
    \item Stopping rule: if $a > 0$ then accept, else reject.
\end{itemize} 
%Variable $a$, init: set $a = 0$, update: on input $\sigma \in \{0, 1\}$, if $\sigma = 0$ then set $a \coloneq a + 1$, if $\sigma = 1$ then set $a \coloneq a - 1$.

%Recall algorithm for $L_1$:
%variable: $a$;
%initialization: set $a \coloneq 0$
%update rule: on input $\sigma \in \{0, 1\}$, if $\sigma = 0$ then set $a \coloneq a + 1$, if $\sigma = 1$ then set $a \coloneq a - 1$.
% stop rule: if $a > 0$ then accept, else reject.
This streaming algorithm takes $O(\log n)$ space because the variable $a$ can take on values from $-n, -n + 1, \dots, n - 1, n$ on an input of length $n$.

The key idea for proving a lower bound on space usage is to identify some input strings which would give different values of $a$ in our algorithm, and moreover prove that not just our algorithm, but any streaming algorithm for the language must have different memory states for those strings.

%Want: Big set of strings of length at most $n$, such that no two would result in the same memory state of our algorithm. 

Let us pick $\{0, 00, 000, 0000, \dots, 0^n\}$, and show the following.

\begin{lemma}\label{lemma: 1}
For any streaming algorithm for $L_1$ and integers $p \ne q$, the strings $0^p$ and $0^q$ must result in different memory states.
\end{lemma}

\begin{proof}
    Without loss of generality, suppose $p < q$. Suppose after reading in either $0^p$ or $0^q$, we then read in $1^p$. That is, the whole input string is $0^p1^p$ or $0^q1^p$. If the algorithm was in the same memory state after reading $0^p$ versus $0^q$, then it must also be in the same memory state after reading $0^p1^p$ versus $0^q1^p$. This is because the update rule depends only on the current memory state and the next symbol we read in. That means the streaming algorithm either accepts both $0^p1^p$ and $0^q1^p$, or rejects both. This is a contradiction since $0^p1^p \notin L_1$, but $0^q1^p \in L_1$ (as we assumed $p < q$).
\end{proof}

%$L_1 \coloneq \{w \in \{0, 1\}^* \mid \text{$w$ has more $0$'s than $1$'s}\}$. 
We are ready to prove the space usage lower bound with \Cref{lemma: 1}.
\begin{theorem}\label{thm: 2}
    Any streaming algorithm for $L_1$ must use at least $\log_2(n)/100$ space for inputs of length up to $n$.
\end{theorem}

\begin{proof}
    Assume to the contrary we have an algorithm for $L_1$ that uses less than $(1/100)\log_2(n)$ space.
    
    Consider the set of inputs $S = \{0, 00, 000, 0000, \dots, 0^n\}$.
    
    Since $A$ uses less than $(1/100)\log_2(n)$ space, the number of possible memory configurations of $A$ is at most\footnote{The exact number depends on our model of memory usage. In the model we use, the algorithm can use up to $m$ bits of memory for some $m$, so if $m = 2$ for example, the memory content can be $\varepsilon, 0, 1, 00, 01, 10$, or $11$ and there will be $7$ possibilities. If we instead required the streaming algorithm to use exactly $m$ bits of memory, then when $m = 2$ for example, the memory content could be $00, 01, 10$, or $11$ and there would be $4$ possibilities. However, there will only be a constant factor of different between different models, and this is one of the reasons we use big-$O$ notation: a factor of constant does not matter under big-$O$ notation.}
    \[\sum_{i = 0}^{\log_2(n)/100}2^i = 2^{\log_2(n)/100 + 1} - 1 \le 2n^{1/100}.\]
    This is much less than $|S| = n$. Therefore, by the pigeonhole principle, there must be two different strings in $S$ that leads to the same memory configuration of $A$. This contradicts \Cref{lemma: 1}.
\end{proof}


%Example: suppose we have a streaming algorithms using $2$ bits of space. All possible configurations of space are $00, 01, 10, 11, 0, 1, \varepsilon$. One reason we use big-$O$ notation is we don't need to care about small details in the model (for example, if you only allow the memory content to be $00, 01, 10, 11$, this has a factor of $2$ difference.

Below we state the general form of the above method for proving streaming lower bounds.

\begin{definition}
    Fix a language $L$ over alphabet $\Sigma$. We say two strings $x, y \in \Sigma^*$ are \emph{distinguishable} if there is a string $z \in \Sigma^*$ such that exactly one of $xz$ and $yz$ is in $L$.
\end{definition}

\begin{definition}
    We call $S_n \subset \Sigma^*$ a \emph{length-$n$ distinguishing set} if
    \begin{enumerate}
        \item all strings in $S_n$ has length at most $n$;
        \item all pairs of strings in $S_n$ are distinguishable.
    \end{enumerate}
\end{definition}


\begin{theorem}\label{thm: 5}
    If a language $L$ has a length-$n$ distinguishing set $S_n$, then any streaming algorithm for $L$ must use at least $(1/100)\log_2|S_n|$ space on inputs of length at most $n$.
\end{theorem}

\begin{proof}
    The proof is similar to \Cref{thm: 2}. Assume to the contrary we have an algorithm $A$ for $L$ that uses less than $(1/100)\log_2|S_n|$ space. The number of possible memory configurations of $A$ is thus at most
    \[\sum_{i = }^{(1/100)\log_2|S_n|}2^i = 2^{(1/100)\log_2|S_n| + 1} - 1 \le 2|S_n|^{1/100}.\]
    This is much less than $|S_n|$. Therefore, by the pigeonhole principle, there must be two different strings $x, y$ in $S_n$ that lead to the same memory configuration of $A$. Since $S_n$ is a distinguishing set, $x, y$ is distinguishable. Therefore, there is a string $z \in \Sigma^*$ such that exactly one of $xz$ and $yz$ is in $L$. 
    
    However, since $x$ and $y$ lead to the same memory configuration of $A$, $xz$ and $yz$ should also lead to the same memory configuration of $A$ (as the update rule depends only on the current memory state and the next symbol). Therefore, $xz$ and $yz$ are either both accepted by $A$ or both rejected by $A$. This contradicts that only one of $xz$ and $yz$ is in $L$.
\end{proof}

%Proof: just like before


Now we use \Cref{thm: 5} to show the following.
\begin{theorem}
    Streaming algorithms for $L_2 \coloneq \{w \in \{0, 1\}^n \mid \text{$w$ is a palindrome}\}$ need at least $n/100$ space.
\end{theorem}

\begin{proof}
    Let $S_n = \{0, 1\}^n$. This is a distinguishing set because for any distinct $x, y \in \{0, 1\}^n$, $x, y$ can be distinguished with $z = \text{reverse}(x)$, where $\text{reverse}(x)$ is the string $x$ flipped backward (for example, $\text{reverse}(00111) = 11100$). Actually, $xz \in L_2$, while $yz \notin L_2$. By \Cref{thm: 5}, streaming algorithms for $L_2$ need at least $(1/100)\log_2|S_n| = n/100$ space.
\end{proof}


%Let $S_n = \{1, 01, 001, 0001, 00001, \dots, 0^{n - 1}1\}$. This is a length-$n$ distinguishing set because $x = 0^p1$, $y = 0^q1$ can be distinguished with $z = 10^p$. Actually, $xz = 0^p110^p \in L_2$, while $yz = 0^q110^p \notin L_2$.

To summarize, today we proved that the space usage of the algorithms we discussed in the last lecture for $L_1$ and $L_2$ are optimal (up to constant factor).

\Cref{thm: 5} also has the following corollary. (Recall that every regular language has $O(1)$-space streaming algorithms.)
\begin{corollary}
    If $L$ has superconstant-sized length-$n$ distinguishing sets, then $L$ is not a regular language. 
\end{corollary}

Here superconstant means not $O(1)$. Formally, $f(n)$ is superconstant if $\forall c > 0$, $\exists n > 0$ such that $f(n) > c$.

\end{document}
