48
COMP-330 Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

COMP-330 Theory of Computation

Fall 2017 -- Prof. Claude Crépeau

Lecture 2 : Regular Expressions & DFAs

Page 2: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

COMP 330 Fall 2017: Lectures Schedule

14. Context-free languages15. Pushdown automata16. Parsing17. The pumping lemma for CFLs18. Introduction to computability19. Models of computation Basic computability theory20. Reducibility, undecidability and Rice’s theorem21. Undecidable problems about CFGs22. Post Correspondence Problem23. Validity of FOL is RE / Gödel’s and Tarski’s thms24. Universality / The recursion theorem 25. Degrees of undecidability26. Introduction to complexity

1-2. Introduction 1.5. Some basic mathematics2-3. Deterministic finite automata +Closure properties,3-4. Nondeterministic finite automata5. Minimization+ Myhill-Nerode theorem 6. Determinization+Kleene’s theorem7. Regular Expressions+GNFA8. Regular Expressions and Languages9-10. The pumping lemma11. Duality12. Labelled transition systems13. MIDTERM

Page 3: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

95% of people cannot solve this!

🍎 + 🍌 + 🍍 = 4 🍌+🍍 🍍+🍎 🍎+🍌 .

Can you find positive whole values for 🍎,🍌, and 🍍?

Simple Puzzle

🍎=154476802108746166441951315019919837485664325669565431700026634898253202035277999🍌=36875131794129999827197811565225474825492979968971970996283137471637224634055579 🍍=4373612677928697257861252602371390152816537558161613618621437993378423467772036

95% 99.9999%

Bremner and MacLeod looked at what happens when we replace the 4 with something else. When you try to represent 178 in this way, you’ll need 398,605,460 digits. If you try 896, you’ll be up to trillions of digits…

Page 4: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Not all problems were born equal...

Page 5: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Is it possible to paint a colour on each region (province) of a map so that no neighbours are of the same colour ?

Vietnam

Page 6: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Obviously, yes, if you can use as many colours as you like...

Vietnam

Page 7: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

2-colouring problem

Vietnam

Page 8: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

2-colouring problem

Page 9: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

3-colouring problem

Vietnam

Page 10: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

3-colouring problem

Page 11: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

4-colouring problem

Vietnam

Page 12: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

K-colouring of Maps (planar graphs)K=1, only the maps with zero or one region are 1-colourable.

K=2, easy to decide. Impossible as soon as 3 regions touch each other.

K=3, No known efficient algorithm to decide. However it is easy to verify a solution.

K≥4, all maps are K-colourable. (looong proof) Not easy to find a K-colouring. However it is easy to verify a solution.

http://people.math.gatech.edu/~thomas/FC/fourcolor.html

Page 13: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

3-colouring of Maps

Seems hard to solve in general,

Is easy to verify when a solution is given,

Is a special type of problem (NP-complete) because an efficient solution to it would yield efficient solutions to MANY similar problems !

Page 14: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Examples of NP-Complete ProblemsSAT: given a boolean formula, is there an assignment of the variables making the formula evaluate to true ?

Travelling Salesman: given a set of cities and distances between them, what is the shortest route to visit each city once.

KnapSack: given items with various weights, is there of subset of them of total weight K.

Page 15: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

NP-Complete Problems

Many practical problems are NP-complete.

If any of them is easy, they are all easy.

In practice, some of them may be solved efficiently in some special cases.

Some books list hundreds of such problems.

Page 16: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Tractable Problems (P)

2-colorability of maps.

Primality testing. (but probably not factoring)

Solving NxNxN Rubik’s cube.

Finding a word in a dictionary.

Sorting elements.

Page 17: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Tractable Problems (P)

Fortunately, many practical problems are tractable. The name P stands for Polynomial-Time computable.

Computer Science studies mostly techniques to approach and find efficient solutions to tractable problems.

Some problems may be efficiently solvable but we might not be able to prove that...

Page 18: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

DecidableLanguages

NP

complete

Complexity Theory

P

P = NP ?

Page 19: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Beyond NP-Completeness

PSpace Completeness: problems that require a reasonable (Poly) amount of space to be solved but may use very long time though.

Many such problems. If any of them may be solved within reasonable (Poly) amount of time, then all of them can.

Page 20: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

PSpace Completeness

Geography Game:Given a set of country names: Arabia, Cuba, Canada, France, Italy, Japan, Korea, Vietnam

A two player game: One player chooses a name. The other player must choose a name that starts with the last letter of the previous name and so on. A player wins when his opponent cannot play any name.

Page 21: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Generalized Geography

Given an arbitrary set of names: w1, ..., wn.

Is there a winning strategy for the first player to the previous game ?

Page 22: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

DecidableLanguages

complete

Complexity Theory

NP

P

NP = PSpace ?

PSpace

Page 23: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Theoretical Computer Science

Challenges of TCS:

FIND efficient solutions to many problems. (Algorithms and Data Structures)

PROVE that certain problems are NOT computable within a certain time or space.

Consider new models of computation.(Such as a Quantum Computer)

Page 24: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Regular expressions, and Deterministic Finite Automata

Page 25: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Regular expressions, and Deterministic Finite Automata

Page 26: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Swing doors

Page 27: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Swing doors

Page 28: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Swing doors

Page 29: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Swing doors

Page 30: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Swing doors

Page 31: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Elevators

Page 32: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Elevators

Page 33: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Elevators

Page 34: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Elevators

Page 35: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

DFA: example

M1

Page 36: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

States

Alphabet

Transition function

Start state

Accept states

Definition of DFA

q1q2 q3

a,b,c,d

q1 q2

b

q1

q2

Page 37: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

Definition of DFA

Page 38: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

10010101

q1

M1

Page 39: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

10010101

q2

1

q2

M1

Page 40: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

10010101

q3

0

q3

M1

Page 41: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

10010101

0

q2

q2

M1

Page 42: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

10010101

q2

1

q2

M1

Page 43: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

10010101

q3

0

q3

M1

Page 44: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

10010101

q2

q2

1

M1

Page 45: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

10010101

q3

0

q3

M1

Page 46: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

10010101

q2

q2

1

M1

Page 47: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

10010101

q2

M1

Page 48: COMP-330 Theory of Computationcrypto.cs.mcgill.ca/~crepeau/COMP330/LECTURE-2.pdf · Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lecture 2 : Regular Expressions & DFAs

COMP-330 Theory of Computation

Fall 2017 -- Prof. Claude Crépeau

Lecture 2 : Regular Expressions & DFAs