\chapter{Search Problems \& Uninformed Search}

\section{Search Problems}

\subsection{Review}

In order to formalize a problem as a search problem, we need to define the following:

\begin{listo}
    \item \term{State Space}: A state is a representation of a configuration of the problem domain. The state space is the set of all states included in our model of the problem.
    \item \term{Initial State}: The starting configuration.
    \item \term{Goal State}: The configuration one wants to achieve. 
    \item \term{Actions} (or \term{State Space Transitions}): Allowed changes to move from one state to another.
\end{listo}

Optionally, we may use the following:
\begin{listo}
    \item \term{Costs}: Representing the cost of moving from state to state.
    \item \term{Heuristics}: Help guide the heuristic process
\end{listo}

\begin{example}[Make Change]
    We start with $0$ cents and we want to reach some number of cents between $[0, 5, 10, 15, ..., 500]$ using the least number of coins. We can add $5, 100, 25, 100$, or $200$ cents.

    Search problem formulation with the goal of $X$ cents:
    \begin{listu}
        \item States: Integers between 0 and 500 that are divisible by 5.
        \item Initial State: 0
        \item Goal State: $X$
        \item Actions: Add 5, 10, 25, 100, or 200
    \end{listu}
\end{example}

\subsection{Questions}

\begin{listo}
    \item Sudoku is a popular number puzzle that works as follows: we are given a $9 \times 9$ square grid; some squares have numbers, while some are blank. Our objective is to fill in the blanks with numbers from $1 - 9$ such that each row, column, and the highlighted $3 \times 3$ squares contain no duplicate entries (see Figure 1).
    
    % TODO: Insert Figure 1 and Figure 2

    Sudoku puzzles can be solved easily after being modelled as a CSP (which will be covered later in the course). We will consider the problem of generating Sudoku puzzles. In particular, consider the following procedure: start with a completely full number grid (see Figure 2), and iteratively make some squares blank. We continue blanking out squares as long as the resulting puzzle can be completed in at least one way.

    Complete the following:
    
    \begin{listu}
        \item Give the representation of a state in this problem.

        \begin{solution}
            A partial valis solution matrik $M \in \{ 0, \dots, 9 \}$. 
        \end{solution}

        \item Using the state representation defined above, specify the initial state and goal state(s).

        \begin{solution}
            \null

            \textbf{Initial State:} A complete filled grip. All rows, columns, and $3 \times 3$ squares contain $\{ 1, 2, \dots, 9 \}$. 

            \textbf{Goal State:} Any state that can be completed in at least one way.
        \end{solution}

        \item Define its actions.

        \begin{solution}
            \[
                M - E_{ij}(a)
            \] where $E_{ij}(a)$ is a matrix of all zero except for the $i$th row and $j$th column, which is $a = M[i, j] \neq 0$.
        \end{solution}

        \item Using the state representation and actions defined above, specify the transition function $T$. (In other words, when each of the actions defined above is applied to a current state, what is the resulting state?)
        
        \begin{solution}
                $T(M - E_{ij}(a)) = M - E_{ij}(a)$
        \end{solution}
    \end{listu}

    \item Assuming that ties (when pushing to the frontier) are broken based on alphabetical order, specify the order of the nodes that would be explored by the following algorithms. Assume that $S$ is the initial node, while $G$ is the goal node. You should express your answer in the form $S - B - A - F - G$ (i.e. no spaces, all uppercase letters, delimited by the dash ($-$) character), which, for example, corresponds to the exploration order of $S, B, A, F$, then $G$.

    \begin{figure}[ht!]
        \centering

        \tikzexternalenable
        \begin{tikzpicture} [
            every node/.style = {draw=black, circle},
        ]

            \node (S) at (0,  0) {$S$};
            \node (A) at (2,  2) {$A$};
            \node (B) at (2,  0) {$B$};
            \node (C) at (2, -2) {$C$};
            \node (D) at (4,  0) {$D$};
            \node (E) at (4, -2) {$E$};
            \node (F) at (4,  2) {$F$};
            \node (G) at (6,  0) {$G$};

            \draw[thick,-latex] (S) -- (B);
            \draw[thick,-latex] (S) -- (C);
            \draw[thick,-latex] (A) -- (F);
            \draw[thick,-latex] (B) -- (A);
            \draw[thick,-latex] (B) -- (D);
            \draw[thick,-latex] (B) -- (E);
            \draw[thick,-latex] (C) -- (E);
            \draw[thick,-latex] (E) -- (D);
            \draw[thick,-latex] (F) -- (G);
        \end{tikzpicture}
        \tikzexternaldisable
        
        \caption{Graph for question 2(b).}
    \end{figure}

    \begin{listo}
        \item Depth-First Search with no cycle-checking (aka tree-based) implementation.

        \begin{solution}
            $S - C - E - D - B - E - D - A - F - G$
        \end{solution}

        \item Breadth-First Search with no cycle-checking (aka tree-based) implementation.

        \begin{solution}
            $S - B - C - A - D - E - F - G$
        \end{solution}

        \item Breadth-First Search with cycle-checking (aka graph-based) implementation.

        \begin{solution}
            $S - B - C - A - D - E - F - G$
        \end{solution}
    \end{listo}

    \item \begin{listo}
        \item The \textbf{Breadth-First Search} algorithm is complete if the state space has infinite depth but finite branching factor.
        
        Determine if the statement above is True or False, and provide a rationale.

        \begin{solution}
            True. 

            Completenes of BFS means whenever there is a path from the initial path to the goal, BFS will find it. 

            Exisance of a path means goal exist in a finite depth, hence BFS will reach it given a finite branching factor.
        \end{solution}

        \item The \textbf{Breadth-First Search} algorithm can be complete even if zero step costs are allowed.

        Determine if the statement above is True or False, and provide a rationale

        \begin{solution}
            True. 

            BFS is complete as long as state space has finite branching factor. 
        \end{solution}

        \item Given that a goal exists within a finite search space, the \textbf{Breadth-First Search} algorithm is cost-optimal if all step costs from the initial state are non-decreasing in the depth of the search tree. That is, for any given level of the search tree, all step costs are greater than the step costs in the previous level.

        Determine if the statement above is True or False, and provide a rationale

        \begin{solution}
            False. 
            
            Consider the following counter example, arbitrary alphabetic order results in $A$ reached before $B$. 

            \begin{figure}[ht!]
                \centering

                \tikzexternalenable
                \begin{tikzpicture}
                    \node[draw, circle] (S) at (0,  0) {$S$};
                    \node[draw, circle] (A) at (1, -1) {$A$};
                    \node[draw, circle] (B) at (1,  1) {$A$};

                    \draw[thick,-latex] (S) -- (A) node[midway, below left] {$9$};
                    \draw[thick,-latex] (S) -- (B) node[midway, above left] {$1$};
                \end{tikzpicture}
                \tikzexternaldisable
            \end{figure}
        \end{solution}
    \end{listo}

    \item Prove that the \textbf{Uniform Cost Search} algorithm is cost-optimal as long as each step cost exceeds some small positive constant $\epsilon$.

    \begin{solution}
        \begin{proof}
            WTS UCS is optimal as long as each step cost exceeds positive value $\epsilon$. 

            Given that each stap cost exceeds $\epsilon$, completenemss can be assumed 

            \begin{listu}
                \item when UCS expands a node $n$, the optimal path to that node has been found. If not, there would be another node $n'$ on the optimal path from the start node to $n$ ($n'$). Bt definition it will have a lower cost than $n$.
                
                Contradition because it was not selected. 

                \item If step costs are non-negative, path never get shorter as nodes are added. Thus, UCS expands nodes in the order of the optimnal path cost. 
            \end{listu}
        \end{proof}
    \end{solution}
