Can someone help me with my algorithm homework? [closed] - algorithm

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a bit of a problem with the algorithm proposed as homework by our teachers. It goes something like this:
Having a number of sticks like so:
4 (the number of piles to use)
11 7 5 4 (length of the sticks)
1 1 3 3 (how many sticks per length)
I have to figure out an algorithm that will form the minimal number of sticks by merging them. The solution for the previous example is this:
15 3 (15(optimal sum) * 3(minimal sticks) = 45 = 11*1 + 7*1 + 5*3 + 4*3)
11 4
7 4 4
5 5 5
Now I am not asking for you guys to solve this problem, but to give me a line to follow, I have tried to reduce it to a "Make Change" problem, it went good until the part where I had to select from the remaining solutions the good ones.
The complexity desired is an exponential one and the restrictions are:
0 < sticks < 100
0 < max_sum_of_sticks < 1000
So do you guys have a second thought on this?
Thank you very much for your time.
Explanation on minimal number of sticks : if for instance i had a set of sticks. The sum to be formed is 80, i have a fair amount of solutions :
1 stick of length 80
2 sticks of length 40
4 sticks of length 20 so on.
The first one is trivial and we discard it,for the remaining solutions I have to test if I can build them with the sets of sticks I have because there is a possibility that the solution chosen, for example 2*40, isn't a reliable one because we have sticks that were not used.

This looks a lot like the Knapsack problem.
You might also take a look at Branch and bound, which is a general algorithm for all kinds of optimization problems.

Like Julian, I'm not entirely sure what you mean, but it sounds a lot like the "Knapsack Problem" over multiple knapsacks, and is NP-complete. There are many different ways of approaching it - from the simple heuristics like "use the big stuff first", down to ant-colony (genetic) optimisation. And almost everything in between.
In fact, there are almost as many approaches as there are candidate sets... I wonder if the question is NP-complete? ;-p

Note: I'm calling merged sticks "piles" in this answer.
Of course, the solution is always "1". Merge all of your sticks into one big pile; you have optimal sum = total length of all sticks and minimal piles = 1.
Now, assuming you want the next smallest number after 1, there are a few feasible options. Would you try 2 minimal piles? Why not? What about 4? 5?
Let's say you are left with two candidates, 3 and 5. (i.e. optimal sum=15,minimal piles=3 and optimal sum=9,minimal piles=5) If you know you can arrange your sticks into 3 piles of length 15, do you need to check 5 piles (and what length would they be)?
So, the problem comes down to finding whether you can arrange your sticks into m piles of length n.
I'm sure there's a lot of literature on this problem, but if I were doing it for homework, I'd start by solving it on my own.
And I would start by trying to form one pile of length n. Then trying to form m-1 piles of length n with the remaining sticks...
The thing to be careful about with this approach is that you may form a wrong pile at any given time, so you'd need a way of backtracking and trying another combination. For example, suppose we have these sticks: 20 1 7 7 7 7 14 6 15 and are trying to form 4 piles of length 21. This is possible with the combination (20 1) (7 7 7) (7 14) (6 15), but if you start with (14 6 1), there's no solution that will give you 3x21 piles for the rest of the sticks. Now, I'm not sure if this indicates that 4x21 is not the answer. (The answer is, in fact, 2x42.) If that were the case, you would not run into this problem of "wrong" piles if you always started with the smaller number, i.e. tried 2x42 before trying 4x21. However, not being sure, I'd write code that would backtrack and try all the different combinations before giving up.

I am not sure I understand the problem.
I assume each pile has to be the same length? That is, the sum of length of sticks has to be same in all piles?
We have here three piles. Where did this three come from? If it were possible to make 2 piles, which one would you choose? For example, if you only have six sticks of X length, would you make three piles each with two sticks or two piles, each with three?
I guess the brute force methods is: you try to make X piles. Put all permutation/combination in each pile and see if you end up with the same total length in each.
Would it help if you give unique names to each stick? In this case, you have 11-1, 7-1, 5-1, 5-2, 5-3, etc.

Related

Find a 4 digits number without repetition

I've received a question to solve. I've manage to solved it by hand but couldn't find an exact algorithm to solved it.
The question:
The user insert a 4 digits number without repetition of any of the digits (can have a leading 0).
The algorithm needs to find this number.
There is a check function that received a 4 digits number and returns how many numbers were correct, and how many of the numbers were in the correct place without overlap, and without mentioning which one is.
The system has 7 tries.
For example:
The user insert the Number: 0584
If the system sends the number: 6549
The return would be:
1 item in place
1 item is correct
I would try to solve this puzzle in 2 steps:
Try to find out which 4 digits the solution contains of.
For the miss-placed positions find the right order.
I'm not sure if there is an algorithm to always find the solution within 7 requests, but this way you should at least get closer to a solution.
It's a puzzle, so think about digit-patters that reveal as much information about the solution as possible. And try to extract as much information from the answer to reduce the number of still possible solutions.

Approaching algorithms with modulo usage

I was doing some coding challenges and a problem came up that said roughly this:
"Two players each taking turns starting with player one. There are N
sticks given, each player takes 1, 2 or 3 sticks on their turn, the
player to take the last stick loses, the goal is to find an algorithm
that lets player one win with certainty (not always possible, player two is supposed to take turns that will ensure victory) and output 1, 2 or 3 as
the starting amount of sticks taken or 0 if it's impossible to win.
Input is N. Example: Input:2 Output:1"
I tried to think about it but all I came up with is that it would take checking every possible outcome because of all the possibilities that could be chained together if N is big. I also thought that if the last stick is to be taken by player 2 so as to not lose, that is N-1 is taken by player 1 (whether by taking N-1 only or N-1, N-2 or N-1, N-2, N-3) leaving N to player 2, that is the only way to ensure victory.
It turned out that the solution was (N-1) mod 4, but I can't understand why that is the case.
So my question is how do you approach a problem like that and why is the solution a modulo? Also is there a way to spot modulo problems like these? Other coders did it fairly quickly so I suppose practice makes perfect, but I have no idea where to start from.
It is modulo 4 because if one player has the advantage, he can keep the same advantage by taking 3 sticks if the first player took 1, 2 if the first player took 2, and 1 stick if the first player took 3. The other player simply doesn't have any control anymore.
Take the problem backwards :
You don't have to care about a big N, you just need to analyze what the situation looks like when only 4 sticks or less are left.
Who will win when there are 1, 2, 3 or 4 sticks left?
Who will win when there are 4n+1, 4n+2, 4n+3 or 4n+4 sticks left?

What can be a possible algorithm for this combinatorics based program?

So, this contest is already over.
I was trying to solve this problem: http://codeforces.com/contest/554/problem/C
I spent like 1 hour to solve this problem. What I thought was, fill the last n positions of the array with one ball of each kind. Then, in the remaining positions, find the permutations by calculating remaining places in array divided by balls of each kind - 1 (since one is fixed at last position). This will obviously miss out on a lot of test cases, since I don't consider cases when 2 largest numbers will be together in the end or 3 largest numbers will be there. Similarly, along with 4 numbers, other similar numbers might be there before them. But I mean, I am not able to think of a approach how should I solve this?
Any inputs will be greatly appreciated. Thanks!
Also, the contest has already ended, so no issues there. :)
Hints
Consider the example given where we have 1,2,3,4 balls of each colour.
Place the first ball: 1 option.
Now consider placing the 2 balls of the next colour. Place one at any position (2 choices - either before or after the first ball), then place the second at the end.
Now consider placing the 3 balls of the next colour. Place two at any position C(1+2+2,2), and the last at the end.
Finally consider placing the 4 balls of the final colour. Place three at any position C(1+2+3+3,3), and the last at the end.
This gives 1680 choices.

Finding the counterfeit coin from a list of 9 coins

Just came across this simple algorithm here to find the odd coin (which weighs heavy) from a list of identical weighing coins.
I can understand that if we take 3 coins at a time, then the minimum number of weighings is just two.
How did I find the answer ?
I manually tried weighing 4 sets of coins at a time, weighing 3 sets of coin at a time, weighing two coins at a time, weighing one coins at a time.
Ofcourse, only if we take 3 coins at a time then the minimum number of steps (two) is achievable.
The question is, how do you know that we have to take 3 coins ?
I am just trying to understand how to approach this puzzle instead of doing all possible combinations and then telling the answer as 2.
1 http://en.wikipedia.org/wiki/Balance_puzzle
In each weighings, exactly three different things can happen, so with two weightings you can only see nine different overall things happening. So with each weighing, you need to be guaranteed of eliminating at least two thirds of the (remaining) possibilities. Weighing three coins on each side is guaranteed to do this. Weighing four coins on each side could maybe eliminate eight coins, but could also eliminate only five.
It can be strictly proved on the ground of Information Theory -- a very beautiful subject, that builds the very foundations of computer science.
There is a proof in those excellent lectures of David MacKay. (sorry but do not remember in which one exactly: probably one of the first five).
The base-case is this:
How do you know that we should take three coins at a time ?
The approach :
First find the base-case.
Here the base-case would be to find the maximum number of coins from which you can find the counterfeit coins in just one-weighing. You can either take two or three coins from which you can find the counterfeit one. So, maximum(two, three) = three.
So, the base-case for this approach would be dividing the available coins by taking three at a time.
2. The generalized formula is 3^n - 3 = (X*2) where X is the available number of coins and n is the number of weighing's required. (Remember n should be floored not ceiled).
Consider X = 9 balls. 3^n = 21 and n is ceiled to 2.
So, the algorithm to tell the minimum number of weighing's would something be similar to:
algo_Min_Weight[int num_Balls]
{
return log base 3 ([num_Balls * 2] + 3);
}

Choosing permutations with constraints [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to solve the “Mastermind” guessing game?
I have to choose k items out of n choices, and my selection needs to be in the correct order (i.e. permutation, not combination). After I make a choice, I receive a hint that tells me how many of my selections were correct, and how many were in the correct order.
For example, if I'm trying to choose k=4 out of n=6 items, and the correct ordered set is 5, 3, 1, 2, then an exchange may go as follows:
0,1,2,3
(3, 0) # 3 correct, 0 in the correct position
0,1,2,5
(3, 0)
0,1,5,3
(3, 0)
0,5,2,3
(3,0)
5,1,2,3
(4,1)
5,3,1,2
(4,4)
-> correct order, the game is over
The problem is I'm only given a limited number of tries to get the order right, so if n=6, k=4, then I only get t=6 tries, if n=10,k=5 then t=5, and if n=35,k=6 then t=18.
Where do I start to write an algorithm that solves this? It almost seems like a constraint solving problem. The hard part seems to be that I only know something for sure if I only change 1 thing at once, but the upper bound on that is way more than the number of tries I get.
A simple strategy for an algorithm is to come up with a next guess that is consistent with all previous hints. This will eventually lead to the right solution, but most likely not in the lowest possible number of guesses.
As I can see, this a variation of mastermind board game http://en.m.wikipedia.org/wiki/Mastermind_(board_game)
Also, you can find more details about the problem in this paper
http://arxiv.org/abs/cs.CC/0512049

Resources