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

Similar documents
1 Solutions to Tute09

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

2 all subsequent nodes. 252 all subsequent nodes. 401 all subsequent nodes. 398 all subsequent nodes. 330 all subsequent nodes

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

CMPSCI 311: Introduction to Algorithms Second Midterm Practice Exam SOLUTIONS

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

Structural Induction

CSCE 750, Fall 2009 Quizzes with Answers

Fundamental Algorithms - Surprise Test

Course Information and Introduction

A relation on 132-avoiding permutation patterns

COSC160: Data Structures Binary Trees. Jeremy Bolton, PhD Assistant Teaching Professor

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

On the Optimality of a Family of Binary Trees

Introduction to Greedy Algorithms: Huffman Codes

Recitation 1. Solving Recurrences. 1.1 Announcements. Welcome to 15210!

Sublinear Time Algorithms Oct 19, Lecture 1

Practical session No. 5 Trees

Successor. CS 361, Lecture 19. Tree-Successor. Outline

Maximum Contiguous Subsequences

Discrete Mathematics for CS Spring 2008 David Wagner Final Exam

UNIT VI TREES. Marks - 14

MAC Learning Objectives. Learning Objectives (Cont.)

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..

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

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

CSE 100: TREAPS AND RANDOMIZED SEARCH TREES

Binary Decision Diagrams

1 Binomial Tree. Structural Properties:

MSU CSE Spring 2011 Exam 2-ANSWERS

Design and Analysis of Algorithms

LECTURE 2: MULTIPERIOD MODELS AND TREES

Supporting Information

Binary Decision Diagrams

Practical session No. 5 Trees

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

Chapter 16. Binary Search Trees (BSTs)

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

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

Heaps

The Binomial Theorem and Consequences

Lecture 4: Divide and Conquer

Outline for this Week

Node betweenness centrality: the definition.

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

Heaps. Heap/Priority queue. Binomial heaps: Advanced Algorithmics (4AP) Heaps Binary heap. Binomial heap. Jaak Vilo 2009 Spring

Practical SAT Solving

Outline for this Week

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

COMP Analysis of Algorithms & Data Structures

Lecture 6. 1 Polynomial-time algorithms for the global min-cut problem

Chapter 15 - The Binomial Formula PART

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

UNIT 2. Greedy Method GENERAL METHOD

arxiv: v1 [math.co] 31 Mar 2009

CSE 417 Algorithms. Huffman Codes: An Optimal Data Compression Method

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

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

Lecture 2. Multinomial coefficients and more counting problems

Advanced Algorithmics (4AP) Heaps

ECE 586GT: Problem Set 1: Problems and Solutions Analysis of static games

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

Binomial Coefficient

PARELLIZATION OF DIJKSTRA S ALGORITHM: COMPARISON OF VARIOUS PRIORITY QUEUES

Practice Second Midterm Exam II

Mathematics Success Grade 8

Outline for Today. Quick refresher on binomial heaps and lazy binomial heaps. An important operation in many graph algorithms.

The following content is provided under a Creative Commons license. Your support

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

The Tree Data Model. Laura Kovács

Sequential allocation of indivisible goods

Notes on Natural Logic

Binary Tree Applications

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

Yao s Minimax Principle

Counting Basics. Venn diagrams

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

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

Week 3: Binomials Coefficients. 26 & 28 September MA204/MA284 : Discrete Mathematics. Niall Madden (and Emil Sköldberg)

Final Project. College Algebra. Upon successful completion of this course, the student will be able to:

Algorithms and Networking for Computer Games

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

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

Notes on the symmetric group

Data Structures and Algorithms February 10, 2007 Pennsylvania State University CSE 465 Professors Sofya Raskhodnikova & Adam Smith Handout 10

TEST 1 SOLUTIONS MATH 1002

Q1. [?? pts] Search Traces

CEC login. Student Details Name SOLUTIONS

Bioinformatics - Lecture 7

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

Lecture 10: The knapsack problem

CH 39 CREATING THE EQUATION OF A LINE

CTL Model Checking. Goal Method for proving M sat σ, where M is a Kripke structure and σ is a CTL formula. Approach Model checking!

