NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt): Name of Neighbor to your right (1 pt):

Similar documents
Midterm 1 for CS 170

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Divide and Conquer algorithms

Mid-term Exam Answers and Final Exam Study Guide CIS 675 Summer 2010

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

CS 170 Algorithms Spring 2009 David Wagner Final

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

CMSC Discrete Mathematics FINAL EXAM Tuesday, December 5, 2017, 10:30-12:30

Design and Analysis of Algorithms March 12, 2015 Massachusetts Institute of Technology Profs. Erik Demaine, Srini Devadas, and Nancy Lynch Quiz 1

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: October 11. In-Class Midterm. ( 7:05 PM 8:20 PM : 75 Minutes )

Data Structure and Algorithm Homework #1 Due: 2:20pm, Tuesday, March 12, 2013 TA === Homework submission instructions ===

CS1800 Discrete Structures Final Version A

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

CS 70 Discrete Mathematics and Probability Theory Fall 2016 Seshia and Walrand Midterm 1 Solutions

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

Math 391: Midterm 1.0 Spring 2016

NAME: Be clear and concise. You may use the number of points assigned toeach problem as a rough

CS325: Analysis of Algorithms, Fall Midterm

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

IS 709/809: Computational Methods in IS Research Fall Exam Review

Divide and Conquer. Maximum/minimum. Median finding. CS125 Lecture 4 Fall 2016

CS 188 Introduction to Fall 2007 Artificial Intelligence Midterm

Fast Convolution; Strassen s Method

COMP 9024, Class notes, 11s2, Class 1

ACO Comprehensive Exam March 20 and 21, Computability, Complexity and Algorithms

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

CS1800 Discrete Structures Fall 2016 Profs. Gold & Schnyder April 25, CS1800 Discrete Structures Final

HW8. Due: November 1, 2018

Midterm 1. Your Exam Room: Name of Person Sitting on Your Left: Name of Person Sitting on Your Right: Name of Person Sitting in Front of You:

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Lecture 7: Dynamic Programming I: Optimal BSTs

Notes on generating functions in automata theory

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CS483 Design and Analysis of Algorithms

COMP 382: Reasoning about algorithms

Algorithm Design and Analysis

How many hours would you estimate that you spent on this assignment?

Review Of Topics. Review: Induction

CPSC 320 Sample Final Examination December 2013

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

1. For each statement, either state that it is True or else Give a Counterexample: (a) If a < b and c < d then a c < b d.

Name: Student ID: Instructions:

CS361 Homework #3 Solutions

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Cpt S 223. School of EECS, WSU

University of New Mexico Department of Computer Science. Final Examination. CS 362 Data Structures and Algorithms Spring, 2007

P, NP, NP-Complete, and NPhard

Copyright 2000, Kevin Wayne 1

Fall 2016 Test 1 with Solutions

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

CSCI Honor seminar in algorithms Homework 2 Solution

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

Midterm 2. Your Exam Room: Name of Person Sitting on Your Left: Name of Person Sitting on Your Right: Name of Person Sitting in Front of You:

Asymptotic Analysis and Recurrences

Name (NetID): (1 Point)

An analogy from Calculus: limits

Algorithm efficiency analysis

Midterm Exam 2 Solutions

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

COE428 Notes Week 4 (Week of Jan 30, 2017)

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006

CS1800 Discrete Structures Spring 2018 February CS1800 Discrete Structures Midterm Version A

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

Midterm 2 for CS 170

CS 170 Algorithms Fall 2014 David Wagner MT2

Problem Point Value Points

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Chapter 4 Divide-and-Conquer

CSI 4105 MIDTERM SOLUTION

Algorithms and Their Complexity

CSE 421 Introduction to Algorithms Final Exam Winter 2005

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

INSTRUCTIONS USEFUL FORMULAS

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

1. Is the set {f a,b (x) = ax + b a Q and b Q} of all linear functions with rational coefficients countable or uncountable?

Lecture 18: P & NP. Revised, May 1, CLRS, pp

Divide and Conquer. Arash Rafiey. 27 October, 2016

Math 42: Fall 2015 Midterm 2 November 3, 2015

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Homework 1 Submission

CS 350 Midterm Algorithms and Complexity

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

Problem Set 1 Solutions

CS 61B Asymptotic Analysis Fall 2017

