Assignments

[Note: this webpage last modified Monday, 30-Apr-2012 10:05:30 EDT]

Homework assignments will be posted to this website. Homework assignments are normally due before class starts one week after handed out (so you have one week to complete them). If you turn in the assignment late, I will grade it so you know how you did, but it will count as a 0.

You should email your homework solution to me at jkinne@cs.indstate.edu before it is due. If you send the assignment at least 24 hours early, I will normally give you feedback on whether it looks correct, and you can then resend again before the due date taking my feedback into account.

Remember to follow the Academic Integrity Policy on the Syllabus.

See the in class code for examples of taking command line arguments in C, Python, and Java.

For programs, you should send me your source code, and it should compile and run on the CS server. For problems, write in complete sentences to explain your answers. For problems, you can put them in a text file, Word, PDF, etc. The format of the file does not matter, but your answers should be well-written.

Project, due May 4

The final document and code for your project will be due during exam week. You will turn in your document, code, and any test data.

The document should have the following parts. (1) Introduction, including a few simple examples and applications of using your problem. (2) What is known about your problem (is it NP-complete?, what are the best-known algorithms?, etc.) You do not need to give proofs, just state these results and explain some. (3) Description of the algorithms you have studied and analysis - what is the running time, are they approximation algorithms, are they randomized, etc. (4) Experimental results - explain how well the algorithms do on various input sizes, do the experimental running times agree with the big-O running time estimates, what are the largest input sizes your algorithms can run on reasonably fast (within a few seconds or a few minutes).

The code should be well documented as to how it works, and include citations for anywhere you took code from or received help from. Include some test files that I can run the code on.

For the project, you each need to schedule a 1/2 hour appointment with me to explain your project to me. This will not be graded officially, but it is a way for me to check that you understand everything you are turning in. If you are working together with a partner, you only need to schedule one time to meet with me. I am putting a list of open project presentation slots in the announcements on the blackboard page for the course, and it will be first come, first served. For the presentation, you can use a computer or the chalk board. It makes no difference, but you need to explain everything well. You should do the following.

Final exam, during exam week.

The final exam is over the entire semester, with slightly more of a focus on the topics covered since the last exam. You can expect the following types of questions.

Here are a few example questions for the last week or so, for which we have not had a homework assignment. Of course, in addition to these problems, you should review the past exams, past homeworks, problems at the end of the chapter in the textbook, and the exam review from the past exams.

HW9, due Apr 26

Will be a "traditional" HW - on NP algorithms, problems will be given out later.

