13
11/8/18 1 Lecture 16 State Machines ECE 241 – Advanced Programming I Fall 2018 Mike Zink ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink Overview 1 Routing Distance Vector Routing

Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

1

Lecture16StateMachines

ECE241–AdvancedProgrammingIFall2018MikeZink

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Overview

1

• Routing• DistanceVectorRouting

Page 2: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

2

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Objective

• UnderstandhowroutingintheInternetisperformed

• Learnabouttwoessential routingalgorithmsthatoperateongraphs

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Regular Expressions

3

• Methodtodescribepatternsoftext• Character-by-characterprocessing

• Specialoperators• | (alternatives)• . (arbitrary character)• * (zero or more repetitions)• + (one or more repetitions)• () (precedence)• …

Page 3: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

3

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Regular Expressions

4

• Examples• abcd

• abcd matches; aabcd does not match• a*bcd

• aabcd matches; bcd matches; cd does not match• (ab|bb)cd

abcd matches; bbcd matches; abbbcd does not match• (ab|bb)*cd

abbbcd matches; bbabcd matches; cd matches; ababcdmatches; abbb does not match

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Use of Regular Expressions

5

• Compiler• Interpreting characters in program• Regular expressions for numbers, keywords, etc.• Example tool: flex

• Networking• Checking network traffic for attacks• Regular expressions for attack patterns• Example tool: snort database

Page 4: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

4

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

More Examples for Regular Expressions

6

• Examining command lines• Parsing user input• Parsing various text files• Examining web server logs• Examining test results• Finding text in emails• Reading configuration files

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

More Examples for Regular Expressions

7

• ^[a-zA-Z''-'\s]{1,40}$

Page 5: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

5

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Limits of Regular Expressions

8

• Regular expression match patterns from “regular language”

• Regular expression cannot describe patterns from more complex language

• What can you not describe with a regular expression?

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Limits of Regular Expressions

9

• Regular expression match patterns from “regular language”

• Regular expression cannot describe patterns from more complex language

• Context-free grammars• Equal number of opening and closing parentheses

• Context-sensitive grammars• Grammatically correct English language

Page 6: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

6

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

State Machine

10

• Regular expression can be matched with a state machine (or finite automaton)

• Statemachineisspecialcaseofdirectedgraph• Nodesrepresentstate• Edgesrepresenttransitions (basedoninput)

• Statemachinescanbeconstructedforanykindofregularexpression

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

State Machine Examples

11

• Example 1: ac+|bd

• Example 2: a(b*|c)d

1 2 5

3 4

a c

c

b

d

1 2

5

6

3

ab d

b

c d

d

Page 7: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

7

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Deterministic vs Non-Deterministic

• What is the problem with (ab)*ac?

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Deterministic vs Non-Deterministic

• What is the problem with (ab)*ac?• Non-deterministic transition on a

• Non-deterministic state machines• A bit more complex to implement• We do not consider them here• There exist algorithms to convert from NFA to DFA

Page 8: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

8

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Deterministic vs Non-Deterministic

• list/lost/lusti s

o s

u s

l

l

l

t

t

t

Start Match

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Deterministic vs Non-Deterministic

• list/lost/lust

is

o s

u s

l t

t

t

Start Match

Page 9: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

9

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Deterministic vs Non-Deterministic

• list/lost/lust

i s

o s

u s

l tStart Match

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Deterministic vs Non-Deterministic

• list/lost/lust

{i|o|u} sl tStart Match

Page 10: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

10

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Implementing a State Machine

18

• Vertex with multiple outgoing edges• Need class to represent edge• Need linked list to store edges

• Matching operation• Start at start node• Follow edge that matches character• At end, check if accepting state• If no edge or no accepting state, then no match

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Edge Class

19

class Edge:def __init__(self, c, dest):

self.destination = destself.character = c

Page 11: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

11

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Vertex Class

20

class Vertex:def __init__(self, n):

self.number = n self.edgeList = []

self.isAcceptingState = None

def setAcceptingState(self):self.isAcceptingState = True

def addEdge(self,e):self.edgeList.append(e)

def followEdge(self,c):for i in self.edgeList:

if i.character == c: return i.destination return None

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Matching Method

21

class DFA:def __init__(self,s):

self.start = s

def match(self,s):self.characters = list(s)self.current = self.start

print("trying to match "+s+": ")

for i in self.characters: if self.current == None: print("no match") return print(self.current.number ," ")

self.current = self.current.followEdge(i)

if self.current == None: print("no match") return

print(self.current.number);if self.current.isAcceptingState:

print("match") else: print("no match") return

Page 12: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

12

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Creating DFA and Patter Matching

22

v1 = Vertex(1)v2 = Vertex(2)v3 = Vertex(3)v4 = Vertex(4) v5 = Vertex(5)v6 = Vertex(6) v4.setAcceptingState()v6.setAcceptingState()

v1.addEdge(Edge("a",v2))v1.addEdge(Edge("b",v1))v2.addEdge(Edge("b",v3))v2.addEdge(Edge("a",v5))v3.addEdge(Edge("c",v4))v5.addEdge(Edge("b",v6))v6.addEdge(Edge("c",v6))v4.addEdge(Edge("d",v1))

stateMachine = DFA(v1)stateMachine.match("abc")stateMachine.match("bbabc")stateMachine.match("baab")stateMachine.match("baabcc")stateMachine.match("abcdbbbabc")stateMachine.match("abcd")stateMachine.match("e")

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Matching Example

23

• Graph:• Matching:

• abc• bbabc• baab• baabcc• abcdbbbabc• abcd• e

1 2 5 6

3 4

a a b

cb

b

c

d

Page 13: Routing Distance Vector Routing - UMass Amherst 241 F18 Lecture 16... · 2018-11-08 · 11/8/18 1 Lecture 16 State Machines ECE 241 –Advanced Programming I Fall 2018 Mike Zink ECE

11/8/18

13

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Next Steps

24

• NextlectureandonTuesday• Homework4dueonTuesday

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink25