Midterm 2 for CS 170

Similar documents
Midterm 1 for CS 170

Source Coding. Master Universitario en Ingeniería de Telecomunicación. I. Santamaría Universidad de Cantabria

EE376A - Information Theory Midterm, Tuesday February 10th. Please start answering each question on a new page of the answer booklet.

Lecture 4 : Adaptive source coding algorithms

SIGNAL COMPRESSION Lecture 7. Variable to Fix Encoding

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

CS:4330 Theory of Computation Spring Regular Languages. Finite Automata and Regular Expressions. Haniel Barbosa

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:

Midterm 1. Name: TA: U.C. Berkeley CS70 : Algorithms Midterm 1 Lecturers: Anant Sahai & Christos Papadimitriou October 15, 2008

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

1. Induction on Strings

Prob. 1 Prob. 2 Prob. 3 Total

CS4800: Algorithms & Data Jonathan Ullman

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

Chapter 5 Arrays and Strings 5.1 Arrays as abstract data types 5.2 Contiguous representations of arrays 5.3 Sparse arrays 5.4 Representations of

Exam in Discrete Mathematics

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:

CS5371 Theory of Computation. Lecture 18: Complexity III (Two Classes: P and NP)

10. The GNFA method is used to show that

Introduction to Theory of Computing

Proof Techniques (Review of Math 271)

CS60007 Algorithm Design and Analysis 2018 Assignment 1

More Dynamic Programming

18.310A Final exam practice questions

More Dynamic Programming

Algorithm Design and Analysis

Discrete Mathematics and Probability Theory Fall 2018 Alistair Sinclair and Yun Song Note 6

CMPT 365 Multimedia Systems. Lossless Compression

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):

1 Non-deterministic Turing Machine

Computational Models - Lecture 4

McGill University Faculty of Science. Solutions to Practice Final Examination Math 240 Discrete Structures 1. Time: 3 hours Marked out of 60

Factoring Algorithms Pollard s p 1 Method. This method discovers a prime factor p of an integer n whenever p 1 has only small prime factors.

CSE 202 Homework 4 Matthias Springer, A

Announcements. Problem Set Four due Thursday at 7:00PM (right before the midterm).

Even More on Dynamic Programming

Lecture 7: Dynamic Programming I: Optimal BSTs

CS280, Spring 2004: Prelim Solutions

Logic: The Big Picture

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Turing Machine Recap

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

Lecture 17: Language Recognition

Discrete Mathematics for CS Fall 2003 Wagner MT2 Soln

Assignment 5: Solutions

Automata Theory. Lecture on Discussion Course of CS120. Runzhe SJTU ACM CLASS

Deterministic Finite Automata (DFAs)

Induction and Recursion

Outline. Number Theory and Modular Arithmetic. p-1. Definition: Modular equivalence a b [mod n] (a mod n) = (b mod n) n (a-b)

CS 70 Discrete Mathematics and Probability Theory Spring 2018 Ayazifar and Rao Final

Efficient Algorithms and Intractable Problems Spring 2016 Alessandro Chiesa and Umesh Vazirani Midterm 2

Discrete Mathematics and Probability Theory Spring 2014 Anant Sahai Midterm 1

Proofs, Strings, and Finite Automata. CS154 Chris Pollett Feb 5, 2007.

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

CPSC 320 Sample Final Examination December 2013

Properties of Context-Free Languages

1 Circuit Complexity. CS 6743 Lecture 15 1 Fall Definitions

General Methods for Algorithm Design

CS173 Lecture B, November 3, 2015

The next sequence of lectures in on the topic of Arithmetic Algorithms. We shall build up to an understanding of the RSA public-key cryptosystem.

ALGEBRA. 1. Some elementary number theory 1.1. Primes and divisibility. We denote the collection of integers

CS250, Fall 2010, Midterm 2, Tuesday November 9, 8am.

Lecture 1 : Data Compression and Entropy

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

