27
HW 2a A random walk problem. Example of a correct program in java language: import java.util.*; public class RandomWalk { public static void main(String[] args) { //Variables Declarations //Total number of steps to take int steps = 0; //Current state int state = 0; //Maximum state value int max = 0; //Minimum state value int min = 0; //Probability of increasing the state by 1 double p = 0.0; //Cumulative sum of all previous states double total = 0.0; //Used for error checking in input boolean isValid = false; while (!isValid) { System.out.println("Enter the initial value, the value of p (a decimal between" + " 0 and 1), and the number of steps (a positive integer)" + "to execute (separated by spaces):"); Scanner sc = new Scanner(System.in); String[] inputs = sc.nextLine().split(" ");

HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

  • Upload
    trananh

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

HW 2a

A random walk problem.

Example of a correct program in java language:

import java.util.*;

public class RandomWalk {

public static void main(String[] args) {

//Variables Declarations

//Total number of steps to take

int steps = 0;

//Current state

int state = 0;

//Maximum state value

int max = 0;

//Minimum state value

int min = 0;

//Probability of increasing the state by 1

double p = 0.0;

//Cumulative sum of all previous states

double total = 0.0;

//Used for error checking in input

boolean isValid = false;

while (!isValid) {

System.out.println("Enter the initial value, the value of p (a decimal between" +

" 0 and 1), and the number of steps (a positive integer)" +

"to execute (separated by spaces):");

Scanner sc = new Scanner(System.in);

String[] inputs = sc.nextLine().split(" ");

//Check for correct number of inputs

if(inputs.length != 3) {

System.out.println("Invalid input.");

continue;

}

Page 2: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

try{

state = Integer.parseInt(inputs[0]);

}catch (NumberFormatException e) {

System.out.println("Invalid input.");

continue;

}

try{

p = Double.parseDouble(inputs[1]);

}catch (NumberFormatException e) {

System.out.println("Invalid input.");

continue;

}

try{

steps = Integer.parseInt(inputs[2]);

}catch (NumberFormatException e) {

System.out.println("Invalid input.");

continue;

}

//Check to see if 'p' and 'steps' are within the valid ranges

if(p > 1.0 || p < 0 || steps <= 0) {

System.out.println("Invalid input.");

continue;

}

isValid = true;

}

//Update variables based on the input values.

max = state;

min = state;

total = state;

Page 3: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

//Print the initial state

System.out.println(state);

//Move through the random walk

for(int x = 0; x < steps; x++) {

if(Math.random() < p)

state = state + 1;

else

state = state - 1;

if(state > max)

max = state;

if(state < min)

min = state;

total+= state;

System.out.println(state);

}

//Print out the results

System.out.println("Maximum: " + max);

System.out.println("Minimum: " + min);

System.out.println("Average: " + (total/(steps+1)));

}

}

COMMON PROBLEMS

Many students did not check the validity of input parameters. The value of probability should be 0 < p < 1. The value of steps should not be negative. The number of input parameters should be 3.

Many students failed to give correct average calculation. It should be divided by (steps+1). Use test case (-1 0.4 0) to see if your calculation is right.

Some students did not use correct variable type, the average could be a decimal.

Also with min and max initialization, it should be initialized with the initial state, in case no comparison is called

HINTS

The best ways for the students to read the input would be to use the scanner class, or to use the Buffered Reader class and then break up the string with the StringTokenizer class.

Page 4: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

In order to get the correct average, students should initialize the sum to the starting value, and then continue to add each calculated value in the for/while loop, and then divide by the number of steps + 1 in the end.

The number of steps that is specified is the number of movement calculations that need to be made after the starting value. So, a walk with 0 steps would still contain 1 value, the starting value.

HW 1a

1) You are writing down all the numbers between 3,000 and 4,000, inclusive. How many times you will have to write digit “ 3”?

Simplify the problem to get started. Say, “between 300 and 400”. First, you notice that the 1 st “3” stays in each number, which makes it 100 threes. Then, there are numbers like 330, 331, ..339. 10 of those.You see the pattern? Now apply it to the original problem (see below).

1000 threes appear on the thousand digit. From 3000 to 3999

For remaining 3 digits, each has 100 different numbers. 100 threes appear on hundred digit. 100 threes appear on ten digit. 100 threes appear on the last digit.

So in total we have to write 1000+100+100+100=1300 threes.

2) You have a perfectly accurate analog watch with hour, minute, and second hands. How many times a day (24 hrs) do all three hands overlap?

It is very useful to solve a simpler problem first, that is with just the hour and minute hands. Obviously, 12.00 is an overlap. Then, there will be an overlap shortly past 1 pm, past 2 pm. etc. Except past 11 pm (that one will occur again at 12.00). There are 11 overlaps in 12 hour period. Ok.

At which times do these occur? Notice that the overlaps are separated by the same time interval because the hands travel at the same (angular) speeds. So, the interval between the overlaps is 12/11 of an hour (11 overlaps in 12 hours), which is 1 hr., 5 minutes and 27 3/11 seconds.

The eleven alignments of minute and hour hands in each 12-hour cycle take place at

12:00:00

1:05:27 3/11

2:10:54 6/11

3:16:21 9/11

4:21:49 1/11

5:27:14 4/11

6:32:43 7/11

7:38:10 10/11

8:43:38 2/11

9:49:05 5/11

10:54:32 8/11

Page 5: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

….

Now bring in the second hand.

There is an overlap between minute and second hands only when the minutes figure (00 here) equals the seconds figure (00). There is a precise three-way overlap at 12:00:00. In general, the minute- and second-hand overlap will occur at a fractional second. For example, here, 12:37:37 the second hand would be at 37 past the minute, while the minute hand would be between 37 and 38 past the hour. The instant of overlap would come a split-second later. OK. But the hour hand wouldn’t be near the others, so this isn’t a three-way overlap.

What you do now is analyze the overlaps listed above to see if for any of them are of the form X.YY.YY. None are, except for 12:00:00.

ANSWER: all three hands align just twice every day, at midnight and noon.

COMMON PROBLEMS

Some students talk about the hour by hour base but forget the second fraction.

EXTRA CREDIT

On a regular analog clock/watch, you cannot sync up all three hands – the second hand is there only for convenience. So, while the hour and the minute hand will still align 11 times, the chance is that the second hand will be perfectly in sync to align with them at 12.00.00 is tiny. So, in reality, there will never be a perfect alignment of the three hands.

At an interview, it is worth specifying whether the problem talks about an ideal or real clock. They hire you to do a real job in a real world, it is always good to show that you understand the difference between reality and idealized solutions you learned in school. As you can see, the “real world” answer is simpler.

HW 2C

Partition Class into Groups with a Leader. Algorithm

Below is one example of a solution. Others are possible.

(1) Exclude group leaders from N students

(2) Partition the rest of the class into maximum possible number of groups of 3. Then partition the remainder, if any, into groups of 2 students. Essentially you do what was done in the lecture notes, but instead of 3 and 4 you do it for 2 and 3.  Count the number of resulting groups.

(3) Assign group leaders to the above groups.

(4) If L > # of groups, un-assign group leader status from one person, repeat the whole thing (steps 1-3) until every group has a leader.

(5) If the number of leaders is not enough, just print out an error message.

Note: the algorithm minimizes the number of groups to work for the minimum possible number of leaders. For example, for N=12, you could in principle have 3 groups of 4 or 4 groups of 3. But if L=3, only the first of the two partitions will work. There can be less trivial examples, e.g. N=31 L=8.  An (optimal) partition that works is 7

Page 6: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

groups of 4 and 1 group of 3. But 10 groups of 3 and 1 of 4 won't. In fact, taking care of this point is the only extension above what was already discussed in class.

COMMON PROBLEMS

Most codes could not handle the simple case N=12, L=3. Some algorithms left students unassigned, although there were enough leaders. (ex. 5 leaders &

15 non-leaders) Some implementations did not print an error message in the case of so few leaders that a

solution was not possible. (ex. 2 leaders and 18 followers)

Extra Credit Assignment 1

An instance of the problem is a finite set of non-overlapping, simple polygons in the plane, plus two points s and t that are not in the interior of any polygon. See Figure 1 for a sample instance. The problem is to describe a continuous path from s to t that avoids the interiors of the polygons and that is as short as possible (use Euclidean distance). It is fine for a path to go through a vertex or go along an edge of a polygon. You are to work towards a method (an algorithm) for finding a shortest path.

One key step is to realize that the shortest path is a straight line. So does this line hit any of the polygons? If so, you want to deviate from the straight line as little as possible. If the line hits only one polygon, then you need to decide which way to go round it. Etc...

COMMON PROBLEMS

Some groups didn’t mention more detailed explanation about pathfinding algorithm.

Page 7: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

In Class Quiz 1

1) How many “0” are there at the end of 100! ?

First, you are not supposed to actually calculate the actual value of 100!.

Instead, start from a simplified case such as 10! or 20!. For example,

10! = 10*9*8*7*6*5*4*3*2*1= 3628800

