cws/cw05.tex
author Christian Urban <urbanc@in.tum.de>
Fri, 03 Mar 2017 14:50:47 +0000
changeset 118 276b3127f84a
parent 111 cws/cw04.tex@cd6b9fe4bce5
child 119 9bfc82cd3351
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
     2
\usepackage{../style}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
     3
\usepackage{../langs}
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     4
\usepackage{../graphics}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\begin{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
     8
\section*{Replacement Coursework 2 (Automata)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    10
This coursework is worth 10\%. It is about deterministic and
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    11
non-deterministic finite automata.  The coursework is due on ??? March
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    12
at 5pm.  Make sure the files you submit can be processed by just
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    13
calling \texttt{scala <<filename.scala>>}.\bigskip
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    14
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    15
\noindent
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    16
\textbf{Important:} Do not use any mutable data structures in your
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    17
submission! They are not needed. This means you cannot use 
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    18
\texttt{ListBuffer}s, for example. Do not use \texttt{return} in your
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    19
code! It has a different meaning in Scala, than in Java.  Do not use
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    20
\texttt{var}! This declares a mutable variable.  Make sure the
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    21
functions you submit are defined on the ``top-level'' of Scala, not
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    22
inside a class or object. Also note that the running time will be
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 109
diff changeset
    23
restricted to a maximum of 360 seconds on my laptop.
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
    24
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
100
93260be6770f updated
Christian Urban <urbanc@in.tum.de>
parents: 94
diff changeset
    26
\subsection*{Disclaimer}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    28
It should be understood that the work you submit represents your own
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    29
effort! You have not copied from anyone else. An exception is the
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    30
Scala code I showed during the lectures or uploaded to KEATS, which
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    31
you can freely use.\bigskip
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    34
\subsection*{Part 1 (Deterministic Finite Automata)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    36
\noindent
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    37
There are many uses for Deterministic Finite Automata (DFAs), for
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    38
example testing whether a string should be accepted or not. The main
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    39
idea is that DFAs consist of some states (circles) and transitions
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    40
(edges) between states. For example consider the DFA
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    41
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    42
\begin{center}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    43
\begin{tikzpicture}[scale=1.5,>=stealth',very thick,auto,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    44
                    every state/.style={minimum size=4pt,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    45
                    inner sep=4pt,draw=blue!50,very thick,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    46
                    fill=blue!20}]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    47
  \node[state, initial]        (q0) at ( 0,1) {$Q_0$};
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    48
  \node[state]                    (q1) at ( 1,1) {$Q_1$};
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    49
  \node[state, accepting] (q2) at ( 2,1) {$Q_2$};
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    50
  \path[->] (q0) edge[bend left] node[above] {$a$} (q1)
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    51
            (q1) edge[bend left] node[above] {$b$} (q0)
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    52
            (q2) edge[bend left=50] node[below] {$b$} (q0)
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    53
            (q1) edge node[above] {$a$} (q2)
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    54
            (q2) edge [loop right] node {$a$} ()
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    55
            (q0) edge [loop below] node {$b$} ();
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    56
\end{tikzpicture}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    57
\end{center}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    58
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    59
\noindent
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    60
where there are three states ($Q_0$, $Q_1$ and $Q_2$). The DFA has a
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    61
starting state ($Q_0$) and an accepting state ($Q_2$), the latter
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    62
indicated by double lines. In general, a DFA can have any number of
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    63
accepting states, but only a single starting state (in this example
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    64
only $a$ and $b$).
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    65
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    66
Transitions are edges between states labelled with a character. The
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    67
idea is that if I am in state $Q_0$, say, and get an $a$, I can go to
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    68
state $Q_1$. If I am in state $Q_2$ and get an $a$, I can stay in
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    69
state $Q_2$; if I get a $b$ in $Q_2$, then I have to go to state
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    70
$Q_0$. The main point of DFAs is that if I am in a state and get a
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    71
character, it is always clear which is the next state---there can only
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    72
be at most one. The task of Part 1 is to implement such DFAs in Scala
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    73
using partial functions for the transitions.
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    74
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    75
\subsubsection*{Tasks}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    76
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    77
\begin{itemize}
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    78
\item[(1)] Write a polymorphic function, called \texttt{share}, that
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    79
  decides whether two sets share some elements (i.e.~the intersection
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    80
  is not empty).\hfill[1 Mark]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    81
 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    82
\item[(2)] The transitions of DFAs are given by partial functions,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    83
  with the type of (state, character)-pair to state. For example
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    84
  the transitions of the DFA given above can be defined as
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    85
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    86
\begin{lstlisting}[language=Scala,numbers=none]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    87
val dfa_trans : PartialFunction[(State,Char), State] = 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    88
  { case (Q0, 'a') => Q1 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    89
    case (Q0, 'b') => Q0
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    90
    case (Q1, 'a') => Q2 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    91
    case (Q1, 'b') => Q0
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    92
    case (Q2, 'a') => Q2 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    93
    case (Q2, 'b') => Q0 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    94
  }
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    95
\end{lstlisting}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    96
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    97
  The main idea of partial functions (as opposed to functions) is that they
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    98
  do not have to be defined everywhere. For example the transitions
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    99
  above only mention characters $a$ and $b$, but leave out any other
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   100
  characters. Partial functions come with a method \texttt{isDefinedAt}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   101
  that can be used to check whether an input produces a result
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   102
  or not. For example
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   103
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   104
  \begin{lstlisting}[language=Scala,numbers=none]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   105
    dfa_trans.isDefinedAt((Q0, 'a'))
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   106
    dfa_trans.isDefinedAt((Q0, 'c'))
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   107
  \end{lstlisting}   
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   108
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   109
  \noindent
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   110
  gives \texttt{true} in the first case and \texttt{false} in the second.
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   111
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   112
  Write a function that takes transition and a (state, character)-pair as arguments
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   113
  and produces an optional state (the state specified by the partial transition
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   114
  function whenever it is defined; if the transition function is undefined,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   115
  return None).\hfill\mbox{[1 Mark]}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   116
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   117
\item[(3)] 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   118
  Write a function that ``lifts'' the function in (2) from characters to strings. That
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   119
  is, write a function that takes a transition, a state and a list of characters
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   120
  as arguments and produces the state generated by following the transitions for
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   121
  each character in the list. For example you are in state $Q_0$ in the DFA above
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   122
  and have the list \texttt{List(a,a,a,b,b,a)}, then you need to generate the
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   123
  state $Q_1$ (as option since there might not be such a state).\\
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   124
  \mbox{}\hfill\mbox{[1 Mark]}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   125
  
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   126
\item[(4)] DFAs are defined as a triple: (staring state, transitions, final states).
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   127
  Write a function \texttt{accepts} that tests whether a string is accepted
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   128
  by an DFA or not. For this start in the starting state of the DFA,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   129
  use the function under (3) to calculate the state after following all transitions
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   130
  according to the characters in the string. If the state is a final state, return
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   131
  true; otherwise false.\\\mbox{}\hfill\mbox{[1 Mark]}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   132
\end{itemize}
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   133
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   134
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   135
\subsection*{Part 2 (Non-Deterministic Finite Automata)}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   136
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   137
The main point of DFAs is that for every given state and character
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   138
there is at most one next state (one if the transition is defined;
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   139
none otherwise). However, this restriction to at most one state can be
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   140
quite limiting for some applications.\footnote{Though there is a
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   141
  curious fact that every NFA can be translated into an ``equivalent''
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   142
  DFA, that is accepting the same set of strings. However this might
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   143
  increase drastically the number of states in the DFA.}  
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   144
Non-Deterministic Automata (NFAs) remove this restriction: there can
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   145
be more than one starting state, and given a state and a character
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   146
there can be more than one next state. Consider for example
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   147
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   148
\begin{center}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   149
\begin{tikzpicture}[scale=0.7,>=stealth',very thick,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   150
    every state/.style={minimum size=0pt,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   151
      draw=blue!50,very thick,fill=blue!20},]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   152
\node[state,initial]  (R_1)  {$R_1$};
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   153
\node[state,initial] (R_2) [above=of R_1] {$R_2$};
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   154
\node[state, accepting] (R_3) [right=of R_1] {$R_3$};
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   155
\path[->] (R_1) edge node [below]  {$b$} (R_3);
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   156
\path[->] (R_2) edge [bend left] node [above]  {$a$} (R_3);
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   157
\path[->] (R_1) edge [bend left] node  [left] {$c$} (R_2);
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   158
\path[->] (R_2) edge [bend left] node  [right] {$a$} (R_1);
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   159
\end{tikzpicture}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   160
\end{center}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   161
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   162
\noindent
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   163
where in state $R_2$ if you get an $a$, you can go to state $R_1$
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   164
\emph{or} $R_3$. If we want to find out whether a NFA accepts a
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   165
string, then we need to explore both possibilities. We will do this
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   166
``exploration'' in the tasks below in a breath-first manner.
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   167
The possibility of having more than one next state in NFAs will
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   168
be implemented by having a \emph{set} of partial transition
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   169
functions. For example the NFA shown above will be represented by the
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   170
set of partial functions
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   171
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   172
\begin{lstlisting}[language=Scala,numbers=none]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   173
val nfa_trans : NTrans = Set(
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   174
  { case (R1, 'c') => R2 },
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   175
  { case (R1, 'b') => R3 },
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   176
  { case (R2, 'a') => R1 },
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   177
  { case (R2, 'a') => R3 }
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   178
)
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   179
\end{lstlisting}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   180
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   181
\noindent
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   182
The point is that the 3rd element in this set states that
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   183
in $R_2$ and given an $a$, I can go to state $R_1$; and the
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   184
4th element, in $R_2$, given an $a$, I can go to state $R_3$.
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   185
When following transitions from a state, we have to look at all
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   186
partial functions in the set and generate the set of all possible
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   187
next states.
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   188
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   189
\subsubsection*{Tasks}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   190
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   191
\begin{itemize}
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   192
\item[(5)]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   193
  Write a function \texttt{nnext} which takes a transition set, a state
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   194
  and a character as arguments, and calculates all possible next states
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   195
  (returned as set).\\
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   196
  \mbox{}\hfill\mbox{[1 Mark]}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   197
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   198
\item[(6)] Write a function \texttt{nnexts} which takes a transition
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   199
  set, a \emph{set} of states and a character as arguments, and
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   200
  calculates \emph{all} possible next states that can be reached from
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   201
  any state in the set.\\ \mbox{}\hfill\mbox{[1 Mark]}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   202
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   203
\item[(7)] Like in (3), write a function \texttt{nnextss} that lifts
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   204
  \texttt{nnexts} from (6) from single characters to lists of characters.
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   205
  \mbox{}\hfill\mbox{[1 Mark]}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   206
  
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   207
\item[(8)] NFAs are also defined as a triple: (set of staring states,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   208
  set of transitions, final states).  Write a function
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   209
  \texttt{naccepts} that tests whether a string is accepted by a NFA
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   210
  or not. For this start in all starting states of the NFA, use the
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   211
  function under (7) to calculate the set of states following all
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   212
  transitions according to the characters in the string. If the set of
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   213
  states shares and state with the set of final states, return true;
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   214
  otherwise false.  Use the function under (1) in order to test
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   215
  whether these two sets of states share any
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   216
  states\mbox{}\hfill\mbox{[1 Mark]}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   217
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   218
\item[(9)] Since we explore in functions under (6) and (7) all
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   219
  possible next states, we decide whether a string is accepted in a
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   220
  breath-first manner. (Depth-first would be to choose one state,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   221
  follow all next states of this single state; check whether it leads
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   222
  to a accepting state. If not, we backtrack and choose another
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   223
  state). The disadvantage of breath-first search is that at every
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   224
  step a non-empty set of states are ``active''\ldots that need to be
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   225
  followed at the same time.  Write similar functions as in (7) and
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   226
  (8), but instead of returning states or a boolean, these functions
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   227
  return the number of states that need to be followed in each
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   228
  step. The function \texttt{max\_accept} should return the maximum
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   229
  of all these numbers.
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   230
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   231
  Consider again the NFA shown above. At the beginning the number of
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   232
  active states will be 2 (since there are two starting states, namely
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   233
  $R_1$ and $R_2$). If we get an $a$, there will be still 2 active
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   234
  states, namely $R_1$ and $R_3$ both reachable from $R_2$. There is
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   235
  no transition for $a$ and $R_1$. So for a string, say, $ab$ which is
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   236
  accepted by the NFA, the maximum number of active states is 2 (it is
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   237
  not possible that all states are active with this NFA; is it possible
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   238
  that no state is active?).
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   239
  \hfill\mbox{[2 Marks]}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   240
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   241
  
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   242
\end{itemize}
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   243
  
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   247
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
%%% End: