Minimum Distance - algorithm

There is a set of 9 students and 3 schools Every school can be alloted at max 3 students .Every school and student has its coordinates .Now we have to allot student in such a way that the sum of distance from all the student to the school should be minimum.
I was asked this question in an interview.Can anyone suggest an algorithm for this?
Initially I tried greedy approach but that does not work.Then I tried applying a dynamic programming approach but could not come up with an optimal sub-structure.

Every school has 3 places, all 3 schools have 9 places. And you should find the best match between 9 places and 9 students.
This assignment problem may be solved with Hungarian algorithm.

How about exhaustive search since the problem size is small enough?
First school chooses 3 students out of 9 to start.
Second school chooses 3 students out of 6 remaining.
Last school gets stuck with the 3 students remaining.
So (9 choose 3) * (6 choose 3) = 1680

Related

Maximize revenue algorithm [duplicate]

This question already has answers here:
Algorithm design: can you provide a solution to the multiple knapsack problem?
(2 answers)
Optimal way of filling 2 knapsacks?
(3 answers)
Closed 6 years ago.
We have two boxes, size of 20 and 30. We want to pack fruits in the boxes in a way that gives us the biggest revenue.
Price Size
APPLES 5 19
ORANGES 11 11
BANANAS 2 5
KIWI 8 16
MANGO 22 23
CHERRY 2 9
Example looks very similar to rod cutting problem. First solve rod cutting problem for the first box (size of 20) and then with the remaining items solve rod cutting problem for the second box (size of 30). Or vice versa.
But then I came up with this example:
If we first pack first box (size of 20), the best combination leads to Oranges (11|11) and Bananas (2|5) which gives us price 13 and size 16. Rod cutting 'algorithm' will always choose best Price/Size combination.
Now lets continue with the second box (size of 30).
We pack Mango (22|23) and that's it. We have ran out of space for any other fruit.
Total revenue of two boxes in this example equals to 13 + 22 = 35. However, we could have had revenue of 37, with Cherry (2|9) in the first box and Bananas (2|5) in the second one.
If we first packed the second, bigger box, we would get correct result. But does that way (packing boxes starting with bigger ones) always works? I believe, despite starting with bigger boxes, there will always be the case where this algorithm will fail. What would be better approach to solve this problem?
EDIT:
In this example we have two boxes but it could be N boxes. I am looking for a solution that would solve this problem in general, N boxes.

Dynamic programming problem for minimum cost [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a cell tower question. There are n towns. We want to build cell tower in some of the towns. Each cell tower can cover itself and its neighbor. Each town has a cost to build cell tower. We want to find out the minimum cost to build the cell tower to cover all towns.
For example,
(1)
TOWN 1 2 3
COST 5 1 2
We select to build cell tower in town-2. The cost is 1.
(2)
TOWN 1 2 3 4
COST 5 1 2 3
We select to build cell tower in town-2/3. The cost is 1+2=3.
(3)
TOWN 1 2 3 4
COST 5 1 3 2
We select to build cell tower in town-2/4. The cost is 1+2=3.
It's a dynamic programming algorithm. How can I solve it?
Thanks
Ling
I'd go with something among the following lines:
f(0,_) = 0
f(1,true) = 0
f(1,false) = cost[1]
f(x,true) = min{ f(x-1,true) + cost[x], f(x-1,false) }
f(x,false) = min { f(x-1,true) + cost[x], f(x-2,true) + cost[x-1]}
The idea is:
x is the current number of city we are looking at, and the boolean is true if this city is already covered (by the city from the left).
f(0,_) is an empty base clause - it is free to cover nothing.
f(1,false) is base where city 1 is not covered, so you must put a tower there, and f(1,true) is a base where city 1 is covered, so you don't need a tower and you are done.
f(x,true) is the case that the city x is already covered, so you can put there a tower, and continue to the next city [which is also covered - f(x-1,true)], or don't put a tower there, and the next city is not covered [f(x-1,false)]
f(x,false) is the case where city x is not covered, so you basically have two choices, or put a tower in there, and then continue to f(x-1,true). Or put a tower in the next city (in x-1) and then continue to f(x-2,true)
Start with f(x,false), where x is the last city, and you'll get the minimal solution.
It is easy to see that this recursive formula can easily be modified to DP.

Unique methods to generate sudoku puzzle [duplicate]

This question already has answers here:
How to generate Sudoku boards with unique solutions
(15 answers)
Closed 8 years ago.
How many possible unique ways are there to generate a Sudoku Puzzle??
I can think of only two possible ways
1) Take a solved Sudoku puzzle and shuffle the rows and columns
2) Generate a random number and check if it violates any Sudoku constraints, repeat untill number does not violate any Sudoku constraint for every square(theoretically possible but normally it leads to deadlocking )
Are there any other ways?
Here is a 20-page PDF, titled "Sudoku Puzzles Generating: from Easy to Evil", that you'd probably find useful in your quest.
To answer your question:
Are there any other ways?
Yes. Yes there are.
Simple way to generate up to Take a Solved Sudoko puzzle,
step 1 ) replace all 1 with A , 2 with B till 9 with I,
Step 2) do a shuffle in each horizontal and vertical block block of using a random between 1 and 3, here in each there can only be 3 possible combinations.
step 3) now shuffle the block there can only be 3 vertical and 3 horizontal shuffle
step 4) rotate the block 1 to 4 time..
step 5) mirror the puzzle vertically and horizontally using a random between 1 and 2.
step 6) replace all A with any number 1 to 9..
guessing this will produce around 38,093,690,880 combos....

number of possible sudoku puzzles [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
Wiki http://en.wikipedia.org/wiki/Mathematics_of_Sudoku says Sudoku has 6,670,903,752,021,072,936,960 possible permutations.I tried to find out but it seems difficult.Can someone tell me how this number is calculated.
You can find all about it in this Wiki: http://en.wikipedia.org/wiki/Mathematics_of_Sudoku.
"the number of valid Sudoku solution grids for the standard 9×9 grid was calculated by Bertram Felgenhauer and Frazer Jarvis in 2005 to be 6,670,903,752,021,072,936,960 . This number is equal to 9! × 722 × 27 × 27,704,267,971, the last factor of which is prime. The result was derived through logic and brute force computation."
You can read the most recent rewrite of the original publication by Bertram Felgenhauer and Frazer Jarvis : Mathematics of Sudoku, it details the computation over 7 pages. The calculation actually isn't trivial (the idea being to enumerate distinct and valid Sudoku grids, rather than all possible arrangements of digits over a 9x9 grid).
Interestingly there was an estimation of the number of possible sudokus posted in an internet forum before the actual value was calculated and published by Felgenhauer & Jarvis.
The author of the post points out that there are some unproven assumptions in his guess. But the estimated value differs by 0.2% from the actual value published later.
In this Wiki you can find some estimation of other types of sudoku based on similar guesses.
Here is the full post from The New Sudoku Players' Forum:
by Guest » Fri Apr 22, 2005 1:27 pm
Lets try this from a whole different direction:
Step A:
Pretend that the only 'rule' was the 'block' rule, and that the row and column rules did not exist. Then each block could be arranged 9! ways, or 9!^9 ways to populate the puzzle (1.0911*10^50 'solutions').
Step B1:
If we then say 'let us add a rule about unique values in a row', then the top three blocks can be filled as follows:
Block 1: 9! ways
Block 2: 56 ways to select which values go in each 3-cell row, and 3! ways to arrange them (remember that we haven't invented a column rule yet).
Block 3: with 1 and 2 filled, the values that go in each row is now defined, but each row can be arranged 3! ways.
Therefore, we have 9! * 56 * 3!^6 ways to fill the top three blocks, and this value cubed to fill all nine blocks. (or 8.5227*10^35 solutions). Note that this represents a 'reduction ratio' (denoted as R) of 1.2802*10^14, by adding this one new rule.
Step B2: But we could have just as easily added a 'unique in columns' rule, and achieved the same results downward instead of across, with the same value of R.
Step C: (and here is where my solution is not rigorous) What if we assume that each of these rules would constrain the number of valid solutions by exactly the same ratio? Then there would be a combined reduction ratio of R^2. So the intitial value of 1.0911*10^50 solutions would reduce by a factor of R^2, or 1.639*10^28, leaving 6.6571*10^21 valid solutions.
This post and the account are attributed to Kevin Kinfoil (Felgenhauer & Jarvis).
Additional notes
Assume the Block 1 is
1 2 3
4 5 6
7 8 9
Then we have the following possibilities for Block2, if we ignore the order of the rows
1 2 3 4 5 6
4 5 6 7 8 9
7 8 9 1 2 3
this is 1 possibility
1 2 3 7 8 9
4 5 6 1 2 3
7 8 9 4 5 6
this is 1 possibility
1 2 3 two of 4,5,6, one of 7,8,9 3*3
4 5 6 the two remaining of 7,8,9, one of 1,2,3 3
7 8 9 the two remaining of 1,2,3, the remaining of (two of 4,5,6) 1
these are (3*3)*3*1=27 possibilities
1 2 3 two of 7,8,9, one of 4,5,6 3*3
4 5 6 two of 1,2,3, the remaining of 7,8,9 3
7 8 9 the two remaining of 4,5,6, the remaining of two of 1,2,3 1
these are (3*3)*3*1=27
So all in all these are 1+1+27+27=56 possibilities.

find number of tennis matches required [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Hi I came across this question from my friend.
Give me a generalised formula to find how many tennis matches (singles) are required for n players ?
Example : if the number of players are 16 then
first : we need 8 mataches (for 16 players) , here 8 players will be eliminated and 8 players will be there
secode : we need 4 matches (for 8 players) , here again 4 players will be eliminated and 4 will be remaining
third :
we need 2 matches (for 4 players) , here again 2 players will be eliminated and 2
will be remaining
Final :
we need 1 macth to decide a winner among the 2 players
so totally 15 matches are required.
I need a generalised formula to find , such that if I give the value n I should get the number of matches required to find the winner
n may be odd or even
For elimination game, the number of matches is always n-1, because one player will be eliminated after one game and n-1 players have to be eliminated in total.
As eventually every player but 1 (the champion) has to lost his match (and every player can lost only in 1 match) then the number of matches required is n-1
n-1, because there is one player to leave after each game. and the champion remains

Resources