We notice that “10!” has two 0s at the end. The two 0s were contributed by its factors of 10, 5 and 2. Actually, 10 can also be written by 2*5. So the factors of 2 and 5 are multiplied together to produce 10s, which adds a 0 to the end of the product. Similarly, for “20!”, we can see that among the factors of “20!”, 20, 15, 10, and 5 are the factors which can produce 5s, adding four 5s in total. All even factors of “20!” can produce at least one factor of 2. In fact, there will always have enough 2s that, when multiplied by 5s produce 10s. In other words, the factorial will contain more even factors than factors divisible by 5. So, counting all the factors of 5 is enough to decide to how many 0s are at the end. Since there are four 5s in the factorial of 20!, we know that there are four 0s at the end of “20!”.

Therefore, to decide how many 0s at the end of “100!”, we need to count how many 5s are in the factorial. As we know 100, 95, 90…10, 5 will produces factors of 5. Notice that 25 adds two factors of 5 (25=5*5). So, 25, 50, 75 and 100 can produce 2 fives. So, in total, we have 20+4=twenty four 5s. There are therefore twenty four 0s at the end of 100!.

COMMON PROBLEMS

Some students say “11” because they only consider factors divisible by 10. Some students say “21”. They find the relationships between the 0s and the factor of 5. But didn’t

count how many 5s correctly. Notice 25, 50, 75 and 100 will produce two 5s.

2) An electronic safe uses a specific, unique 100-bit string as its password (a string of “0”’s and “1”s of length 100. e.g. 1001 is a 4-bit string). Is it realistic to guess the right string using your laptop connected to the safe? The assumption is that the laptop can generate N-bit strings and transmit them to the safe one by one until the one that opens it is found.

Number of possible 100-bit strings = 2^100 = (2^10) ^10 ~ 1000^10 = (10^3) ^10 = 10^30. This is how many strings you will need to generate to guarantee that the right one is found.

On the other hand, your laptop's speed is only about 3 GHz or 3*10^9 flops, or ~ 3*10^14 elementary operations per day. Even if generating a single 100-bit string takes only one operation (an underestimation), you would need more than 10^30/10^15 ~ 10^15 seconds ~ 10^8 years to find the right string for sure. One any realistic time-scale, like a day or a month, the chance of accidentally finding the right one is virtually zero. So, the answer is a big fat "No".

COMMON PROBLEMS

Some students correctly estimated the number of possible passwords. But they didn’t estimate how long it would take the laptop to finish the calculation.

Some students failed to estimate the number of possible 100-bit strings. For example, some of them selected 100^2 as the number of possible 100-bit strings. This is a gross underestimation of the huge true magnitude of the problem, which leads to a completely wrong answer that the calculation is possible.

Page 8: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

HW 3

Random Walk Problem on Cubes and Brownian Simulation

Example of a correct program in java language:

import java.util.Scanner;

import java.text.DecimalFormat;

public class rwc {

// Error string

static final String USAGE_STRING = "Usage: \"s p q r... n\"\n" +

"s\tThe starting point for the walker (int)\n"

+ "p\tThe probability the first bit will flip(double).\n"

+ "q\tThe probability the second bit will flip(double).\n"

+ "r\tThe probability the third bit will flip(double).\n"

+ "n\tThe number of times to run the simulation (int)\n";

/**

* The entry point for the program.

*

* @param args Command Line Arguments

*/

public static void main(String[] args) {

Scanner scan = new Scanner(System.in); // Used to read in from the

// command prompt

System.out.println("Assignment #2\nKenneth Lee\n\n");

System.out.println("Please enter your input string:");

String inputLine = scan.nextLine(); // Read in a line from the user.

String[] inputParts = inputLine.split(" "); // Split the string by

// spaces to get our 3 parts

int dimensions = inputParts.length - 2;

/*Initialize some variables

* changeValues keeps track of p, q, and r values

* startingSpace stores the initial state

* currentSpace stores the current state

* numOfTests will store the number of times the simulation will be run

* startingSpaceInt is the number input by the user which must be translated

Page 9: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

* to a binary type of representation.

*/

double[] changeValues = new double[dimensions];

boolean[] startingSpace = new boolean[dimensions];

boolean[] currentSpace = new boolean[dimensions];

int numOfTests = 0;

int startingSpaceInt = 0;

//Store the p, q, and r values.

try

{

startingSpaceInt = Integer.parseInt(inputParts[0]);

for (int i = 0; i < dimensions; i++)

{

changeValues[i] = Double.parseDouble(inputParts[i+1]);

}

numOfTests = Integer.parseInt(inputParts[inputParts.length - 1]);

}

catch (NumberFormatException e)

{

System.out.println(USAGE_STRING);

System.exit(1);

}

//This will take the three digit "integer" read in from the command line and

//reinterpret it as a string of bits (represented by boolean values where

//false = 0 and true = 1).

for(int i = dimensions - 1; i >= 0; i--)

{

if(startingSpaceInt % 10 == 1)

{

startingSpace[i] = true;

currentSpace[i] = true;

}

Page 10: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

else

{

startingSpace[i] = false;

currentSpace[i] = false;

}

startingSpaceInt /= 10;

}

double average = 0;

for(int i = 0; i < numOfTests; i++)

{

printBoolArray(currentSpace);

while(!finish(startingSpace, currentSpace))

{

double randNumber = Math.random();

double totalProb = 0.0;

//Cycle through the probability values until a scenario is true

for(int k = 0; k < changeValues.length; k++)

{

totalProb += changeValues[k];

if(randNumber < totalProb)

{

currentSpace[k] = !currentSpace[k];

break;

}

}

printBoolArray(currentSpace);

average++;

}

//Copy the values from startingSpace into currentSpace

for(int k = 0; k < currentSpace.length; k++)

{

currentSpace[k] = startingSpace[k];

Page 11: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

}

System.out.println();

}

average /= (double)(numOfTests);

//Used to format the average

DecimalFormat doubleFormat = new DecimalFormat("0.0");

//Print out the special data

System.out.println("Average: " + doubleFormat.format(average));

}

/**

* Prints out an array of booleans as if it were as String of bits.

* @param b the array to be printed

*/

static void printBoolArray(boolean[] b)

{

for(int i = 0; i < b.length; i++)

{

if(b[i])

System.out.print("1");

else

System.out.print("0");

}

System.out.println();

}

/**

* Checks to see if the particle has reached the antipode (the point when

* a and b are the compliments of each other). Assume that a and b have the

* same length.

* @param a A boolean array representing bit values

Page 12: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

* @param b A boolean array representing bit values

*/

static boolean finish(boolean[] a, boolean[] b)

{

for(int i = 0; i < a.length; i++)

{

if(a[i] == b[i])

return false;

}

return true;

}

}

Question: how does the displacement delta L of the particle from its starting point depend on time, on average, for large enough times?

Answer: L ~ sqrt(t)

For the extra credit, you need at least demonstrate the graph of the L and t with different d values (like d=4,5 and 6). Then from those graphs you can see similar pattern as for d=3. So, for higher d > 3 one still has L ~ sqrt(t). Pretty curious results showing that some fundamental laws are dimension-invariant.

COMMON PROBLEMS

Question 1:

The most common problem was that some groups made a wrong assignment of the probabilities for each bit.

Question 2:

Some groups could not find the correct trend ( L ~ sqrt(t) ) probably because they graphed t vs L instead of L vs t. It causes misunderstanding.

HINTS

Question 1:

This problem is a general version of the problem 1 of Homework #2, so it is a good idea to start from that.

The key for this problem is the assignment of the 3 probabilities from a given random number (assuming that 0< Random Number < 1). One way to do this is through the following code:

if( RandomNumber< p)

bit_1 = complementary_bit(bit_1) ;

else if (RandomeNumber< p + q)

Page 13: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

bit_2 = complementary_bit(bit_2) ;

else

bit_3 = complementary_bit(bit_3) ;

Question 2:

One can get a very clear and correct trend by averaging the results of different trials for a set of values of “t”. The averaging is done separately for each value.

HW 4Problem #1:  

In the doubling strategy you walk away with more money if you win at least one bet. The key is to realize that the only way you lose money with this strategy is if you can no longer place bets. Then you cannot recover and have to leave. The probability of never winning (or always losing) over k bets: P(loss) = P(lose 1st spin)*P(lose 2nd spin)*...*(lose kth spin) = (1/2)^k. In this question, the minimum bet is 1 dollar, so possible consecutive losses are 1, 2, 4, 8, 16, 32, 64, 128, 256, adding up to 511 dollars. With a k of 9, the probability of taking a loss is (1/2)^9 = 1/512.

Problem #2:A tricky part is that the money lost after kth unsuccessful bet is 2^(k) - 1. With an initial $1000, you can place 9 bets at maximum utilizing the doubling strategy. So the money lost after 9th unsuccessful bet is (2^9)-1=511 dollars. The money that remains in your pocket is 489 dollars.

COMMON PROBLEMS

Some students calculated the probability of a single bet and got a result of ½. This is not what the question was looking for.

