Below is my attempt at solving the 8 queens problem to print one solution. (place 8 queens on a chessboard such that none of them are attacking each other). However, this solution only places 6 queens. I need another opinion of where I am making a mistake. I'm doing this in more of a BFS style instead of backtracking.
It seems your algorithm is malfunctioning at some point. Upon running it, I found the following issues:
You are constantly setting visited[i][j] to 0 in your for loop in main. This always resets visited to 0 even if a recursion call is made. In fact, when you declare both visited and board they are initiated to arrays full of 0s. So you can get rid of both set statements in there. In addition, because you reset the arrays, your recursive function ends up setting both values to 0 and then finds them again."
For debugging, in the !hasQueen statement, you should output the board[row][col] coordinates, which show you the coordinates that have been found. The final list before it prints out the grid shows that 2,4 and 1,6 are found and set twice.
The actual chessboard that is output ends up with an impossible solution:
1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
0 0 0 X 0 Y 0 0
0 0 0 Y 0 X 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
(sorry I can't get the numbers to format)
Both layout X and layout Y fail the 8 queens rules.
If you run your program with the setting to 0 commented out, you will see that it grinds to a halt after finding 6 locations.
Related
I want to find efficient algorithm based on which subset it is. New condition is to be executed for each subset.
For eg: I have 4 flags ABCD and each subset will have seperate condition. What is the most efficient algorithm to solve the following condition. It can be made easily but I want to find the most efficient algorithm. Is there already an algorithm which solves this kind of problem?
A B C D
0 0 0 0 Subset 1 Execute Condition 1
0 0 0 1 Subset 2 Execute Condition 2
0 0 1 0 Subset 3 Execute Condition 3
0 0 1 1 Subset 4 Execute Condition 4
0 1 0 0 Subset 5 Execute Condition 5
0 1 0 1 Subset 6 Execute Condition 6
0 1 1 0 Subset 7 Execute Condition 7
0 1 1 1 Subset 8 Execute Condition 8
1 0 0 0 Subset 9 Execute Condition 9
1 0 0 1 Subset 10 Execute Condition 10
1 0 1 0 Subset 11 Execute Condition 11
1 0 1 1 Subset 12 Execute Condition 12
1 1 0 0 Subset 13 Execute Condition 13
1 1 0 1 Subset 14 Execute Condition 14
1 1 1 0 Subset 15 Execute Condition 15
1 1 1 1 Subset 16 Execute Condition 16
Bitmasking can be used to generate all subsets. There are four values. Therefore, you have 2^4 subsets. All you have to do is iterate this mask 2^4 times and mask it with each of the four values. In each iteration, the result of masking is a subset of the given values. Here's an idea:
allSubsets = {}
for mask in range(1<<4):
subsets = []
for i in range(0,3):
val = mask & (1<<i)
if(val)
subsets.append(a[i]) # Individual subset. Here assume array a has 4 values. Can be just 1s and 0s as in your case.
allSubsets[mask] = subset #keep appending each generated subset
return allSubsets # Do your operation by iterating on each of these subsets
I'm having a hard time thinking of an appropriate data structure to use to represent an adjacency matrix for an undirected graph.
I want to be able to take the nodes from these graphs and insert them into random positions in arrays, and then "score" the arrays based on how well they've managed to keep the adjacent nodes apart. i.e if node A and node B are connected in my graph, and the array places them next to each other, +1 would be added to the array's score, with the lowest scoring array being the best.
So what would be the best data structure to use to represent a collection of nodes, and the neighbouring nodes of each one in the collection?
If I understand your question which I do not think it really clear.
For an adjacency matrix, I think the best way to go is an Array. You can access each positions in O(1) and since it is an undirected graph, it should be easy to create. see graph below
0 --- 1------5---6
| \ \ | /
| \ \ | /
2 3----4---7
0 1 2 3 4 5 6 7
-----------------
0 | 0 1 1 1 0 0 0 0
1 | 1 0 0 0 1 1 0 0
2 | 1 0 0 0 0 0 0 0
3 | 1 0 0 0 1 0 0 0
4 | 0 1 0 1 0 0 0 1
5 | 0 1 0 0 0 0 1 1
6 | 0 0 0 0 0 1 0 1
7 | 0 0 0 0 1 1 1 0
------------------
You can implement your matrix like so and perform whatever operation you want on it. And all that matters is that if a location is not 0 then the graph is connected and you can just pick the highest value for whatever you are doing.
hello guys i have 2d char array opt[][] and i have 2 sequence in my arrays like in example
my
`opt[0][0]=A
opt[0][1]=T
opt[0][2]=G
opt[0][3]=A`
and
opt[1][0]=A
opt[2][0]=G
opt[3][0]=C
opt[4][0]=T
i have this output currently
x/y| A T G A -
_______________________
0 A | 0 0 0 0
1 G | 0 0 0 0
2 C | 0 0 0 0
3 T | 0 0 0 0
4 - | 0 0 0 0
my problem is this how can i use dynamic programming
to create this array into this
http://i.stack.imgur.com/ViHc9.png
if its a match 0 penalty
if its a mismatch 1 penalty
if its a gap its 2 penalty
i can compare chars of my array like this
for(int i=0;i<4;i++){
if(opt[0][i]==opt[i+1][0]){
result[0][i] =1;
}
but this is just a simple test i made to see if i can compare and it turned out i can.
how can i go from here to there(to the picture array
I suggest you read these articles.
http://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorithm
http://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm
The implementation in any language is pretty trivial.
And if you need information about dynamic programming in general,
either Google for it yourself, or check these two links.
http://en.wikipedia.org/wiki/Dynamic_programming
https://www.topcoder.com/tc?d1=tutorials&d2=dynProg&module=Static
In an incidence matrix we have:
e1 e2 e3 e4 e5 e6
v1 1 1 0 0 0 0
v2 0 0 1 1 0 1
v3 0 0 0 0 1 1
v4 1 0 1 0 0 0
v5 0 1 0 1 1 0
To find a path, we should find if the last vertex of and edge i is the start of another edge, and the last of the last edge is the first of the first edge.
Can someone help me to find a solution? I understand very well what it is but not how to implement it!
What do you determine as a path? Do you want to find out if it's possible to visit all node exactly once?
There are two general ways of searching a path: depth first or breadth first. If you have a look at some of the examples, it shouldn't be too hard to implement yourself.
I have a series of images. Each one is typically (but not always) similar to the previous one, with 3 or 4 small rectangular regions updated. I need to record these changes using a minimum of disk space.
The source images are not compressed, but I would like the deltas to be compressed.
I need to be able to recreate the images exactly as input (so a lossy video codec is not appropriate.)
I am thinking of something along the lines of:
Composite the new image with a negative of the old image
Save the composited image in any common format that can compress using RLE (probably PNG.)
Recreate the second image by compositing the previous image with the delta.
Although the images have an alpha channel, I can ignore it for the purposes of this function.
Is there an easy-to-implement algorithm or free Java library with this capability?
Experiment a little with existing lossless compressors -- PNG, lossless JPEG, etc -- on an image consisting of the changes only (you can use transparent background for PNG, or some uniform color). These algorithms are very efficient when it comes to compressing an image which is mostly constant, you'll not be able to beat them if you are not an expert.
If the number of rectangles is typically small, and the rectangles themselves are small, you can makes out rows and columns with differences, use that to come up with rectangles that might be different...
Imagine the images with the following pixel values...
0 0 0 1 1 1 2 2 3 3
0 0 1 1 0 0 1 1 2 2
0 0 1 1 0 0 0 1 1 2
0 0 1 1 0 0 0 1 1 2
0 1 1 0 0 3 0 0 1 1
0 1 1 0 0 3 0 0 1 1
0 0 1 1 0 0 0 1 1 2
0 0 1 1 0 0 0 1 1 2
0 0 0 1 1 1 1 1 0 2
2 2 2 2 2 1 1 2 2 2
...and...
0 0 0 1 1 1 2 2 3 3
0 1 1 1 0 0 1 1 2 2
0 1 2 4 0 0 0 1 1 2
0 1 2 3 0 0 0 1 1 2
0 1 1 0 0 3 0 0 1 1
0 1 1 0 0 3 0 0 1 1
0 0 1 1 0 3 3 2 1 2
0 0 1 1 0 3 3 2 1 2
0 0 0 1 1 2 2 2 0 2
2 2 2 2 2 1 1 2 2 2
First you would come up with a mask of which pixels rows, rows, and columns had differences...
0 1 1 1 0 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0 0
1 0 1 1 1 0 0 0 0 0 0
1 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 1 1 1 0 0
1 0 0 0 0 0 1 1 1 0 0
1 0 0 0 0 0 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0
The row and column data give us guidance as to where there might be rectangles...
0 1 1 1 0 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0
1 0 ? ? ? 0 ? ? ? 0 0
1 0 ? ? ? 0 ? ? ? 0 0
1 0 ? ? ? 0 ? ? ? 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
1 0 ? ? ? 0 ? ? ? 0 0
1 0 ? ? ? 0 ? ? ? 0 0
1 0 ? ? ? 0 ? ? ? 0 0
0 0 0 0 0 0 0 0 0 0 0
Iterate over each of the possible rectangles and decide if there are changes or not and then encode them. You can add other hashing axes instead of rows and columns, if you need to... like you can subdivide the picture into regions and hash on whether a region has any changes, then use the hash to decide whether or not a region needs to be encoded. That you could do an arbitrary number of times and have a reasonably quick algorithm that also produces small files.
Whatever the case, I think your best bet is to build a map of what has been changed and use aggregates that tell you if blocks have been changed to guide your decision-making. If you collect enough of these, you could even create a couple different algorithms that do good jobs under different circumstances and then put them in a Chain of Responsibility that decides which algorithm to use based on the characteristics of the map and hashes you built.
If the changes are going to stay rectangular you could save these sections separately, i.e. the original image plus the changes and their positions.