progs/automata_sol.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 10 Mar 2017 23:01:17 +0000
changeset 121 4fc05d4f0e01
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
121
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// NFAs and DFAs based on Scala's partial functions
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// (1) Write a polymorphic function that tests whether the 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
// intersection of two sets is non-empty
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
def share[A](a: Set[A], b: Set[A]) : Boolean =
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  !(a intersect b).isEmpty
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
share(Set(1,2,3), Set(2, 3, 4))   // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
share(Set(1,2,3), Set(4, 5, 6))   // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// State nodes of the DFAs and NFAs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
abstract class State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
type States = Set[State]
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// Some states for test cases
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
case object Q0 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case object Q1 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case object Q2 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case object Q3 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case object Q4 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case object Q5 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case object Q6 extends State
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// Transitions for DFAs and NFAs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
type Trans = PartialFunction[(State, Char), State]
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
type NTrans = Set[Trans]
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
// example transition of an DFA
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
val dtrans : Trans = 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  { case (Q0, 'a') => Q1 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    case (Q0, 'b') => Q0
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    case (Q1, 'a') => Q2 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
    case (Q1, 'b') => Q0
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    case (Q2, 'a') => Q2 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
    case (Q2, 'b') => Q0 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// (2) Write a function that takes a transition and a 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// (state, character)-pair as arguments and produces an 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// optional state (the state specified by the partial transition
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// function whenever it is defined; if the transition function 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// is undefined, return None.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
def fire(e: Trans, qc: (State, Char)) : Option[State] = 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  e.lift.apply(qc)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
// (3) Write a function that takes a transition, a state 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
// and a list of characters as arguments and produces 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
// the state generated by following the transitions for 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
// each character in the list.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
def nexts(trans: Trans, q: State, s: List[Char]) : Option[State] = s match {
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  case Nil => Some(q)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  case c::cs => fire(trans, (q, c)).flatMap(nexts(trans, _, cs))
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
}
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
// class for DFAs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
case class DFA(start: State,      // starting state
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
               trans: Trans,      // transition
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
               fins:  States)     // final states
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
// (4) Write a function that tests whether a string is accepted 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
// by an DFA or not.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
def accepts(dfa: DFA, s: String) : Boolean = nexts(dfa.trans, dfa.start, s.toList) match {
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  case None => false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  case Some(q) => dfa.fins contains q
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
}
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
// DFA examples
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
val dtrans1 : Trans = 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  { case (Q0, 'a') => Q0 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
    case (Q0, 'b') => Q1 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
val dfa1 = DFA(Q0, dtrans1, Set[State](Q1))
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
accepts(dfa1, "aaab")     // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
accepts(dfa1, "aacb")     // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
// NFAs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
// (5) Write a function that takes a transition set, a state
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
// and a character as arguments, and calculates all possible 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
// next states (returned as set).
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
def nnext(trans: NTrans, q: State, c: Char) : States = {
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  trans.map(fire(_, (q, c))).flatten
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
}
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
// (6) Write a function that takes a transition set, a set of states 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
// and a character as arguments, and calculates all possible 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
// next states that can be reached from any state in the set.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
def nnexts(trans: NTrans, qs: States, c: Char) : States = {
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
  qs.flatMap(nnext(trans, _, c))
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
}
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
// (7) Write a function that lifts nnexts from from single 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
// characters to lists of characters.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
def nnextss(trans: NTrans, qs: States, s: List[Char]) : States = s match {
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  case Nil => qs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  case c::cs => {
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
    val ns = nnexts(trans, qs, c)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
    nnextss(trans, ns, cs) 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
}
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
// class for NFAs
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
case class NFA(start: States,       // starting state
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
               trans: NTrans,       // transition edges
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
               fins:  States)       // final states
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
// (8) Write a function that tests whether a string is 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
// accepted by an NFA or not.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
def naccepts(nfa: NFA, s: String) : Boolean = {
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  share(nnextss(nfa.trans, nfa.start, s.toList), nfa.fins)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
}
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
// (9) Write similar functions as in (7) and (8), but instead of 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
// returning states or a boolean, calculate the number of states 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
// that need to be followed in each step.
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
def max_nextss(trans: NTrans, qs: States, s: List[Char], max: Int) : Int = s match {
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  case Nil => max
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  case c::cs => {
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
    val ns = nnexts(trans, qs, c)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
    val ns_size = ns.size
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
    if (max < ns_size) max_nextss(trans, ns, cs, ns_size) 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
    else max_nextss(trans, ns, cs, max)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
  }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
}
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
def max_accepts(nfa: NFA, s: String) : Int = {
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
  max_nextss(nfa.trans, nfa.start, s.toList, 0)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
}
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
// NFA examples
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
// 1 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
val trans1 : NTrans = Set(
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
  { case (Q0, 'a') => Q1 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
  { case (Q0, _)   => Q0 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
  { case (Q1, _)   => Q2 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
  { case (Q2, _)   => Q3 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
  { case (Q3, _)   => Q4 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  { case (Q4, 'b') => Q5 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
  { case (Q5, 'c') => Q6 }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
val nfa1 = NFA(Set[State](Q0), trans1, Set[State](Q6))
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
naccepts(nfa1, "axaybzbc")     // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
naccepts(nfa1, "aaaaxaybzbc")  // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
naccepts(nfa1, "axaybzbd")     // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
// the nfa has five states, which might be all 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
// active
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
max_accepts(nfa1, "axaybzbc")     // 3 
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
max_accepts(nfa1, "aaaaxaybzbc")  // 5
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
max_accepts(nfa1, "axaybzbd")     // 3
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
max_accepts(nfa1, "aaaaaaaaaaaaaxaybzbd")   // 5
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
// 2
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
val trans2 : NTrans = Set(
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
  { case (Q0, 'a') => Q0 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
  { case (Q0, 'a') => Q1 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
  { case (Q0, 'b') => Q2 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
  { case (Q1, 'a') => Q1 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
  { case (Q2, 'b') => Q2 }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
val nfa2 = NFA(Set[State](Q0), trans2, Set[State](Q2))
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
naccepts(nfa2, "aa")             // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
naccepts(nfa2, "aaaaa")          // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
naccepts(nfa2, "aaaaab")         // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
naccepts(nfa2, "aaaaabbb")       // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
naccepts(nfa2, "aaaaabbbaaa")    // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
naccepts(nfa2, "ac")             // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
// 3
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
val trans3 : NTrans = Set(
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
  { case (Q0, _)   => Q0 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
  { case (Q0, 'a') => Q1 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
  { case (Q0, 'b') => Q3 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
  { case (Q1, 'b') => Q2 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
  { case (Q2, 'c') => Q5 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
  { case (Q3, 'c') => Q4 },
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
  { case (Q4, 'd') => Q5 }
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
)
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
val nfa3 = NFA(Set[State](Q0), trans3, Set[State](Q5))
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
naccepts(nfa3, "aaaaabc")      // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
naccepts(nfa3, "aaaabcd")      // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
naccepts(nfa3, "aaaaab")       // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
naccepts(nfa3, "aaaabc")       // true
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
naccepts(nfa3, "aaaaabbbaaa")  // false
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
4fc05d4f0e01 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222