CS 374: Algorithms & Models of Computation Chandra Chekuri - - PowerPoint PPT Presentation

cs 374 algorithms models of computation
SMART_READER_LITE
LIVE PREVIEW

CS 374: Algorithms & Models of Computation Chandra Chekuri - - PowerPoint PPT Presentation

CS 374: Algorithms & Models of Computation Chandra Chekuri University of Illinois, Urbana-Champaign Spring 2017 Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 33 CS 374: Algorithms & Models of Computation, Spring 2017 Strings and


slide-1
SLIDE 1

CS 374: Algorithms & Models of Computation

Chandra Chekuri

University of Illinois, Urbana-Champaign

Spring 2017

Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 33

slide-2
SLIDE 2

CS 374: Algorithms & Models of Computation, Spring 2017

Strings and Languages

Lecture 1

January 17, 2017

Chandra Chekuri (UIUC) CS374 2 Spring 2017 2 / 33

slide-3
SLIDE 3

Part I Strings

Chandra Chekuri (UIUC) CS374 3 Spring 2017 3 / 33

slide-4
SLIDE 4

String Definitions

Definition

1

An alphabet is a finite set of symbols. For example Σ = {0, 1}, Σ = {a, b, c, . . . , z}, Σ = {moveforward, moveback} are alphabets.

2

A string/word over Σ is a finite sequence of symbols over Σ. For example, ‘0101001’, ‘string’, ‘movebackrotate90’

3

ǫ is the empty string.

4

The length of a string w (denoted by |w|) is the number of symbols in w. For example, |101| = 3, |ǫ| = 0

5

For integer n ≥ 0, Σn is set of all strings over Σ of length n. Σ∗ is th set of all strings over Σ.

Chandra Chekuri (UIUC) CS374 4 Spring 2017 4 / 33

slide-5
SLIDE 5

Formally

Formally strings are defined recursively/inductively: ǫ is a string of length 0 ax is a string if a ∈ Σ and x is a string. The length of ax is 1 + |x| The above definition helps prove statements rigorously via induction. Alternative recursive defintion useful in some proofs: xa is a string if a ∈ Σ and x is a string. The length of xa is 1 + |x| Convention a, b, c, . . . denote elements of Σ w, x, y, z, . . . denote strings A, B, C, . . . denote sets of strings

Chandra Chekuri (UIUC) CS374 5 Spring 2017 5 / 33

slide-6
SLIDE 6

Much ado about nothing

ǫ is a string containing no symbols. It is not a set {ǫ} is a set containing one string: the empty string. It is a set, not a string. ∅ is the empty set. It contains no strings. {∅} is a set containing one element, which itself is a set that contains no elements.

Chandra Chekuri (UIUC) CS374 6 Spring 2017 6 / 33

slide-7
SLIDE 7

Concatenation and properties

If x and y are strings then xy denotes their concatenation. Formally we define concatenation recursively based on definition

  • f strings:

xy = y if x = ǫ xy = a(wy) if x = aw

Sometimes xy is written as x·y to explicitly note that · is a binary operator that takes two strings and produces another string. concatenation is associative: (uv)w = u(vw) and hence we write uvw not commutative: uv not necessarily equal to vu identity element: ǫu = uǫ = u

Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 33

slide-8
SLIDE 8

Substrings, prefix, suffix, exponents

Definition

1

v is substring of w iff there exist strings x, y such that w = xvy.

If x = ǫ then v is a prefix of w If y = ǫ then v is a suffix of w

2

If w is a string then w n is defined inductively as follows: w n = ǫ if n = 0 w n = ww n−1 if n > 0 Example: (blah)4 = blahblahblahblah.

Chandra Chekuri (UIUC) CS374 8 Spring 2017 8 / 33

slide-9
SLIDE 9

Set Concatenation

Definition

Given two sets A and B of strings (over some common alphabet Σ) the concatenation of A and B is defined as: AB = {xy | x ∈ A, y ∈ B} Example: A = {fido, rover, spot}, B = {fluffy, tabby} then AB = {fidofluffy, fidotabby, roverfluffy, . . .}.

Chandra Chekuri (UIUC) CS374 9 Spring 2017 9 / 33

slide-10
SLIDE 10

Σ∗ and languages

Definition

1

Σn is the set of all strings of length n. Defined inductively as follows: Σn = {ǫ} if n = 0 Σn = ΣΣn−1 if n > 0

2

Σ∗ = ∪n≥0Σn is the set of all finite length strings

3

