\documentclass[11pt]{amsart}

\usepackage{amsmath,amsthm,amssymb}
\usepackage{stmaryrd}

\usepackage{fancyhdr} % Allows customization of verbatim environments

\usepackage{xcolor}

%\usepackage[textheight=8.5in, margin=1.6in]{geometry}
\usepackage[
  hmarginratio={1:1},     % equal left and right margins
%  vmarginratio={1:1},     % equal top and bottom margins
  %textwidth=400pt,        % new text width
  heightrounded,          % always useful
  %bindingcorrection=5mm,  % binding correction
  total={6in,8in}
]{geometry}
\headheight=13pt
\footskip=13pt

\usepackage{needspace}  % <-- to prevent bad pagebreak over environment titles
% Use:  \Needspace{3\baselineskip} before opening an environment

\usepackage{graphicx}

% Tikz stuff.
\usepackage{tikz}
%\usepackage{tkz-graph}
\usepackage{pgfplots}
%\usepackage{tikz-3dplot}
\pgfplotsset{compat=newest}
%\usepgfplotslibrary{fillbetween}
%\usetikzlibrary{cd}
%\usetikzlibrary{positioning}
%\usetikzlibrary{decorations.pathmorphing}    
%\usetikzlibrary{decorations.pathreplacing}
%\usetikzlibrary{arrows.meta,calc}
%\usetikzlibrary{bending}
%\usetikzlibrary{decorations.markings,shapes.geometric}
%\tikzset{->-/.style={decoration={markings, mark=at position #1 with
%  {\arrow{>}}},postaction={decorate}}}
%\tikzset{-|-/.style={decoration={markings, mark=at position #1 with
%  {\arrow{stealth}}},postaction={decorate}}}
%\tikzset{movearrow/.style 2 args ={
%        decoration={markings,
%	  mark= at position {#1} with {\arrow{#2}} ,
%        },
%        postaction={decorate}
%    }
%}
%\tikzset{<--/.style={decoration={markings, mark=at position #1 with
%  {\arrow{<}}},postaction={decorate}}}
\tikzstyle{ball} = [circle,shading=ball, ball color=black,
    minimum size=1mm,inner sep=1.3pt]
%\tikzstyle{bball} = [circle,shading=ball, ball color=blue,
%    minimum size=1mm,inner sep=1.3pt]
%\tikzstyle{miniball} = [circle,shading=ball, ball color=black,
%    minimum size=1mm,inner sep=0.5pt]
%\tikzstyle{redminiball} = [circle,shading=ball, ball color=red,
%    minimum size=1mm,inner sep=0.5pt]
%\tikzstyle{mminiball} = [circle,shading=ball, ball color=black,
%    minimum size=0.6mm,inner sep=0.1pt]
%\tikzstyle{mycircle} = [draw,circle, minimum size=1mm,inner sep=1.3pt]
%\newcommand*{\DashedArrow}[1][]{\mathbin{\tikz [baseline=-0.25ex,-latex, dashed,#1] \draw [#1] (0pt,0.5ex) -- (1.3em,0.5ex);}}%

%%%% tikz style for finite automata
\usetikzlibrary{automata, positioning, arrows}
\tikzset{fa/.style={
    ->, % makes the edges directed
    >=stealth, % makes the arrow heads bold
    node distance=3cm, % specifies the minimum distance between two nodes. Change if necessary.
    every state/.style={thick, fill=gray!10}, % sets the properties for each ’state’ node
    initial text=$ $, % sets the text that appears on the start arrow
}}
%%%%

\usepackage{needspace}  % <-- to prevent bad pagebreak over environment titles
% Use:  \Needspace{3\baselineskip} before opening an environment

\usepackage[shortlabels]{enumitem}
\setlist[enumerate]{itemsep=4pt,leftmargin=*,label={(\alph*)}}
%\setlist[enumerate]{itemsep=2pt,label={\arabic*.}}

\usepackage[T1]{fontenc}
\renewcommand{\labelitemi}{\guillemotright}

\allowdisplaybreaks

\usepackage[colorlinks=true,citecolor=black,linkcolor=black,urlcolor=blue]{hyperref}

\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\W}{\mathbb{W}}
\newcommand{\KK}{\Bbbk}
\newcommand{\ve}{\varepsilon}
\newcommand{\scribe}{
  \begin{center}%
    Scribe~$=$ \rule{7cm}{0.15mm}%
  \end{center}%
  \medskip%
}
\DeclareMathOperator{\im}{\mathrm{im}}
\DeclareMathOperator{\id}{\mathrm{id}}
\DeclareMathOperator{\glb}{\mathrm{glb}}
\DeclareMathOperator{\lub}{\mathrm{lub}}

\newcounter{problem}
\renewcommand \thesection {\theproblem}
\newenvironment{prob}[1][]
{
\refstepcounter{problem}\par\medskip
\def\myArg{#1}
  \ifx\myArg\empty
    \noindent \textsc{Problem~\theproblem.} \rmfamily
  \else
    \noindent \textsc{Problem~\theproblem\ (\textnormal{#1}).} \rmfamily
  \fi
}
{
\medskip

}

\newenvironment{sol}
{\emph{Solution.}
}
{
  \medskip

}

\parskip = 0.05in
\parindent = 0.0in

%\pagestyle{empty}
%\pagenumbering{gobble}

\fancypagestyle{firststyle}
{
\fancyhead[L,LO]{
\textit{\small Math 387 Group problems, Friday Week 2}
}
\renewcommand{\headrulewidth}{0pt}
}

\begin{document}
\thispagestyle{firststyle}

For the following, let~$\Sigma=\{0,1\}$. 
\bigskip

\begin{prob}  Find CFGs for the following languages: .
  \begin{enumerate}
    \item $L=\{w: w \text{ has odd length and its middle symbol is~$0$}\}$.
    \item $L=\{w: w \text{ has twice as many $0$s as $1$s}\}$.
  \end{enumerate}
\end{prob}

\begin{sol}
  \begin{enumerate}
    \item \
      \[
	S\to 0S0|0S1|1S0|1S1|0
      \]
    \item \
      \begin{align*}
	S&\to T|\varepsilon\\
	T&\to TT1|T1T|1TT|0.
      \end{align*}
  \end{enumerate}
\end{sol}

\begin{prob}
  \begin{enumerate}
    \item Show that context free languages are closed under the union, concatenation, and
      star operations.  (By the way: they are not closed under intersection and
      complementation, in general.)  For notation, let $A$ and~$B$ be context
      free grammars with start states $S_{A}$ and $S_{B}$, respectively.
      Describe context free grammars generating the languages $L(A)\cup L(B)$,
      $L(A)L(B)$, and $L(A)^{*}$.  You may assume that the variables for $A$ and
      $B$ are distinct.
    \item With notation as above, give an example that shows $L(A)^{*}$ is
      not necessarily generated by the context free grammar obtained from $A$ by adding the rule
      $S_{A}\to S_{A}S_{A}$ to the starting rules for $A$.
  \end{enumerate}
\end{prob}

\begin{prob} 
  Draw the state diagram of a PDA that accepts the language 
  \smallskip

  \[
    L=\{w: w \text{ contains more $0$s than $1$s}\}.
  \]
\end{prob}

\begin{sol}
  A possible PDA is
  \begin{center}
    \begin{tikzpicture}[fa,baseline={([yshift=-1em] current bounding box.north)}]
      \node[state, initial] (s) {$q_{0}$};
      \node[state, right of=s] (l1) {$q_{1}$};
      \node[state, right of=l1] (l2) {$q_{2}$};
      \node[state, accepting, right of=l2] (l3) {$q_{3}$};
      \draw (s) edge[above] node{$\varepsilon,\ \varepsilon\to\$$} (l1);
      \draw (l1) edge[loop above, above] node{$\mathrm{loop}$} (l1);
      \draw (l1) edge[above] node{$\varepsilon,\ 0\to \varepsilon$} (l2);
      \draw (l2) edge[loop above,  above] node{$\varepsilon,\ 0\to \varepsilon$} (l2);
      \draw (l2) edge[above] node{$\varepsilon,\ \$\to \varepsilon$} (l3);
    \end{tikzpicture}
  \end{center}
  where the loop has transitions:
  \begin{align*}
    0,&\ \$\to 0\$\\
    0,&\ 0\to 00\\
    0,&\ 1\to \varepsilon\\
    1,&\ \$\to 1\$\\
    1,&\ 0\to \varepsilon\\
    1,&\ 1\to 11.
  \end{align*}
\end{sol}

\begin{prob} (Bonus)
   Create a CFG that generates the language $L=\{ xy: |x|=|y|, x\neq y\}$.
\end{prob}
\end{document}
