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

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

Recursively Defined Functions

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

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

5.3 Recursive definitions and structural induction

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

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

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

Recursive Definitions and Structural Induction

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Range Searching and Windowing

Inference rule for Induction

Announcements. CS243: Discrete Structures. Strong Induction and Recursively Defined Structures. Review. Example (review) Example (review), cont.

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

Recursive definition of sets and structural induction

Mathematical Induction

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

1 Variations of the Traveling Salesman Problem

Recursion and Structural Induction

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

CSC236H Lecture 5. October 17, 2018

Analyzing Complexity of Lists

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

Advanced Induction. Drawing hands by Escher. Discrete Structures (CS 173) Madhusudan Parthasarathy, University of Illinois 1

CHAPTER 10 GRAPHS AND TREES. Alessandro Artale UniBZ - artale/

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

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Lecture 6: Analysis of Algorithms (CS )

CS 220: Discrete Structures and their Applications. Loop Invariants Chapter 3 in zybooks

Rank-Pairing Heaps. Bernard Haeupler Siddhartha Sen Robert E. Tarjan. SIAM JOURNAL ON COMPUTING Vol. 40, No. 6 (2011), pp.

CSE 331: Introduction to Algorithm Analysis and Design Graphs

CS402 - Theory of Automata Glossary By

Greedy algorithms. Given a problem, how do we design an algorithm that solves the problem? There are several strategies:

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

Indicate the option which most accurately completes the sentence.

Recall our recursive multiply algorithm:

Ma/CS 6b Class 5: Graph Connectivity

CS 3343 Fall 2007 Red-black trees Carola Wenk

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

Slides for Faculty Oxford University Press All rights reserved.

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

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

Figure 4.1: The evolution of a rooted tree.

CS250: Discrete Math for Computer Science. L20: Complete Induction and Proof of Euler s Characterization of Eulerian-Walks

Lecture 5: Formation Tree and Parsing Algorithm

CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon

Binary Search Trees, etc.

Orthogonal range searching. Range Trees. Orthogonal range searching. 1D range searching. CS Spring 2009

CS 310 Advanced Data Structures and Algorithms

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

CSE 20 DISCRETE MATH WINTER

Binary Trees. Recursive definition. Is this a binary tree?

Discrete Structures. Fall Homework3

Trees. Arash Rafiey. 20 October, 2015

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

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

Recursion I and II. Discrete Structures (CS 173) Madhusudan Parthasarathy, University of Illinois 1

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

NOTE ON MINIMALLY k-connected GRAPHS

Definition of Graphs and Trees. Representation of Trees.

Graph Definitions. In a directed graph the edges have directions (ordered pairs). A weighted graph includes a weight function.

Scheduling, Map Coloring, and Graph Coloring

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

Trees and Inductive Definitions

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

CS256 Applied Theory of Computation

Graph Algorithms. Chromatic Polynomials. Graph Algorithms

Lecture: Analysis of Algorithms (CS )

CSC236 Week 5. Larry Zhang

Computational Geometry

Lecture 1. 1 Notation

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

Analysis of Algorithms

March 20/2003 Jayakanth Srinivasan,

Copyright 2000, Kevin Wayne 1

Mathematical Induction

Finite State Automata are Limited. Let us use (context-free) grammars!

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

Context-Free Grammars

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

memoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming

Recursion vs Induction

CS302 Data Structures using C++

Induction Review. Graphs. EECS 310: Discrete Math Lecture 5 Graph Theory, Matching. Common Graphs. a set of edges or collection of two-elt subsets

Today. Types of graphs. Complete Graphs. Trees. Hypercubes.

(Re)Introduction to Graphs and Some Algorithms

SKEW HEAPS: Self-Adjusting Heaps

CSE Qualifying Exam, Spring February 2, 2008

CSC 1052 Algorithms & Data Structures II: Recursion

Paths, Flowers and Vertex Cover

CS521 \ Notes for the Final Exam

Discrete Mathematics for CS Spring 2008 David Wagner Note 13. An Introduction to Graphs

CS200: Recursion and induction (recap from cs161)

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

Computational Geometry. Lecture 17

Reasoning about programs

Transcription:

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

Using recursion to define objects We can use recursion to define functions: The factorial function can be defined as: n! = 1 for n = 0; otherwise n! = n (n - 1)! This gives us a way of computing the function for any value of n.

factorial This is all we need to put together the function: def factorial(n): # precondition: n is an integer >= 0 if (n == 0) return 1 else : return n * factorial(n-1);

