CSC 344
Homework 3

Assigned Mar 8, due Mar 17

Problems from the textbook
A problem from another textbook: (2 points)

Write and analyze an algorithm which, given two numbers a and n, computes an. You may assume that n is a non-negative integer. A brute-force approach to this takes O(n) time; give me a significantly more efficient solution.

A problem from another textbook: (3 points)

You are given a board with n holes of various sizes drilled in it, and n bolts to be inserted into them. You are guaranteed that there is a bolt exactly matching the size of each hole, but none of the bolts or holes are labelled, and you don't have a measuring device, nor any way to compare two holes or two pegs together. The only thing you can do is try a peg in a hole to learn whether it's too large, too small, or just right.

Give an algorithm to match each bolt to the appropriate hole, in time O(n log(n)).

Another problem from another textbook: (3 points)

An electronic chip is designed in such a manner that any two chips can be connected to test one another; each chip reports whether the other one is good or bad. A good chip always reports correctly whether the other chip is good or bad, but a bad chip's report cannot be trusted. Thus there are four possible outcomes:
Chip A saysChip B saysConclusion
B is goodA is goodEither both are good, or both are bad
B is goodA is badA (and possibly B) is bad
B is badA is goodB (and possibly A) is bad
B is badA is badAt least one is bad

  1. Suppose more than half of the n chips are good. Describe an algorithm you could use to reduce the problem to one of at most approximately n/2 size, using approximately n/2 tests. (Note: the new problem must still satisfy the assumption that more than half of the chips are good, or it doesn't count as the same problem.)
  2. Still assuming that more than half of the n chips are good, describe an algorithm to find exactly which chips are good, using θ(n) tests. (This will require stating and solving a recurrence.)
  3. Assume that at least half of the chips are bad. Convince me (i.e. "prove") that under these circumstances, there is no way to tell which ones are good and which are bad. Hint: imagine that the bad chips are all controlled by a single malevolent demon who's trying to lead you to a wrong answer. Describe an algorithm this demon could use to fool you, no matter what algorithm you use.

Programming:
Implement one of the versions of the union/find data structure from section 4.6 (and Mar. 6's lecture) in a real programming language. To test it, I suggest writing a simple text-driven command loop with four commands:
initialize N
which initializes a collection of N sets, each containing a different one of the numbers 1, 2, 3, ... N.
find N
which prints out which set the number N is in.
union M,N
which finds both M and N and, if they're not already in the same set, merges their sets together. If they are already in the same set, prints an error message.
quit
Three guesses.
This problem is worth more or less credit depending on which version of the data structure you implement: the more sophisticated, more efficient ones are worth more.


Last modified: Tue Mar 7 16:47:10 EST 2006
Stephen Bloch / sbloch@adelphi.edu