\end{listo}

\section{UCS and A* \& Heuristics}

\subsection{Problems}

\begin{listo}
    \item Consider the search problem of figure 1. Execute UCS (without cycle-checking) starting from node $S$, and the goal node being $G$.

    % TODO: Insert Figure 1

    \begin{table}[ht!]
        \centering

        \begin{tabular}{r | l}
            (Node, Path)  & Frontier                                   \\ \hline
                          & $\{ (S, 0) \}$                             \\
            $(S, S)$      & $\{ (SP, 1), (SD, 3), (SE, 9) \}$          \\
            $(P, SP)$     & $\{ (SD, 3), (SE, 9), (SPQ, 16) \}$        \\
            $(D, SD)$     & $\{ (SDE, 5), (SE, 9), (SPQ, 16) \}$       \\
            $(E, SDE)$    & $\{ (SDEH, 6), (SE, 9), (SPQ, 16) \}$      \\
            $(H, SDEH)$   & $\{ (SE, 9), (SDEHQ, 10), (SPQ, 16) \}$    \\
            $(E, SE)$     & $\{ (SEH, 10), (SDEHQ, 10), (SPQ, 16) \}$  \\
            $(H, SEH)$    & $\{ (SDEHQ, 10), (SEHQ, 14), (SPQ, 16) \}$ \\
            $(Q, SDEHQ)$  & $\{ (DEHQG, 11), (SEHQ, 14), (SPQ, 16) \}$ \\
            $(G, SDEHQG)$ & $\{ (SEHQ, 14), (SPQ, 16) \}$              \\
        \end{tabular}
    \end{table}

    \item A tracing example of executing A* (without cycle-checking) on the problem of figure 2
    
    % TODO: Insert Figure 2

    Each cost is depicted as the cost to get that node plus the heuristic cost at that node. Next node to be expanded is highlighted in blue. Here we maintain (Node, Path, Cost) as the tuple in the frontier.

    \begin{table}[ht!]
        \centering

        \begin{tabular}{||c|c||}
            \hline
            Node Popped & Frontier                                                                                                     \\ \hline \hline
                        & $\{ {\color{blue}(A,A, 0+8=8)} \}$                                                                           \\ \hline
            $A$         & $\{ (C,AC, 1+7=8), {\color{blue}(B,AB, 4+3=7)} \}$                                                           \\ \hline
            $B$         & $\{ {\color{blue}(C,AC, 1+7=8)}, (C,ABC, 6+7=13), (D,ABD, 10+0=10) \}$                                       \\ \hline
            $C$         & $\{ {\color{blue}(B,ACB, 3+3=6)}, (D,ACD, 10+0=10), (C,ABC, 6+7=13), (D,ABD, 10+0=10) \}$                    \\ \hline
            $B$         & $\{ (C,ACBC, 5+7=12), {\color{blue}(D,ACBD, 9+0=9)}, (D,ACD, 10+0=10), (C,ABC, 6+7=13), (D,ABD, 10+0=10) \}$ \\ \hline
        \end{tabular}
    \end{table}

    \begin{listo}
        \item Execute A* with cycle-checking on the problem of figure 2.

        \begin{solution} \begin{table}[ht!]
            \centering
        
            \begin{tabular}{||c|c||}
                \hline
                Node Popped & Frontier                                                                                                     \\ \hline \hline
                            & $\{ {\color{blue}(A,A, 0+8=8)} \}$                                                                           \\ \hline
                $A$         & $\{ (C,AC, 1+7=8), {\color{blue}(B,AB, 4+3=7)} \}$                                                           \\ \hline
                $B$         & $\{ {\color{blue}(C,AC, 1+7=8)}, (D,ABD, 10+0=10) \}$                                       \\ \hline
                $C$         & $\{ {\color{blue}(B,ACB, 3+3=6)}, (D,ABD, 10+0=10) \}$                    \\ \hline
                $B$         & $\{ (C,ACBC, 5+7=12), {\color{blue}(D,ACBD, 9+0=9)} \}$ \\ \hline
            \end{tabular}
        \end{table} \end{solution}

        \item Prove that the A* Search algorithm with cycle-checking is optimal when a consistent heuristic is utilized.

        \item In the search problem below, we have listed 5 heuristics. Indicate whether each heuristic is admissible and/or consistent in the table below.

        \begin{remark}
            \begin{listu}
                \item Consistent: $h(v_i) \le h(v_j) + c(v_i, v_j)$

                \item Admissible: $h(v) \le c^*(v)$
            \end{listu}
        \end{remark}

        \begin{table}[ht!]
            \centering
        
            \begin{tabular}{||c|c|c|c|c|c|c||}
                \hline
                      & S & A & B & G & Admissible            & Consistent            \\ \hline \hline
                $h_1$ & 0 & 0 & 0 & 0 & \color{primary} True  & \color{primary} True  \\ \hline
                $h_2$ & 8 & 1 & 1 & 0 & \color{primary} True  & \color{primary} False \\ \hline
                $h_3$ & 9 & 3 & 2 & 0 & \color{primary} True  & \color{primary} False \\ \hline
                $h_4$ & 6 & 3 & 1 & 0 & \color{primary} True  & \color{primary} True  \\ \hline
                $h_5$ & 8 & 4 & 2 & 0 & \color{primary} False & \color{primary} False \\ \hline
            \end{tabular}
        \end{table}

        \item Given that a heuristic $h$ is such that $h(s_g) = 0$ where $s_g$ is any goal state, prove that if $h$ is consistent, then it must be admissible

        \item \begin{enumerate}[label=\alph*)]
            \item Provide a counter-example to show that the \textbf{Greedy Best-First Search} algorithm without cycle-checking is incomplete.

            \begin{solution}
                \begin{center} \begin{tikzpicture}
                    \node[draw,circle] (S0) at (0, 0) {$S_0$};
                \end{tikzpicture} \end{center}
            \end{solution}
            
            \item Provide a counter-example to show that Greedy Best-First Search (with or without cycle-checking) is not optimal.
        \end{enumerate}
    \end{listo}
\end{listo}