Σ+ = ∪n≥1Σn is the set of non-empty strings.

Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 33

slide-11
SLIDE 11

Σ∗ and languages

Definition

1

Σn is the set of all strings of length n. Defined inductively as follows: Σn = {ǫ} if n = 0 Σn = ΣΣn−1 if n > 0

2

Σ∗ = ∪n≥0Σn is the set of all finite length strings

3

Σ+ = ∪n≥1Σn is the set of non-empty strings.

Definition

A language L is a set of strings over Σ. In other words L ⊆ Σ∗.

Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 33

slide-12
SLIDE 12

Exercise

Answer the following questions taking Σ = {0, 1}.

1

What is Σ0?

2

How many elements are there in Σ3?

3

How many elements are there in Σn?

4

What is the length of the longest string in Σ? Does Σ∗ have strings of infinite length?

5

If |u| = 2 and |v| = 3 then what is |u·v|?

6

Let u be an arbitrary string Σ∗. What is ǫu? What is uǫ?

7

Is uv = vu for every u, v ∈ Σ∗?

8

Is (uv)w = u(vw) for every u, v, w ∈ Σ∗?

Chandra Chekuri (UIUC) CS374 11 Spring 2017 11 / 33

slide-13
SLIDE 13

Canonical order and countability of strings

Definition

An set A is countably infinite if there is a bijection f between the natural numbers and A. Alternatively: A is countably infinite if A is an infinite set and there enumeration of elements of A

Chandra Chekuri (UIUC) CS374 12 Spring 2017 12 / 33

slide-14
SLIDE 14

Canonical order and countability of strings

Definition

An set A is countably infinite if there is a bijection f between the natural numbers and A. Alternatively: A is countably infinite if A is an infinite set and there enumeration of elements of A

Theorem

Σ∗ is countably infinite for every finite Σ. Enumerate strings in order of increasing length and for each given length enumerate strings in dictionary order (based on some fixed

  • rdering of Σ).

Example: {0, 1}∗ = {ǫ, 0, 1, 00, 01, 10, 11, 000, 001, 010, . . .}. {a, b, c}∗ = {ǫ, a, b, c, aa, ab, ac, ba, bb, bc, . . .}

Chandra Chekuri (UIUC) CS374 12 Spring 2017 12 / 33

slide-15
SLIDE 15

Exercise

Question: Is Σ∗ × Σ∗ = {(x, y) | x, y ∈ Σ∗} countably infinite?

Chandra Chekuri (UIUC) CS374 13 Spring 2017 13 / 33

slide-16
SLIDE 16

Exercise

Question: Is Σ∗ × Σ∗ = {(x, y) | x, y ∈ Σ∗} countably infinite? Question: Is Σ∗ × Σ∗ × Σ∗ = {(x, y, z) | x, y, x ∈ Σ∗} countably infinite?

Chandra Chekuri (UIUC) CS374 13 Spring 2017 13 / 33

slide-17
SLIDE 17

Inductive proofs on strings

Inductive proofs on strings and related problems follow inductive definitions.

Definition

The reverse w R of a string w is defined as follows: w R = ǫ if w = ǫ w R = xRa if w = ax for some a ∈ Σ and string x

Chandra Chekuri (UIUC) CS374 14 Spring 2017 14 / 33

slide-18
SLIDE 18

Inductive proofs on strings

Inductive proofs on strings and related problems follow inductive definitions.

Definition

The reverse w R of a string w is defined as follows: w R = ǫ if w = ǫ w R = xRa if w = ax for some a ∈ Σ and string x

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Example: (dog·cat)R = (cat)R·(dog)R = tacgod.

Chandra Chekuri (UIUC) CS374 14 Spring 2017 14 / 33

slide-19
SLIDE 19

Principle of mathematical induction

Induction is a way to prove statements of the form ∀n ≥ 0, P(n) where P(n) is a statement that holds for integer n. Example: Prove that n

i=0 i = n(n + 1)/2 for all n.

Induction template: Base case: Prove P(0) Induction Step: Let n > 0 be arbitrary integer. Assuming that P(k) holds for 0 ≤ k < n, prove that P(n) holds. Unlike the simple cases we will be working with various more complicated “structures” such as strings, tuples of strings, graphs

  • etc. We need to translate a statement “Q” into a (stronger or

equivalent) statement that looks like “∀n ≥ 0, P(n) and then apply

  • induction. We call ∀n ≥ 0, P(n) the induction hypothesis.

Chandra Chekuri (UIUC) CS374 15 Spring 2017 15 / 33

slide-20
SLIDE 20

Proving the theorem

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof: by induction. On what?? |uv| = |u| + |v|? |u|? |v|? What does it mean to say “induction on |u|”?

Chandra Chekuri (UIUC) CS374 16 Spring 2017 16 / 33

slide-21
SLIDE 21

By induction on |u|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |u| means that we are proving the following. Induction hypothesis: ∀n ≥ 0, for any string u of length n (for all strings v ∈ Σ∗, (uv)R = v RuR).

Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 33

slide-22
SLIDE 22

By induction on |u|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |u| means that we are proving the following. Induction hypothesis: ∀n ≥ 0, for any string u of length n (for all strings v ∈ Σ∗, (uv)R = v RuR). Base case: Let u be an arbitrary stirng of length 0. u = ǫ since there is only one such string. Then (uv)R = (ǫv)R = v R = v Rǫ = v RǫR = v RuR

Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 33

slide-23
SLIDE 23

By induction on |u|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |u| means that we are proving the following. Induction hypothesis: ∀n ≥ 0, for any string u of length n (for all strings v ∈ Σ∗, (uv)R = v RuR). Base case: Let u be an arbitrary stirng of length 0. u = ǫ since there is only one such string. Then (uv)R = (ǫv)R = v R = v Rǫ = v RǫR = v RuR Note that we did not assume anything about v, hence the statement holds for all v ∈ Σ∗.

Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 33

slide-24
SLIDE 24

Inductive step

Let u be an arbitrary string of length n > 0. Assume inductive hypothesis holds for all strings w of length < n. Since |u| = n > 0 we have u = ay for some string y with |y| < n and a ∈ Σ. Then

Chandra Chekuri (UIUC) CS374 18 Spring 2017 18 / 33

slide-25
SLIDE 25

Inductive step

Let u be an arbitrary string of length n > 0. Assume inductive hypothesis holds for all strings w of length < n. Since |u| = n > 0 we have u = ay for some string y with |y| < n and a ∈ Σ. Then (uv)R =

Chandra Chekuri (UIUC) CS374 18 Spring 2017 18 / 33

slide-26
SLIDE 26

Inductive step

Let u be an arbitrary string of length n > 0. Assume inductive hypothesis holds for all strings w of length < n. Since |u| = n > 0 we have u = ay for some string y with |y| < n and a ∈ Σ. Then (uv)R = ((ay)v)R = (a(yv))R = (yv)RaR = (v Ry R)aR = v R(y RaR) = v R(ay)R = v RuR

Chandra Chekuri (UIUC) CS374 18 Spring 2017 18 / 33

slide-27
SLIDE 27

Induction on |v|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |v| means that we are proving the following.

Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 33

slide-28
SLIDE 28

Induction on |v|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |v| means that we are proving the following. Induction hypothesis: ∀n ≥ 0, for any string v of length n (for all strings u ∈ Σ∗, (uv)R = v RuR).

Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 33

slide-29
SLIDE 29

Induction on |v|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |v| means that we are proving the following. Induction hypothesis: ∀n ≥ 0, for any string v of length n (for all strings u ∈ Σ∗, (uv)R = v RuR). Base case: Let v be an arbitrary stirng of length 0. v = ǫ since there is only one such string. Then (uv)R = (uǫ)R = uR = ǫuR = ǫRuR = v RuR

Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 33

slide-30
SLIDE 30

Inductive step

Let v be an arbitrary string of length n > 0. Assume inductive hypothesis holds for all strings w of length < n. Since |v| = n > 0 we have v = ay for some string y with |y| < n and a ∈ Σ. Then (uv)R = (u(ay))R = ((ua)y)R = y R(ua)R = ??

Chandra Chekuri (UIUC) CS374 20 Spring 2017 20 / 33

slide-31
SLIDE 31

Inductive step

Let v be an arbitrary string of length n > 0. Assume inductive hypothesis holds for all strings w of length < n. Since |v| = n > 0 we have v = ay for some string y with |y| < n and a ∈ Σ. Then (uv)R = (u(ay))R = ((ua)y)R = y R(ua)R = ?? Cannot simplify (ua)R using inductive hypotheis. Can simplify if we extend base case to include n = 0 and n = 1. However, n = 1 itself requires induction on |u|!

Chandra Chekuri (UIUC) CS374 20 Spring 2017 20 / 33

slide-32
SLIDE 32

Induction on |u| + |v|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |u| + |v| means that we are proving the following.

Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 33

slide-33
SLIDE 33

Induction on |u| + |v|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |u| + |v| means that we are proving the following. Induction hypothesis:

Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 33

slide-34
SLIDE 34

Induction on |u| + |v|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |u| + |v| means that we are proving the following. Induction hypothesis: ∀n ≥ 0, for any u, v ∈ Σ∗ with |u| + |v| ≤ n, (uv)R = v RuR.

Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 33

slide-35
SLIDE 35

Induction on |u| + |v|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |u| + |v| means that we are proving the following. Induction hypothesis: ∀n ≥ 0, for any u, v ∈ Σ∗ with |u| + |v| ≤ n, (uv)R = v RuR. Base case: n = 0. Let u, v be an arbitrary stirngs such that |u| + |v| = 0. Implies u, v = ǫ.

Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 33

slide-36
SLIDE 36

Induction on |u| + |v|

Theorem

Prove that for any strings u, v ∈ Σ∗, (uv)R = v RuR. Proof by induction on |u| + |v| means that we are proving the following. Induction hypothesis: ∀n ≥ 0, for any u, v ∈ Σ∗ with |u| + |v| ≤ n, (uv)R = v RuR. Base case: n = 0. Let u, v be an arbitrary stirngs such that |u| + |v| = 0. Implies u, v = ǫ. Inductive stepe: n > 0. Let u, v be arbitrary strings such that |u| + |v| = n.

Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 33

slide-37
SLIDE 37

Part II Languages

Chandra Chekuri (UIUC) CS374 22 Spring 2017 22 / 33

slide-38
SLIDE 38

Languages

Definition

A language L is a set of strings over Σ. In other words L ⊆ Σ∗.

Chandra Chekuri (UIUC) CS374 23 Spring 2017 23 / 33

slide-39
SLIDE 39

Languages

Definition

A language L is a set of strings over Σ. In other words L ⊆ Σ∗. Standard set operations apply to languages. For languages A, B the concatenation of A, B is AB = {xy | x ∈ A, y ∈ B}. For languages A, B, their union is A ∪ B, intersection is A ∩ B, and difference is A \ B (also written as A − B). For language A ⊆ Σ∗ the complement of A is ¯ A = Σ∗ \ A.

Chandra Chekuri (UIUC) CS374 23 Spring 2017 23 / 33

slide-40
SLIDE 40

Exponentiation, Kleene star etc

Definition

For a language L ⊆ Σ∗ and n ∈ N, define Ln inductively as follows. Ln = {ǫ} if n = 0 L·(Ln−1) if n > 0 And define L∗ = ∪n≥0Ln, and L+ = ∪n≥1Ln

Chandra Chekuri (UIUC) CS374 24 Spring 2017 24 / 33

slide-41
SLIDE 41

Exercise

Problem

Answer the following questions taking A, B ⊆ {0, 1}∗.

1

Is ǫ = {ǫ}? Is ∅ = {ǫ}?

2

What is ∅·A? What is A·∅?

3

What is {ǫ}·A? And A·{ǫ}?

4

If |A| = 2 and |B| = 3, what is |A·B|?

Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 33

slide-42
SLIDE 42

Exercise

Problem

Consider languages over Σ = {0, 1}.

1

What is ∅0?

2

If |L| = 2, then what is |L4|?

3

What is ∅∗, {ǫ}∗, ǫ∗?

4

For what L is L∗ finite?

5

What is ∅+, {ǫ}+, ǫ+?

Chandra Chekuri (UIUC) CS374 26 Spring 2017 26 / 33

slide-43
SLIDE 43

Languages and Computation

What are we interested in computing? Mostly functions. Informal defintion: An algorithm A computes a function f : Σ∗ → Σ∗ if for all w ∈ Σ∗ the algorithm A on input w terminates in a finite number of steps and outputs f (w). Examples of functions: Numerical functions: length, addition, multiplication, division etc Given graph G and s, t find shortest paths from s to t Given program M check if M halts on empty input Posts Correspondence problem

Chandra Chekuri (UIUC) CS374 27 Spring 2017 27 / 33

slide-44
SLIDE 44

Languages and Computation

Definition

A function f over Σ∗ is a boolean if f : Σ∗ → {0, 1}.

Chandra Chekuri (UIUC) CS374 28 Spring 2017 28 / 33

slide-45
SLIDE 45

Languages and Computation

Definition

A function f over Σ∗ is a boolean if f : Σ∗ → {0, 1}. Observation: There is a bijection between boolean functions and languages. Given boolean function f : Σ∗ → {0, 1} define language Lf = {w ∈ Σ∗ | f (w) = 1}

