Recursive definition of sets and structural induction

Similar documents
COMP Logic for Computer Scientists. Lecture 23

Recursively Defined Functions

Recursive Definitions and Structural Induction

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Recursive Definitions Structural Induction Recursive Algorithms

Chapter Summary. Recursively defined Functions. Recursively defined sets and Structures Structural Induction

2.2 Syntax Definition

Lamé s Theorem. Strings. Recursively Defined Sets and Structures. Recursively Defined Sets and Structures

Inference rule for Induction

Recursive Algorithms II

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Warmup Problem. Translate the following sentence from English into Propositional Logic. I want to eat ice cream even though I am on a diet.

To illustrate what is intended the following are three write ups by students. Diagonalization

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

Dynamic Programming Homework Problems

5.3 Recursive definitions and structural induction

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

A Simple Syntax-Directed Translator

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Propositional Logic Formal Syntax and Semantics. Computability and Logic

CS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions

Recursion and Structural Induction

CS 220: Discrete Structures and their Applications. Recursive objects and structural induction in zybooks

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

This book is licensed under a Creative Commons Attribution 3.0 License

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Solving Linear Recurrence Relations (8.2)

Propositional Logic. Andreas Klappenecker

Figure 4.1: The evolution of a rooted tree.

Indicate the option which most accurately completes the sentence.

Recursion vs Induction

Recursion vs Induction. CS3330: Algorithms The University of Iowa

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

Recursive Data Structures and Grammars

Lecture 6: Combinatorics Steven Skiena. skiena

ITEC2620 Introduction to Data Structures

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

ANS:

Backtracking. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. Chapter 5

Solutions to the Second Midterm Exam

Mathematical Induction

CSE 20 DISCRETE MATH. Winter

SECTION 5.1. Sequences

1.3. Conditional expressions To express case distinctions like

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

Context-Free Grammars

CS 380/480 Foundations of Artificial Intelligence Winter 2007 Assignment 2 Solutions to Selected Problems

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

Trees and Inductive Definitions

Software Tools and Systems Programming: Assignment 3

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

Formal Predicate Calculus. Michael Meyling

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

Programming Languages Third Edition

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

16 Greedy Algorithms

Lecture Chapter 6 Recursion as a Problem Solving Technique

A Small Interpreted Language

It is important that you show your work. There are 134 points available on this test.

An Interesting Way to Combine Numbers

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Syntax Analysis Check syntax and construct abstract syntax tree

10/9/17. Using recursion to define objects. CS 220: Discrete Structures and their Applications

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

Backtracking. Module 6. The Eight Queens Problem. CS 147 Sam Houston State University Dr. McGuire. Backtracking. Recursion Revisited

CSE 311: Foundations of Computing. Lecture 8: Predicate Logic Proofs

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Recursively Enumerable Languages, Turing Machines, and Decidability

CSE 331: Introduction to Algorithm Analysis and Design Graphs

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

CpSc 421 Final Solutions

Algorithms Dr. Haim Levkowitz

CS 314 Principles of Programming Languages

Lecture 5: Predicate Calculus. ffl Predicate Logic ffl The Language ffl Semantics: Structures

Lecture 24 Tao Wang 1

Chapter 3. Describing Syntax and Semantics

Propositional Logic. Part I

This is already grossly inconvenient in present formalisms. Why do we want to make this convenient? GENERAL GOALS

Compilers Course Lecture 4: Context Free Grammars

CmpSci 187: Programming with Data Structures Spring 2015

Reading 8 : Recursion

Summary. csci 210: Data Structures Recursion. Recursive algorithms. Recursion. Topics READING:

csci 210: Data Structures Recursion

Warm-Up Problem. Let L be the language consisting of as constant symbols, as a function symbol and as a predicate symbol. Give an interpretation where

Computer Science 236 Fall Nov. 11, 2010

Ambiguous Grammars and Compactification

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

Predicate Logic CHAPTER What This Chapter Is About

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Lecture 6,

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Computation Club: Gödel s theorem

UNIT 5A Recursion: Basics. Recursion

COMP Logic for Computer Scientists. Lecture 25

Transcription:

CS2209A 2017 Applied Logic for Computer Science Lecture 21, 22 Recursive definition of sets and structural induction Instructor: Marc Moreno Maza 1

Tower of Hanoi game Rules of the game: Start with all disks on the first peg. At any step, can move a disk to another peg, as long as it is not placed on top of a smaller disk. Goal: move the whole tower onto the second peg. Question: how many steps are needed to move the tower of 8 disks? How about n disks?

Tower of Hanoi game Rules of the game: Start with all disks on the first peg. At any step, can move a disk to another peg, as long as it is not placed on top of a smaller disk. Goal: move the whole tower onto the second peg. Question: how many steps are needed to move the tower of 8 disks? How about n disks? Let us call the number of moves needed to transfer n disks H(n). Names of pegs do not matter: from any peg to any peg would take the same number of steps. Basis: only one disk can be transferred in one step. So H(1) = 1 Recursive step: suppose we have n-1 disks. To transfer them all to peg 2, need ( 1) number of steps. To transfer the remaining disk to peg 3, 1step. To transfer n-1 disks from peg 2 to peg 3 need H(n-1) steps again. So H(n) = 2H(n-1)+1 (recurrence). Closed form: H(n) = 2 1.

Recurrence relations Recurrence: an equation that defines an element in a sequence in terms of one or more of previous terms. H(n) = 2H(n-1)+1 F(n) = F(n-1)+F(n-2) T(n) = at(n-1) A closed form of a recurrence relation is an expression that defines an element in a sequence in terms of directly. Often use recurrence relations and their closed forms to describe performance of (especially recursive) algorithms.

Recursive definitions of sets So far, we talked about recursive definitions of sequences. We can also give recursive definitions of sets. E.g: recursive definition of a set S = 0,1 Basis: empty string is in S. Recursive step: if, then 0 and 1 Here, 0 means string w with 0 appended at the end; same for w1

Recursive definitions of sets Recursive definition of a set S = 0,1 Alternatively: Basis: empty string, 0 and 1 are in S. Recursive step: if s and tare in S, then st here, stis concatenation: symbols of s followed by symbols of t If s = 101 and t= 0011, then st= 1010011 Additionally, need a restriction condition: the set S contains only elements produced from basis using recursive step rule.

Trees In computer science, a tree is an undirected graph without cycles Undirected: all edges go both ways, no arrows. Cycle: sequence of edges going back to the same point. 2 1 3 Undirected cycle (not a tree)

Trees Recursive definition of trees: Base: A single vertex is a tree. Recursion: Let be a tree, and a new vertex. Then a new tree consist of,, and an edge (connection) between some vertex of and. Restriction: Anything that cannot be constructed with this rule from this base is not a tree.

Arithmetic expressions Suppose you are writing a piece of code that takes an arithmetic expression and, say evaluates it. 5*3-1, 40-(x+1)*7, etc How to describe a valid arithmetic expression?

Arithmetic expressions How to describe a valid arithmetic expression? Define a set of all valid arithmetic expressions recursively. Base: A number or a variable is a valid arithmetic expression. 5, 100, x, a Recursion: If A and B are valid arithmetic expressions, then so are (A),,,, / B. Constructing 40-(x+1)*7: first construct 40, x, 1, 7. Then (x+1). Then (x+1)*7, finally 40-(x+1)*7 Caveat: how do we know the order of evaluation? On that later. Restriction: nothing else is a valid arithmetic expression.

Formulas What is a well-formed propositional logic formula? "# % " % Base: a propositional variable,#,% Or a constant ()*,+,* Recursion: If F and G are propositional formulas, so are +, "+, + -,+ -,+ -,+ -. And nothing else.

Formulas What is a well-formed predicate logic formula? 0 1 3 4 5 0,3 6 0,7 0 83 Base: a predicate with free variables P(x), x=y, Recursion: If F and G are predicate logic formulas, so are +, "+, + -,+ -,+ -,+ -. If + is a predicate logic formula with a free variable x, then 0 1 + and 0 1 + are predicate logic formulas. And nothing else. So 0 59: ;9,<9= 0,3 0,,<9= 3 0 is not a well-formed predicate logic formula!

Grammars A context-free grammar consists of A set V of variables (using capital letters) Including a start variable S. A set Σof terminals (disjoint from V; alphabet) A set R of rules, where each rule consists of a variable from V and a string of variables and terminals. If is a rule, we say variable yields string w. This is not the same " as implication, a different use of the same symbol. We use shortcut when the same variable might yield several possible strings: A Can use A again within the rule: Recursion! Different occurrences of the same variable can be interpreted as different strings. When left with just terminals, a string is derived.

Grammars A general recursive definition for these is called a grammar. In particular, here we have context-free grammars, where symbols have the same meaning wherever they are. A language generated by a grammar consists of all strings of terminals that can be derived from the start variable by applying the rules. All strings are derived by repeatedly applying the grammar rules to each variable until there are no variables left (just the terminals).

Examples of grammars Example: language {1, 00} consisting of two strings 1 and 00 1 00 Variables: S. Terminals: 1 and 00. Example: stringsover {0, 1} with all 0s before all 1s. 0 1 _ Variables: S. Terminals: 0 and 1.

Examples of grammars Example: propositional formulas. 1. + + + 2. + + + 3. + "+ 4. + + 5. + # % ()* +,* Here, the only variable is F (it is a start variable), and terminals are,,",,,,#,%,()*,+,* To obtain "# %, first apply rule 2, then rule 1, then rule 5 to get p, then rule 3, then rule 5 to get q, then rule 5 to get r.

Examples of grammars Example: arithmetic expressions *F5( *F5(*F5( *F5( *F5( *F5( *F5( *F5( / *F5( *F5( H)I*( -NUMBER NUMBER 01J-J 91J-J 1J-J _ H)I*( Here, _stands for empty string. Variables: EXPR, NUMBER, DIGITS (S is starting). Terminals: +,-,*, /, 0,,9. We used separate NUMBER to avoid multiple -. And separate DIGITS to have an empty string to finish writing a number, but to avoid an empty number.

Encoding order of precedence Easier to specify in which order to process parts of the formula. Better grammar for arithmetic expressions (for simplicity, with x,y,z instead of numbers): 1. *F5( *F5(*(I *F5( *(I *(I 2. *(I *(I +LM( *(I / +LM( +LM( 3. +LM( *F5( x y z Here, variables are EXPR, TERM and FACTOR (with EXPR a starting variable). Now can encode precedence. And put parentheses more sensibly.

Parse trees. Visualization of derivations: parse trees. 1. *F5( *F5( *(I *F5( *(I *(I 2. *(I *(I +LM( *(I / +LM( +LM( 3. +LM( *F5( x y z EXPR TERM TERM * FACTOR ( EXPR ) EXPR + TERM FACTOR z String (x+y)*z TERM FACTOR x FACTOR y ( x + y ) * z

Parse trees. Visualization of derivations: parse trees. Simpler example: 0 1 _ String 001 S S 0 S S 1 0 S 0 S S 1 _ 0 0 1 0 S _ 0 0 1

Puzzle Do the following two English sentences have the same parse trees? Time flies like an arrow. Fruit flies like an apple.

Structural induction Let ) be a recursively defined set, and F(x) is a property (of 0 )). Then if all 0 in the base of S have the property, and applying the recursion rules preserves the property, then all elements in S have the property.

Multiples of 3 Let s define a set S of numbers as follows. Base: 3 Recursion: if 0,3, then 03 Claim: all numbers in S are divisible by 3 That is, 0 7 O 0 837.

Multiples of 3 Proof (by structural induction). Base case: 3 is divisible by 3 (y=1). Recursion: Let 0,3. Then 7,P O 0 837 3 83P. Then 03 8373P 83 7P. Therefore, 03 is divisible by 3. As there are no other elements in S except for those constructed from 3 by the recursion rule, all elements in S are divisible by 3.

Binary trees Rooted trees are trees with a special vertex designated as a root. Rooted trees are binary if every vertex has at most three edges: one going towards the root, and two going away from the root. Full if every vertex has either 2 or 0 edges going away from the root.

Binary trees Recursivedefinition of full binary trees: Base: A single vertex is a full binary tree with that vertex as a root. Recursion: Let, be full binary trees with roots %,%, respectively. Let be a new vertex. Anew full binary tree with root is formed by connecting % and % to. Restriction: Anything that cannot be constructed with this rule from this base is not a full binary tree.

Height of a full binary tree The height of a rooted tree, h, is the maximum number of edges to get from any vertex to the root. Height of a tree with a single vertex is 0. Claim: Let () be the number of vertices in a full binary tree T. Then 2 S T 1 Height 2

Height of a full binary tree Proof (by structural induction) Base case: a tree with a single vertex has 81 and h 8 0. So 2 S T 1= 1 1 Recursion: Suppose was built by attaching, to a new root vertex. Number of vertices in is n T 8 1 Every vertex in or now has one extra step to get to the new root in. So h 81max (h,h ) By the induction hypothesis, 2 S [ T 1and 2 S \ T 1 n T 8 1 1(2 S [ T 1)+(2 S \ T 1) 2 max (2 S [ T, 2 S \ T ) 1 Height 2 2 2^_` S [, S \ T 1 82 2 S 182 S T 1 Therefore, the number of vertices of any binary tree is less than 2 S T 1

Height of a full binary tree Claim: Let () be the number of vertices in a full binary tree T. Then 2 S T 1 Alternatively, height of a binary tree is at least log () If you have a recursive program that calls itself twice (e.g, within if then else ) Then if this code executes n times (maybe on n different cases) Then the program runs in time at least log, even when cases are checked in parallel. Height 2