My coworker gave me a challenging question that I believe is NP but he won't take that as an answer.
Given a matrix determine how many non repeating numbers/letter combinations there are by picking only one number per column. It isn't acceptable to brute force (try all possible combinations) for this. He wants a formula to solve this problem.
For example he gave me this matrix
1 2 2 3
2 3 3 4
3 4 4 5
4 5 5 6
Some example results would be
1) 1 2 3 4
2) 1 2 3 5
3) 1 2 3 6
4) 1 3 2 4
5) 1 3 2 5
6) etc...
I wrote a java program which basically consisted of 4 for loops to go through all possible combinations (4x4x4x4=256 combos) to get I believe the answer was 36 possible combos. But to him this in unacceptable. And for the solution it can't be independent to one matrix alone it has to work for all n x n matrices.
Been racking my brain on this and I believe the problem is np(hard/complete) because it can be solved in polynomial time but there is no general algorithm you can do...you have to brute force it.
Any help/pointers/places of reference would be greatly appreciated...
Related
I am unable to understand this question.What this question want us to find and what is given.Can anyone just explain it in a naive manner .
Question
The Sheriff of the Hackers' community had a very urgent job to do. He
needs to diffuse all the bombs placed by Joker. Joker is extremely
clever and cruel. He selected a region of 1000 X 1000 points, chose N
center points of the form (X,Y) and placed bombs on every integral
point that was on or inside the circular region of radius R around
that chosen point. Then he went to every such point and activated
those bombs that lied on or inside at least two such circular regions.
In order to make an efficient plan for the Bomb Disposal Squad, You
need to help the Sheriff to determine the number of active bombs.
INPUT:
The first line of the input contains the N. Next N lines contain 3
space separated integers X Y R as described above.
OUTPUT:
Print the number of active bombs.
CONSTRAINTS:
1 <= N <= 10
1 <= X,Y,R <= 1000
NOTE :
The search area for the Sheriff is a square with diagonal points (1,1)
and (1000,1000)
Sample Input
2
5 5 2
3 4 3
Sample Output
9
Explanation
All of the following 9 points are inside atleast 2 circles
3 5
4 4
4 5
4 6
5 3
5 4
5 5
5 6
6 4
I have no idea how to approach this problem.Where did the joker plant the bomb and how should I check for it whether a particular region has bomb planted.Sorry I might have misunderstood this also.
the best way to explain this is starting with squares....
imagine a grid as such:
a 1 2 3 4 5 6 7
b 1 2 3 4 5 6 7
c 1 2 3 4 5 6 7
d 1 2 3 4 5 6 7
e 1 2 3 4 5 6 7
f 1 2 3 4 5 6 7
Lets say the center point was (c,4) and the width was 3. Your square would be drawn on points (b,3) (b,4) (b,5)(c,3),(d,3) (d,4) (d,5) (c,5)
(c,4) would be in the middle.....
So... with a square you would have 9 (add up all the edge points I described and the center point which is contained in the square).
You are being asked to do the same thing..... with circles.... Good luck...
The simplest way to explain what this question wants is:
Given a list of circles, defined by a center (X, Y) and a radius (R), how many integral points (i.e. integers, (1,4) vs (1.1, 4.4)) of the 1000x1000 grid are within 2 or more of those circles.
That's what is being asked.
There are a lot of ways this could be solved, but that would be a different questions.
The most basic technique would be to iterate across all 1000x1000 points, calculate teh distance from each point to the center of each circle, and for those points who distance is less than R for a given circle, add 1. All points with a value more than 1 would be reported.
This is not necessarily the most efficient algorithm. For example, if there are 4 circles in the list, you would be calculating 4,000,000 distances.
But it will work.
There are a lot of ways to solve this. One way is to check each point in the matrix to see how many circles it lies in. Another way is to keep a list of points inside a circle, and increment a counter if the point already exists.
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 9 years ago.
Improve this question
We have n cards with each card numbered from 1 to n.
All cards are randomly shuffled.
We are allowed only operation MoveCard(n) which moves the card with value n to the top of the pile.
We need to sort the pile of cards with minimum number of MoveCard operations.
The naive approach which i can think of is start with MoveCard(n), MoveCard(n-1), MoveCard(n-2).... MoveCard(1).
This approach will solve the problem in n MoveCard operations.
But can we optimize it.
For instance, If the input is like: 3 1 4 2
As per my approach:
4 3 1 2
3 4 1 2
2 3 4 1
1 2 3 4
MoveCard operations is 4.
But we can solve this problem with minimum number of moves:
Optimized solution is:
3 1 4 2
2 3 1 4
1 2 3 4
MoveCard operations is 2.
From the optimized solution above, I am feeling the following approach will solve the problem.
Always we are picking the element to move which gives the sorted elements on the top and bottom with a condition the maximum element in the sorted sub array from the start should be less than the minimum element of the sorted sub array from the bottom.
In this case:
3 1 4 2
Moving 2 we are getting 2 3 1 4 { 2,3 sorted from the start and 4 sorted from the bottom}
Now we are choosing 1 which gives the full sorted array. 1 2 3 4.
A simple way to do this is looking at the numbers in reverse order. If the top two aren't in order, move the lower one. If they are in order, look for the next one down. After you find one not in order, move that one, and then each other card below it in descending order.
Basically, find n. If n-1 comes after n in the array, move n-1 to the front. n--, and repeat.
For example:
2 4 3 1 // 3 comes after 4, so move 3
3 2 4 1 // move 2
2 3 4 1 // move 1
1 2 3 4 // done after 3 moves
3 1 4 2 // 3 comes -before- 4, so leave it alone. 2 comes after 3, move it
2 3 1 4 // move 1
1 2 3 4 // done after 2 moves
It ends up being the same as the naive approach, but starting only with the "optimum" start. You don't always have to move the top cards.
Worst case time complexity is O(n^2), simply because you have to do an unordered search for each number. I can't prove this is the best complexity possible, but it's surely the simplest and clearest way to do it.
Worst case number of moves is n-1, since you can always just leave the n card alone.
Now, if you just want to know how many moves you need, instead of actually sorting, you can stop at the first move. For example, if you have to move 3 because it comes after 4, then you'll need 3 moves. You can see this because if 3 is at the front, you'll always have to move every card below it to the front.
I'm creating an algorithm that can build an adjacency list from a list of edges.
For example, if the data input was:
1 2
1 8
2 8
3 5
3 1
4 5
4 6
5 2
5 9
6 4
6 8
7 4
7 10
8 4
8 6
9 4
9 5
10 7
10 3
The output would be:
1: 8 4 6
2: 4 6
3: 9 2 8
4: 2 9 8
5: 8 4
6: 5 4
7: 5 6 3
8: 5 6 4
9: 5 6 2
10: 4 5 1
The algorithm is obviously bounded by the number of vertices and edges so originally I was thinking it would be O(v + e). But I could only get the program to work by implementing for loops inside for loops with 2d arrays, which I believe cause complexity of O(N^2).
Can anyone help me better understand?
It depends a fair bit on what sort of data structure you are using to store the map from vertices to lists of adjacent vertices. Iterating through the list of edges is of course going to have time complexity O(e). Any larger time complexity is going come from the time required to find a vertex in the map and the time required to insert a new item into a vertex's list of adjacent vertices. If you were using flat arrays then you could have O(v*e) complexity (for each edge, loop through the vertex list to find the desired vertex), but this could be improved quite a lot by using a hash-table or tree data structure that gave you better lookup performance.
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.
I have a problem but I can't figure out a solution. It goes like this:
I have a directed graph with N nodes and M links and without cycles. I need to find out the minimum numbers of chains so every node belongs to only one chain.
Example:
7 11 7 nodes; 11 links
1 2
1 5
2 3
2 5
2 7
3 4 // link exists between 3 and 4
3 6
4 6
5 4
5 6
7 3
Answer is: 2
An example is
Chain: 2-7-3-6
Chain: 1-5-4
Thanks.
He doesn't need to know if the graph is hamiltonian - knowing that it's a DAG is enough. It's probably a contest or online judge problem? It does look too hard to be homework.
Solution here: http://www2.cs.science.cmu.ac.th/person/rogaway/ps3-solns.pdf
To find the matching efficiently, consider the Hopcroft Karp algorithm: http://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm