Lecture 7: Trees and Structural Induction

Similar documents
Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

6.841/18.405J: Advanced Complexity Wednesday, February 12, Lecture Lecture 3

Lecture 16. Error-free variable length schemes (contd.): Shannon-Fano-Elias code, Huffman code

CS 395T Computational Learning Theory. Scribe: Mike Halcrow. x 4. x 2. x 6

This lecture covers Chapter 5 of HMU: Context-free Grammars

5.3 Conditional Probability and Independence

Models of Computation,

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

This lecture covers Chapter 7 of HMU: Properties of CFLs

Context Free Language Properties

HW 4 SOLUTIONS. , x + x x 1 ) 2

Dictionary: an abstract data type

Chapter 1. Logic and Proof

CSC236H Lecture 2. Ilir Dema. September 19, 2018

Mathematical Preliminaries. Sipser pages 1-28

CMPT 710/407 - Complexity Theory Lecture 4: Complexity Classes, Completeness, Linear Speedup, and Hierarchy Theorems

Lecture 1: September 25, A quick reminder about random variables and convexity

Lecture 2: Proof of Switching Lemma

CS 124 Math Review Section January 29, 2018

2. Syntactic Congruences and Monoids

Lecture 1 : Data Compression and Entropy

Lecture Notes on From Rules to Propositions

Nearest Neighbor Search with Keywords: Compression

Section 3.2 Recursively Defined Functions and Procedures

Information Theory and Statistics Lecture 2: Source coding

Further discussion of Turing machines

Chapter 5: Context-Free Languages

CS1800: Mathematical Induction. Professor Kevin Gold

The semantics of propositional logic

Lecture 21: Algebraic Computation Models

Discrete Structures for Computer Science

A note on monotone real circuits

1 More finite deterministic automata

Math 1313 Experiments, Events and Sample Spaces

Suppose h maps number and variables to ɛ, and opening parenthesis to 0 and closing parenthesis

Chapter 5 The Witness Reduction Technique

DRAFT. Algebraic computation models. Chapter 14

Chapter 2 Propositional Logic: Formulas, Models, Tableaux

Discrete Mathematics

Seminaar Abstrakte Wiskunde Seminar in Abstract Mathematics Lecture notes in progress (27 March 2010)

Proofs, Strings, and Finite Automata. CS154 Chris Pollett Feb 5, 2007.

Structural Induction

Turing machine recap. Universal Turing Machines and Undecidability. Alphabet size doesn t matter. Robustness of TM

CS4800: Algorithms & Data Jonathan Ullman

CONSTRUCTION OF THE REAL NUMBERS.

1. Prove: A full m- ary tree with i internal vertices contains n = mi + 1 vertices.

6.5.3 An NP-complete domino game

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

Binary Decision Diagrams. Graphs. Boolean Functions

Computational and Statistical Learning theory

Mathematical Induction

INDUCTIVE DEFINITION

Equational Logic. Chapter Syntax Terms and Term Algebras

Deterministic Finite Automata (DFAs)

CIS 500: Software Foundations

Deterministic Finite Automata (DFAs)

Lecture 4: Proposition, Connectives and Truth Tables

Theory of Computation

Discrete Mathematics for CS Spring 2006 Vazirani Lecture 22

Nondeterministic Finite Automata

CS Introduction to Complexity Theory. Lecture #11: Dec 8th, 2015

10-704: Information Processing and Learning Fall Lecture 10: Oct 3

Notes on ordinals and cardinals

Comp487/587 - Boolean Formulas

6.1 The Pumping Lemma for CFLs 6.2 Intersections and Complements of CFLs

Classes of Boolean Functions

CSE200: Computability and complexity Space Complexity

Theory of Computation

Notes on induction proofs and recursive definitions

Context-Free Languages

Predicate Logic - Introduction

Properties of Context-Free Languages

Lecture 24: Randomized Complexity, Course Summary

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Lecture 23 : Nondeterministic Finite Automata DRAFT Connection between Regular Expressions and Finite Automata

(Ordinary) mathematical induction is a way to prove things about natural numbers. We can write it as a rule: φ(0) x. φ(x) φ(succ(x)) x.

Space and Nondeterminism

Adam Blank Spring 2017 CSE 311. Foundations of Computing I

Lecture 2: Connecting the Three Models

Ramanujan Graphs of Every Size

Proof Techniques (Review of Math 271)

1 Extensive Form Games

Data selection. Lower complexity bound for sorting

Non-context-Free Languages. CS215, Lecture 5 c

The exam is closed book, closed calculator, and closed notes except your one-page crib sheet.

Lecture 4 : Quest for Structure in Counting Problems

1 Introduction (January 21)

CSE 200 Lecture Notes Turing machine vs. RAM machine vs. circuits

CVO103: Programming Languages. Lecture 2 Inductive Definitions (2)

Incomplete version for students of easllc2012 only. 6.6 The Model Existence Game 99

T (s, xa) = T (T (s, x), a). The language recognized by M, denoted L(M), is the set of strings accepted by M. That is,

Boolean circuits. Lecture Definitions

Deterministic Finite Automata (DFAs)

SOME TRANSFINITE INDUCTION DEDUCTIONS

Introduction to Tree Logics

Lecture 14: IP = PSPACE

MATH 51H Section 4. October 16, Recall what it means for a function between metric spaces to be continuous:

CS60007 Algorithm Design and Analysis 2018 Assignment 1

SIGNAL COMPRESSION Lecture 7. Variable to Fix Encoding

Transcription:

Lecture 7: Trees Structural Induction Natural numbers, positive numbers, rational numbers, pairs, tuples etc. are the objects that we usually study in discrete math. But when we consider discrete math in terms of information technology, we find many more structure in information then just numbers. For example, we can find information that is captured in lists. Or trees, for this matter. In an object oriented language, classes are organized in form of inheritance trees, there are decisions trees, or game trees. HTML, XML, the like are trees that describe documents. Trees are therefore interesting important. We will need to learn more about them. Let s spend some time understing lists. Lists are di erent form sets. They are ordered. Elements can appear several times in a list, but they only appear one time in a list. We use the :: notation for lists. For example 2::3::5::7::11::nil is a list. We can think of a list as being constructed from a head 2, the rest of the list 5::7::11::nil. Sometimes, I will just write [2, 3, 5, 7, 11] as an abbreviation. Recall that we introduced the principle of induction for natural number: During a previous lecture, we convinced ourselves that if P (0) 8n 2 IN. P (n)! P (n + 1) 8n 2 N.P(n) Lists are constructed just like natural numbers. Natural numbers are constructed by 0 the successor operation, lists are constructed by nil the :: (over elements from some set S). By analogy we can come up with an induction principle for lists. If P (nil) 8x 2 S.8l 2 IL. P (l)! P (x :: l) 8l 2 IL. P (l) Let s demonstrate how to use this by the means of an example. From last lecture, we had some examples already, lists of coins, like nickels, dimes, quarters. Or bits, such as 0s 1s. Definition 34 Let IB be the set of all finite binary strings, which are lists of the set 0, 1. For example 0 :: 0 :: 1 :: 0 :: 1 :: 0 :: nil 2 IB,1::1::0::1::0::nil 2 IB, 1::0::0::1::nil 2 IB. Of course, we prefer the less verbose form 001010 2 IB, 11010 2 IB, 1001 2 IB, which we will use hence force. 35

We can define a simple functions on lists, for example: l 1 @l 2 = l2 if l 1 =nil x::(l 0 1@l 2 ) if l 1 = x::l 0 1 And we may now apply the induction principle to show properties about lists. Lemma 35 (Appending an empty list from the right) For all list l 2 IL, the following holds: l@nil = l Proof: by induction on the structure of l. Case: l = nil. The claim follows directly from the definition of @. Case: l = x::l 0. Let l 0 be arbitrary fixed. Assume l 0 @nil = l 0 the induction hypothesis (x::l 0 )@nil = x::(l 0 @nil) by definition of @ = x::l 0 by induction hypothesis Theorem 36 (Append is associative) l 1 @(l 2 @l 3 )=(l 1 @l 2 )@l 3 Proof: l 1 = nil. nil@l 2 = l 2 nil@(l 2 @l 3 )=l 2 @l 3 nil@(l 2 @l 3 )=l 2 @l 3 =(nil@l 2 )@l 3 by definition of append by definition of append by equality reasoning Case Let l 0 1 be arbitrary but fixed Assume l 1 = X :: l 0 1 Assume l 0 1@(l 2 @l 3 )=(l 0 1@l 2 )@l 3 (X :: l 0 1)@(l 2 @l 3 ) = X :: (l 0 1@(l 2 @l 3 )) = X :: ((l 0 1@l 2 )@l 3 ) =(X :: (l 0 1@l 2 ))@l 3 =((X :: l 0 1)@l 2 )@l 3 36

Here is another common operation on lists: list reversal. rev(l) = rev (l, nil) where rev (l 1,l 2 ) = l2 if l 1 =nil rev (l 0 1,x::l 2 ))) if l 1 = x::l 0 1 Can we prove mathematically, that the reverse of the reverse of a list is the list itself? Conjecture 37 For all l 2 IL,itholdsthatrev(rev(l)) = l. This however, we cannot prove directly. We need to first develop a lemma, stating a useful property about rev, which is on the one h true, on the other necessary to prove the conjecture above. Lemma 38 For all l 1,l 2,l 3 2 IL,itholdsthatrev (rev (l 1,l 2 ),l 3 )=rev (l 2,l 1 @l 3 ). Proof: by structure induction on l 1 Case: l 1 =nil. rev (rev (nil,l 2 ),l 3 ) =rev (l 2,l 3 ) =rev (l 2, nil@l 3 ) by definition of @ Case: l 1 = x::l 0 1. Assume forall l 2 2 IL it holds that rev (rev (l1,l 0 2 ),l 3 )=rev (l 2,l1@l 0 3 ) by induction hypothesis rev (rev (x::l1,l 0 2 ),l 3 ) rev (rev (l1,x::l 0 2 ),l 3 ) rev (x::l 2,l1@l 0 3 ) by induction hypothesis rev (l 2,x::(l1@l 0 3 )) rev (l 2, (x::l1)@l 0 3 )) by definition of @ Now we can finish the proof the conjecture: Proof: rev(rev(l)) = rev(rev (l, nil)) by definition of rev = rev (rev (l,nil), nil) by definition of rev = rev (nil,l@nil) by the above lemma = l@nil = l by Lemma 35 37

No information TT 0 Information on in the leaves TT 1 Information on in the nodes TT 2 Information on in the leaves nodes TT 3 Figure 2: Sets of trees Let s look at another example of structures, which are a little bit more complicated than lists: Trees. Trees are constructed very similar to lists, except that the :: symbol plays now the role of an internal node that can have one or more children. If all nodes in a tree have the same number of children, let s say m, then we call the tree an m-ary tree. In this sense, a list is simply a unary tree, where each node has only one child, namely the tail of the list. The leaf of a tree corresponds to the nil. A tree is a binary tree, if every node has exactly two children. For the purpose of this lecture, we consider only binary trees, but all results generalize directly to more general trees. Trees exhibit more structure than lists which allows us to represent store information in various places in a tree. For example, considering a tree of natural numbers, we can choose to store numbers as part of the nodes, within the leaves, or both, or none. We introduce the abbreviations for the sets of trees depicted in Figure 2. Trees also give raise to induction principles. Here is the induction principle for trees in TT 3 :If 8x 2 S.P (leaf(x)) 8x 2 S.8t 1 2 TT 3.8t 2 2 TT 3.P (t 1 ) ^ P (t 2 )! P (node(x, t 1,t 2 )) 8t 2 TT 3.P (t) If we consider only trees with information at the leaves, we obtain the following principles for TT 1.If 8x 2 S.P (leaf(x)) 8t 1 2 TT 1.8t 2 2 TT 1.P (t 1 ) ^ P (t 2 )! P (node(t 1,t 2 )) 8t 2 TT 1.P (t) Exercise 3 Write out the induction principles for TT 0 TT 2. 38

With these induction principles, we can prove many interesting things. Actually, the induction principles on trees will For example, consider the mirror operation: leaf(x) mirror(t) = node(mirror(t 1 ), mirror(t 2 )) if t =node(t 1,t 2 ) Now we can prove Theorem 39 For all trees t 2 TT 1 : mirror(mirror(t)) = t. Proof: by induction on t, which means it is a TT 1 induction. Case: t = leaf(x) mirror(mirror(leaf(x))) = leaf(x) by definition of mirror, apply twice. Case: t =node(t 1,t 2 ) Assume t 1,t 2 arbitrary but fixed trees Assume mirror(mirror(t 1 )) = t 1 Assume mirror(mirror(t 2 )) = t 2 mirror(mirror(node(t 1,t 2 ))) = mirror(node(mirror(t 2 ), mirror(t 1 ))) = node(mirror(mirror(t 1 )), mirror(mirror(t 2 ))) =node(t 1,t 2 ) as induction hypothesis as induction hypothesis by equality reasoning Besides mirroring, there are some other very useful operations on trees for example, converting a tree into a list by traversing the tree in preorder, i.e. depth first left to right manner. Let s consider TT 3 trees for this: preorder(t) = x::(preorder(t 1 )@preorder(t 2 )) if t =node(t 1,t 2 ) inorder(t) = inorder(t 1 )@(x::inorder(t 2 )) if t =node(t 1,t 2 ) postorder(t) = postorder(t 1 )@(postorder(t 2 )) if t =node(t 1,t 2 )@(x::nil) Exercise 4 Show, using the definition or list reversal that preorder(t) = rev(postorder(t)). 39