COMPUTER SCIENCE 20, SPRING 2014 Homework Problems Recursive Definitions, Structural Induction, States and Invariants

Similar documents
Structural Induction

Lecture 5: Tuesday, January 27, Peterson s Algorithm satisfies the No Starvation property (Theorem 1)

1 Solutions to Tute09

AVL Trees. The height of the left subtree can differ from the height of the right subtree by at most 1.

MSU CSE Spring 2011 Exam 2-ANSWERS

CSE 21 Winter 2016 Homework 6 Due: Wednesday, May 11, 2016 at 11:59pm. Instructions

Introduction to Greedy Algorithms: Huffman Codes

Practice Second Midterm Exam II

Notes on Natural Logic

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

SET 1C Binary Trees. 2. (i) Define the height of a binary tree or subtree and also define a height balanced (AVL) tree. (2)

Stanford University, CS 106X Homework Assignment 5: Priority Queue Binomial Heap Optional Extension

Homework #4. CMSC351 - Spring 2013 PRINT Name : Due: Thu Apr 16 th at the start of class

Decidability and Recursive Languages

Heap Building Bounds

CS360 Homework 14 Solution

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Lecture l(x) 1. (1) x X

On the Optimality of a Family of Binary Trees Techical Report TR

An effective perfect-set theorem

Design and Analysis of Algorithms

Initializing A Max Heap. Initializing A Max Heap

Binary and Binomial Heaps. Disclaimer: these slides were adapted from the ones by Kevin Wayne

Math 546 Homework Problems. Due Wednesday, January 25. This homework has two types of problems.

1 Binomial Tree. Structural Properties:

Algebra homework 8 Homomorphisms, isomorphisms

> asympt( ln( n! ), n ); n 360n n

The potential function φ for the amortized analysis of an operation on Fibonacci heap at time (iteration) i is given by the following equation:

Heaps. c P. Flener/IT Dept/Uppsala Univ. AD1, FP, PK II Heaps 1

Priority Queues. Fibonacci Heap

Algorithmic Game Theory and Applications. Lecture 11: Games of Perfect Information

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

Outline for this Week

NOTES ON FIBONACCI TREES AND THEIR OPTIMALITY* YASUICHI HORIBE INTRODUCTION 1. FIBONACCI TREES

Lecture 2: The Simple Story of 2-SAT

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Discrete Mathematics for CS Spring 2008 David Wagner Final Exam

*****CENTRAL LIMIT THEOREM (CLT)*****

IEOR E4004: Introduction to OR: Deterministic Models

Proof Techniques for Operational Semantics. Questions? Why Bother? Mathematical Induction Well-Founded Induction Structural Induction

Outline for this Week

LECTURE 2: MULTIPERIOD MODELS AND TREES

Design and Analysis of Algorithms 演算法設計與分析. Lecture 9 November 19, 2014 洪國寶

Mathematics Notes for Class 12 chapter 1. Relations and Functions

We begin, however, with the concept of prime factorization. Example: Determine the prime factorization of 12.

5 Deduction in First-Order Logic

A relation on 132-avoiding permutation patterns

COMP Analysis of Algorithms & Data Structures

CS 4110 Programming Languages & Logics. Lecture 2 Introduction to Semantics

Course Information and Introduction

The Binomial Theorem and Consequences

CMPSCI 311: Introduction to Algorithms Second Midterm Practice Exam SOLUTIONS

2 Deduction in Sentential Logic

Binary Tree Applications

Fibonacci Heaps Y Y o o u u c c an an s s u u b b m miitt P P ro ro b blle e m m S S et et 3 3 iin n t t h h e e b b o o x x u u p p fro fro n n tt..

Design and Analysis of Algorithms. Lecture 9 November 20, 2013 洪國寶

Tug of War Game. William Gasarch and Nick Sovich and Paul Zimand. October 6, Abstract

Optimal Satisficing Tree Searches

3/7/13. Binomial Tree. Binomial Tree. Binomial Tree. Binomial Tree. Number of nodes with respect to k? N(B o ) = 1 N(B k ) = 2 N(B k-1 ) = 2 k

Fibonacci Heaps CLRS: Chapter 20 Last Revision: 21/09/04

MAT385 Final (Spring 2009): Boolean Algebras, FSM, and old stuff

Q1. [?? pts] Search Traces

On the Optimality of a Family of Binary Trees

arxiv: v1 [math.co] 31 Mar 2009

Fundamental Algorithms - Surprise Test

Sequential allocation of indivisible goods

1.6 Heap ordered trees

Lecture 14: Basic Fixpoint Theorems (cont.)

Data Structures. Binomial Heaps Fibonacci Heaps. Haim Kaplan & Uri Zwick December 2013

The exam is closed book, closed calculator, and closed notes except your three crib sheets.

CS792 Notes Henkin Models, Soundness and Completeness

Basic Data Structures. Figure 8.1 Lists, stacks, and queues. Terminology for Stacks. Terminology for Lists. Chapter 8: Data Abstractions

Ch 10 Trees. Introduction to Trees. Tree Representations. Binary Tree Nodes. Tree Traversals. Binary Search Trees

The Traveling Salesman Problem. Time Complexity under Nondeterminism. A Nondeterministic Algorithm for tsp (d)

TR : Knowledge-Based Rational Decisions and Nash Paths

CSCI 104 B-Trees (2-3, 2-3-4) and Red/Black Trees. Mark Redekopp David Kempe

The Turing Definability of the Relation of Computably Enumerable In. S. Barry Cooper

Algebra and Number Theory Exercise Set

Realizability of n-vertex Graphs with Prescribed Vertex Connectivity, Edge Connectivity, Minimum Degree, and Maximum Degree

Equational reasoning. Equational reasoning. Equational reasoning. EDAN40: Functional Programming On Program Verification

PRIORITY QUEUES. binary heaps d-ary heaps binomial heaps Fibonacci heaps. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley

TABLEAU-BASED DECISION PROCEDURES FOR HYBRID LOGIC

Binomial Coefficient

Design and Analysis of Algorithms 演算法設計與分析. Lecture 8 November 16, 2016 洪國寶

Algorithms PRIORITY QUEUES. binary heaps d-ary heaps binomial heaps Fibonacci heaps. binary heaps d-ary heaps binomial heaps Fibonacci heaps

Problem Set 2: Answers

Swaps and Inversions

Gödel algebras free over finite distributive lattices

Arborescent Architecture for Decentralized Supervisory Control of Discrete Event Systems

Microeconomics of Banking: Lecture 5

A Testing Read-Once Formula Satisfaction

June 11, Dynamic Programming( Weighted Interval Scheduling)

Sublinear Time Algorithms Oct 19, Lecture 1

The Real Numbers. Here we show one way to explicitly construct the real numbers R. First we need a definition.

Priority Queues 9/10. Binary heaps Leftist heaps Binomial heaps Fibonacci heaps

Experimental Mathematics with Python and Sage

