cws/cw05.tex
author Christian Urban <urbanc@in.tum.de>
Thu, 07 Dec 2017 12:05:10 +0000
changeset 164 7ada03135b2e
parent 121 4fc05d4f0e01
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
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
    11
non-deterministic finite automata.  The coursework is due on 21 March
118
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
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
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
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    22
inside a class or object. Also note that when marking, the running time
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    23
will be 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
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    38
example for testing whether a string matches a pattern or not.  DFAs
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
    39
consist of some states (circles) and some transitions (edges) between
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
    40
states. For example the DFA
118
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
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    60
has three states ($Q_0$, $Q_1$ and $Q_2$), whereby $Q_0$ is the
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    61
starting state of the DFA and $Q_2$ is the accepting state. The latter
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
    62
is indicated by double lines. In general, a DFA can have any number of
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    63
accepting states, but only a single starting state.
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    64
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    65
Transitions are edges between states labelled with a character. The
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    66
idea is that if we are in state $Q_0$, say, and get an $a$, we can go
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    67
to state $Q_1$. If we are in state $Q_2$ and get an $a$, we can stay
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
    68
in state $Q_2$; if we get a $b$ in $Q_2$, then can go to state
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    69
$Q_0$. The main point of DFAs is that if we are in a state and get a
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    70
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
    71
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
    72
using partial functions for the transitions.
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    73
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    74
A string is accepted by a DFA, if we start in the starting state,
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    75
follow all transitions according to the string; if we end up in an
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    76
accepting state, then the string is accepted. If not, the string is
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    77
not accepted. The technical idea is that DFAs can be used to
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    78
accept strings from \emph{regular} languages. 
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    79
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    80
\subsubsection*{Tasks}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    81
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    82
\begin{itemize}
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    83
\item[(1)] Write a polymorphic function, called \texttt{share}, that
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    84
  decides whether two sets share some elements (i.e.~the intersection
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    85
  is not empty).\hfill[1 Mark]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    86
 
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    87
\item[(2)] The transitions of DFAs will be implemented as partial
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    88
  functions. These functions will have the type (state,
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    89
  character)-pair to state, that is their input will be a (state,
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    90
  character)-pair and they return a state. For example the transitions
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    91
  of the DFA shown above can be defined as the following
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
    92
  partial function:
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    93
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    94
\begin{lstlisting}[language=Scala,numbers=none]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    95
val dfa_trans : PartialFunction[(State,Char), State] = 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    96
  { case (Q0, 'a') => Q1 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    97
    case (Q0, 'b') => Q0
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    98
    case (Q1, 'a') => Q2 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
    99
    case (Q1, 'b') => Q0
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   100
    case (Q2, 'a') => Q2 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   101
    case (Q2, 'b') => Q0 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   102
  }
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   103
\end{lstlisting}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   104
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   105
  The main point of partial functions (as opposed to ``normal''
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   106
  functions) is that they do not have to be defined everywhere. For
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   107
  example the transitions above only mention characters $a$ and $b$,
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   108
  but leave out any other characters. Partial functions come with a
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   109
  method \texttt{isDefinedAt} that can be used to check whether an
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   110
  input produces a result or not. For example
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   111
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
   112
\begin{lstlisting}[language=Scala,numbers=none]
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   113
    dfa_trans.isDefinedAt((Q0, 'a'))
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   114
    dfa_trans.isDefinedAt((Q0, 'c'))
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
   115
\end{lstlisting}   
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   116
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   117
  \noindent
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   118
  gives \texttt{true} in the first case and \texttt{false} in the
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
   119
  second.  There is also a method \texttt{lift} that transforms a
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   120
  partial function into a ``normal'' function returning an optional
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   121
  value: if the partial function is defined on the input, the lifted
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   122
  function will return \texttt{Some}; if it is not defined, then
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   123
  \texttt{None}.
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   124
  
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   125
  Write a function that takes a transition and a (state, character)-pair as arguments
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   126
  and produces an optional state (the state specified by the partial transition
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   127
  function whenever it is defined; if the transition function is undefined,
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   128
  return \texttt{None}).\hfill\mbox{[1 Mark]}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   129
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   130
\item[(3)] 
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   131
  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
   132
  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
   133
  as arguments and produces the state generated by following the transitions for
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   134
  each character in the list. For example if you are in state $Q_0$ in the DFA above
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   135
  and have the list \texttt{List(a,a,a,b,b,a)}, then you need to return the
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   136
  state $Q_1$ (as option since there might not be such a state in general).\\
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   137
  \mbox{}\hfill\mbox{[1 Mark]}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   138
  
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
   139
\item[(4)] DFAs are defined as a triple: (starting state, transitions,
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   140
  set of accepting states).  Write a function \texttt{accepts} that tests whether
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   141
  a string is accepted by an DFA or not. For this start in the
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   142
  starting state of the DFA, use the function under (3) to calculate
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   143
  the state after following all transitions according to the
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   144
  characters in the string. If the resulting state is an accepting state,
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   145
  return \texttt{true}; otherwise \texttt{false}.\\\mbox{}\hfill\mbox{[1 Mark]}
105
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   146
\end{itemize}
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   147
67ce930b5935 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
   148
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   149
\subsection*{Part 2 (Non-Deterministic Finite Automata)}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   150
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   151
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
   152
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
   153
none otherwise). However, this restriction to at most one state can be
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   154
quite limiting for some applications.\footnote{Though there is a
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   155
  curious fact that every (less restricted) NFA can be translated into
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   156
  an ``equivalent'' DFA, whereby accepting means accepting the same
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   157
  set of strings. However this might increase drastically the number
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   158
  of states in the DFA.}  Non-Deterministic Automata (NFAs) remove
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   159
this restriction: there can be more than one starting state, and given
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   160
a state and a character there can be more than one next
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   161
state. Consider for example the NFA
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   162
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   163
\begin{center}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   164
\begin{tikzpicture}[scale=0.7,>=stealth',very thick,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   165
    every state/.style={minimum size=0pt,
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   166
      draw=blue!50,very thick,fill=blue!20},]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   167