Problem Points Score Total 100

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Analysis of Algorithms - Using Asymptotic Bounds -

Quiz 1 Date: Monday, October 17, 2016

Introduction to Algorithms 6.046J/18.401J/SMA5503

Transcription:

CS 170 First Midterm 26 Feb 2010 NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt): Name of Neighbor to your right (1 pt): Instructions: This is a closed book, closed calculator, closed computer, closed network, open brain exam, but you are permited a 1 page, double-sided set of notes, large enough to read without a magnifying glass. You get one point each for filling in the 5 lines at the top of this page. Each other question is worth 20 points. Write all your answers on this exam. If you need scratch paper, ask for it, write your name on each sheet, and attach it when you turn it in (we have a stapler). 1 2 3 4 Total 1

Question 1 (20 points). a) Do a depth-first search on the graph below, and show the pre and post numbers on the figure. Whenever there is a choice of vertices to explore, always pick the one that is alphabetically first.!" #" &" $" %" '" (" )" *" Answer: +","-"./"!" #" +"5"-,."/" &" +",5"-",3/" +"0"-",,"/" +"1"-",2/" $" %" '" +"6-"7"/" (" )" *" +"3"-"4/" +",0"-",1"/" +",6-,7/" b) What are the cross edges? (write an edge from i to j as (i,j)). Answer: The cross edges are: (B,A) and (F,E) c) What are the strongly connected components? Answer: the SCCs are: {D, G, H, E}, {C, F }, {A}, {B} and {I}. d) Draw a DAG (consistent with the graph given) from the SCC nodes. 2

Answer:!" #" $%&" '%(%)%*" +" e) Can a DAG be built from the SCC nodes of the graph such that I is the first node (the leftmost node if the ordering is from left to right)? Explain briefly. Answer: YES. There are no edges from or to node I so it can be placed anywhere in the DAG ordering. f) For a given graph G = (V, E) we define two vertices, i and j, as a pair if there is a path from i to j and a path from j to i. Describe an algorithm that given some graph G = (V, E), returns the number of pairs in the graph, and has running time O( V + E ). (Note: You may use any algorithm learned in class as a black box). Answer: Compute all of the SCCs with the algorithm learned in class. For all SCCs: SCC 1, SCC 2,..., SCC k, compute there sizes: s 1, s 2,..., s k, (for example by running a depth-first search on each). By definition, every two nodes within a SCC are a pair. Therefore, the number of pairs in the graph is: k i=1 ( ) si 2 3

Question 1 (20 points). a) Do a depth-first search on the graph below, and show the pre and post numbers on the figure. Whenever there is a choice of vertices to explore, always pick the one that is alphabetically first.!" #" &" $" %" '" (" )" *" Answer: +","-"."/"!" #" +"4"-",5"/" &" +,,-,3"/" +,0-,1/" $" %" +"6"-"7"/" '" +",.-,2/" +"2-1"/" (" )" *" +""3-"0"/" +",4"-,6/" b) What are the cross edges? (write an edge from i to j as (i,j)). Answer: The cross edges are: (B,A) and (F,H) c) What are the strongly connected components? Answer: the SCCs are: {G, H, E}, {C, F }, {A}, {B}, {D} and {I}. d) Draw a DAG (consistent with the graph given) from the SCC nodes. 4

Answer: #"!" $%&" +" '%(%)" *" e) Can a DAG be built from the SCC nodes of the graph such that D is the first node (the leftmost node if the ordering is from left to right)? Explain briefly. Answer: YES. There are no edges from or to node D so it can be placed anywhere in the DAG ordering. f) For a given graph G = (V, E) we define two vertices, i and j, as a pair if there is a path from i to j and a path from j to i. Describe an algorithm that given some graph G = (V, E), returns the number of pairs in the graph, and has running time O( V + E ). (Note: You may use any algorithm learned in class as a black box). Answer: Compute all of the SCCs with the algorithm learned in class. For all SCCs: SCC 1, SCC 2,..., SCC k, compute there sizes: s 1, s 2,..., s k, (for example by running a depth-first search on each). By definition, every two nodes within a SCC are a pair. Therefore, the number of pairs in the graph is: k i=1 ( ) si 2 5

Question 1 (20 points). a) Do a depth-first search on the graph below, and show the pre and post numbers on the figure. Whenever there is a choice of vertices to explore, always pick the one that is alphabetically first.!" #" &" $" %" '" (" )" *" Answer: +","-"."/"!" #" +"4"-",6"/" &" +,,-,2"/" +0-1/" $" %" +5-7/" '" +",.-,0/" +2-3"/" (" )" *" +,4-,5"/" +",3-,1/" b) What are the cross edges? (write an edge from i to j as (i,j)). Answer: The cross edges are: (B,A) and (F,E). c) What are the strongly connected components? Answer: the SCCs are: {A}, {B}, {D, E, G}, {C, F }, {H} and {I}. d) Draw a DAG (consistent with the graph given) from the SCC nodes. 6

Answer: #"!" $%&" +" '%(%)" *" e) Can a DAG be built from the SCC nodes of the graph such that I is the first node (the leftmost node if the ordering is from left to right)? Explain briefly. Answer: YES. There are no edges from or to node I so it can be placed anywhere in the DAG ordering. f) For a given graph G = (V, E) we define two vertices, i and j, as a pair if there is a path from i to j and a path from j to i. Describe an algorithm that given some graph G = (V, E), returns the number of pairs in the graph, and has running time O( V + E ). (Note: You may use any algorithm learned in class as a black box). Answer: Compute all of the SCCs with the algorithm learned in class. For all SCCs: SCC 1, SCC 2,..., SCC k, compute there sizes: s 1, s 2,..., s k, (for example by running a depth-first search on each). By definition, every two nodes within a SCC are a pair. Therefore, the number of pairs in the graph is: k i=1 ( ) si 2 7

Question 2 (20 points). You will derive a divide-and-conquer algorithm for finding all the roots of a function y = f(x) in the interval x [0, 1) (i.e. 0 x < 1). The inputs of your recursive algorithm should be (1) the end points of the interval you are searching for roots (initially 0 and 1), (2) a function #roots(x lower, x upper ) that can count the number of roots of f(x) that lie in the interval [x lower, x upper ), and (3) the desired number of bits b of accuracy with which to find the roots (see below for more explanation of b). The output of your algorithm should be a set of triples {(x lower,1, x upper,1, n 1 ),..., (x lower,k, x upper,k, n k )} where (1) the intervals [x lower,1, x upper,1 ),..., [x lower,k, x upper,k ) are disjoint and lie inside [0, 1), (2) each interval [x lower,i, x upperi ) has length at most 2 b, and (3) each interval [x lower,i, x upper,i ) contains exactly n i > 0 roots of f(x). For example the triple (.5,.75, 6) would mean that there are 6 roots in the interval [.5,.75). For full credit, your algorithms should be as efficient as possible no matter where the roots of f(x) are located. Part 2.a. (15 points) Give an algorithm to solve this problem, clearly labeling the following parts of your answer: Main Idea (a few sentences), Pseudocode, Proof of Correctness (short!), Running time (worst case) in O() notation (in terms of b), and Running time analysis. You may assume that calling #roots(x lower, x upper ) costs O(1). Answer: The Main Idea is binary search: divide the interval in half at each step, count the number of roots in each half, and keep dividing until either an interval is empty, or has length 2 b. The Pseudocode is binary search(0,1,#roots,b) proc binary search(x,y,#roots,b) count = #roots(x,y) if count > 0 if y-x 2 b print [x,y,count] else mid = (x+y)/2 binary search(x,mid,#roots,b) binary search(mid,y,#roots,b) endif endif The proof of correctness is induction: At each step there are two possibilities: the interval [x, y) contains no roots (in which case the routine returns without doing anything) or it does contain roots. In the latter case there are again two possibilities: the interval is as narrow as desired (in which case the routine prints the desired answer) or it isn t (in which case the routine divides the interval in half and tries again). The worst case running time is O(2 b ). The worst-case running time analysis assumes that all intervals are nonempty. Then we get a binary tree of intervals, where the width drops in half with each call to binary search, so there are at most m recursive calls, and a full binary tree of depth b, with at most 1 + 2 + 4 + + 2 b = 2 b+1 1 calls to #roots. 8

Part 2.b. (5 points) Now assume that we know that the function f(x) can have at most n distinct roots. What is the worst case running time now, as a function of b and n? You may assume n is a power of 2. Answer: Now there can be at most n nonempty intervals at each level of the divide-andconquer tree. So the maximum number of nonempty intervals (the maximum number of calls to #roots) at each level of the tree can grow like 1, 2, 4, 8,..., n/2, n, n,... So there are 2 cases: if 2 b n, the complexity is still O(2 b ), and if 2 b > n, the complexity is O(n + n(b log 2 n)) which you may simplify to O(nb). 9

Question 2 (20 points). You will derive a divide-and-conquer algorithm for finding all the roots of a function z = g(y) in the interval y [0, 1) (i.e. 0 y < 1). The inputs of your recursive algorithm should be (1) the end points of the interval you are searching for roots (initially 0 and 1), (2) a function #roots(y lower, y upper ) that can count the number of roots of g(y) that lie in the interval [y lower, y upper ), and (3) the desired number of bits B of accuracy with which to find the roots (see below for more explanation of B). The output of your algorithm should be a set of triples {(y lower,1, y upper,1, n 1 ),..., (y lower,k, y upper,k, n k )} where (1) the intervals [y lower,1, y upper,1 ),..., [y lower,k, y upper,k ) are disjoint and lie inside [0, 1), (2) each interval [y lower,i, y upperi ) has length at most 2 B, and (3) each interval [y lower,i, y upper,i ) contains exactly n i > 0 roots of g(y). For example the triple (.25,.5, 3) would mean that there are 3 roots in the interval [.25,.5). For full credit, your algorithms should be as efficient as possible no matter where the roots of g(y) are located. Part 2.a. (15 points) Give an algorithm to solve this problem, clearly labeling the following parts of your answer: Main Idea (a few sentences), Pseudocode, Proof of Correctness (short!), Running time (worst case) in O() notation (in terms of B), and Running time analysis. You may assume that calling #roots(y lower, y upper ) costs O(1). Answer: The Main Idea is binary search: divide the interval in half at each step, count the number of roots in each half, and keep dividing until either an interval is empty, or has length 2 B. The Pseudocode is binary search(0,1,#roots,b) proc binary search(y,z,#roots,b) count = #roots(y,z) if count > 0 if z-y 2 B print [y,z,count] else mid = (y+z)/2 binary search(y,mid,#roots,b) binary search(mid,z,#roots,b) endif endif The proof of correctness is induction: At each step there are two possibilities: the interval [y, z) contains no roots (in which case the routine returns without doing anything) or it does contain roots. In the latter case there are again two possibilities: the interval is as narrow as desired (in which case the routine prints the desired answer) or it isn t (in which case the routine divides the interval in half and tries again). The worst case running time is O(2 B ). The worst-case running time analysis assumes that all intervals are nonempty. Then we get a binary tree of intervals, where the width drops in half with each call to binary search, so there are at most m recursive calls, and a full binary tree of depth B, with at most 1 + 2 + 4 + + 2 B = 2 B+1 1 calls to #roots. 10

Part 2.b. (5 points) Now assume that we know that the function g(y) can have at most m distinct roots. What is the worst case running time now, as a function of B and m? You may assume m is a power of 2. Answer: Now there can be at most m nonempty intervals at each level of the divide-andconquer tree. So the maximum number of nonempty intervals (the maximum number of calls to #roots) at each level of the tree can grow like 1, 2, 4, 8,..., m/2, m, m,... So there are 2 cases: if 2 B m, the complexity is still O(2 B ), and if 2 B > m, the complexity is O(m + m(b log 2 m)) which you may simplify to O(mB). 11

Question 2 (20 points). You will derive a divide-and-conquer algorithm for finding all the roots of a function s = h(t) in the interval t [0, 1) (i.e. 0 t < 1). The inputs of your recursive algorithm should be (1) the end points of the interval you are searching for roots (initially 0 and 1), (2) a function #roots(t lower, t upper ) that can count the number of roots of h(t) that lie in the interval [t lower, t upper ), and (3) the desired number of bits B of accuracy with which to find the roots (see below for more explanation of B). The output of your algorithm should be a set of triples {(t lower,1, t upper,1, n 1 ),..., (t lower,k, t upper,k, m k )} where (1) the intervals [t lower,1, t upper,1 ),..., [t lower,k, t upper,k ) are disjoint and lie inside [0, 1), (2) each interval [t lower,i, t upperi ) has length at most 2 B, and (3) each interval [t lower,i, t upper,i ) contains exactly m i > 0 roots of h(t). For example the triple (.5,.75, 4) would mean that there are 4 roots in the interval [.5,.75). For full credit, your algorithms should be as efficient as possible no matter where the roots of h(t) are located. Part 2.a. (15 points) Give an algorithm to solve this problem, clearly labeling the following parts of your answer: Main Idea (a few sentences), Pseudocode, Proof of Correctness (short!), Running time (worst case) in O() notation (in terms of B), and Running time analysis. You may assume that calling #roots(t lower, t upper ) costs O(1). Answer: The Main Idea is binary search: divide the interval in half at each step, count the number of roots in each half, and keep dividing until either an interval is empty, or has length 2 B. The Pseudocode is binary search(0,1,#roots,b) proc binary search(y,z,#roots,b) count = #roots(y,z) if count > 0 if z-y 2 B print [y,z,count] else mid = (y+z)/2 binary search(y,mid,#roots,b) binary search(mid,z,#roots,b) endif endif The proof of correctness is induction: At each step there are two possibilities: the interval [y, z) contains no roots (in which case the routine returns without doing anything) or it does contain roots. In the latter case there are again two possibilities: the interval is as narrow as desired (in which case the routine prints the desired answer) or it isn t (in which case the routine divides the interval in half and tries again). The worst case running time is O(2 B ). The worst-case running time analysis assumes that all intervals are nonempty. Then we get a binary tree of intervals, where the width drops in half with each call to binary search, so there are at most m recursive calls, and a full binary tree of depth B, with at most 1 + 2 + 4 + + 2 B = 2 B+1 1 calls to #roots. 12

Part 2.b. (5 points) Now assume that we know that the function h(t) can have at most k distinct roots. What is the worst case running time now, as a function of B and k? You may assume m is a power of 2. Answer: Now there can be at most k nonempty intervals at each level of the divide-andconquer tree. So the maximum number of nonempty intervals (the maximum number of calls to #roots) at each level of the tree can grow like 1, 2, 4, 8,..., k/2, k, k,... So there are 2 cases: if 2 B k, the complexity is still O(2 B ), and if 2 B > k, the complexity is O(k + k(b log 2 k)) which you may simplify to O(kB). 13

Question 3 (20 points). a) Suppose you have to multiply the polynomials a(x) = x and b(x) = x 2 + 1 using the Fast Fourier Transform. Choose an appropriate N and find the FFT of the two polynomials. Answer: We have N=4 because the product polynomial will have degree 3. So ω = e 2πi/4 = i, and the two FFTs are the polynomials evaluated at 1, i, 1, i, so they are (1, i, 1, i) and (2, 0, 2, 0) b) Once we have the two FFTs, how do we recover the product of the two polynomials? What is the FFT of their product (numerically)? Answer: First we multiply the two FFTs component-wise, and we get (2, 0, 2, 0) (which is also the FFT of their product). Then we take the inverse FFT, which in this case is equivalent to multiplying by 1 4F F T ( i), and we get (0, 1, 0, 1), which represents x 3 + x c) In lectures and problems you saw how to use the Fast Fourier Transform to multiply polynomials. Now you will explore a new application of the FFT: you will show how to use the FFT to find the period of a function. First let s look at the FFT matrix. For a given N, what is the value of ω? Answer: ω = e 2πi/N d) What is the value of the entry at position (j, k) in the FFT matrix (where 0 j < N and 0 k < N)? 14

Answer: ω jk = e 2πijk/N e) Now we will look at a periodic function f(z), i.e. there exists a number T such that f(z) = f(z + T ) for all z (T is called the period of the function). One of the simplest periodic functions is cos(z). In particular, we will look at a cosine function of period N/7, so the function is actually f(z) = cos(14π/nz). Now we will take the FFT of this function in the following way: multyply the FFT matrix you found above with the column vector that has f(z) at position z. The result will be another column vector V. List all elements of V that are non-zero (i.e. at which positions V has a non-zero entry). Hint: cos z = eiz +e iz 2, where i = 1. Answer: V (x) = N 1 y=0 F F T (x, y)f(y) = N 1 y=0 e2πixy/n e14πiy/n +e 14πiy/N 2 = 1/2 N 1 y=0 e2πiy(x+7)/n + 1/2 N 1 y=0 e2πiy(x 7)/N Let s look at the first sum: it is geometric with ratio e 2πi(x+7)/N, and evaluating it we get: S 1 = N/2 if e 2πi(x+7)/N = 1, and e2πi(x+7) 1 e 2πi(x+7)/N 1 = 0 otherwise. e2πi(x+7)/n = 1 is equivalent to saying that x = N 7. Similarly, the second sum is geometric with ratio e 2πi(x 7)/N, and evaluating it we get: S 2 = N/2 if e 2πi(x 7)/N = 1, and e2πi(x 7) 1 e 2πi(x 7)/N 1 = 0 otherwise. e2πi(x 7)/N = 1 is equivalent to saying that x = 7 To conclude, the only non-zero elements of V are at positions 7 and N 7 15