recursive sets Some sets are most naturally specified by recursive definitions. A recursive definition of a set shows how to construct elements in the set by putting together simpler elements. Example: balanced parentheses (()(())) is balanced ()) and ())()(() are not

recursive sets Some sets are most naturally specified by recursive definitions. A recursive definition of a set shows how to construct elements in the set by putting together simpler elements. Example: balanced parentheses Basis: The sequence () is properly nested. Recursive rules: If u and v are properly-nested sequences of parentheses then: 1 (u) is properly nested. 2 uv is properly nested.

recursive sets Example: balanced parentheses Basis: The sequence () is properly nested. Recursive rules: If u and v are properly-nested sequences of parentheses then: 1 (u) is properly nested. 2 uv is properly nested.

balanced parentheses Basis: The sequence () is properly nested. Recursive rules: If u and v are properly-nested sequences of parentheses then: 1 (u) is properly nested. 2 uv is properly nested. Is there a unique way to construct ()()()?

recursive sets Some sets are most naturally specified with recursive definitions. A recursive definition of a set shows how to construct elements in the set by putting together simpler elements. The basis explicitly states that one or more specific elements are in the set. Recursive rules show how to construct more complex elements from elements already known to be in the set.

binary strings Let B = {0, 1} B k : the set of binary strings of length k: {0, 1} k The empty string: λ B 0 = {λ} The set of all binary strings: B = B 0 [ B 1 [ B 2...

binary strings The set of all binary strings: B = B 0 [ B 1 [ B 2... This set can be defined recursively: Base case: λ B* Recursive rule: if x B* then ü x0 B* ü x1 B*

strings Example: length of a string Let Σ be an alphabet The length of a string over the alphabet Σ can be defined recursively: l(λ) = 0 l(wx) = l(w) + 1 if w Σ* and x Σ

perfect binary trees Basis: A single vertex with no edges is a perfect binary tree. Recursive rule: If T is a perfect binary tree, a new perfect binary tree T' can be constructed by taking two copies of T, adding a new vertex v and adding edges between v and the roots of each copy of T. The new vertex v is the root of T'.

perfect binary trees Basis: A single vertex with no edges is a perfect binary tree. Recursive rule: If T is a perfect binary tree, a new perfect binary tree T' can be constructed by taking two copies of T, adding a new vertex v and adding edges between v and the roots of each copy of T. The new vertex v is the root of T'.

structural induction We can use induction to prove properties of recursively defined objects. This is called structural induction. As an example, we'll prove the following: Theorem: Properly nested strings of left and right parentheses are balanced. A string of parentheses x is called balanced if left[x] = right[x], where left[x] (right[x]) is the number of left (right) parentheses in x. In fact, they are more than balanced (as defined above).

structural induction Theorem: Properly nested strings of left and right parentheses are balanced. Proof. By induction. Base case: () is properly nested. left[ () ] = right[ () ] = 1.

structural induction Inductive step: If x is a string of properly nested parentheses then x was constructed by applying a sequence of recursive rules starting with the string (). We consider two cases, depending on the last recursive rule that was applied to construct x. Case 1: Rule 1 is the last rule applied to construct x. Then x = (u), where u is properly nested. We assume that left[u] = right[u] and prove that left[x] = right[x]:

structural induction Inductive step: If x is a string of properly nested parentheses then x was constructed by applying a sequence of recursive rules starting with the string (). We consider two cases, depending on the last recursive rule that was applied to construct x. Case 2: rule 2 is the last rule applied to construct x. Then x = uv, where u and v are properly nested. We assume that left[u] = right[u] and left[v] = right[v] and then prove that left[x] = right[x]:

number of vertices in a perfect binary tree Theorem: Let T be a perfect binary tree. Then the number of vertices in T is 2 k - 1 for some positive integer k. v(t): the number of vertices in T

number of vertices in a perfect binary tree Theorem: Let T be a perfect binary tree. Then the number of vertices in T is 2 k - 1 for some positive integer k. Proof. By induction. Base case: the tree with one vertex has 2 1 1 = 1 leaves

number of vertices in a perfect binary tree Theorem: Let T be a perfect binary tree. Then the number of vertices in T is 2 k - 1 for some positive integer k. Inductive step: Let T' be a perfect binary tree. The last recursive rule that is applied to create T' takes a perfect binary tree T, duplicates T and adds a new vertex v with edges to each of the roots of the two copies of T. We assume that v(t) = 2 k - 1, for some positive integer k and prove that v(t') = 2 j - 1 for some positive integer j.

number of vertices in a perfect binary tree Theorem: Let T be a perfect binary tree. Then the number of vertices in T is 2 k - 1 for some positive integer k. The number of vertices in T' is twice the number of vertices in T (because of the two copies of T) plus 1 (because of the vertex v that is added), so v(t') = 2 v(t) + 1. By the inductive hypothesis, v(t) = 2 k - 1 for some positive integer k. Therefore v(t') = 2 v(t) + 1 = 2(2 k - 1) + 1 = 2 2 k - 2 + 1 = 2 k+1 1