W13:Homework:H08 From 40wiki CS40 Foundations of Computer Science W13 W13:Exams W13:Homework in Class and Web Work W13:Calendar W13:Syllabus and Lecture Notes UCSB-CS40-W13 on Facebook (https://www.facebook.com/groups/ucsb.cs40.w13/) Next in the Array of Talks Series: Talks[5]: From Academia to Business: All about startups! (https://www.facebook.com/events/101188656717515 Wednesday Jan 23, 3pm-5pm click above to learn more and/or to RSVP on Facebook! CS40 on Gauchospace (https://gauchospace.ucsb.edu/courses/cour id=5907) H01 H02 H03 H04 H05 H06 H07 H08 H09 H10 H11 H12 H13 H14 H15 H16 H17 H18 H19 H20 H21 W01 W02 W03 W04 W05 W06 W07 W08 W09 W10 IC01 IC02 IC03 IC04 IC05 IC06 IC07 IC08 IC09 IC10 IC11 IC12 [Print-friendly PDF (http://www.cs.ucsb.edu/~pconrad/cs40/13w/hwk/cs40_w13_h08.pdf) ]
H08-W13-CS40 page 1 First name (color-in initial) A B C D E F G H I J K L M N O P Q R S T U V W X Y Z section (1 or 4) first name initial last name initial Last name (color-in initial) A B C D E F G H I J K L M N O P Q R S T U V W X Y Z H08: Due Monday 01.28.2012 in Lecture. Total Points: 50 MAY ONLY BE TURNED IN DURING Lecture ON Monday 01.28.2012, or offered in person, for in person grading, during instructor or TAs office hours. See the course syllabus at https://foo.cs.ucsb.edu/40wiki/index.php/w13:syllabus for more details. (1) (10 pts) Fill in the information below. Also, fill in the A-Z header by coloring in the first letter of your first and last name (as it would appears in Gauchospace), writing either 1 or 4 to indicate your discussion section meeting time, i.e., Tuesday at 1pm or Tuesday at 4pm. writing your first and last initial in large capital letters (e.g. P C). All of this helps us to manage the avalanche of paper that results from the daily homework. name: umail address: @umail.ucsb.edu For this assignment, please also refer to the handout that was given with Homework H08 (on the web at the link on the wiki.) (2) See the handout and the textbook for info on the mod function. Let g(x) = x mod 3, where x is any integer. (a) (2 pts) How would you fill in the blanks here: g: (b) (2 pts) What set is the co-domain of g? (Review definition of co-domain in Section 2.1.1) (c) (2 pts) What set is the domain of g? (Review definition of co-domain in Section 2.1.1) (d) (2 pts) Is the function g injective (i.e. 1-to-1)? (See Section 2.3.1) Explain your answer. (e) (2 pts) Is the function g surjective (i.e. onto)? (See Section 2.3.1) Explain your answer. (f) (2 pts) Is the function g a bijection? (See Section 2.3.2) Explain your answer. (g) (2 pts) Consider the set N 5 (see Section 2.1.4, p.87 in text for definition.) Review the definition of image of S under f, where S is a subset of the domain of the function (Section 2.1.1) Now consider the image of N 2 under g, i.e. g(n 2 ) List the elements of g(n 2 using set notation. (h) (3 pts) Continuing along these lines, consider the definition of pre-image of T, where T is a subset of the co-domain of a function (Section 2.1.1). The pre-image of the set {1,2} under g, i.e. g -1 ({1,2}) is an infinite set. Using set notation, list eight elements of this set, using ellipsis (...) to indicate that the set goes on forever in both directions. Include at least four negative numbers and four positive numbers in your answer.
CS40-W13-H08 page 2 (3) For this assignment, please also refer to the handout that was given with Homework H08 (on the web at the link on the wiki.) There is information about floor, ceiling, log, and powers of 2. (a) (2 pts) What is log 2 (4)? (b) (2 pts) What is log 2 (1024)? (c) (2 pts) What is log 2 (512)? (c) (3 pts) What is log 2 (500)? (d) (3 pts) What is log 2 (500)? (e) (3 pts) Refer to the handout that came with H08, and read about how we can divide an array of size 14 "in half" in multiple recursive steps, until we are left with arrays only of size 1. Your job is to do the same thing for an array of 18 elements, showing all of the intermediate splits. (4) Let P = the set of all human beings. Let f: P P be the function f(x) = the biological mother of x. (a) (4 pts) Is f injective? Explain. (b) (4 pts) Is f surjective? Explain.
CS40-W13-H08 HANDOUT page 1 Info on mod The mod function (known to C, C++, Java and Python programmers as the % operator) is defined as the remainder after integer division. Formally, (see p. 86), if a and b are integers, where b > 0, then the division algorithm states that there are two unique integers q and r such that: a = bq + r where 0 r < b q is the quotient, r = a-bq is the remainder, and the definition of a mod b The book also denotes the set of integers from from 0 up to n-1 with the notation N n (see p. 87), and that Z is a notation for the set of all integers. Recall that f: A B is the notation for a function that maps each value in the set A to exactly one value in the set B. Info on powers of 2 Powers of 2 are very important in Computer Science. So is the function f(x) = log 2 (x), which is the inverse of the function g(x)=2 x. In general (see p. 89): log b = y means b y = x The main reason this comes up is that many algorithms and data structures that enable us to speed up operations such as inserting, deleting, and finding and sorting involve a "divide and conquer" approach, where we take our data set and cut it in half, repeatedly (and recursively) until we get down to either a pair of elements, or a single element. If you consider the number 8, you can cut it in half three times before you get to 1: These three steps correspond to these facts, all equivalent: 8 4 2 1 2 2 2 = 8 2 3 =8 log 2 (8)=3 We also often combine the floor and ceiling functions with log 2 (x), especially when dealing with divide and conquer on problems where the data set sizes are not exact powers of two. For example, how many times can I cut the number 24 in half before reaching 1? At some point, I reach a place where I can't divide evenly in half. Here, I show how we might divide an array of 14 elements into two arrays of 7, then 4 arrays with uneven sizes, etc until each array is of size one. Note that the number of steps needed is four. Note that 14 falls between 2 3 = 8 and 2 4 = 16. Therefore log 2 (14)) =4, which is the number of steps needed to divide and conquer 14 elements down to individual elements. 14 (7,7) (4,3,4,3) (2,2,2,1,2,2,2,1) (1,1,1,1,1,1,1,1,1,1,1,1,1,1)
CS40-W13-H08 HANDOUT page 2 Retrieved from "https://foo.cs.ucsb.edu/40wiki/index.php?title=w13:homework:h08&oldid=2819" This page was last modified on 22 January 2013, at 16:30. Content is available under Attribution-NonCommercial-ShareAlike 3.0 Unported.