Some students missed that "Your game strategy is to maximize the probability of a win" was mentioned. This is achieved by maximizing the number of bets that are placed, or equivalently, minimizing the starting wager ($1 in this case). These students did not mention that the first bet needs to be $1.

Some students made arithmetic errors (e.g., 1/256, $488). The primary source of confusion seemed to be that the probability of losing k bets is (1/2) ^ k but the money wagered in each bet is 2 ^ (k-1).

HINTS

You need to understand the condition of ‘Loss’ that problem defines. First, the ‘Win’ is defined as an event in which you leave the casino with more $1000. You can determine that, using the doubling strategy, a ‘Win’ occurs upon winning a single bet. So, a ‘Loss’ can only occur when you lose repeatedly until you lack the funds to continue betting.

Page 14: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

HW 5

You have a set of N elements. You choose, at random, a subset of it. Then, independently, you choose another subset of the original set of N elements. There are no limits on the size of either of the subsets, each can contain anywhere from zero to N elements. Neither size is specified. What is the probability p that the second subset is a subset of the first? Eliminate (cross out) as many wrong answers as you can from the list below, and briefly explain why next to each. Remember that an empty (zero elements) set is a subset of any set, and a set is obviously a subset of itself.

a) 1/N

b) (3/4)N

c) (1 + 1/N)N

d) (1 + 1/N)−N

e) (1/2)N

f) p > 1/2

Eliminate wrong answers by considering trivial limiting cases such as N=1. This specific case will eliminate all but (b) and (f).

Choice (f) is eliminated by noticing that when N → infinity, p → 0. Imagine a bowl filled with N >> 1 white marbles. You grab a handful, color them black and drop back into the bowl. Mix well. Then take out another handful of marbles. Obviously, the chance that all of them are black is miniscule.

COMMON PROBLEM

Some students test (3/4)N with N=1 and report that p = 1/2. There are four outcomes in this situation: {φ}-{φ}, {1}-{φ}, {φ}-{1} and {1} - {1} (elements in the first set vs. in the second set). Only the third pair doesn’t satisfy “the second subset is a subset of the first”. So, p=3/4.

Some students attempted to explain why (b) is correct without explaining why the rest of the options are incorrect.

Optimal packing of complex-shaped objects is a hard problem, but can sometimes be solved by clever algorithms (and excellent body mechanics). Prove that even if all of the faculty members of Computer Science at VT (40 professors!) put their heads together, they still will not be able to design a packing algorithm that would allow them to simultaneously fit into an average telephone booth. Assume booth dimensions of 1 × 1 × 2 meters. Further assume that professors are average people: weight 70 kg, height 175 cm, arm span 1.5 m.

The key to solve this problem is to find the ultimate upper bound: all available volume of the booth is filled, that is you assume that people are liquid. Since human buoyancy is neutral, human density is rho = 1000 kg/m^3. Now, 40 people of 70 kg each weigh a total of 2800 kg, and thus occupy at least 2800/rho = 2.8 m^3 volume. This is greater than the booth volume = 1x2 = 2m^3. It is also accepted to calculate the density when 40 people are fitted into the telephone booth, which is 2800 kg/m^3. This density is

Page 15: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

impossible for human beings. It is okay to use lb/inch if you are not familiar with metric system, but the density or the concept of liquidizing should be mentioned to solve the problem.

COMMON PROBLEM

Some students did not calculate the total volume of 40 professors from weight and density. Using density is the key to solving this problem.

Some students did not give specific calculations to explain why the booth volume (2m^3) is not enough for 40 professors.

Considering the human body as a cube or cylinder and calculating the volume of all 40 professors is not correct. No matter what kind of human geometry is used to solve the problem.

It is not correct to show that if one professor’s volume is greater than 2/40 m^3, then all of the professors cannot fit in the booth.

In-class extra credit assignment 3

In a large house there is an empty basement with a desk and a lamp on it. The lamp has a regular light bulb in it. There are no windows in any of the rooms and no light switches. In the attic there are three switches that look exactly the same. Let's call them switch #1, #2 and #3. Each switch has only "On" and "Off" positions. Only one of the three switches controls the light bulb in the basement. Once you leave the attic you may not return. You may visit the basement once, and once only. The basement, or the light emanating from it, is not visible from the attic. You may not call upon the help of any other persons, or use any additional apparatus. You cannot look inside the walls to see which wire is connected to the bulb, remove the lamp from the basement, etc.

The key to solve this problem is noticing the lamp heat can be used to determine the correct switch. Two possible solutions here: turn on #1 for a while and turn it off and turn the #2 on. Then go to the basement and check the lamp. If the lamp is warm and off then #1 is controlling switching. If the lamp is cold and off then #3. If the lamp is on then #2. Another solution is turn the #1 and #2 on for a while and turn one of them off. The result is the same with the first solution.

In-class extra credit assignment 2

How many days does it take to rescue all the N redhead people on the planet?

The easiest way to solve the problem is by using “simplify”. First work out the cases where the number of redheads is equal to 1, 2, and 3, and then generalize to where the number of redheads is equal to N+1. Use induction if you want a rigorous proof (suppose it’s correct for N, and based on this assumption, to prove the case N+1 is also correct). Think it through from your own perspective. N=1: “I come to the square. I see only greenheads around me. Ergo, I am a redhead. I must board. Ergo, all redheads leave that same day”. N=2: “I come to the square. I see one redhead. But he has not boarded that day. Ergo, it is not N=1 case, and I am a redhead too. We both board next day”. For greenheads, the N=2 logic would be like “I see two redheads. They boarded on day 2. Ergo, I am not a redhead, I don’t need to board.”

COMMON PROBLEM

Page 16: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

One common problem was stating that all redheads would board on the first day when they see the other redheads not moving. This could not be true, as just seeing the other redheads not moving is not enough to guarantee that you are a redhead.

Also note that “N” is not known apriori

HW6

1. Shown above is the only remaining color photo of an extinct butterfly species lepidoptera Darwinica. It was taken by C. Darwin on his last voyage to the Galapagos, but as you can see the time was not kind to the image: black areas on the wings are missing.

Task: Propose an algorithm that would restore the image. Use reasonable assumptions about butterflies. Do not code anything, just describe the main idea.

The biggest hint for this problem is that the wings of the butterfly are symmetric. Find the axis of symmetry and reflect the right wing against it: that will cover the missing bits on the left wing. Repeat for the left wing. That will repair the picture, unless there are symmetric missing parts.

2. How many N-bit strings contain anywhere from none to N/2 zeros (inclusive)? N is even. Hint: Use Mississimetry.

The biggest part of this problem is figuring out where the symmetry occurs. By looking at the first few even N = 2,4, etc., you can see that the number of permutations with a certain number of 0s is symmetric about N/2 zeroes (the number with no zeros is the same as the number with all zeros, the number with 1 zero is the same as the number with all but one zero, … until you reach the middle, N/2). Suppose there is X of such strings (and, by symmetry, X strings with up to N/2 ones. Now, unlike the odd N case worked out in class, we have a special string with exactly N/2 zeroes, which is the same string as one with exactly N/2 ones. Suppose the total number of such strings is Y. Obviously, 2X + Y = 2^N. But what you need is X + Y. You can now use the Mississippi formula to compute Y = N!/((N/2)! (N/2)!). Thus X = (2^N –Y)/2 with Y from the above.

COMMON PROBLEMS

The most common problem with this question was not finding where the symmetry occurs. Some students use the sigma sign to sum the number of strings with the number of zeros varying from 0 to n/2. Note that this procedure gives a valid answer but is less illustrative of the symmetry in the problem when compared to the more algebraic solution presented in the key.

Some students failed to notice that the problem for an even N is different than the problem for an odd N.

Some students didn’t use the correct logic to count the number of strings where there are exactly N/2 zeros.

Some students didn’t apply the Mississippi formula correctly.

Page 17: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

3. A subset of vertices of 5-dimensional (hyper) cube is used in an error detection code. Only those vertices that contain an odd number of zeros code for “words”. For example, (00000), (01111), or (00011) are code words, while (11100) or (11111) are not. How many such code words are there? Now generalize to N dimensions, where N is odd. Give a general solution.

There are N+1 different numbers of zeroes that the word could have (since it can from 0 to N inclusive), and since N is odd, N+1 must be even. With this in mind, you can see that there must be the same number of words with an odd number of zeros as there are with an even number of zeroes. 2^(N-1)

COMMON PROBLEMS

The most significant problem with this question was stating that the number of code words is half of N instead of half of the total number of N bit strings.

Some students just give the case of N=5, leaving out how to deal with an arbitrary N.

HW7

1. A bar of candy often comes organized into an m by n grid of rectangles so that it can be broken into smaller pieces. See the top of Figure 1, where the candy bar is a 3 by 4 grid of rectangles. In one move, you can pick up any piece of candy that has at least two rectangles and break it into two pieces along a horizontal or vertical line. See the two sample moves in Figure 1. Prove that, no matter how you move, it will take the same number of moves to break the candy bar down to its individual rectangles. If the bar consists of m × n rectangles, what is that number of moves?

There are two common ways to go about proving that the number of breaks for an m*n bar is m*n-1. The easiest way is to see that every time you make a break in a bar, you are left with 1 more piece than you had before. Since you need to be left with m*n pieces in the end, you must make m*n-1 breaks, with each break creating one additional piece.

2. Let O be the origin of the plane. Imagine that five points, not equal to O, are placed on the plane. Prove that there are two of those points, P and Q, such that the angle ∠POQ is acute. View this problem as an application of the pigeonhole principle. What are the pigeonholes? What are the pigeons? Complete the argument.

The easiest way to solve this is to see that any time there are two points in the same quadrant they must have an acute angle between them. Since there are only 4 quadrants, and 5 points, 2 of the points must be in the same quadrant.

3. You generate a random N-bit string, and compute X=∑i

N

x i, where xi are the 0 and 1

entries of the string. What is the probability that X is odd, if N is odd? (15 points extra credit) What is the probability that X is odd if N is even?

The problem can be solved by symmetry. For each string you can construct a 1->0 symmetric “image” of it by changing every entry of the string from 0 to 1 and from 1 to 0. Obviously, the total number of strings equals the number of their images; together, they make up a total of 2N strings. Now observe that for odd N the parity of the sum will change upon “imaging”; thus there are exactly the same number of “odd sum”

Page 18: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

strings as there are “even sum” string. Therefore, the probability that a random string is “odd sum”, which is exactly ½.

Answer=1/2. The extra credit problem can be solved by induction. Since N is even, N-1 is odd. We have shown in the previous problem that, for N-1 the probability is ½. To prove that form the (N-1) th statement there follows the Nth statement, just add one more bit to the N-1 bit string, at random. Adding the 0 bit does not change the parity of the sum, adding the 1 bit does. The probability of adding either is ½, the same as the “sum = odd” probability for the N-1 bit string. So the resulting probability is still ½ for the N bit string.

Quiz 1

1) The “Boys and Girls” problem we discussed in class assumed that “each couple continues to have children until a boy is born”. Solve the problem, that is find the ratio of boys to girls, using a more realistic assumption: “each couple continues to have children until either a boy is born or until they lose faith and stop after several (N>1) attempts. Each couple may have their own N”