Question 3 (20 points). a) Suppose you have to multiply the polynomials a(x) = x + 1 and b(x) = x 2 using the Fast Fourier Transform. Choose an appropriate N and find the FFT of the two polynomials. Answer: We have N=4 because the product polynomial will have degree 3. So ω = e 2πi/4 = i, and the two FFTs are the polynomials evaluated at 1, i, 1, i, so they are (2, 1 + i, 0, 1 i) and (1, 1, 1, 1) b) Once we have the two FFTs, how do we recover the product of the two polynomials? What is the FFT of their product (numerically)? Answer: First we multiply the two FFTs component-wise, and we get (2, 1 i, 0, 1+ i) (which is also the FFT of their product). Then we take the inverse FFT, which in this case is equivalent to multiplying by 1 4F F T ( i), and we get (0, 0, 1, 1), which represents x 3 + x 2 c) In lectures and problems you saw how to use the Fast Fourier Transform to multiply polynomials. Now you will explore a new application of the FFT: you will show how to use the FFT to find the period of a function. First let s look at the FFT matrix. For a given N, what is the value of ω? Answer: ω = e 2πi/N d) What is the value of the entry at position (j, k) in the FFT matrix (where 0 j < N and 0 k < N)? 16

Answer: ω jk = e 2πijk/N e) Now we will look at a periodic function f(z), i.e. there exists a number T such that f(z) = f(z + T ) for all z (T is called the period of the function). One of the simplest periodic functions is cos(z). In particular, we will look at a cosine function of period N/11, so the function is actually f(z) = cos(22π/nz). Now we will take the FFT of this function in the following way: multyply the FFT matrix you found above with the column vector that has f(z) at position z. The result will be another column vector V. List all elements of V that are non-zero (i.e. at which positions V has a non-zero entry). Hint: cos z = eiz +e iz 2, where i = 1. Answer: V (x) = N 1 y=0 F F T (x, y)f(y) = N 1 y=0 e2πixy/n e22πiy/n +e 22πiy/N 2 = 1/2 N 1 y=0 e2πiy(x+11)/n + 1/2 N 1 y=0 e2πiy(x 11)/N Let s look at the first sum: it is geometric with ratio e 2πi(x+11)/N, and evaluating it we get: S 1 = N/2 if e 2πi(x+11)/N = 1, and e2πi(x+11) 1 e 2πi(x+11)/N 1 = 0 otherwise. e2πi(x+11)/n = 1 is equivalent to saying that x = N 11. Similarly, the second sum is geometric with ratio e 2πi(x 11)/N, and evaluating it we get: S 2 = N/2 if e 2πi(x 11)/N = 1, and e2πi(x 11) 1 e 2πi(x 11)/N 1 = 0 otherwise. e2πi(x 11)/N = 1 is equivalent to saying that x = 11 To conclude, the only non-zero elements of V are at positions 11 and N 11 17

Question 3 (20 points). a) Suppose you have to multiply the polynomials a(x) = x + 1 and b(x) = x 2 + 1 using the Fast Fourier Transform. Choose an appropriate N and find the FFT of the two polynomials. Answer: We have N=4 because the product polynomial will have degree 3. So ω = e 2πi/4 = i, and the two FFTs are the polynomials evaluated at 1, i, 1, i, so they are (2, 1 + i, 0, 1 i) and (2, 0, 2, 0) b) Once we have the two FFTs, how do we recover the product of the two polynomials? What is the FFT of their product (numerically)? Answer: First we multiply the two FFTs component-wise, and we get (4, 0, 0, 0) (which is also the FFT of their product). Then we take the inverse FFT, which in this case is equivalent to multiplying by 1 4 F F T ( i), and we get (1, 1, 1, 1), which represents x3 + x 2 + x + 1 c) In lectures and problems you saw how to use the Fast Fourier Transform to multiply polynomials. Now you will explore a new application of the FFT: you will show how to use the FFT to find the period of a function. First let s look at the FFT matrix. For a given N, what is the value of ω? Answer: ω = e 2πi/N d) What is the value of the entry at position (j, k) in the FFT matrix (where 0 j < N and 0 k < N)? 18

Answer: ω jk = e 2πijk/N e) Now we will look at a periodic function f(z), i.e. there exists a number T such that f(z) = f(z + T ) for all z (T is called the period of the function). One of the simplest periodic functions is cos(z). In particular, we will look at a cosine function of period N/13, so the function is actually f(z) = cos(26π/nz). Now we will take the FFT of this function in the following way: multyply the FFT matrix you found above with the column vector that has f(z) at position z. The result will be another column vector V. List all elements of V that are non-zero (i.e. at which positions V has a non-zero entry). Hint: cos z = eiz +e iz 2, where i = 1. Answer: V (x) = N 1 y=0 F F T (x, y)f(y) = N 1 y=0 e2πixy/n e26πiy/n +e 26πiy/N 2 = 1/2 N 1 y=0 e2πiy(x+13)/n + 1/2 N 1 y=0 e2πiy(x 13)/N Let s look at the first sum: it is geometric with ratio e 2πi(x+13)/N, and evaluating it we get: S 1 = N/2 if e 2πi(x+13)/N = 1, and e2πi(x+13) 1 e 2πi(x+13)/N 1 = 0 otherwise. e2πi(x+13)/n = 1 is equivalent to saying that x = N 13. Similarly, the second sum is geometric with ratio e 2πi(x 13)/N, and evaluating it we get: S 2 = N/2 if e 2πi(x 13)/N = 1, and e2πi(x 13) 1 e 2πi(x 13)/N 1 = 0 otherwise. e2πi(x 13)/N = 1 is equivalent to saying that x = 13 To conclude, the only non-zero elements of V are at positions 13 and N 13 19

