Docsity

Computer Science: Classifying and Solving Problems - Prof. Eric Torng, Papers of Computer Science

Partial preview of the text

Download Computer Science: Classifying and Solving Problems - Prof. Eric Torng and more Papers Computer Science in PDF only on Docsity!

1

Strings and Languages Operations

Concatenation

Exponentiation

Kleene Star

Regular Expressions

Strings and Language Operations

• Concatenation

• Exponentiation

• Kleene star

  • Pages 27-30 of the text

• Regular expressions

  • Pages 71-75 of the text

3

String Concatenation

• If x and y are strings over alphabet Σ, the

concatenation of x and y is the string xy

formed by writing the symbols of x and the

symbols of y consecutively.

• Suppose x = abb and y = ba

  • xy = abbba
  • yx = baabb

Properties of String Concatenation

• Suppose x, y, and z are strings.

• Concatenation is not commutative.

  • xy is not guaranteed to be equal to yx

• Concatenation is associative

  • (xy)z = x(yz) = xyz

• The empty string is the identity for

concatenation

  • x/\ = /\x = x

7

Associativity of Language

Concatenation

• (L 1 L 2 )L 3 = L 1 (L 2 L 3 ) = L 1 L 2 L 3

• Example

  • Let L 1 ={a,b}, L 2 ={c,d}, and L 3 ={e,f}
  • L 1 L 2 L 3 =({a,b}{c,d}){e,f} ={ac, ad, bc, bd}{e,f} ={ ace,acf,ade,aef,bce,bcf,bde,bdf }
  • L 1 L 2 L 3 ={a,b}({c,d}{e,f}) ={a,b}{ce, df, ce, df} ={ ace,acf,ade,aef,bce,bcf,bde,bdf }

Special Cases

  • What language is the identity for language concatenation? - The set containing only the empty string /: {/} - Example - {aab,ba,abc}{/} = {/}{aab,ba,abc} = {aab,ba,abc}
  • What about {}?
    • For any language L, L {} = {} L = {}
      • Thus {} for concatenation is like 0 for multiplication
    • Example
      • {aab,ba,abc}{} = {}{aab,ba,abc} = {}
      • The intuitive reason is that we must choose a string from both sets that are being concatenated, but there is nothing to choose from {}.

9

Exponentiation

  • We use exponentiation to indicate the number of items being concatenated - Symbols - Strings - Set of symbols (Σ for example) - Set of strings (languages)
  • a^3 = aaa
  • x 3 = xxx
  • Σ^3 = ΣΣΣ = { x ∈ Σ* | |x|=3 }
  • L^3 = LLL

Examples of Exponentiation

• Let x=abb, Σ={a,b}, L={ab,b}

• a 4 = aaaa

• x 3 = (abb)(abb)(abb) = abbabbabb

• Σ 3 = ΣΣΣ = {a,b}{a,b}{a,b}

={aaa,aab,aba,abb,baa,bab,bba,bbb}

• L^3 = LLL = {ab,b}{ab,b}{ab,b}

= {ababab,ababb,abbab,abbb,

babab,babb,bbab,bbb}

13

Kleene Star

  • Kleene * is a unary operation on languages.
  • Kleene * is not an operation on strings
    • However, see the pages on regular expressions.
  • L* represents any finite number of concatenations of L.

L* = Uk>0 Lk^ = L^0 U L^1 U L^2 U …

  • For any L, /\ is always an element of L*
    • because L^0 = { /\ }
  • Thus, for any L, L* != φ

Example of Kleene Star

  • Let L={aa}
  • L^0 ={ /\ }
  • L^1 =L={aa }
  • L^2 ={ aaaa }
  • L^3 = …
  • L* = L 0 ∪ L 1 ∪ L 2 ∪ L 3 …
  • = { /, aa, aaaa, aaaaaa, … }
  • = set of all strings that can be obtained by concatenating 0 or more copies of aa

15

Example of Kleene Star

• Let L={aa, b}

• L^0 ={ /\ }

• L^1 =L={aa,b}

• L^2 = LL={ aaaa, aab, baa, bb}

• L^3 = …

• L* = L 0 ∪ L 1 ∪ L 2 ∪ L 3 …

• = set of all strings that can be obtained by

concatenating 0 or more copies of aa and b

Regular Languages

• Regular languages are languages that can be

obtained from the very simple languages

over Σ, using only

  • Union
  • Concatenation
  • Kleene Star

• See lecture 14 and pages 71-75 of the text

19

Examples of Regular Expressions

Regular Expressions

Set notation

{a}∪{b} a+b

{a}{bb,/} = {a}({bb}∪{/}) a*(bb+/)

{bb}{a,b}* = {bb}({a}∪{b})* bb(a+b)*

{a,b}* = ({a}∪{b})* (a+b)*

{aa,b} = {aa}∪{b} aa+b

{aab} aab

String or Language?

  • Consider the regular expression a*(bb+/)
  • a*(bb+/) is a string over alphabet {a, b, *, +, /, (, ), φ }
  • a*(bb+/) represents a language over alphabet {a, b}
    • It represents the language of strings over {a,b} that begin with any number of a’s and end with an optional bb.
  • Some regular expressions look just like strings over alphabet {a,b} - Regular expression aaba represents the language {aaba} - Regular expression /\ represents the language {/}
  • It should be clear from the context whether a sequence of symbols is a regular expression or just a string.

21

Module 1: Course Overview

• Course: CSE 460

• Instructor: Dr. Eric Torng

• TA: To be determined

What is this course?

• Philosophy of computing course

  • We take a step back to think about computing in broader terms

• Science of computing course

  • We study fundamental ideas/results that shape the field of computer science

• “Applied” computing course

  • We learn study a broad range of material with relevance to computing today

25

Applied computing

• Applications are not immediately obvious

  • In some cases, seeing the applicability of this material requires advanced abstraction skills - Every year, there are people who leave this course unable to see the applicability of the material
  • Others require more material in order to completely understand their application - for example, to understand how regular expressions and context-free grammars are applied to the design of compilers, you need to take a compilers course

Some applications

  • Important programming languages
    • regular expressions (perl)
    • finite state automata (used in hardware design)
    • context-free grammars
  • Proofs of program correctness
  • Subroutines
    • Using them to prove problems are unsolvable
  • String searching/Pattern matching
  • Algorithm design concepts such as recursion

27

Fundamental Theme *

• What are the capabilities and limitations of

computers and computer programs?

  • What can we do with computers/programs?
  • Are there things we cannot do with computers/programs?

Module 2: Fundamental Concepts

• Problems

• Programs

  • Programming languages

31

How to specify a problem

• Input

  • Describe what an input instance looks like

• Output

  • Describe what task should be performed on the input
  • In particular, describe what output should be produced

Example Problem Specifications*

  • Sorting problem
    • Input
      • Integers n 1 , n 2 , ..., n (^) k
    • Output
      • n 1 , n 2 , ..., n (^) k in nondecreasing order
  • Find element problem
    • Input
      • Integers n 1 , n 2 , …, n (^) k
      • Search key S
    • Output
      • yes if S is in n 1 , n 2 , …, n (^) k, no otherwise

33

Programs

Programs solve problems

Purpose

• Why do we write programs?

• One answer

  • To solve problems
  • What does it mean to solve a problem?
    • Informal answer: For every legal input, a correct output is produced.
    • Formal answer: To be given later

37

Maximum Element Problem

  • Input
    • integer n >= 1
    • List of n integers
  • Output
    • The largest of the n integers

C++ Program which solves the

Maximum Element Problem*

int main(int A[], int n) { int i, max;

if (n < 1) return (“Illegal Input”); max = A[0]; for (i = 1; i < n; i++) if (A[i] > max) max = A[i]; return (max); }

39

Fundamental Theme

Exploring capabilities and limitations

of C++ programs

Restating the Fundamental Theme *

• We will study the capabilities and limits of

C++ programs

• Specifically, we will try and identify

  • What problems can be solved by C++ programs
  • What problems cannot be solved by C++ programs