Chandra Chekuri (UIUC) CS374 28 Spring 2017 28 / 33

slide-46
SLIDE 46

Languages and Computation

Definition

A function f over Σ∗ is a boolean if f : Σ∗ → {0, 1}. Observation: There is a bijection between boolean functions and languages. Given boolean function f : Σ∗ → {0, 1} define language Lf = {w ∈ Σ∗ | f (w) = 1} Given language L ⊆ Σ∗ define boolean function f : Σ∗ → {0, 1} as follows: f (w) = 1 if w ∈ L and f (w) = 0 otherwise.

Chandra Chekuri (UIUC) CS374 28 Spring 2017 28 / 33

slide-47
SLIDE 47

Language recognition problem

Definition

For a language L ⊆ Σ∗ the language recognition problem associate with L is the following: given w ∈ Σ∗, is w ∈ L?

Chandra Chekuri (UIUC) CS374 29 Spring 2017 29 / 33

slide-48
SLIDE 48

Language recognition problem

Definition

For a language L ⊆ Σ∗ the language recognition problem associate with L is the following: given w ∈ Σ∗, is w ∈ L? Equivalent to the problem of “computing” the function fL. Language recognition is same as boolean function computation How difficult is a function f to compute? How difficult is the recognizing Lf ?

Chandra Chekuri (UIUC) CS374 29 Spring 2017 29 / 33

slide-49
SLIDE 49

Language recognition problem

Definition

For a language L ⊆ Σ∗ the language recognition problem associate with L is the following: given w ∈ Σ∗, is w ∈ L? Equivalent to the problem of “computing” the function fL. Language recognition is same as boolean function computation How difficult is a function f to compute? How difficult is the recognizing Lf ? Why two different views? Helpful in understanding different aspects?

Chandra Chekuri (UIUC) CS374 29 Spring 2017 29 / 33

slide-50
SLIDE 50

How many languages are there?

Recall:

Definition

An set A is countably infinite if there is a bijection f between the natural numbers and A.

Theorem

Σ∗ is countably infinite for every finite Σ. The set of all languages is P(Σ∗) the power set of Σ∗

Chandra Chekuri (UIUC) CS374 30 Spring 2017 30 / 33

slide-51
SLIDE 51

How many languages are there?

Recall:

Definition

An set A is countably infinite if there is a bijection f between the natural numbers and A.

Theorem

Σ∗ is countably infinite for every finite Σ. The set of all languages is P(Σ∗) the power set of Σ∗

Theorem (Cantor)

P(Σ∗) is not countably infinite if |Σ| ≥ 2.

Chandra Chekuri (UIUC) CS374 30 Spring 2017 30 / 33

slide-52
SLIDE 52

Cantor’s diagonalization argument

Theorem (Cantor)

P(N) is not countably infinite. Suppose P(N) is countable infinite. Let S1, S2, . . . , be an enumeration of all subsets of numbers. Let D be the following diagonal subset of numbers. D = {i | i ∈ Li} Since D is a set of numbers, by assumption, D = Sj for some j. Question: Is j ∈ D?

Chandra Chekuri (UIUC) CS374 31 Spring 2017 31 / 33

slide-53
SLIDE 53

Consequences for Computation

How many C programs are there? The set of C programs is countably infinite since each of them can be represented as a string over a finite alphabet. How many languages are there? Uncountably many! Hence some (in fact almost all!) languages/boolean functions do not have any C program to recognize them. Questions:

Chandra Chekuri (UIUC) CS374 32 Spring 2017 32 / 33

slide-54
SLIDE 54

Consequences for Computation

How many C programs are there? The set of C programs is countably infinite since each of them can be represented as a string over a finite alphabet. How many languages are there? Uncountably many! Hence some (in fact almost all!) languages/boolean functions do not have any C program to recognize them. Questions: Maybe interesting languages/functions have C programs and hence computable. Only uninteresting langues uncomputable? Why should C programs be the definition of computability? Ok, there are difficult problems/languages. what lanauges are computable and which have efficient algorithms?

Chandra Chekuri (UIUC) CS374 32 Spring 2017 32 / 33

slide-55
SLIDE 55

Easy languages

Definition

A language L ⊆ Σ∗ is finite if |L| = n for some integer n. Exercise: Prove the following.

Theorem

The set of all finite languages is countably infinite.

Chandra Chekuri (UIUC) CS374 33 Spring 2017 33 / 33