Solution

20
Solution Prove by induction the following statement: If there is an edge connecting every pair of nodes in a graph G and n is the number of nodes in G, then the number of edges in G is n(n-1)/2

description

Solution. Prove by induction the following statement: If there is an edge connecting every pair of nodes in a graph G and n is the number of nodes in G, then the number of edges in G is n(n-1)/2. Finite Automata (Deterministic) (Chapter 1). - PowerPoint PPT Presentation

Transcript of Solution

Page 1: Solution

Solution

Prove by induction the following statement:

• If there is an edge connecting every pair of nodes in a graph G and n is the number of nodes in G, then the number of edges in G is n(n-1)/2

Page 2: Solution

Finite Automata (Deterministic)

(Chapter 1)

Note: once again you are strongly advised to read the covered chapters from the book carefully. Particularly the examples!

Page 3: Solution

Reminder: Functions vs Relations

Let P = {p: p is a person} M = {m: m is a male}

S1 = {(m,p): m is in M, p is in P and m is the father of p}

S2 = {(m,p): m is in M, p is in P and m is an ancestor of p}

1. True or false: S1 M P2. True or false: S2 M P3. Is either S1 or S2 a relation in M P?4. Is either S1 or S2 a function f:M P?

Page 4: Solution

You have seen Finite Automata before!

1. Write a program that indicates if a given input text contains the string “Britney Spears” (or “Justin Timberlake”)

2. Check this video:

Page 5: Solution

NPC’s Behavior is Modeled through Finite State Machines (I)

• State: an activity performed by an avatar

• Event: something that happens in the game world that makes state change

PatrolEnemy on sight

Attack

Page 6: Solution

NPC’s Behavior is Modeled through Finite State Machines (II)

• States– Attack– Chase– Spawn– Wander

• Events– E: see an enemy– S: hear a sound– D: die

Spawn

Wander

~E

D

Attack

~E

E

E

D

~S

Chase

E

S

S

D

Page 7: Solution

Deterministic Automata (Informal)

Key questions: if a automaton is confronted with a certain state where a choice must be made,

1. are all the alternatives transitions known?, and2. given some input data, is it known which transition the machine will make?

If the answer to both of these questions is “yes”, the automaton is said to be deterministic

“current state”

“transition”

“new state”

Page 8: Solution

Nondeterministic Automata (Informal)

If the answer to any of these questions is “no”, the automaton is said to be nondeterministic

That is, either

• some transitions are unknown, or

• given some input data, the machine can make more than one transition

Page 9: Solution

Deterministic Automata (Informal)

We are going to define automata indicating for a state s and some input data d, which is the state that will be reached

Transition: ((s,d), s’)

s

d s’

Let Q be the set of all states and be the set of all input data. Then, the set of transitions is a subset of (Q ) Q

of the computation

Page 10: Solution

Determinism, Nondeterminism, Relations and Functions

The set of transitions defining an automaton is a subset of (Q ) Q

If the automaton is deterministic, should the set of transitions be a relation or a function?

• Deterministic automata: Since for each pair (s,d) there should be one and only one s’, the set of transitions must be a function

• Nondeterministic automata: Since for each pair (s,d) there might not be any s’ or there might be more than one s’, the set of transitions must be a relation

Page 11: Solution

Finite Automata

•Problem 1: Design a computer program that given a sequence of numbers a1, a2, …, an returns their sum a1 + a2 +… + an

•Problem 2: Design a computer program that given a sequence of numbers a1, a2, …, an returns the list in the inverted order:

an, …, a2, a1

•How many memory units are needed for a program to execute:

problem 1:

Problem 2:

Finite automata use a constant amount of memory

1

n

Page 12: Solution

Deterministic Finite Automaton (finite automaton)

A deterministic finite automaton (DFA) or finite automaton is a 5-tuple (Q,,,s,F) where:

•Q is a finite set of elements called states is a finite input alphabet is a transition function, (Q × ) × Q (or : (Q × ) Q) •s Q called the start state•F Q called the favorable states

…a1 a2The fact that is a function makes the automaton deterministic

no! (or yes!)

As in yes! (or no!) The input word is (not) in the language recognized by the automaton

Constant!

Page 13: Solution

Finite State Diagram

•A finite state diagram is a graphic representation for a finite automaton

•A finite state diagram is a directed graph, where nodes represent elements in Q (i.e., states) and arrows are characters in such that:

q q’a Indicates: ((q,a),q’) is a transition in

The start state is marked with: >

The favorable states are marked with:

Page 14: Solution

Example # 1

s qb

a

b

a r

a,b

qb

a

a r>

= {a,b}

Q = {s,q,r}: 6 transitions:

• ((s,b), s)• ((s,a), q)• ((q,a), r)• ((q,b), s)• ((r,a), r)• ((r,b), r)

s: start state

F = {r}

Formally, this automaton (Q,,,s,F) is defined as:

Is this automaton deterministic?

Page 15: Solution

Kleene Star• Given a set B of characters, B* denote the set of all strings made of elements in B.

•Typically we take * for all words that can be given as input for a DFA (Q,,,s,F)

• If = {0,1}, what is *?• If = {a,b}, what is *?• If = {a,b,c, …, z}, what is *?

the empty word, e, is always in *

Page 16: Solution

Formal Definition of Computation

• Given an finite automaton M= (Q,,,s,F), and let w = w1w2 …wn, where each wi is in (i.e., w in *)

M accepts w if there is a sequence of states r0 , r1, r2 , … rn in Q such that:

1. r0 is the start state of M2. (ri , wi+1) = ri+1

3. rn in F

• Language recognized by M: {w in * : M accepts w}

• Given a language A, with A *, the language A is regular if a finite automata exists recognizing A

“such that”

Page 17: Solution

Example

1. Is abb accepted by this automaton?

2. Is abba accepted by this automaton?

3. What is the language recognized by this automaton?

s qb

a

b

a r

a

b

qb

a

a r>

Page 18: Solution

Example # 2

What is the language recognized by this automaton?

s qb

a

b

a r

a

b

qb

a

a r>

Page 19: Solution

Homework Friday Sept. 7

• 1.6: a, f, j

• 1.31

• 1.36

Page 20: Solution

Sample Problems

1.6: b Let = {0,1}, Show that the following language over is regular:

{w : w contains at least three 1’s}

1.31 Let A be a regular language. Show that:

AR = {wR | w is in A} is also regular