S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 163

Similar documents
Solutions to Homework 6

Dynamic programming. Chapter Shortest paths in dags, revisited S C A B D E

Approximation Algorithms

International Journal of Advanced Research in Computer Science and Software Engineering

Near Optimal Solutions

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

Computational Geometry. Lecture 1: Introduction and Convex Hulls

Dynamic Programming. Lecture Overview Introduction

CMPSCI611: Approximating MAX-CUT Lecture 20

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite

Introduction to Algorithms. Part 3: P, NP Hard Problems

Shortest Inspection-Path. Queries in Simple Polygons

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

14.1 Rent-or-buy problem

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

CSC2420 Fall 2012: Algorithm Design, Analysis and Theory

Handout #Ch7 San Skulrattanakulchai Gustavus Adolphus College Dec 6, Chapter 7: Digraphs

Catalan Numbers. Thomas A. Dowling, Department of Mathematics, Ohio State Uni- versity.

One last point: we started off this book by introducing another famously hard search problem:

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

TU e. Advanced Algorithms: experimentation project. The problem: load balancing with bounded look-ahead. Input: integer m 2: number of machines

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

GRAPH THEORY LECTURE 4: TREES

Lecture 17 : Equivalence and Order Relations DRAFT

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Analysis of Algorithms, I

Problem Set 7 Solutions

COMBINATORIAL PROPERTIES OF THE HIGMAN-SIMS GRAPH. 1. Introduction

Chapter Load Balancing. Approximation Algorithms. Load Balancing. Load Balancing on 2 Machines. Load Balancing: Greedy Scheduling

REPEATED TRIALS. The probability of winning those k chosen times and losing the other times is then p k q n k.

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

13. Write the decimal approximation of 9,000,001 9,000,000, rounded to three significant

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Feb 7 Homework Solutions Math 151, Winter Chapter 4 Problems (pages )

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Lecture 3. Linear Programming. 3B1B Optimization Michaelmas 2015 A. Zisserman. Extreme solutions. Simplex method. Interior point method

Social Media Mining. Graph Essentials

Chapter 6: Graph Theory

6.3 Conditional Probability and Independence

1. Nondeterministically guess a solution (called a certificate) 2. Check whether the solution solves the problem (called verification)

Symbol Tables. Introduction

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 10

Lecture 1: Systems of Linear Equations

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

Ready, Set, Go! Math Games for Serious Minds

Theory of Computation Chapter 2: Turing Machines

Solve addition and subtraction word problems, and add and subtract within 10, e.g., by using objects or drawings to represent the problem.

Ph.D. Thesis. Judit Nagy-György. Supervisor: Péter Hajnal Associate Professor

4. How many integers between 2004 and 4002 are perfect squares?

Diversity Coloring for Distributed Data Storage in Networks 1

SHARP BOUNDS FOR THE SUM OF THE SQUARES OF THE DEGREES OF A GRAPH

Actually Doing It! 6. Prove that the regular unit cube (say 1cm=unit) of sufficiently high dimension can fit inside it the whole city of New York.

LZ77. Example 2.10: Let T = badadadabaab and assume d max and l max are large. phrase b a d adadab aa b

Triangulation by Ear Clipping

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

Data Structures and Algorithms Written Examination

n 2 + 4n + 3. The answer in decimal form (for the Blitz): 0, 75. Solution. (n + 1)(n + 3) = n lim m 2 1

Finding and counting given length cycles

INCIDENCE-BETWEENNESS GEOMETRY

CHOOSING A COLLEGE. Teacher s Guide Getting Started. Nathan N. Alexander Charlotte, NC

The Goldberg Rao Algorithm for the Maximum Flow Problem

CHAPTER 9. Integer Programming

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

BALTIC OLYMPIAD IN INFORMATICS Stockholm, April 18-22, 2009 Page 1 of?? ENG rectangle. Rectangle

Lecture 3: Finding integer solutions to systems of linear equations

Network File Storage with Graceful Performance Degradation

A Non-Linear Schema Theorem for Genetic Algorithms

Reading 13 : Finite State Automata and Regular Expressions

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Colored Hats and Logic Puzzles

Algorithm Design and Analysis

International Indian School, Riyadh SA1 Worksheet Class: VI Mathematics

INDISTINGUISHABILITY OF ABSOLUTELY CONTINUOUS AND SINGULAR DISTRIBUTIONS

NP-Completeness and Cook s Theorem

Lecture 1: Course overview, circuits, and formulas

6.080 / Great Ideas in Theoretical Computer Science Spring 2008

Number Sense and Operations