arxiv: v1 [cs.ds] 9 Apr 2018

PCPs and Inapproximability Gap-producing and Gap-Preserving Reductions. My T. Thai

NOTE: You have 2 hours, please plan your time. Problems are not ordered by difficulty.

SMT 2013 Power Round Solutions February 2, 2013

ICS141: Discrete Mathematics for Computer Science I

Attempt QUESTIONS 1 and 2, and THREE other questions. penalised if you attempt additional questions.

Data Structures in Java

CS/MATH 111 Winter 2013 Final Test

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

Lecture 2: Divide and conquer and Dynamic programming

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

Lecture 6: Introducing Complexity

Classes of Boolean Functions

Discrete Mathematics GCD, LCM, RSA Algorithm

Data Compression Techniques

Deterministic Finite Automata (DFAs)

Prob. 1 Prob. 2 Prob. 3 Total

CS325: Analysis of Algorithms, Fall Midterm

Module 9: Tries and String Matching

CSE 311: Foundations of Computing. Lecture 16: Recursion & Strong Induction Applications: Fibonacci & Euclid

On Universal Types. Gadiel Seroussi Hewlett-Packard Laboratories Palo Alto, California, USA. University of Minnesota, September 14, 2004

Lecture 16. Error-free variable length schemes (contd.): Shannon-Fano-Elias code, Huffman code

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

Deterministic Finite Automata (DFAs)

Entropy and Compression for Sparse Graphs with Vertex Names

CS 229r Information Theory in Computer Science Feb 12, Lecture 5

EECS 70 Discrete Mathematics and Probability Theory Fall 2015 Walrand/Rao Final

CMPT 365 Multimedia Systems. Final Review - 1

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

CVO103: Programming Languages. Lecture 2 Inductive Definitions (2)

Further discussion of Turing machines

Lecture 9. Greedy Algorithm

CS294: Pseudorandomness and Combinatorial Constructions September 13, Notes for Lecture 5

Converting SLP to LZ78 in almost Linear Time

Transcription:

UC Berkeley CS 170 Midterm 2 Lecturer: Gene Myers November 9 Midterm 2 for CS 170 Print your name:, (last) (first) Sign your name: Write your section number (e.g. 101): Write your sid: One page of notes is permitted. No electronic devices, e.g. cell phones and calculators, are permitted. Do all your work on the pages of this examination. If you need more space, you may use the reverse side of the page, but try to use the reverse of the same page where the problem is stated. You have 80 minutes. The questions are of varying difficulty, so avoid spending too long on any one question. In all algorithm design problems, you may use high-level pseudocode. DO NOT TURN THE PAGE UNTIL YOU ARE TOLD TO DO SO. Problem Score/Points 1 /10 2 /10 3 /10 4 /10 5 /10 Total /50