ANSWER: The overall ratio of boys to girls is 1:1.

This is because for each generation the ratio of boys to girls is 1:1 and this is unaffected by the number of people who dropped out (lost faith) in the previous generation and the number of people who drop out this generation will also not affect the ratio in the next generation.

COMMON PROBLEMS

Many students forgot to take the couples losing faith into consideration. Many just considered couples making specific number of attempts, but without generalizing to

arbitrary numbers.

2) You are taken to a secret bunker, and asked if you can solver the following problem in 24 hrs: given a file with names and global positions of every human being on Earth, find a pair that is furthest away from each. Assume that each person’s position is given by accurate (x,y,z) coordinates. Your solution must be guaranteed to be correct, regardless of how people are distributed at the moment (e.g. everyone might happen to be in Australia). You only have a laptop with you with a C++ (or java) compiler. No internet. Start with a “Yes” or “No” then present quantitative, logical arguments.

Answer: NO.

Need to estimate problem size and resource, and compare the two. 

Problem size: 

the 1st step, before any sorting is done, is to calculate the person-person distance for each pair of people. 

Page 19: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

For N people you have N(N-1)/2 pairs, that is of an order of N^2. There are ~ 7 billion people on Earth. Thus, the problem size is around 2.5*10^19 calculations (or 4.9*10^19 if we used N^2. It does not matter, really, whether or not you take the factor of ½ into account here.)

Resource: laptop, see lecture notes. 3 GHz = 3*10^9 operations per second. 86400 seconds in a day. Total number of calculations in a single day: 3*10^9 *86400 = 2.5*10^14

Even being unrealistically generous and assuming it only costs one operation to find the distance between a pair of people, the resource is still vastly smaller than the problem size: 2.5*10^14 << 2.5*10^19.

COMMON PROBLEMS

Some students try to use convex hull algorithms to solve the problem. Note that since we do not know anything about how people are distributed, we can’t use any clever “coarse-graining” approximations, e.g. exclude from consideration all the pairs who are known beforehand to be close to each other, e.g. all people in Blacksburg. This is because, by the problem statement, we don’t have any information on that. As the problem states, all the people may happen to be in Australia.

Quiz 3

1. The Shuffle Play key on your I-Touch works as follows. When pressed, it plays a random song from your library of N songs. Suppose you have pressed Shuffle Play k times. What is the probability you will have heard your one most favorite song (at least once)? Provide ample explanations. (20 points).