Question 4: True/False (20 points). Circle your answer. T or F: T (n) = T (n 1) + 200 has solution Θ(n). Answer: True. At each step a constant amount of work is done. Since there are n steps until we reach 1, the running time is Θ(n). T or F: T (n) = 4T (n 1) + 6 has solution Θ(4 n ) Answer: True. T (n) = i = 0 n 4 i Θ(1) = Θ(4 n ) T or F: An algorithm for sorting an array of integers must take Ω(n log n) (where n is the size of the array). Answer: False. For an array of integers, we can use bucket-sort, which has a running time of O(n + m), where m is the difference between the maximal and the minimal elements of the array. T or F: We want to multiply a polynomial of degree 3 by a polynomial of degree 7. The following statements are all true: the final polynomial has degree 10; the minimum number of points we need to sample the final polynomial is 11; if we use the divide-andconquer FFT algorithm in the book, we need to choose N=16 as the size of the FFT. Answer: All True. T or F: In any directed graph, the sum of in-degrees and out-degrees over all vertices is an even number. Answer: True, because each edge (u, v) adds two to the sum, one to the out-degree of u and one to the in-degree of v. T or F: In any connected directed graph, by removing one edge we can create at most 2 Strongly Connected Components. Answer: False: if you have n vertices connected in a ring, and no other edges, removing any one edge makes each vertex a strongly connected component. T or F: In any connected undirected graph, we have to remove at least k edges to create k+1 connected components. Answer: True, because removing one edge (u, v) from a connected component breaks it into at most 2 connected components (those vertices whose only path to v passes through (u, v), and those with a path to v excluding (u, v)). T or F: (log 2 n) n = Ω(n log 2 n ). Answer: True: Take logarithms of both sides. On the left you get n log 2 log 2 n, which is asymptotically larger than (log 2 n) 2. T or F: Adding an edge to a DAG creates a cycle. Answer: False: If there are no edges in the DAG at all, adding any one edge (u, v) does not create a cycle. T or F: If f = O(g) then 2 f = O(2 g ). Answer: False: Consider f(n) = 2n and g(n) = n. 20