Midterm 2 2 1. Compression (a) Consider the Lempel-Ziv compression scheme where the dictionary is limited to 8 words and 3 bits are used to encode an index into the dictionary. Recall that the dictionary begins with the empty string as the sole entry with index 0. Give the Lempel-Ziv encoding for the string below and list the words in the dictionary in the order in which they were added to it. 0 1 00 01 010 10 11 001 100 011 1 < ɛ, 0, 1, 00, 01, 010, 10, 11 > 000 0 000 1 001 0 001 1 100 0 010 0 010 1 011 1 110 0 100 1 000 1 (b) Uncompress the Lempel-Ziv string below under the assumptions that each index is coded with 3 bits and the dictionary holds a maximum of 8 words. Also list, in order, the words in the dictionary. 0001 0000 0100 0110 0011 0111 0101 1001 1011 0011 < ɛ, 1, 0, 00, 000, 11, 001, 01 > 1 0 00 000 11 001 01 0001 111 11 (c) Consider the alphabet { a, b, c, d, e, f and assume the characters occur within a file with frequencies.01,.25,.35,.20,.03,.16., respectively. Give the optimal Huffman encoding tree for this situation. 1.0.4.6.2 d,.2 b,.25 c,.35.04 f,.16 a,.01 e,.03 Put a 0 and a 1 label on the pair of edges out of each vertex as you please.

Midterm 2 3 2. Number Theory and RSA (a) What is 3 28 (mod 10)? 3 2 = 9(mod 10), 3 4 = 1(mod 10), 3 8 = 1(mod 10), 3 16 = 1(mod 10) 3 28 = 3 16 3 8 3 4 = 1 1 1 = 1(mod 10) (b) What is the inverse of 32 (mod 113)? From extended-euclid s algorithm (x,y ) (x mod y,x) and (a,b) (b,a b x/y ), where ax + by = gcd(x,y). x 113 32 17 15 2 1 y 32 17 15 2 1 0 a 17-9 8-1 1 1 b -60 17-9 8-1 1 So 60 32 + 17 113 = gcd(32,113) = 1, so 32 1 = 60 = 53 (mod 113). (c) Given a number n = pq that is the product of two prime numbers p and q, how many numbers in Z n = {0,1,... n 1 do not have multiplicative inverses modulo n? p + q 1: ip for i [1,q 1], iq for i [1,p 1], and 0. (unless p = q in which case there are p such numbers. No points deducted if you missed this.) (d) Give a decryption key (private key) for the public RSA key (77,7). The private key d = e 1 ( mod (p 1)(q 1) = 7 1 ( mod 60). As for problem 2.b we use Extended Euclid: x 60 7 4 3 1 y 7 4 3 1 0 a -5 3-2 1 1 b 43-5 3-2 1 So 43 7 5 60 = gcd(7,60) = 1, so 7 1 = 43 (mod 60).

Midterm 2 4 3. Automaton Recognition Consider a directed graph G = (V,E) where each edge is labeled with a character from an alphabet Σ, and we designate a special vertex s as the start vertex, and another f as the final vertex. We say that G accepts a string A = a 1 a 2... a n if there is a path from s to f of n edges whose labels spell the sequence A. Design an O(( V + E )n) dynamic programming algorithm to determine whether or not A is accepted by G. (a) Formally define the set of sub-problems you will solve. For v V and i [0,n], let: A[v,i] = path from s to v spelling a 1 a 2...a i. (b) Give your recurrence for the solution of a given sub-problem in terms of other subproblems. A[v,i] = { ORw v (label(v w) = a i and A[w,i 1]) if i > 0 v = s if i = 0 (c) Give a non-recursive pseudo-code specification of the algorithm. A[s, 0] true for v V {s do A[v, 0] false for i 1... n do for v V s do { A[v,i] false for w v E do if label(w v) = a i and A[w,i 1] then A[v,i] true if A[t, n] then print Accept else print Reject

Midterm 2 5 4. Palindrome Parsing A palindrome is a word w 1 w 2...w k whose reverse w k w k 1...w 1 is the same string, e.g. abbabba. Consider a string A = a 1 a 2...a n. A partitioning of the string is a palindrome partitioning if every substring of the partition is a palindrome. For example, aba b bbabb a b aba is a palindrome partitioning of ababbbabbababa. Design a dynamic programming algorithm to determine the coarsest (i.e. fewest cuts) palindrome partition of A. (a) Formally define the set of sub-problems you will solve. For all 1 i j n we solve two subproblems: P[i,j] = a i a i+1...a j is a palindrome (true/false), and C[i,j] = no. of cuts in the best palindrome partition of a i a i+1...a j. (b) Give your recurrence for the solution of a given sub-problem in terms of other subproblems. a i = a j and P[i + 1,j 1] if i < j 1 P[i,j] = a i = a j if i = j 1 true if i = j C[i,j] = min{ min k [i,j 1] {C[i,k] + C[k + 1,j], if P[i,j] then 0 else (c) Give a non-recursive pseudo-code specification of the algorithm and state its complexity in terms of n. for i 1... n do (P[i,i],C[i,i]) (true,1) for l 1...n 1 do for i 1... n l do { j i + l if l > 1 then P[i,j] (a i = a j ) and P[i + 1,j 1] else P[i,j] (a i = a j ) C[i,j] for k i...j 1 do C[i,j] min{c[i,j],c[i,k] + C[k + 1,j] if P[i,j] then C[i,j] 0 Best palindrome partition has C[1,n] cuts The algorithm is O(n 3 ). (This is the complexity you were expected to acheive.)

Midterm 2 6 4. Palindrome Parsing A palindrome is a word w 1 w 2...w k whose reverse w k w k 1...w 1 is the same string, e.g. abbabba. Consider a string A = a 1 a 2...a n. A partitioning of the string is a palindrome partitioning if every substring of the partition is a palindrome. For example, aba b bbabb a b aba is a palindrome partitioning of ababbbabbababa. Design a dynamic programming algorithm to determine the coarsest (i.e. fewest cuts) palindrome partition of A. (a) Formally define the set of sub-problems you will solve. For all 1 i j n we solve the subproblems: P[i,j] = a i a i+1...a j is a palindrome (true/false), and and for all 0 i n we solve the subproblems: C[i] = no. of cuts in the best palindrome partition of a 1 a 2...a i. (b) Give your recurrence for the solution of a given sub-problem in terms of other subproblems. a i = a j and P[i + 1,j 1] if i < j 1 P[i,j] = a i = a j if i = j 1 true if i = j C[i] = { mink [0,i 1] {C[k] + 1 : P[k + 1,i] if i > 0 0 if i = 0 (c) Give a non-recursive pseudo-code specification of the algorithm and state its complexity in terms of n. P[1,1] true for j 2... n do { P[j,j] true P[j 1,j] (a j 1 = a j ) for i j 2...1 do P[i,j] (a i = a j ) and P[i + 1,j 1] C[0] 0 for i 1... n do { C[i] C[i 1] + 1 for k 1... i 2 do C[i] min{c[i],c[k] + P[k + 1,i] Best palindrome partition has C[n] cuts The algorithm is O(n 2 ). (Congratulations if you got this one.)

Midterm 2 7 5. Entropy and Huffman Codes [Hard] Suppose that for alphabet Σ the probability of seeing character a is p a. Recall the formula for the entropy of the alphabet E(Σ) = a Σ p 1 a log 2 p a. We asserted in class that the length of a Huffman encoded file is O(nE(Σ)). The essential fact needed to show this is that for a character a, its depth in an optimal Huffman tree is O(log 1 p a ). Prove this with a careful argument (Hint: Consider a node with this frequency and think about what must be true of the frequencies of all the children immediately off the path from this node to the root.) Suppose that the leaf node v for character a is placed at depth k in an optimal Huffman tree. Let w 0 = v,w 1,w 2,... w k 1 be the sequence of nodes encountered from v going back to the root of the tree, and let s i be the child of w i not on this path. Let p(v) be the probability of the tree rooted at v. The figure illustrates. w_k 1 s_k 1 w_2 w_1 s_2 v=w_0 s_1 Consider s j for any j 2. Because s j 1 and w j 2 are joined before s j is joined to the subtree rooted at w j 1 it must be that p(s j ) p(s j 1 ) and p(s j ) p(w j 2 ). Therefore, p(s j ) (p(s j 1 ) + p(w j 2 ))/2 = p(w j 1 )/2 and p(w j ) = p(w j 1 ) + p(s j ) 1.5p(w j 1 ). By induction it then follows that p(w k 1 ) 1.5 k 2 p(w 1 ). But p(w 1 ) p a and p(w k 1 ) = 1, so it must be that p a 1.5 k 2 1 which implies that k log 1.51/p a + 2 = O(log 1/p a ).