3. How many winning lines are there in 5x5 Tic-Tac-Toe? 4. How many winning lines are there in n x n Tic-Tac-Toe?

ALGEBRA. Find the nth term, justifying its form by referring to the context in which it was generated

Gambling and Data Compression

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

5 INTEGER LINEAR PROGRAMMING (ILP) E. Amaldi Fondamenti di R.O. Politecnico di Milano 1

Compact Representations and Approximations for Compuation in Games

Stationary random graphs on Z with prescribed iid degrees and finite mean connections

MATH 21. College Algebra 1 Lecture Notes

Automata and Computability. Solutions to Exercises

Pigeonhole Principle Solutions

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Lecture 2: Regular Languages [Fa 14]

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Math , Fall 2012: HW 1 Solutions

Lecture 5 - CPA security, Pseudorandom functions

arxiv: v1 [math.pr] 5 Dec 2011

Randomized algorithms

Lesson 3.1 Factors and Multiples of Whole Numbers Exercises (pages )

Computational Models Lecture 8, Spring 2009

Transcription:

S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 163 5.15. We use Huffman s algorithm to obtain an encoding of alphabet {a, b, c} with frequencies f a, f b, f c. In each of the following cases, either give an example of frequencies (f a, f b, f c ) that would yield the specified code, or explain why the code cannot possibly be obtained (no matter what the frequencies are). (a) Code: {0, 10, 11} (b) Code: {0, 1, 00} (c) Code: {10, 01, 00} 5.16. Prove the following two properties of the Huffman encoding scheme. (a) If some character occurs with frequency more than 2/5, then there is guaranteed to be a codeword of length 1. (b) If all characters occur with frequency less than 1/3, then there is guaranteed to be no codeword of length 1. 5.17. Under a Huffman encoding of n symbols with frequencies f 1, f 2,..., f n, what is the longest a codeword could possibly be? Give an example set of frequencies that would produce this case. 5.18. The following table gives the frequencies of the letters of the English language (including the blank for separating words) in a particular corpus. blank 18.3% r 4.8% y 1.6% e 10.2% d 3.5% p 1.6% t 7.7% l 3.4% b 1.3% a 6.8% c 2.6% v 0.9% o 5.9% u 2.4% k 0.6% i 5.8% m 2.1% j 0.2% n 5.5% w 1.9% x 0.2% s 5.1% f 1.8% q 0.1% h 4.9% g 1.7% z 0.1% (a) What is the optimum Huffman encoding of this alphabet? (b) What is the expected number of bits per letter? (c) Suppose now that we calculate the entropy of these frequencies H = 26 i=0 p i log 1 p i (see the box in page 156). Would you expect it to be larger or smaller than your answer above? Explain. (d) Do you think that this is the limit of how much English text can be compressed? What features of the English language, besides letters and their frequencies, should a better compression scheme take into account? 5.19. Entropy. Consider a distribution over n possible outcomes, with probabilities p 1, p 2,..., p n.

S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 193 a b c a b b a b c b a c a c c Find an efficient algorithm that examines a string of these symbols, say bbbbac, and decides whether or not it is possible to parenthesize the string in such a way that the value of the resulting expression is a. For example, on input bbbbac your algorithm should return yes because ((b(bb))(ba))c = a. 6.7. A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence A, C, G, T, G, T, C, A, A, A, A, T, C, G has many palindromic subsequences, including A, C, G, C, A and A, A, A, A (on the other hand, the subsequence A, C, T is not palindromic). Devise an algorithm that takes a sequence x[1... n] and returns the (length of the) longest palindromic subsequence. Its running time should be O(n 2 ). 6.8. Given two strings x = x 1 x 2 x n and y = y 1 y 2 y m, we wish to find the length of their longest common substring, that is, the largest k for which there are indices i and j with x i x i+1 x i+k 1 = y j y j+1 y j+k 1. Show how to do this in time O(mn). 6.9. A certain string-processing language offers a primitive operation which splits a string into two pieces. Since this operation involves copying the original string, it takes n units of time for a string of length n, regardless of the location of the cut. Suppose, now, that you want to break a string into many pieces. The order in which the breaks are made can affect the total running time. For example, if you want to cut a 20-character string at positions 3 and 10, then making the first cut at position 3 incurs a total cost of 20 + 17 = 37, while doing position 10 first has a better cost of 20 + 10 = 30. Give a dynamic programming algorithm that, given the locations of m cuts in a string of length n, finds the minimum cost of breaking the string into m + 1 pieces. 6.10. Counting heads. Given integers n and k, along with p 1,..., p n [0, 1], you want to determine the probability of obtaining exactly k heads when n biased coins are tossed independently at random, where p i is the probability that the ith coin comes up heads. Give an O(n 2 ) algorithm for this task. 2 Assume you can multiply and add two numbers in [0, 1] in O(1) time. 6.11. Given two strings x = x 1 x 2 x n and y = y 1 y 2 y m, we wish to find the length of their longest common subsequence, that is, the largest k for which there are indices i 1 < i 2 < < i k and j 1 < j 2 < < j k with x i1 x i2 x ik = y j1 y j2 y jk. Show how to do this in time O(mn). 6.12. You are given a convex polygon P on n vertices in the plane (specified by their x and y coordinates). A triangulation of P is a collection of n 3 diagonals of P such that no two diagonals intersect (except possibly at their endpoints). Notice that a triangulation splits the polygon s interior into n 2 disjoint triangles. The cost of a triangulation is the sum of the lengths of the diagonals in it. Give an efficient algorithm for finding a triangulation of minimum cost. (Hint: Label the vertices of P by 1,..., n, starting from an arbitrary vertex and walking clockwise. For 1 i < j n, let the subproblem A(i, j) denote the minimum cost triangulation of the polygon spanned by vertices i, i + 1,..., j.) 2 In fact, there is also a O(n log 2 n) algorithm within your reach.