HW8, due Apr 19

  1. (2 points) Plot the feasible region and identify an optimal solution for the following LP.

    max 3x-4y
    x, y ≤ 0
    y - 2x ≤ 20
    3x + y ≤ 30
    x ≤ 8

  2. (2 points) Show how to phrase the following optimization problem as an LP. Namely, say what the variables are, what the objective function is, and what all the constraints are. You do not need to solve the LP, just set it up. Then, explain if there are any problems with solving this problem using LP solvers like the simplex method.

    You own a business that sells picture frames. You have three different kinds that you sell. Each is made from the same kind of wood. Large frames sell for $100, require 10 feet of wood, and take 30 minutes to build. Medium frames sell for $60, require 5 feet of wood, and take 20 minutes to build. Small frames sell for $20, require 1.5 feet of wood, and take 15 minutes to build. You have 200 feet of wood that you can use, and you can only spend 5 hours making the frames. You want to maximize your profit within these constraints.

  3. (1 points) Give an LP that is infeasible. Give another that is unbounded.

  4. (3 points) Show that the optimal solution to the following LP is x = 1.5, y=.5, z=0. You must prove this. Hint - use LP duality.

    max x-2z
    x-y ≤ 1
    2y-z ≤ 1
    x,y,z ≥ 0

  5. (3 points) Consider a graph given by the following adjacency matrix. Use any algorithm (from the book or elsewhere), to compute the maximum flow from vertex 1 to vertex 6 in the graph. Then, also give a minimum cut that separates vertex 1 from vertex 6 (you only need to give a minimum cut, you don't need to show how you found it). Note that the max flow and min cut should be the same.

       1  2  3  4  5  6
    -------------------
    1| 0  7  6  0  0  0
    2| 0  0  4  2  0  0
    3| 0  0  0  2  3  0
    4| 0  0  0  0  0  9
    5| 0  0  0  0  0  5
    6| 0  0  0  0  0  0
        
  6. (3 points) Use the simplex algorithm to solve the following LP. Show your work.

    max x+2y+3z
    x,y,z ≥ 0
    x ≤ 10
    y ≤ 12
    z ≤ 13
    3x+2y+z ≤ 25
    2x+3y+2z ≤ 20

HW7, due Apr 12, grading

You will work more on your project for this project grade. Previously, you have given some basic information about the problem you chose and turned in a working program to solve the problem.

For this week, you need to have two different algorithms to solve your problem that you understand and have code for that works on CS. You should update your document from before to include three parts: (1) introduction to your problem (statement of the problem, what is it used for, what is known about it), (2) basic description of one algorithm, (3) basic description of second algorithm.

For the homework assignment, you will turn in your updated document and code for the two different algorithms. You may look online for the code, or write the code yourself. If you get anything from online, you MUST include a citation at the top of your code and at the end of your document. The code should compile and run on CS without me having to modify it.

HW6, due Mar 20, grading

  1. (5 Points) Use the rules of probability to prove what the following probabilities/expectations are.

    1. Expected number of times to roll a 13-sided die before a prime number is rolled.

    2. Expected number of times to roll a 13-sided die before it happens that twice in a row a prime number is rolled (not necessarily the same one both times).

    3. Probability that 10 coins are flipped, and the total number of heads is at most 2.

    4. Assume faculty in the math department were chosen at random from those having PhDs in Math and CS. Assume that 1% of all PhDs were from the University of Wisconsin-Madison. What is the probability that 3 out of 16 faculty have PhDs from Wisconsin?

    5. Assume all ISU students are tested for illegal drug use and that the drug test has a .1% chance of a false positive. If no ISU students use drugs, what is the probability that at least one student tests positive? What are the expected number of students with a positive test? Assume there are 12,000 ISU students.

  2. (5 Points) For either the randomized algorithm you chose or the project you chose, turn in a working program to solve the problem. For this problem, you may search online for code to use as a starting point, or you may write the program yourself. You can turn in a "trivial, brute-force algorithm" if you write it yourself.

    What you turn in should compile and run on CS. You need to provide instructions for how to use program (what the input should be, etc.). You will explain the code to me and/or the class, so you need to understand it. You should put comments into the code to explain how it works.

  3. (5 Points) Program for randomized selection. Your program should take as input the name of a text file that will have a bunch of numbers, one on each line. The program should implement the randomized selection algorithm (from pages 64-65 of the textbook) to find the median of the numbers. You should output the median, and should also output the number of comparisons that were performed.

    Advice: first get a program working that finds the smallest in the list. Doing this successfully will make sure you are correctly reading the file and traversing the list. Next, make your program output a random element of the list. This will make sure you are getting random numbers appropriately. After these work, make a program that finds the median.

HW5, due Mar 13, grading

  1. (5 Points) Initial project choice. For the final project, you can choose from the following problems to study. You will study the best algorithms that are known for solving the problem, you will implement some of these, will write a paper about the problem, and will present your results to the class.

    Problems: primality testing, integer multiplication, linear programming, graph isomorphism, factoring integers, steiner tree, facility location, subset sum, traveling salesperson, multiprocessor scheduling, knapsack, quadratic programming, matrix determinant, satisfiability, min cut, circuit minimization.

    If you would like to use a problem that is not on this list, you need to check with me first.

    For this HW, you need to choose a problem, and summarize in 1/2-1 page the basic information about the problem - definition of the problem, what are the best-known algorithms, are there any conjectures about what the best algorithm can be for the problem, are there sub-cases of the problem that are more easily solved than the general case, what are the uses of the problem.

    This is an individual assignment. At a later time, I may allow you to work on your project in groups, but you should do this problem on your own.

  2. (5 Points) Use the rules of probability to prove what the following probabilities/expectations are.

    1. Probability of flipping 100 coins and getting more than 60 heads or 60 tails. Give an exact formula, and give an estimate of the value of the formula.

    2. Probability of flipping 100 coins and getting exactly 50 heads.

    3. Expected number of times that in 100 coin flips, there are two coins in a row that are the same (either heads or tails). Hint: use linearity of expectation.

    4. Expected number of times that in 100 coin flips, there are 5 coins in a row that are the same. Hint: use linearity of expectation.

    5. Consider choosing 1000 people at random from all voters out of a voting population of 3 million. Suppose the true vote of the 3 million would produce a winner with at least 55% of the vote. What is the probability that sampling 1000 people gives the wrong majority answer? Hint: give an estimate using the Chernoff bound.

    6. Probability of flipping 100 coins and getting some sequence of heads in a row followed by a sequence of tails in a row (for example, 60 heads and then 40 tails, or 33 heads and then 77 tails).

    7. Probability that a randomly chosen integer with 2048 bits is a prime. Hint: use the prime number theorem.

  3. (5 Points) Find one randomized algorithm to present to the class. You should email Jeff to confirm that the algorithm you choose is okay. You then need to understand the algorithm and write a description how it works to turn in. You will explain the algorithm to Jeff in person before presenting to the class. You also need to provide a reference or link to where you get your information from.

    Here are some suggestions: small-space algorithm for verifying connectivity in an undirected graph, independent set, interactive proof for graph non-isomorphism, volume of convex body.

    Already taken: closest pair of points, expected running time of quicksort, satisfiability, Pollard's rho algorithm for integer factorization, load balancing, packet routing, contention resolution, coupon collector's problem, polynomial identity testing, set balancing, verifying matrix multiplication.

    Note that I will not allow two people to use the same algorithm. First come, first served.

HW4, due Feb 28, grading

  1. (5 Points) Big O. Put the following in the proper "big O" order. Use the definition of big-O to prove your ordering.

    nlog(log(n))*.001
    100n.5*(log n)10
    n*n-n-100n.5
    1.001n
    n*(log n) + n*n*n*500 - n
    n*.008

  2. (5 Points) Program. Turn in a program that uses the Fermat primality test to estimate the number of primes. Your program should take a single number N as input off of the command line. The program should use the Fermat primality test with bases 2, 3, 5, 7 to compute for each number from 2 up to N whether the number is "probably prime". You should print the number of "probable primes", and the fraction of numbers that are at most N that are "probable primes".

    The prime number theorem tells us that the true fraction of prime numbers that are at most N gets close to N/ln(N) as N gets large. Your program should also print this value. So your program prints how many probable primes there are, and also prints out the value N/ln(N) that is the prediction of the prime number theorem.

    Note: If you get the basic program working, you should try to make it faster by using fast modular exponentiation instead of the slow version, or look for other ways to make it faster.

  3. (5 Points) Program. This program is to compute digits of Pi using the formula that Pi is equal to the Sum, from k=0 to infinity, of
    (1/16k) (4/(8k+1) - 2/(8k+4) - 1/(8k+5) - 1/(8k+6)).

    Your program should not take any command line arguments or inputs. It should simply use the formula up to some value of k and print the value for Pi that you come up with. Set the value of k in your program so that it finishes within 30 seconds when run on the CS server.

    Note: be careful of overflow!

    Note: if you get that working, then try using the GMP library (or other arbitrary precision package) rather than standard int/float variables so that you can get even more digits of Pi.
    Note 2: Also, if you get the basic program working, you could experiment with using a different formula and see if it converge faster or slower. But make sure you keep the basic program working too.

HW0, due Jan 17, grading

  1. (4 Points) Problem: compute the following values, and show your work (0 credit will be given if work is not shown).
    gcd(2012, 202), gcd(1155, 2002), 2-1 mod 27, 2-1 mod 2, 15-1 mod 50, 6-1 mod 25

  2. (5 Points) Program: Euclid's algorithm from Figure 1.5. Your program should take two positive integers as command line arguments and output the gcd.

  3. (3 Points) Program: modular division, using Sections 1.2.4, 1.2.5. Your program should take positive integers x and N as command line arguments and output the number a such that a*x = 1 mod N. If such an a does not exist, the program should say so.

  4. (3 Points) Problem: Give a big-O estimate of the running time of your algorithm for modular division. Count each individual subtraction, addition, or multiplication as "one unit of time".

HW1, due Jan 24, grading

Remember to write in complete sentences, and explain clearly.

  1. (3 Points) Suppose q is prime and k>=1. Let n=q^k. How many integers in {0, 1, 2, ..., n-1} have a greatest common divisor of 1 with n? Explain why.

    As a warmup, first answer the question for q=3, k=2, and q=5, k=3. You can compute the answers to these two examples by brute force, or if you have the answer to the general question just use your formula.

  2. (3 Points) Consider the problem of computing mm exactly. Devise an algorithm to compute the answer and analyze its running time, assuming multiplication of n-bit integers takes O(n2) time. Remember to set n to be the number of bits in the input (the number of bits in m). You do not need to implement this algorithm, just give pseudocode.

  3. (4 Points) Suppose RSA were changed so that the modulus N were simply a prime rather than a product of primes. That is, N=p is chosen to be a large prime, e is chosen to be relatively prime to p-1, and a message m is encrypted as me mod N. Explain how the encryption can be broken: given p, e and y=me mod N, describe an algorithm that recovers the message m. And analyze the running time of your algorithm.

  4. (5 Points) Program: write a program that experimentally tests the failure rate of the "primality test" from the book (Figure 1.8). The program should take the following command-line inputs: N, A. The program should first check by brute force how many primes there are between 2 and N, and should print this value. Your program should then estimate the same value using the primality test from Figure 1.8 instead of checking by brute force, and output this value. When running the algorithm of Figure 1.8, your program should test all bases between 2 and A to determine if a number is prime.

  5. Additional problems. None of these should be turned in, but here are extra problems that would be good to do for extra practice.

    Gather statistics on the running time of the euclidean algorithm for all numbers less than some amount. Compare that with worst-case analysis.

    Problems from the end of chapter 1, in particular 1.25, 1.27, 1.28, 1.32.

HW2, due Jan 31, grading

NOTE: no more paper, turn everything in by email.

  1. (5 Points) (a) Give an algorithm that efficiently determines whether a given number N is a perfect square or not. Describe the algorithm, and analyse the running time.
    Hint: letting n=log(N) be the number of bits in the input, you should be able to come up with an algorithm that runs in polynomial(n) time. Try binary search.

    (b) Show that if N=pd for a prime p and some integer d>=1, then d <= log(N).

    (c) Explain how parts (a) and (b) can be used to give an efficient algorithm for determining if a number N is a prime power.
    Hint: do like in (a) also for testing if N is a perfect cube, perfect 4th power, etc. up to log(N). Then add up the total running time.

  2. (5 Points) Use the divide-and-conquer algorithm for integer multiplication to multiply the two binary integers 10010110 and 11001010. Show your work.

  3. (5 Points) Suppose you have the choice of the following algorithms to solve a problem. Let n be the number of bits in the input.

    (1) An algorithm that solves the problem by dividing the problem into six subproblems of size n/3, recursively solve each subproblem, and combine them into a solution in O(n) time.
    (2) An algorithm that solves the problem by recursively solving three subproblems of size n-2 and then combining them in O(1) time.
    (3) An algorithm that solves the problem by dividing the problem into seven subproblems of size n/4, recursively solve each subproblem, and combine them into a solution in n2 time.

    Show what the running times are for each algorithm using big-O notation, and explain which algorithm you would choose.

    Hint: Set up a recurrence relation or draw a recursion tree for each algorithm, and solve the recurrence relation using the master theorem or add up the total running time on the recursion tree.

HW3, due Feb 7, grading

  1. (5 Points) For each of the following, use the definition of big-O to either prove the statemtn true or false.

    1. n = O(10n2)
    2. nlog(n) = O(n10)
    3. n = O(1010)
    4. 2n * n10 = O(22n)
    5. 10n2 = O(n3)
  2. (5 Points) Suppose you have a polynomial
    p(x) = a0 + a1 x + a2 x2 + ... + an xn where the ai are constants and x is the variable.

    1. One way to evaluate the polynomial at a point x=w is to first compute w, w2, w3, ..., wn, and then plug these values into the formula. Give an expression for the exact number of multiplications and additions that are needed to evaluate the formula in this way. Justify your answer.

    2. Here is another way to evaluate the polynomial at w. Start with the value x(n) = an.
      Compute x(n-1) = an-1 + x(n) * w.
      Compute x(n-2) = an-2 + x(n-1) * w.
      ...
      ...
      Compute x(0) = a0 + x(1) * w.

      Show that x(0) is the correct value of p(w).

    3. Give an exact formula for the number of multiplications and additions needed to compute x(0). Justify your answer.

  3. (5 Points) Use mult.cpp as a template, and complete the program to implement the divide and conquer integer multiplication algorithm we covered in class. The template is C++.

Exam 1

What is covered

Everything since the last test, and big-O and recurrence relations that was also on the last test.

Format

Similar to first exam.

Sample exam

Exam 0

What is covered

Everything we have done up through chapter 2 of the book.

In particular, you should be able to reproduce code/pseudocode for the following algorithms: addition, multiplication, fast modular exponentiation, gcd, extended euclidean algorithm, modular division, RSA encryption/decryption, fermat primality test, divide and conquer multiplication, FFT polynomial multiplication, "standard" polynomial multiplication. You should be able to give pseudocode for any related problems that have similar algorithms.

Any question from the homeworks is fair game on the exams.

Given pseudocode or a description of an algorithm, you should be able to analyze the running time. Remember that this means as a function of the number of input bits, and you need to get this correct. You should be able to solve recurrence relations with the master theorem or by drawing a recursion tree. You should know the formulae for arithmetic and geometric sums. You should be able to apply the definitions of big-O. You should know the laws of exponents, logarithms, etc. See TCS cheat sheet.

Format

On paper, no computers, phones, etc. You can bring one sheet of paper with anything you want on it - a "cheat sheet". People have reported in the past that this has not actually been useful, so do not assume it will "save the day" for you. You will have the 1 hour 15 minutes of class time. I will not give you extra time, so you need to manage your time accordingly.

Sample Exam

The following is an approximation of what the actual exam will be.