Question 4: True/False (20 points). Circle your answer. T or F: Adding an edge to a DAG creates a cycle. Answer: False: If there are no edges in the DAG at all, adding any one edge (u, v) does not create a cycle. T or F: S(n) = 4S(n 1) + 1 has solution Θ(4 n ). Answer: True. S(n) = i = 0 n 4 i Θ(1) = Θ(4 n ) T or F: An algorithm for sorting an array of integers must take Ω(k log k) (where k is the size of the array). Answer: False. For an array of integers, we can use bucket-sort, which has a running time of O(k + m), where m is the difference between the maximal and the minimal elements of the array. T or F: We want to multiply a polynomial of degree 4 by a polynomial of degree 8. The following statements are all true: the final polynomial has degree 12; the minimum number of points we need to sample the final polynomial is 13; if we use the divide-andconquer FFT algorithm in the book, we need to choose N=16 as the size of the FFT. Answer: All True. T or F: In any directed graph, the sum of in-degrees and out-degrees over all vertices is an odd number. Answer: False, because each edge (u, v) adds two to the sum, one to the out-degree of u and one to the in-degree of v, so it is even. T or F: In any connected undirected graph, we have to remove at least s edges to create s+1 connected components. Answer: True, because removing one edge (u, v) from a connected component breaks it into at most 2 connected components (those vertices whose only path to v passes through (u, v), and those with a path to v excluding (u, v)). T or F: (log 3 r) r = Ω(r log 3 r ). Answer: True: Take logarithms of both sides. On the left you get r log 3 log 3 r, which is asymptotically larger than (log 3 r) 2. T or F: If g = O(h) then 2 g = O(2 h ). Answer: False: Consider g(n) = 2n and h(n) = n. T or F: S(n) = S(n 1) + 70 has solution Θ(n). Answer: True. At each step a constant amount of work is done. Since there are n steps until we reach 1, the running time is Θ(n). T or F: In any connected directed graph, by removing one edge we can create at most 2 Strongly Connected Components. Answer: False: if you have n vertices connected in a ring, and no other edges, removing any one edge makes each vertex a strongly connected component. 21