\node[state,initial]  (R_1)  {$R_1$};
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   168
\node[state,initial] (R_2) [above=of R_1] {$R_2$};
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   169
\node[state, accepting] (R_3) [right=of R_1] {$R_3$};
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   170
\path[->] (R_1) edge node [below]  {$b$} (R_3);
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   171
\path[->] (R_2) edge [bend left] node [above]  {$a$} (R_3);
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   172
\path[->] (R_1) edge [bend left] node  [left] {$c$} (R_2);
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   173
\path[->] (R_2) edge [bend left] node  [right] {$a$} (R_1);
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   174
\end{tikzpicture}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   175
\end{center}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   176
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   177
\noindent
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   178
where in state $R_2$ if we get an $a$, we can go to state $R_1$
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   179
\emph{or} $R_3$. If we want to find out whether an NFA accepts a
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   180
string, then we need to explore both possibilities. We will do this
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   181
``exploration'' in the tasks below in a breadth-first manner.
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   182
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   183
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   184
The feature of having more than one next state in NFAs will be
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   185
implemented by having a \emph{set} of partial transition functions
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   186
(DFAs had only one). For example the NFA shown above will be
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   187
represented by the set of partial functions
118
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
\begin{lstlisting}[language=Scala,numbers=none]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   190
val nfa_trans : NTrans = Set(
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   191
  { case (R1, 'c') => R2 },
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   192
  { case (R1, 'b') => R3 },
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   193
  { case (R2, 'a') => R1 },
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   194
  { case (R2, 'a') => R3 }
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   195
)
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   196
\end{lstlisting}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   197
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   198
\noindent
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents: 119
diff changeset
   199
The point is that the 3rd element in this set makes sure that in state $R_2$ and
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   200
given an $a$, we can go to state $R_1$; and the 4th element, in $R_2$,
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   201
given an $a$, we can also go to state $R_3$.  When following
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   202
transitions from a state, we have to look at all partial functions in
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   203
the set and generate the set of \emph{all} possible next states.
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   204
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   205
\subsubsection*{Tasks}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   206
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   207
\begin{itemize}
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   208
\item[(5)]
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   209
  Write a function \texttt{nnext} which takes a transition set, a state
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   210
  and a character as arguments, and calculates all possible next states
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   211
  (returned as set).\\
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   212
  \mbox{}\hfill\mbox{[1 Mark]}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   213
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   214
\item[(6)] Write a function \texttt{nnexts} which takes a transition
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   215
  set, a \emph{set} of states and a character as arguments, and
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   216
  calculates \emph{all} possible next states that can be reached from
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   217
  any state in the set.\mbox{}\hfill\mbox{[1 Mark]}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   218
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   219
\item[(7)] Like in (3), write a function \texttt{nnextss} that lifts
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   220
  \texttt{nnexts} from (6) from single characters to lists of characters.
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   221
  \mbox{}\hfill\mbox{[1 Mark]}
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   222
  
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   223
\item[(8)] NFAs are also defined as a triple: (set of staring states,
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   224
  set of transitions, set of accepting states).  Write a function
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   225
  \texttt{naccepts} that tests whether a string is accepted by an NFA
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   226
  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
   227
  function under (7) to calculate the set of states following all
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   228
  transitions according to the characters in the string. If the
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   229
  resulting set of states shares at least a single state with the set
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   230
  of accepting states, return \texttt{true}; otherwise \texttt{false}.
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   231
  Use the function under (1) in order to test whether these two sets
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   232
  of states share any states or not.\mbox{}\hfill\mbox{[1 Mark]}
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   233
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   234
\item[(9)] Since we explore in functions (6) and (7) all possible next
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   235
  states, we decide whether a string is accepted in a breadth-first
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   236
  manner. (Depth-first would be to choose one state, follow all next
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   237
  states of this single state; check whether it leads to an accepting
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   238
  state. If not, we backtrack and choose another state). The
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   239
  disadvantage of breadth-first search is that at every step a
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   240
  non-empty set of states are ``active''\ldots{} states that need to
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   241
  be followed at the same time.  Write similar functions as in (7) and
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   242
  (8), but instead of returning states or a boolean, calculate the
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   243
  number of states that need to be followed in each step. The function
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   244
  \texttt{max\_accept} should then return the maximum of all these
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   245
  numbers.
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   246
119
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   247
  As a test case, consider again the NFA shown above. At the beginning
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   248
  the number of active states will be 2 (since there are two starting
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   249
  states, namely $R_1$ and $R_2$). If we get an $a$, there will be
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   250
  still 2 active states, namely $R_1$ and $R_3$ both reachable from
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   251
  $R_2$. There is no transition for $a$ and $R_1$. So for a string,
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   252
  say, $ab$ which is accepted by the NFA, the maximum number of active
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   253
  states is 2 (it is not possible that all three states of this NFA
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   254
  are active at the same time; is it possible that no state is
9bfc82cd3351 updated
Christian Urban <urbanc@in.tum.de>
parents: 118
diff changeset
   255
  active?).  \hfill\mbox{[2 Marks]}
118
276b3127f84a updated
Christian Urban <urbanc@in.tum.de>
parents: 111
diff changeset
   256
109
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   257
  
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   258
\end{itemize}
293ea84d82ca updated
Christian Urban <urbanc@in.tum.de>
parents: 105
diff changeset
   259
  
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   263
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
%%% End: