CS 10: Problem solving via Object Oriented Programming - - PowerPoint PPT Presentation

cs 10 problem solving via object oriented programming
SMART_READER_LITE
LIVE PREVIEW

CS 10: Problem solving via Object Oriented Programming - - PowerPoint PPT Presentation

CS 10: Problem solving via Object Oriented Programming Winter 2017 Tim Pierson 260 (255) Sudikoff Day 19 PaHern Matching Agenda 1. Regular


slide-1
SLIDE 1

CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡

Winter ¡2017 ¡

¡

Tim ¡Pierson ¡

260 ¡(255) ¡Sudikoff ¡

Day ¡19 ¡– ¡PaHern ¡Matching ¡

slide-2
SLIDE 2

2 ¡

Agenda ¡

  • 1. Regular ¡expressions ¡
  • 2. Finite ¡automata ¡
  • 3. ValidaSng ¡input ¡

¡

  • 4. Modeling ¡a ¡complex ¡system ¡
slide-3
SLIDE 3

3 ¡

SomeSmes ¡it ¡is ¡useful ¡to ¡be ¡able ¡to ¡detect ¡

  • r ¡require ¡paHerns ¡

Email ¡addresses ¡follow ¡a ¡paHern: ¡ ¡ mailbox@domain.TLD ¡ ¡ ¡ example: ¡tjp@cs.dartmouth.edu ¡ ¡ We ¡can ¡specify ¡a ¡paHern ¡or ¡rules ¡for ¡email ¡addresses: ¡

¡<characters> ¡@ ¡<characters>.<com ¡| ¡edu ¡| ¡org ¡| ¡…> ¡

One ¡or ¡more ¡ characters ¡ Followed ¡ ¡ by ¡@ ¡ One ¡or ¡ more ¡ characters ¡ Followed ¡by ¡one ¡of ¡a ¡set ¡ predefined ¡of ¡values ¡ Followed ¡ ¡ by ¡. ¡

slide-4
SLIDE 4

4 ¡

Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡

Regular ¡expressions ¡(regex) ¡

  • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡
  • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡

Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡

slide-5
SLIDE 5

5 ¡

Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡

Regular ¡expressions ¡(regex) ¡

  • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡
  • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡

Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ R1 ¡R2 ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡

slide-6
SLIDE 6

6 ¡

Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡

Regular ¡expressions ¡(regex) ¡

  • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡
  • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡

Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ R1 ¡R2 ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ AlternaSve: ¡R1 ¡| ¡R2 ¡ One ¡or ¡the ¡other ¡ a|e|i|o|u ¡matches ¡any ¡vowel ¡

slide-7
SLIDE 7

7 ¡

Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡

Regular ¡expressions ¡(regex) ¡

  • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡
  • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡

Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ R1 ¡R2 ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ AlternaSve: ¡R1 ¡| ¡R2 ¡ One ¡or ¡the ¡other ¡ a|e|i|o|u ¡matches ¡any ¡vowel ¡ Grouping: ¡(R) ¡ Establishes ¡order; ¡allows ¡ reference/extracSon ¡ c(a|o)t ¡matches ¡“cat” ¡or ¡“cot” ¡

slide-8
SLIDE 8

8 ¡

Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡

Regular ¡expressions ¡(regex) ¡

  • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡
  • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡

Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ R1 ¡R2 ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ AlternaSve: ¡R1 ¡| ¡R2 ¡ One ¡or ¡the ¡other ¡ a|e|i|o|u ¡matches ¡any ¡vowel ¡ Grouping: ¡(R) ¡ Establishes ¡order; ¡allows ¡ reference/extracSon ¡ c(a|o)t ¡matches ¡“cat” ¡or ¡“cot” ¡ Character ¡classes ¡ [c1-­‑c2] ¡and ¡[^c1-­‑c2] ¡ AlternaSve ¡characters ¡and ¡ excluded ¡characters ¡ [a-­‑c] ¡matches ¡“a” ¡or ¡“b” ¡or ¡“c”, ¡ while ¡[^a-­‑c] ¡matches ¡any ¡but ¡abc ¡

slide-9
SLIDE 9

9 ¡

Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡

Regular ¡expressions ¡(regex) ¡

  • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡
  • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡

Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ R1 ¡R2 ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ AlternaSve: ¡R1 ¡| ¡R2 ¡ One ¡or ¡the ¡other ¡ a|e|i|o|u ¡matches ¡any ¡vowel ¡ Grouping: ¡(R) ¡ Establishes ¡order; ¡allows ¡ reference/extracSon ¡ c(a|o)t ¡matches ¡“cat” ¡or ¡“cot” ¡ Character ¡classes ¡ [c1-­‑c2] ¡and ¡[^c1-­‑c2] ¡ AlternaSve ¡characters ¡and ¡ excluded ¡characters ¡ [a-­‑c] ¡matches ¡“a” ¡or ¡“b” ¡or ¡“c”, ¡ while ¡[^a-­‑c] ¡matches ¡any ¡but ¡abc ¡ RepeSSon: ¡R* ¡ Matches ¡0 ¡or ¡more ¡Smes ¡ “ca*t” ¡matches ¡“ct”, ¡“cat”, ¡“caat” ¡

slide-10
SLIDE 10

10 ¡

Regular ¡expressions ¡(RegEx) ¡are ¡a ¡common ¡ way ¡of ¡looking ¡for ¡paHerns ¡in ¡Strings ¡

Regular ¡expressions ¡(regex) ¡

  • Most ¡programming ¡languages ¡have ¡support ¡for ¡regex ¡
  • Can ¡be ¡really ¡complex ¡ ¡and ¡messy, ¡but ¡there ¡are ¡basic ¡paHerns ¡

Opera2on ¡ Meaning ¡ Example ¡ Character ¡ Match ¡a ¡character ¡next ¡ “a” ¡matches ¡“a” ¡ ConcatenaSon: ¡ ¡ R1 ¡R2 ¡ One ¡acer ¡the ¡other ¡ “cat” ¡matches ¡“c” ¡then ¡“a” ¡then ¡“t” ¡ AlternaSve: ¡R1 ¡| ¡R2 ¡ One ¡or ¡the ¡other ¡ a|e|i|o|u ¡matches ¡any ¡vowel ¡ Grouping: ¡(R) ¡ Establishes ¡order; ¡allows ¡ reference/extracSon ¡ c(a|o)t ¡matches ¡“cat” ¡or ¡“cot” ¡ Character ¡classes ¡ [c1-­‑c2] ¡and ¡[^c1-­‑c2] ¡ AlternaSve ¡characters ¡and ¡ excluded ¡characters ¡ [a-­‑c] ¡matches ¡“a” ¡or ¡“b” ¡or ¡“c”, ¡ while ¡[^a-­‑c] ¡matches ¡any ¡but ¡abc ¡ RepeSSon: ¡R* ¡ Matches ¡0 ¡or ¡more ¡Smes ¡ “ca*t” ¡matches ¡“ct”, ¡“cat”, ¡“caat” ¡ Non-­‑zero ¡ repeSSon: ¡R+ ¡ Matches ¡1 ¡or ¡more ¡Smes ¡ “ca+t” ¡matches ¡“cat” ¡or ¡“caat” ¡or ¡ “caaat”, ¡but ¡not ¡“ct” ¡

slide-11
SLIDE 11

We ¡can ¡use ¡RegEx ¡to ¡see ¡if ¡an ¡email ¡ address ¡is ¡valid ¡ ¡

Email ¡addresses ¡follow ¡a ¡paHern: ¡ ¡ mailbox@domain.TLD ¡ ¡ ¡ example: ¡tjp@cs.dartmouth.edu ¡ ¡ We ¡can ¡specify ¡a ¡paHern ¡or ¡rules ¡for ¡email ¡addresses: ¡

¡<characters> ¡@ ¡<characters>.<com ¡| ¡edu ¡| ¡org ¡| ¡…> ¡

¡ As ¡a ¡simple ¡RegEx: ¡[a-­‑z]+@[a-­‑z.]* ¡[a-­‑z]+. ¡(com ¡| ¡edu ¡| ¡org ¡…) ¡ Check: ¡ tjp@cs.dartmouth.edu ¡-­‑-­‑ ¡valid ¡ Blob.x ¡-­‑-­‑ ¡invalid ¡

slide-12
SLIDE 12

Email ¡addresses ¡follow ¡a ¡paHern: ¡ ¡ mailbox@domain.TLD ¡ ¡ ¡ example: ¡tjp@cs.dartmouth.edu ¡

¡ ¡ ¡ ¡

¡ ¡ We ¡can ¡specify ¡a ¡paHern ¡or ¡rules ¡for ¡email ¡addresses: ¡

¡<characters> ¡@ ¡<characters>.<com ¡| ¡edu ¡| ¡org ¡| ¡…> ¡

¡ A ¡Graph ¡can ¡represent ¡the ¡paHern ¡for ¡email ¡addresses ¡ Sample ¡addresses ¡can ¡be ¡easily ¡verified ¡if ¡in ¡correct ¡form ¡

12 ¡

A ¡Graph ¡can ¡implement ¡a ¡RegEx ¡

a-­‑z ¡ @ ¡

Start ¡

a-­‑z ¡

com ¡

. ¡

edu ¡

  • rg ¡

. ¡ . ¡ . ¡

slide-13
SLIDE 13

13 ¡

Key ¡points ¡

  • 1. We ¡can ¡define ¡a ¡set ¡of ¡rules ¡that ¡must ¡

be ¡followed ¡

  • 2. We ¡can ¡represent ¡those ¡rules ¡with ¡a ¡

graph ¡

slide-14
SLIDE 14

14 ¡

Agenda ¡

  • 1. Regular ¡expressions ¡
  • 2. Finite ¡automata ¡
  • 3. ValidaSng ¡input ¡

¡

  • 4. Modeling ¡a ¡complex ¡system ¡
slide-15
SLIDE 15

15 ¡

Finite ¡Automata ¡(FA) ¡can ¡be ¡used ¡for ¡many ¡ problems, ¡two ¡uses ¡are ¡common ¡

Common ¡Finite ¡Automata ¡use ¡cases ¡ ¡

  • 1. ValidaSng ¡input ¡
  • 2. Tracking ¡the ¡state ¡of ¡a ¡system, ¡changing ¡state ¡

as ¡a ¡response ¡to ¡events ¡

slide-16
SLIDE 16

16 ¡

We ¡can ¡model ¡States ¡as ¡verSces ¡and ¡ TransiSons ¡as ¡edges ¡in ¡a ¡directed ¡graph ¡

A ¡ C ¡ B ¡

Finite ¡Automata ¡valida2ng ¡input ¡

0 ¡ 1 ¡ 0,1 ¡ 0,1 ¡

States ¡as ¡ verSces ¡ Edges ¡as ¡ transiSons ¡ TransiSon ¡from ¡A ¡to ¡B ¡ if ¡input ¡0, ¡else ¡C ¡ Edges ¡can ¡ loop ¡back ¡to ¡ same ¡vertex ¡ Stay ¡in ¡C ¡ regardless ¡if ¡ given ¡0 ¡or ¡1 ¡ Double ¡circle ¡ indicates ¡valid ¡end ¡ states ¡ What ¡does ¡ this ¡do? ¡ Accepts ¡any ¡ input ¡starSng ¡ with ¡0 ¡

Start ¡

Set ¡of ¡labels ¡called ¡ alphabet ¡

slide-17
SLIDE 17

17 ¡

Agenda ¡

  • 1. Regular ¡expressions ¡
  • 2. Finite ¡automata ¡
  • 3. ValidaSng ¡input ¡

¡

  • 4. Modeling ¡a ¡complex ¡system ¡
slide-18
SLIDE 18

Finite ¡Automata ¡can ¡validate ¡input ¡

Finite ¡Automata ¡valida2ng ¡input ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ Input ¡ Result ¡ 00 ¡ a ¡ a ¡ b ¡ c ¡ Assume ¡leaves ¡represent ¡valid ¡end ¡states ¡ Can ¡loop ¡back ¡to ¡root ¡from ¡leaf ¡ Invalid ¡if ¡input ¡ends ¡and ¡not ¡at ¡end ¡state ¡ Extension ¡of ¡Huffman, ¡go ¡back ¡to ¡root ¡acer ¡finding ¡leaf ¡ Start ¡

slide-19
SLIDE 19

Finite ¡Automata ¡can ¡validate ¡input ¡

Finite ¡Automata ¡valida2ng ¡input ¡ Input ¡ Result ¡ 00 ¡ a ¡ 01 ¡ b ¡ Assume ¡leaves ¡represent ¡valid ¡end ¡states ¡ Can ¡loop ¡back ¡to ¡root ¡from ¡leaf ¡ Invalid ¡if ¡input ¡ends ¡and ¡not ¡at ¡end ¡state ¡ Extension ¡of ¡Huffman, ¡go ¡back ¡to ¡root ¡acer ¡finding ¡leaf ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ a ¡ b ¡ c ¡ Start ¡

slide-20
SLIDE 20

Finite ¡Automata ¡can ¡validate ¡input ¡

Finite ¡Automata ¡valida2ng ¡input ¡ Input ¡ Result ¡ 00 ¡ a ¡ 01 ¡ b ¡ 1 ¡ c ¡ Assume ¡leaves ¡represent ¡valid ¡end ¡states ¡ Can ¡loop ¡back ¡to ¡root ¡from ¡leaf ¡ Invalid ¡if ¡input ¡ends ¡and ¡not ¡at ¡end ¡state ¡ Extension ¡of ¡Huffman, ¡go ¡back ¡to ¡root ¡acer ¡finding ¡leaf ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ a ¡ b ¡ c ¡ Start ¡

slide-21
SLIDE 21

Finite ¡Automata ¡can ¡validate ¡input ¡

Finite ¡Automata ¡valida2ng ¡input ¡ Input ¡ Result ¡ 00 ¡ a ¡ 01 ¡ b ¡ 1 ¡ c ¡ 0 ¡ invalid ¡ Assume ¡leaves ¡represent ¡valid ¡end ¡states ¡ Can ¡loop ¡back ¡to ¡root ¡from ¡leaf ¡ Invalid ¡if ¡input ¡ends ¡and ¡not ¡at ¡end ¡state ¡ Extension ¡of ¡Huffman, ¡go ¡back ¡to ¡root ¡acer ¡finding ¡leaf ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ a ¡ b ¡ c ¡ Start ¡

slide-22
SLIDE 22

Finite ¡Automata ¡can ¡validate ¡input ¡

Finite ¡Automata ¡valida2ng ¡input ¡ Input ¡ Result ¡ 00 ¡ a ¡ 01 ¡ b ¡ 1 ¡ c ¡ 0 ¡ Invalid ¡ 001100 ¡ acca ¡ Assume ¡leaves ¡represent ¡valid ¡end ¡states ¡ Can ¡loop ¡back ¡to ¡root ¡from ¡leaf ¡ Invalid ¡if ¡input ¡ends ¡and ¡not ¡at ¡end ¡state ¡ Extension ¡of ¡Huffman, ¡go ¡back ¡to ¡root ¡acer ¡finding ¡leaf ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ a ¡ b ¡ c ¡ Start ¡

slide-23
SLIDE 23

23 ¡

Finite ¡Automata ¡come ¡in ¡two ¡flavors, ¡ DeterminisSc ¡and ¡NondeterminisSc ¡

A ¡ C ¡ B ¡

Determinis2c ¡Finite ¡ Automaton ¡(DFA) ¡

0 ¡ 1 ¡ 0,1 ¡ 0,1 ¡ A ¡ B ¡ 0 ¡ 0,1 ¡

Nondeterminis2c ¡Finite ¡ Automaton ¡(NFA) ¡

Exactly ¡one ¡choice ¡for ¡ each ¡possible ¡input ¡ ¡ No ¡ambiguity ¡ May ¡have ¡0, ¡1, ¡or ¡more ¡ choices ¡for ¡transiSon ¡from ¡ each ¡state ¡ These ¡both ¡do ¡the ¡same ¡ thing ¡ Start ¡ Start ¡

slide-24
SLIDE 24

With ¡DFAs ¡we ¡have ¡to ¡specify ¡each ¡State ¡ transiSon, ¡with ¡NFAs ¡we ¡do ¡not ¡

NFA ¡valida2ng ¡input ¡ c ¡ Start ¡

  • ¡

a ¡ t ¡ t ¡ Valid ¡inputs: ¡ cot ¡or ¡cat ¡ ¡ All ¡else ¡invalid ¡ ¡ There ¡are ¡0, ¡1, ¡or ¡ more ¡choices ¡for ¡ each ¡leHer ¡

slide-25
SLIDE 25

With ¡NFAs ¡we ¡can ¡have ¡0, ¡1 ¡or ¡more ¡ choices ¡for ¡each ¡input ¡

NFA ¡valida2ng ¡input ¡ c ¡ Start ¡ coat ¡or ¡cot ¡

  • ¡

a ¡ t ¡ t ¡ c ¡ Start ¡ coat ¡or ¡cot ¡

  • ¡

a ¡ t ¡ c ¡

  • ¡

t ¡

slide-26
SLIDE 26

26 ¡

SomeSmes ¡we ¡cannot ¡map ¡from ¡a ¡State ¡a ¡ single ¡next ¡State ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

0 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Key ¡ Input ¡ Paths ¡ A ¡ 0 ¡ {B,C} ¡ A ¡ 1 ¡ {B} ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡

slide-27
SLIDE 27

27 ¡

SomeSmes ¡we ¡cannot ¡map ¡from ¡a ¡State ¡a ¡ single ¡next ¡State ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Key ¡ Input ¡ Paths ¡ A ¡ 0 ¡ {B,C} ¡ A ¡ 1 ¡ {B} ¡ B ¡ 0 ¡ {A} ¡ B ¡ 1 ¡ {E} ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡ 0 ¡

slide-28
SLIDE 28

28 ¡

SomeSmes ¡we ¡cannot ¡map ¡from ¡a ¡State ¡a ¡ single ¡next ¡State ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Key ¡ Input ¡ Paths ¡ A ¡ 0 ¡ {B,C} ¡ A ¡ 1 ¡ {B} ¡ B ¡ 0 ¡ {A} ¡ B ¡ 1 ¡ {E} ¡ C ¡ 0 ¡ {} ¡ C ¡ 1 ¡ {D} ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡ 0 ¡

slide-29
SLIDE 29

29 ¡

SomeSmes ¡we ¡cannot ¡map ¡from ¡a ¡State ¡a ¡ single ¡next ¡State ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

0,1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Key ¡ Input ¡ Paths ¡ A ¡ 0 ¡ {B,C} ¡ A ¡ 1 ¡ {B} ¡ B ¡ 0 ¡ {A} ¡ B ¡ 1 ¡ {E} ¡ C ¡ 0 ¡ {} ¡ C ¡ 1 ¡ {D} ¡ D ¡ 0 ¡ {B,D} ¡ D ¡ 1 ¡ {} ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡

slide-30
SLIDE 30

30 ¡

SomeSmes ¡we ¡cannot ¡map ¡from ¡a ¡State ¡a ¡ single ¡next ¡State ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

0,1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Key ¡ Input ¡ Paths ¡ A ¡ 0 ¡ {B,C} ¡ A ¡ 1 ¡ {B} ¡ B ¡ 0 ¡ {A} ¡ B ¡ 1 ¡ {E} ¡ C ¡ 0 ¡ {} ¡ C ¡ 1 ¡ {D} ¡ D ¡ 0 ¡ {B,D} ¡ D ¡ 1 ¡ {} ¡ E ¡ 0 ¡ {} ¡ E ¡ 1 ¡ {} ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡

slide-31
SLIDE 31

31 ¡

In ¡that ¡case, ¡must ¡keep ¡track ¡of ¡all ¡possible ¡ States ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

0,1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Input ¡ Possible ¡ States ¡ Start ¡ {A} ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡

slide-32
SLIDE 32

32 ¡

In ¡that ¡case, ¡must ¡keep ¡track ¡of ¡all ¡possible ¡ States ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

0,1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Input ¡ Possible ¡ States ¡ Start ¡ {A} ¡ 0 ¡ {B,C} ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡

slide-33
SLIDE 33

33 ¡

In ¡that ¡case, ¡must ¡keep ¡track ¡of ¡all ¡possible ¡ States ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

0,1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Input ¡ Possible ¡ States ¡ Start ¡ {A} ¡ 0 ¡ {B,C} ¡ 1 ¡ {E,D} ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡

slide-34
SLIDE 34

34 ¡

In ¡that ¡case, ¡must ¡keep ¡track ¡of ¡all ¡possible ¡ States ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

0,1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Input ¡ Possible ¡ States ¡ Start ¡ {A} ¡ 0 ¡ {B,C} ¡ 1 ¡ {E,D} ¡ 0 ¡ {B,D} ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡

slide-35
SLIDE 35

35 ¡

In ¡that ¡case, ¡must ¡keep ¡track ¡of ¡all ¡possible ¡ States ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

0,1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Input ¡ Possible ¡ States ¡ Start ¡ {A} ¡ 0 ¡ {B,C} ¡ 1 ¡ {E,D} ¡ 0 ¡ {B,D} ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡

slide-36
SLIDE 36

36 ¡

In ¡that ¡case, ¡must ¡keep ¡track ¡of ¡all ¡possible ¡ States ¡

A ¡

0,1 ¡

B ¡ C ¡ D ¡ E ¡

0,1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ Input ¡ Possible ¡ States ¡ Start ¡ {A} ¡ 0 ¡ {B,C} ¡ 1 ¡ {E,D} ¡ 0 ¡ {B,D} ¡ … ¡ NFAs ¡can ¡have ¡mul2ple ¡next ¡States ¡ Start ¡

slide-37
SLIDE 37

37 ¡

What ¡does ¡this ¡NFA ¡do? ¡

A ¡ B ¡ D ¡ 0 ¡ 1 ¡ 0,1 ¡ 0,1 ¡

Start ¡

C ¡ E ¡ 1 ¡ 0 ¡ 0,1 ¡

slide-38
SLIDE 38

38 ¡

Agenda ¡

  • 1. Regular ¡expressions ¡
  • 2. Finite ¡automata ¡
  • 3. ValidaSng ¡input ¡

¡

  • 4. Modeling ¡a ¡complex ¡system ¡
slide-39
SLIDE 39

39 ¡

Finite ¡Automata ¡are ¡also ¡used ¡to ¡track ¡the ¡ State ¡of ¡a ¡system ¡as ¡event ¡occur ¡

Sensors ¡detect ¡arrival ¡and ¡departure ¡of ¡cars ¡in ¡spaces ¡

Image: ¡Fybr.com ¡

slide-40
SLIDE 40

40 ¡

Finite ¡Automata ¡are ¡also ¡used ¡to ¡track ¡the ¡ State ¡of ¡a ¡system ¡as ¡event ¡occur ¡

Parking ¡meters ¡detect ¡payments ¡and ¡payment ¡expira2ons ¡

Image: ¡Fybr.com ¡

slide-41
SLIDE 41

41 ¡

Combine ¡data ¡for ¡all ¡spaces ¡on ¡a ¡block ¡to ¡ show ¡drivers ¡where ¡they ¡can ¡find ¡parking ¡

Fisherman’s ¡Wharf ¡in ¡San ¡Francisco, ¡CA ¡

Image: ¡sfpark.org ¡

Green ¡ ¡< ¡75% ¡occupied, ¡yellow ¡= ¡75-­‑90% ¡occupied, ¡red ¡> ¡90% ¡occupied ¡

slide-42
SLIDE 42

42 ¡

CombinaSon ¡of ¡occupancy ¡and ¡payments ¡ leads ¡to ¡four ¡states ¡for ¡each ¡space ¡

Simplified ¡automobile ¡parking ¡ Occupancy ¡ Vacant ¡ Occupied ¡ Payment ¡ status ¡ Not ¡Paid ¡ ¡ Vacant ¡ Not ¡paid ¡ Occupied ¡ Not ¡paid ¡ Paid ¡ ¡ Vacant ¡ Paid ¡ Occupied ¡ Paid ¡

slide-43
SLIDE 43

43 ¡

Occupancy ¡and ¡payment ¡events ¡can ¡occur ¡ and ¡change ¡the ¡state ¡of ¡the ¡space ¡

Simplified ¡automobile ¡parking ¡ Occupancy ¡event ¡raised ¡by ¡sensor: ¡

  • Vehicle ¡arrives ¡
  • Vehicle ¡departs ¡

¡

Payment ¡events ¡raised ¡by ¡parking ¡meter: ¡

  • Payment ¡made ¡
  • Time ¡expires ¡

¡ Occupancy ¡ Vacant ¡ Occupied ¡ Payment ¡ status ¡ Not ¡Paid ¡ ¡ Vacant ¡ Not ¡paid ¡ Occupied ¡ Not ¡paid ¡ Paid ¡ ¡ Vacant ¡ Paid ¡ Occupied ¡ Paid ¡ Events ¡ cause ¡the ¡ system ¡to ¡ transi2on ¡ between ¡ states ¡

slide-44
SLIDE 44

44 ¡

The ¡parking ¡space ¡could ¡be ¡modeled ¡with ¡a ¡ complicated ¡if-­‑then ¡structure ¡

Simplified ¡automobile ¡parking ¡ void ¡handleEvent(Event ¡e) ¡{ ¡ ¡if ¡(event==“Payment”) ¡{ ¡ ¡ ¡ ¡if ¡(occupancy==“Occupied” ¡&& ¡payment==“Not ¡Paid”) ¡{ ¡ ¡ ¡ ¡//add ¡Sme ¡on ¡meter ¡ ¡ ¡elseif ¡(occupancy=“Occupied” ¡&& ¡payment==“Paid”) ¡{ ¡ ¡ ¡ ¡//increment ¡Sme ¡on ¡meter ¡ ¡ ¡… ¡ Occupancy ¡ Vacant ¡ Occupied ¡ Payment ¡ status ¡ Not ¡Paid ¡ ¡ Vacant ¡ Not ¡paid ¡ Occupied ¡ Not ¡paid ¡ Paid ¡ ¡ Vacant ¡ Paid ¡ Occupied ¡ Paid ¡

slide-45
SLIDE 45

45 ¡

The ¡parking ¡space ¡could ¡be ¡modeled ¡more ¡ simply ¡with ¡a ¡Finite ¡Automata ¡

Vacant, ¡ Not ¡Paid ¡

Simplified ¡automobile ¡parking ¡ ¡

Vacant, ¡ Paid ¡ Occupied, ¡ Not ¡Paid ¡ Occupied, ¡ Paid ¡

Start ¡

Sensor ¡detects ¡ vehicle ¡arrival ¡ Meter ¡paid ¡ Meter ¡paid ¡ Sensor ¡detects ¡ vehicle ¡departure ¡ Payment ¡ expired ¡ Payment ¡ expired ¡ Sensor ¡detects ¡ vehicle ¡departure ¡ Sensor ¡detects ¡ vehicle ¡arrival ¡

slide-46
SLIDE 46

Meter ¡paid ¡

46 ¡

The ¡parking ¡space ¡could ¡be ¡modeled ¡more ¡ simply ¡with ¡a ¡Finite ¡Automata ¡

Vacant, ¡ Not ¡Paid ¡

Simplified ¡automobile ¡parking ¡ ¡

Vacant, ¡ Paid ¡ Occupied, ¡ Not ¡Paid ¡ Occupied, ¡ Paid ¡

Start ¡

Sensor ¡detects ¡ vehicle ¡arrival ¡ Meter ¡paid ¡ Sensor ¡detects ¡ vehicle ¡departure ¡ Payment ¡ expired ¡ Payment ¡ expired ¡ Sensor ¡detects ¡ vehicle ¡departure ¡ Sensor ¡detects ¡ vehicle ¡arrival ¡

Sensor ¡ probably ¡ missed ¡ vehicle ¡ arrival, ¡go ¡ figure ¡out ¡ why! ¡

slide-47
SLIDE 47

47 ¡

Code ¡review ¡

DFA.java ¡

  • Store ¡start ¡as ¡String ¡
  • Store ¡ends ¡as ¡a ¡Set ¡(could ¡be ¡mulSple ¡ends) ¡
  • Constructor ¡ ¡
  • Takes ¡set ¡of ¡States ¡(with ¡Start ¡and ¡Ends ¡labeled) ¡and ¡

transiSons ¡(A,B,0 ¡means ¡from ¡A ¡go ¡to ¡B ¡if ¡given ¡0) ¡

  • Track ¡start ¡and ¡end ¡verSces ¡
  • Track ¡transiSons ¡as ¡Map ¡(state-­‑> ¡Map(character, ¡next ¡state)) ¡
  • match ¡
  • Start ¡with ¡current ¡= ¡ ¡start ¡
  • For ¡each ¡input ¡
  • Ensure ¡transiSon ¡to ¡next ¡state ¡is ¡valid ¡
  • Move ¡to ¡next ¡state ¡
  • Return ¡final ¡state ¡
slide-48
SLIDE 48

48 ¡

Code ¡review ¡

NFA.java ¡

  • Store ¡start ¡as ¡String ¡
  • Store ¡ends ¡as ¡a ¡Set ¡(could ¡be ¡mulSple ¡ends) ¡
  • TransiSons ¡now ¡store ¡list ¡of ¡possible ¡states ¡
  • Constructor ¡ ¡
  • Takes ¡set ¡of ¡States ¡(with ¡Start ¡and ¡Ends ¡labeled) ¡and ¡

transiSons ¡(A,B,0 ¡means ¡from ¡A ¡go ¡to ¡B ¡if ¡given ¡0) ¡

  • Track ¡start ¡and ¡end ¡verSces ¡
  • TransacSons: ¡Map ¡(state-­‑> ¡Map(character, ¡List(String))) ¡
  • match ¡
  • Start ¡with ¡currentStates ¡= ¡ ¡start ¡(could ¡be ¡mulSple ¡valid ¡

current ¡states!) ¡

  • For ¡each ¡input ¡
  • Check ¡possible ¡next ¡states ¡from ¡all ¡valid ¡current ¡states ¡