194 Algorithms 6.13. Consider the following game. A dealer produces a sequence s 1 s n of cards, face up, where each card s i has a value v i. Then two players take turns picking a card from the sequence, but can only pick the first or the last card of the (remaining) sequence. The goal is to collect cards of largest total value. (For example, you can think of the cards as bills of different denominations.) Assume n is even. (a) Show a sequence of cards such that it is not optimal for the first player to start by picking up the available card of larger value. That is, the natural greedy strategy is suboptimal. (b) Give an O(n 2 ) algorithm to compute an optimal strategy for the first player. Given the initial sequence, your algorithm should precompute in O(n 2 ) time some information, and then the first player should be able to make each move optimally in O(1) time by looking up the precomputed information. 6.14. Cutting cloth. You are given a rectangular piece of cloth with dimensions X Y, where X and Y are positive integers, and a list of n products that can be made using the cloth. For each product i [1, n] you know that a rectangle of cloth of dimensions a i b i is needed and that the final selling price of the product is c i. Assume the a i, b i, and c i are all positive integers. You have a machine that can cut any rectangular piece of cloth into two pieces either horizontally or vertically. Design an algorithm that determines the best return on the X Y piece of cloth, that is, a strategy for cutting the cloth so that the products made from the resulting pieces give the maximum sum of selling prices. You are free to make as many copies of a given product as you wish, or none if desired. 6.15. Suppose two teams, A and B, are playing a match to see who is the first to win n games (for some particular n). We can suppose that A and B are equally competent, so each has a 50% chance of winning any particular game. Suppose they have already played i + j games, of which A has won i and B has won j. Give an efficient algorithm to compute the probability that A will go on to win the match. For example, if i = n 1 and j = n 3 then the probability that A will win the match is 7/8, since it must win any of the next three games. 6.16. The garage sale problem (courtesy of Professor Lofti Zadeh). On a given Sunday morning, there are n garage sales going on, g 1, g 2,..., g n. For each garage sale g j, you have an estimate of its value to you, v j. For any two garage sales you have an estimate of the transportation cost d ij of getting from g i to g j. You are also given the costs d 0j and d j0 of going between your home and each garage sale. You want to find a tour of a subset of the given garage sales, starting and ending at home, that maximizes your total benefit minus your total transportation costs. Give an algorithm that solves this problem in time O(n 2 2 n ). (Hint: This is closely related to the traveling salesman problem.) 6.17. Given an unlimited supply of coins of denominations x 1, x 2,..., x n, we wish to make change for a value v; that is, we wish to find a set of coins whose total value is v. This might not be possible: for instance, if the denominations are 5 and 10 then we can make change for 15 but not for 12. Give an O(nv) dynamic-programming algorithm for the following problem. Input: x 1,..., x n ; v. Question: Is it possible to make change for v using coins of denominations x 1,..., x n? 6.18. Consider the following variation on the change-making problem (Exercise 6.17): you are given denominations x 1, x 2,..., x n, and you want to make change for a value v, but you are allowed to use each denomination at most once. For instance, if the denominations are 1, 5, 10, 20, then you can make change for 16 = 1 + 15 and for 31 = 1 + 10 + 20 but not for 40 (because you can t use 20 twice).