Math 167: Mathematical Game Theory Instructor: Alpár R. Mészáros

1) S = {s}; 2) for each u V {s} do 3) dist[u] = cost(s, u); 4) Insert u into a 2-3 tree Q with dist[u] as the key; 5) for i = 1 to n 1 do 6) Identify

Math 14 Lecture Notes Ch The Normal Approximation to the Binomial Distribution. P (X ) = nc X p X q n X =

Algebra homework 8 Homomorphisms, isomorphisms

IEOR E4004: Introduction to OR: Deterministic Models

Microeconomics of Banking: Lecture 5

Notes on the EM Algorithm Michael Collins, September 24th 2005

Transcription:

CSE 1 Winter 016 Homework 6 Due: Wednesday, May 11, 016 at 11:59pm Instructions Homework should be done in groups of one to three people. You are free to change group members at any time throughout the quarter. Problems should be solved together, not divided up between partners. A single representative of your group should submit your work through Gradescope. Submissions must be received by 11:59pm on the due date, and there are no exceptions to this rule. Homework solutions should be neatly written or typed and turned in through Gradescope by 11:59pm on the due date. No late homeworks will be accepted for any reason. You will be able to look at your scanned work before submitting it. Please ensure that your submission is legible (neatly written and not too faint or your homework may not be graded. Students should consult their textbook, class notes, lecture slides, instructors, TAs, and tutors when they need help with homework. Students should not look for answers to homework problems in other texts or sources, including the internet. Only post about graded homework questions on Piazza if you suspect a typo in the assignment, or if you don t understand what the question is asking you to do. Other questions are best addressed in office hours. Your assignments in this class will be evaluated not only on the correctness of your answers, but on your ability to present your ideas clearly and logically. You should always explain how you arrived at your conclusions, using mathematically sound reasoning. Whether you use formal proof techniques or write a more informal argument for why something is true, your answers should always be well-supported. Your goal should be to convince the reader that your results and methods are sound. For questions that require pseudocode, you can follow the same format as the textbook, or you can write pseudocode in your own style, as long as you specify what your notation means. For example, are you using = to mean assignment or to check equality? You are welcome to use any algorithm from class as a subroutine in your pseudocode. For example, if you want to sort list A using InsertionSort, you can call InsertionSort(A instead of writing out the pseudocode for InsertionSort. Required Reading Rosen 10.4 through Theorem 1, 11.1, 11. through Theorem Key Concepts Rooted and unrooted trees, binary search trees, basic counting principles (sum and product rules, inclusion-exclusion, quotient (category rule, coding, coding 1

1. A binomial tree is a special kind of rooted tree used for various data structures in computer science. A degree d binomial tree can be defined recursively as follows. A degree 0 binomial tree is a single vertex with no edges. A degree d binomial tree has a root vertex with out-degree d. The first (that is, leftmost subtree is a degree d 1 binomial tree. The second (that is, second to left subtree is a degree d binomial tree. Continue on in this way so that the last (rightmost subtree is a degree 0 binomial tree. (a ( points Draw binomial trees of degree 1,, 3, and 4. Solution: Binomial trees of degrees 1 through 4 appear from left to right. (b (3 points What is the height h(d of a degree d binomial tree? Prove your result by induction on d. Solution: A binomial tree of degree d has a height of d. We prove this statement by strong induction. Base case: When d = 0 the tree is just one node, so it has a height of 0. Strong induction hypothesis: Suppose that for all degrees d less than or equal to some positive integer k, a binomial tree of degree d has a height of d. Suppose we have a binomial tree of degree k + 1. Its root has children of degrees k,k-1,... 0. By our induction hypothesis, the subtree rooted at the child of degree k has a height k (and this is the maximum height of all children subtrees. Thus the height of our binomial tree is k + 1. (c ( points Write a recurrence for the number of nodes n(d in a binomial tree of degree d. Solution: n(d = n(0 + n(1 + n( +... n(d 1 + 1; n(0 = 1 (d (3 points Use the guess-and-check method to guess a formula for n(d. Prove that your formula holds by induction on d. Solution: We guess that n(d = d and prove our guess by strong induction on d. Base case: When d = 0 the tree is just one node so n(0 = 1, which is equal to d = 0. Strong induction hypothesis: Suppose that for all degrees d less or equal to some positive integer k, we have that n(d = d. Proof of statement for k + 1 Suppose we have a binomial tree of degree k + 1. Its root has children of degrees k,k-1,... 0. As was given in the recurrence from part (c, the number of nodes in the whole tree is equal to the

number of nodes in all of the root s subtrees plus 1 (for the root. Using this recurrence along with our induction hypothesis we get: n(k + 1 = n(0 + n(1 + n( +... n(k + 1 = 0 + 1 + +... k + 1 (by IH = k+1. Give an algorithm that, given a vertex in a binary search tree x with associated value V (x, finds the successor of that value, the vertex y with the smallest value V (y with V (y > V (x. Your algorithm should run in time O(h, where h is the height of the binary search tree. ( points correct algorithm, points correctness proof, 1 point time analysis. Then use this algorithm to give another algorithm that given the root r of a binary search tree of size n and a number 1 k n, outputs a sorted list of the k smallest values in the tree. This algorithm should run in time O(hk. ( points correct algorithm, points correctness proof, 1 point time analysis. Say the succesor exists and is at vertex y. Consider the paths from the root to x and to y. At some point, these paths must differ. Since the value at y is greater than that of x, at the first difference, the path to y must end or go right, and the path to x must end or go left. If the path to y goes right, that to x must end, since otherwise the value of the vertex where x goes left and y goes right is intermediate between the two values. Then y is in the right sub-tree of x, and must have the smallest value in this subtree (since any smaller value in the subtree would be intermediate between x s and y s values. We can find y in this case as follows: Go to the right child of x (which must exist in this case. Then go left until the left child no longer exists. This will end at y. Since we only go down in the tree, this process will take no more time than the height of the tree. If the path to y ends, x must be in the left-subtree of y and must have the greatest value in this subtree, for any value in this sub-tree greater than x would be intermediate between x and y s values. If we follow the path from y to x, then, we must go left as the first step, and from then on, go right. So to find y, we follow the path starting at x to its ancestors, and the first step that went from a left-child must end at y. This method always goes up in the tree, so its time is at most O(h. If we don t know what case we were in, we could do both above and compare the results. But actually, the first case can only happen if x has a right child, and the second can only happen if x does not have a right child. So we could first look to see if x s right child pointer is null, and use that to decide whether to look up the tree or down the tree. If x has no right child, and we go all the way to the root without a left turn, then x had the largest value in the tree, and we output no successor exists. To get the smallest k elements using the above method, we can find the smallest element in the tree by going left until the left-child is null, and then using the above method k = 1 times to get the next smallest k 1 elements in order. Since finding the smallest and the successor are both O(h time, and we use it k times, the total time would be O(hk. However, this is too pessimistic (although it is full credit. The total time is actually the smaller of O(h + k and O(n, where n is the size of the tree. If you don t care about this, skip the rest. We can view the algorithm recursively as follows: InOrder (r: root; k: integer: returns the k smallest elements of the tree rooted at r, or all of them if k n. (a If r = NUL return the empty list. (b If k = 0 return the empty list (c If left(r = NUL, return r InOrder(right(r, k 1. (d L InOrder(left(r, k. 3

(e IF L = k return L (f IF L = k 1 return L r (g Return L r InOrder(right(r, k 1 L. We will prove the time analysis by strong induction on n. For constants c and d, the time is at most c min((h + k, n and at most dn. We can pick c to make the base case n = 1 true. For the induction step, consider running the algorithm to find the smallest k elements in a tree of height h and size n. Let n L and n R be the number of vertices in the left and right sub-trees. The total time for our algorithm is at most the time to make a recursive call to n L and to n R, plus a constant c. So the total time is at most cn L + cn R + c cn. So the time is at most linear. Also, if n L > k 1, we actually will only make one recursive call, since L will have at least k 1 values in line 4. Since the height of the left sub-tree is at most h 1. the total time will be at most c(k + h 1 + c c(h + k. Otherwise, the first call takes time at most cn L and will return all n L values in the left sub-tree. Then the second call will take time at most c(k n L 1 + c(h 1 and the total time will be at most cn L + c(k n L 1 + c(h 1 + c < c(k + h. So the algorithm is actually linear time in the greater of the height and the number of elements you return. A three coloring of a graph labels each vertex v with one of three colors, say R, B or G, so that the two endpoints of any edge have different colors. Consider an undirected graph which is a single path, i.e., where the vertices are v 1...v n, and there is an edge between each v i and v i+1 for i = 1..n 1. How many 3 colorings does this graph have? ( points correct answer. 1 point short explanation. How many bits are required to describe such a 3-coloring? ( points correct answer, 1 point short explanation Give coding and decoding algorithms that given the 3-coloring, outputs a string of the length above that codes it, and given the code, outputs the original 3-coloring. (3 points algorithm description, 1 point short explanation. We can choose any of the three colors for v 1. From then on, we must choose one of two colors for v i+1, either of the two colors not used for v i. This gives 3 n 1 possible colorings. To represent such a coloring, we will need log (3 n 1 bits, or after algebraic simplification occurs, + n 1 = n + 1. The reason above for the number of colors also inspires an efficient encoding. We give each color a two bit name, and use the first two bits to describe the color of v 1. Organize the colors in a triangle. If color of v i+1 is clockwise from that of v i, write 1 as the i + nd bit of the code, and otherwise write 0. To decode, we reverse the process. We use the first two bits to get the color of v 1, and then if the i + nd bit is 1, we give v i+1 the color that is clockwise from taht of v i, and else give it the one that is counter-clockwise. Since we are using bits for the first color and 1 bit each for the remaining n 1, the code has the optimal length of n + 1. 3. A normal deck of cards has, for each of 13 possible values (..10, J,K,Q,A, one card with that face value for each of the four suits (hearts, spades, diamonds, clubs. A can either be the highest or lowest value. Compute the number of five card hands (unordered sets of five distinct cards with the following properties ( points each, You can leave your answer in terms of factorials or binomial co-efficients. 1 point correct answer, 1 point explanation: (a The highest value card in the hand is 9 (and aces count higher than 9. The easiest solution is to subtract hands that have only...8 s from the number of hands that have only..9. I.e., remove from the number of hands with highest card at most 9, those that have highest value strictly less. There are 3 cards with values...9, and 8 with values...8. Since we are picking any five of them, the total is ( ( 3 5 = 8 (b Two pairs: there are two pairs with the same value, but no three have the same value. Lets first select the three face values involved, and then the suits of the cards with those face values. However, a subtle point is that the two pairs are symmetric to each other but not to the singleton. So we pick the two face values for the two pairs in ( 13 ways, leaving 11 face values for the singleton. For the two pairs we then pick of the four suits, and one of the four for the singleton. This gives a total of ( 13 11 ( 4 ( 4 1 = (13 1/ 11 (4 3/ 4. 4

There are many other ways to think about it that give the same answer, but there are also many ways to think about it incorrectly that give you the wrong answer. (c Flush: All cards have the same suit If we pick the suit first, we can pick any five of the thirteen cards of that suit. So the total is r ( 13 (d Have at least one card of each suit. Like the two pairs example, we need to think about symmetric vs. non-symmetric choices. Three of the suits will have exactly one card, and one will have two. There are four ways to pick the one with two. Then the two cards of this suit will be symmetric, but the others will be unique. This gives a total of 4 ( 13 13 3 hands. (e Have at least one royal card (J,K,Q. Like the first problem, it is easier to count the hands that do not have a royal and subtract that from the total number of hands. This gives ( ( 5 5 = 40 4. There are n people in the tennis club who want to play (individual tennis. How many different ways are there of pairing them up into n matches so that each member is in exactly one match? You can leave your answer in terms of factorials or binomial co-efficients. (5 points correct answer, 5 points explanation. This uses the categories method. Say we present a roster of matches, of the form P 1 vs.p, P 3 vsp 4,..P n 1 vs.p n. We can make such a roster using any of the (n! orders of the n players. But different lists are identical if they just involve permuting the order of the n matches or switching the order pairs of matched players appear. This means rosters come in categories of n! n equivalent rosters. So the total number of matchings is (n!/(n! n = (n 1(n 3...3 1. 5