Songs are not eliminated after they are played. Which makes it an easier problem than the one discussed in class. Again, we want to use the “complementarity” heuristic and find probability of NOT hearing the song and then use it to answer the question. Each time you have a 1/N chance of hearing the song and (N-1)/N chance of NOT hearing the song. After k presses, there is a ((N-1)/N)^k chance that you will not hear your song. And 1-((N-1)/N)^k chance that you hear your song at least once.

COMMON PROBLEMS

A common problem of this question is considering playing each song a non-independent event. Another problem is failed to use complementarity logic. Some students starts from a simplified case (N=1), and recursively test N=2 and more. If not

using the complementarity, the question is hard to solve.

2. Without a calculator, estimate the above probability for N=k=100. You must show the math. Assume e≈3, base of natural log. HINT: If X^k=Y. then klnX=lnY.

First calculate ((N-1)/N)^k; Assume ((N-1)/N)^k = X.

Then log X = log(((N-1)/N)^k) = k * log((N-1)/N) = k * log(1-1/N)

k* log(1-1/N): Taylor series: k * log(1-1/N) ~= k * (-1/N ) = Y;

Substituting the values of k and N we can find log X = Y;

Page 20: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

In this problem e = 3, and e^Y = X , we know X=3^(-1)=1/3;

So 1 - ((N-1)/N)^k = 1 – X = 2/3;

COMMON PROBLEMS

If the answer of the first question is incorrect, it is hard to solve the extra-credit problem. Some student’s answer is greater than 1, which is impossible.

3. How many distinct permutations of letters in the word “MISSISSIPPI” are there, if each “I” is written in a different color ink (e.g. the first “I” is blue, the second is red, etc.). In this problem, a permutation does not have to be a real word, e.g. IMSSISSIPPI is legitimate. (10 points).

As there are 4 letters S, which can be counted as same and 2 letters P which also can be counted as same, and all other letters (including colored Is) I are counted as different, the formula is:

11! / (4! * 2!)

COMMON PROBLEMS

Some students did not treat “I”s as different letters, thus come to a conclusion of 11! / (4! * 4! * 2!) Some students did not use the idea of repeating alphabets making difference in the overall

arrangements.

In-Class Extra Credit Assignment 4

You have a set of N elements. You choose, at random, a subset of it. Then, independently, you choose another subset of the original set of N elements. There are no limits on the size of either of the subsets, each can contain anywhere from zero to N elements. Neither size is specified. What is the probability p that the second subset is a subset of the first? An empty (zero elements) set is a subset of any set, and a set is obviously a subset of itself. Give full solution with ample explanation.

Pick the first subset with k elements. There are 2^k subsets to choose from. The probability that you would pick another subset that is one of these subsets is

2^k/2^n=2^(k-n)

That’s for one particular subset with k elements. The probability of you picking such a subset is

( 12 )n

(nk)Summing up all possible k gives you the answer:

∑k=0

k=n

2k−n2−n(nk )Since

∑k=0

n

(nk ) xn−k yk=(x+ y )n

Page 21: HW 2acourses.cs.vt.edu/.../Assignments/CS2104_Problems_Hi…  · Web view2018-04-28 · ANSWER: all three hands align just twice every day, ... HW7. 1. A bar of candy often ... How

We have:

∑k=0

k=n

2k−n2−n(nk )=∑k=0

k=n

2k−n2−n2−k 2k (nk)=∑k=0k=n

22(k−n)( 12 )k

(nk )=(12 + 14 )

n

=( 34 )n

In-CLASS extra credit #5

“The prisoner’s dilemma”.

The strategy is to use the color the first prisoner calls as a code message for everybody else: the code contains information about the rest of the hats that the first prisoner sees. E.g. prisomers agree that "blue" means  "I see an even number of blue hats in front" while red = "I see an odd number of blue hats". Then everybody else in front can figure out their hat color by knowing this first response and carefully taking into account all of the responses from everybody else behind them.

Whether this is a practical strategy is a different matter, but it is an algorithm that, in theory, saves 99 people guaranteed, while the 1st prisoner has a 50/50 chance of survival. There is no algorithm to guarantee his survival.

At a real interview, it is a good idea to mention that this perfect algorithm may not work in practice because it is extremely vulnerable to a single mistake by any of the 100 prisoners. You may want to ask if robustness of the solution is an issue and if they want you to think about more robust alternatives. The interviewers are unlikely to make you dig in further, but will be very pleased that you thought about real life aspects of your solution.