196 Algorithms with which keywords are accessed, we can use an even more fine-tuned cost function, the average number of comparisons to look up a word. For the search tree on the left, it is cost = 1(0.04) + 2(0.40 + 0.10) + 3(0.05 + 0.08 + 0.10 + 0.23) = 2.42. By this measure, the best search tree is the one on the right, which has a cost of 2.18. Give an efficient algorithm for the following task. Input: n words (in sorted order); frequencies of these words: p 1, p 2,..., p n. Output: The binary search tree of lowest cost (defined above as the expected number of comparisons in looking up a word). 6.21. A vertex cover of a graph G = (V, E) is a subset of vertices S V that includes at least one endpoint of every edge in E. Give a linear-time algorithm for the following task. Input: An undirected tree T = (V, E). Output: The size of the smallest vertex cover of T. For instance, in the following tree, possible vertex covers include {A, B, C, D, E, F, G} and {A, C, D, F } but not {C, E, F }. The smallest vertex cover has size 3: {B, E, G}. A D B E G C F 6.22. Give an O(nt) algorithm for the following task. Input: A list of n positive integers a 1, a 2,..., a n ; a positive integer t. Question: Does some subset of the a i s add up to t? (You can use each a i at most once.) (Hint: Look at subproblems of the form does a subset of {a 1, a 2,..., a i } add up to s? ) 6.23. A mission-critical production system has n stages that have to be performed sequentially; stage i is performed by machine M i. Each machine M i has a probability r i of functioning reliably and a probability 1 r i of failing (and the failures are independent). Therefore, if we implement each stage with a single machine, the probability that the whole system works is r 1 r 2 r n. To improve this probability we add redundancy, by having m i copies of the machine M i that performs stage i. The probability that all m i copies fail simultaneously is only (1 r i ) mi, so the probability that stage i is completed correctly is 1 (1 r i ) mi and the probability that the whole system works is n i=1 (1 (1 r i) mi ). Each machine M i has a cost c i, and there is a total budget B to buy machines. (Assume that B and c i are positive integers.) Given the probabilities r 1,..., r n, the costs c 1,..., c n, and the budget B, find the redundancies m 1,..., m n that are within the available budget and that maximize the probability that the system works correctly. 6.24. Time and space complexity of dynamic programming. Our dynamic programming algorithm for computing the edit distance between strings of length m and n creates a table of size n m and therefore needs O(mn) time and space. In practice, it will run out of space long before it runs out of time. How can this space requirement be reduced?

Algorithms Lecture 27: Lower Bounds [Sp 10] 2. We say that an array A[1.. n] is k-sorted if it can be divided into k blocks, each of size n/k, such that the elements in each block are larger than the elements in earlier blocks, and smaller than elements in later blocks. The elements within each block need not be sorted. For example, the following array is 4-sorted: 1 2 4 3 7 6 8 5 10 11 9 12 15 13 16 14 (a) Describe an algorithm that k-sorts an arbitrary array in O(n log k) time. (b) Prove that any comparison-based k-sorting algorithm requires Ω(n log k) comparisons in the worst case. (c) Describe an algorithm that completely sorts an already k-sorted array in O(n log(n/k)) time. (d) Prove that any comparison-based algorithm to completely sort a k-sorted array requires Ω(n log(n/k)) comparisons in the worst case. In all cases, you can assume that n/k is an integer. 3. Recall the nuts-and-bolts problem from the lecture on randomized algorithms. We are given n bolts and n nuts of different sizes, where each bolt exactly matches one nut. Our goal is to find the matching nut for each bolt. The nuts and bolts are too similar to compare directly; however, we can test whether any nut is too big, too small, or the same size as any bolt. (a) Prove that in the worst case, Ω(n log n) nut-bolt tests are required to correctly match up the nuts and bolts. (b) Now suppose we would be happy to find most of the matching pairs. Prove that in the worst case, Ω(n log n) nut-bolt tests are required even to find n/2 arbitrary matching nut-bolt pairs. (c) Prove that in the worst case, Ω(n + k log n) nut-bolt tests are required to find k arbitrary matching pairs. [Hint: Use an adversary argument for the Ω(n) term.] (d) Describe a randomized algorithm that finds k matching nut-bolt pairs in O(n + k log n) expected time. 4. Suppose you want to determine the largest number in an n-element set X = {x 1, x 2,..., x n }, where each element x i is an integer between 1 and 2 m 1. Describe an algorithm that solves this problem in O(n + m) steps, where at each step, your algorithm compares one of the elements x i with a constant. In particular, your algorithm must never actually compare two elements of X! [Hint: Construct and maintain a nested set of pinning intervals for the numbers that you have not yet removed from consideration, where each interval but the largest is either the upper half or lower half of the next larger block.] c Copyright 2011 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 6