FDPE Microeconomics 3 Spring 2017 Pauli Murto TA: Tsz-Ning Wong (These solution hints are based on Julia Salmi s solution hints for Spring 2015.

Abstract stack machines for LL and LR parsing

Syllogistic Logics with Verbs

Data Structures, Algorithms, & Applications in C++ ( Chapter 9 )

Transcription:

COMPUTER SCIENCE 20, SPRING 2014 Homework Problems Recursive Definitions, Structural Induction, States and Invariants Due Wednesday March 12, 2014. CS 20 students should bring a hard copy to class. CSCI E-120 students should submit an electronic copy. Note: Use the following definition of the set of valid integer binary trees T. Base Cases: ɛ T Constructor Case: If x Z, l T, r T, then (x, l, r) T A node of a tree is any element in T that is a three-tuple, that is, it is of the form (x, l, r). This means that ɛ T is not a node. If z = (x, l, r) is a node, then l and r are child nodes of z, if they are non-empty. 1. The nodes in a tree obey the heap property if, for every node z in the tree, the value in z, that is x, is at least as big as the value in each of z s children (the tree in module 15 obeys the heap property). Prove that if a binary tree has the heap property, then the value in the root of the tree is at least as large as the value in any node of the tree. Proof by induction: Base Case: A tree of size 1, for which this property is trivially true. (It is also possible to induct on the empty tree) Inductive Hypothesis: For all trees with n nodes in it, the claim in question holds. Now we want to show that the claim holds for a tree T of size n+1 that has the heap property. Any such tree can be written as (x, l, r) by definition, with x being the root node. For the rest of this proof, we are assuming l and r are non-empty (if one or both is empty, we may ignore them as then there are no values to compare our root node to). Since T has the heap property, the trees l and r must also have the heap property. We also know that l and r are both at most size n, since T itself is only size n + 1. 1

By the inductive hypothesis, we know that the root nodes l 0 and r 0 are at least as large as any other nodes in l and r respectively. Then by the heap property, x l 0 and x r 0, so x must also be at least as large as any other nodes in l and r. Thus the root of the tree T is at least as large as any value in any of its nodes. 2. A complete binary tree (example below) is a binary tree in which every level, except possibly the last level, is completely filled, and all nodes are as far left as possible. Also, remember that an internal node is a node that has children. Prove that the number of internal nodes in a complete binary tree with n nodes is n/2 where x equals the largest integer that is not greater than x. Base case: A tree of size 1, for which there are 1/2 = 0 internal nodes. Inductive Hypothesis: A complete binary tree with n nodes has n/2 internal nodes. We will consider two cases: n is odd and n is even. First we shall prove that n is odd if and only if every node has either 0 or 2 children, and n is even iff there is one node with one child. Note that in a complete binary tree, there can be at most one node with only one child. Then, proof: Suppose our tree s nodes each have either 0 or 2 children. We can count the number of nodes in a tree by starting at the root and adding in its number of children, and then proceeding as such with each of those children. However, since every node can only have 2 or 0 children, we will only ever add 2 or 0 to our initial root count of 1, and so n is odd. If the tree has one node with one child, then we can apply the same process, but at one point we will only add 1 node, and so n will be even. Now consider the two cases n odd and n even: n is odd. Since every node in the tree with n nodes has 0 or 2 children, the complete tree of size n + 1 will create a node with 1 child, creating 2

one more internal node. Since by the inductive hypothesis a tree of size n has n/2 = (n 1)/2 internal nodes for odd n, the tree of size n + 1 will have (n 1)/2 + 1 = (n + 1)/2 = (n + 1)/2 internal nodes, as n + 1 is even. n is even. Then the complete tree of size n + 1 will add on another child to the node that only has 1 child, so no new internal nodes are created. Then it will still have n/2 internal nodes by the inductive hypothesis. However, this is equal to (n + 1)/2 for even n, which is our desired result. Since we have showed the tree of size n + 1 has (n + 1)/2 internal nodes, we see by induction that the claim is true for all n. 3. (From Meyer, problem 6.6) Let m, n Z where m 0 and n 0. Then, let s define a set of integers, L m,n, recursively as follows: Base cases: m, n L m,n Constructor cases: If j, k L m,n, then (a) j L m,n. and (b) j + k L m,n Let L be an abbreviation for L m,n for the rest of this problem. (a) Prove by structural induction that every common divisor of m and n also divides every member of L. (b) Prove that any integer multiple of an element of L is also in L. (c) Show that if j, k L and k 0 then the remainder of j divided by k is also in L; that is, that rem(j, k) L. (a) Proof by induction. Base case: m, n L m,n. m and n are obviously both divisible by all of their common divisors. For the induction, assume all current members of L m,n are divisible by all common divisors of m and n, and then show that for any new element we can add, this property still holds. The constructor cases are then: j for j L m,n. j is divisible by anything j is. By the inductive hypothesis, j is divisible by all common divisors of m and n, and so j is as well. 3

j + k for j, k L m,n. For any common divisor x of m and n, we can write j = ax, k = bx for some integers a, b, since j and k are both divisible by x. Then j +k = x(a+b), and so j +k is divisible by x as well. This holds for any common divisor x, so j + k is divisible by any common divisor of m and n Thus any element of L m,n is divisible by all common divisors of m and n. (b) Consider any element x L. We wish to show that for any integer a, ax L. We can do a proof by induction here: Base case: x, x L. This can also be written as 1 x and 1 x are in L. Inductive hypothesis: nx L for some integer n. We then see (n 1)x = nx + ( x) and (n + 1)x = nx + x. Since nx, x, x L by the inductive hypothesis and base case, it follows that (n 1)x, (n + 1)x L. We can reach every integer multiple of x in this fashion, and so the claim holds. (c) The value rem(j, k) is defined such that j = nk + rem(j, k) for some integer n such that 0 < rem(j, k) < k. We already showed in part b that nk L if k L, and so j nk L by the second constructor case. This is equal to rem(j, k), and so rem(j, k) L. 4. (From Sipser, exercise 1.6) Draw state machines that only accept strings in the following set. Assume that the alphabet is Σ = {0, 1}; that is, that all strings s Σ. (Bonus point(s) if you draw your raw state machines in LaTeX; check out the TikZ package to do this.) {w : w contains the substring 0101, i.e. w = x0101y for some x and y} 4

Note: this is a nondeterministic machine, meaning that it accepts a string as long as there exists a path from the start state to the accept state. The arrow indicates the start state, and the double-circles indicate the accept state. 5. (From Meyer, problem 5.29) A robot named Wall-E wanders around a twodimensional grid. He starts at (0,0) and is allowed to take four different types of steps: (a) (+2,-1) (b) (+1, -2) (c) (+1, +1) (d) (-3, 0) For example, Wall-E might take the following stroll. The types of his steps are denoted by each arrow s subscript: (0, 0) a (2, 1) c (3, 0) b (4, 2) d (1, 2)... Wall-E s true love, the fashionable and high-powered robot, Eve, awaits at (0,2). (a) Describe a state machine model of this problem. (b) Will Wall-E ever find his true love? Either find a path from Wall-E to Eve or use the Invariant Principle to prove that no such path exists. (a) In our state machine model, every state takes the form (x, y). The start state is (0, 0), and the possible transitions out of a state (x, y) are to (x + 2, y 1), (x + 1, y 2), (x + 1, y + 1), (x 3, y). (b) He will not. Consider if we take all of the possible states and take the x and y coordinates mod 3. Then the possible transitions are equivalent to: i. (+2, 1) (+2, +2) (mod 3) ii. (+1, 2) (+1, +1) (mod 3) iii. (+1, +1) (+1, +1) (mod 3) iv. ( 3, +0) (+0, +0) (mod 3) We then propose the following invariant: for any state (x, y) x mod 3 = y mod 3 We see that it is true for the start state (0, 0), and all of the transitions preserve this invariant, as they add the same amount to both the x and y coordinates mod 3. However, our desired state (0, 2) does not satisfy this invariant, and so it must be impossible to reach. 5