Question 4: True/False (20 points). Circle your answer. T or F: We want to multiply a polynomial of degree 5 by a polynomial of degree 8. The following statements are all true: the final polynomial has degree 13; the minimum number of points we need to sample the final polynomial is 14; if we use the divide-andconquer FFT algorithm in the book, we need to choose N=16 as the size of the FFT. Answer: All True. T or F: An algorithm for sorting an array of integers must take Ω(s log s) (where s is the size of the array). Answer: False. For an array of integers, we can use bucket-sort, which has a running time of O(s + m), where m is the difference between the maximal and the minimal elements of the array. T or F: In any directed graph, the sum of in-degrees and out-degrees over all vertices is an odd number. Answer: False, because each edge (u, v) adds two to the sum, one to the out-degree of u and one to the in-degree of v, so it is even. T or F: In any connected directed graph, by removing one edge we can create at most 2 Strongly Connected Components. Answer: False: if you have n vertices connected in a ring, and no other edges, removing any one edge makes each vertex a strongly connected component. T or F: In any connected undirected graph, we have to remove at least t edges to create t + 1 connected components. Answer: True, because removing one edge (u, v) from a connected component breaks it into at most 2 connected components (those vertices whose only path to v passes through (u, v), and those with a path to v excluding (u, v)). T or F: R(m) = R(m 1) + 200 has solution Θ(m). Answer: True. At each step a constant amount of work is done. Since there are m steps until we reach 1, the running time is Θ(m). T or F: If r = O(s) then 2 r = O(2 s ). Answer: False: Consider r(n) = 2n and s(n) = n. T or F: (log 10 m) m = Ω(m log 10 m ). Answer: True: Take logarithms of both sides. On the left you get m log 10 log 10 m, which is asymptotically larger than (log 10 m) 2. T or F: Adding an edge to a DAG creates a cycle. Answer: False: If there are no edges in the DAG at all, adding any one edge (u, v) does not create a cycle. T or F: R(m) = 4R(m 1) + 6 has solution Θ(4 m ). Answer: True. R(m) = i = 0 m 4 i Θ(1) = Θ(4 m ) 22