Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
SHORT STORY :
I need a 8x8 matrix, that can have many solutions (a
well known existing problem)
Solutions as in, it should have only 8 positions which are 1, rest 0.
A good example can be 8 queen problem.(Placing 8 queens in such a way that they don't kill each other)
Queen problem
so we can create a matrix, which will have 64 positions in all,and 1 represents the current queen position, and 0 a blank position.
LONG STORY:
I am creating an algorithm for Steganography which needs a 8x8 matrix(say A) to store the pixels of an image(8 pixels at a time,8 bits each).
Another 8x8 matrix say B, is to be created which contains data such that Only 8 places should have a 1,rest 0.
These bits are then collectively mapped to matrix A, to check and evaluate an 8 bit resultant
ASCII valued string.
So, I can make many combinations like that, say combinations of 8 kings on a 8x8 chessboard so that the don't kill each other.But it's not a well known,or a tricky problem.
Any ideas for creating such a matrix? The idea can be from anywhere, not generally related to a chessboard.
Consider the 8x8 fields as the adjacency matrix of a graph with 8 nodes and 8 edges
Enumerate all these graphs
Find "interesting" properties of these graphs.
Example: "Has a chromatic number of 3"
Example: "Contains at least / at most / exactly A nodes with degree B"
...
Profit
One way to proceed is to start with a unit matrix (i.e. 1 entries only in the main diagonal and 0 entries elsewhere) and to randomly shuffle the rows.
Since you're not going into detail about the problem itself, I guess what you are asking for is a data structure to accommodate the matrix, and not the algorithm to solve the problem.
A valid datastructure would be a two-dimensional array int[8][8] or even boolean[8][8], since you only need two states per field.
If you're asking for a datastructure that verifies the rule of not more or less than eight marked fields, you can implement a wrapper class around such array that contain the logic to validate.
I might have grossly misunderstood you though.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a long with a single bit set and I need to know which it is, e.g. the index. I need to calculate this as fast as possible.
The naive idea is to divide by 2 and check whether the result is 1. But this would need up to 63 iterations (worst case).
My next idea was to make like a binary search, e.g. to look wether it is bit 63-32 or 31-0 then 63 - 48, 47 - 31, 31 - 16, 15 - 0 and so on having many if-else statements, but this gives me hell of a bunch of code...
Furthermore I'd like to minimize object creation and memory used. You might suggest that I'm wrong then with Java and should use perhaps C/C++. Well it's for a school competition and I don't have a choice :)
I'd like to see some sample code!
Use Long.numberOfTrailingZeros - this will be exactly the index you are looking for.
Long.numberOfLeadingZeros can be also useful if you count bits starting from the highest one.
Both methods are JVM intrinsics, i.e. they are treated specially by JIT compiler. These methods are translated to a special CPU instruction (TZCNT / LZCNT) and thus are very efficient.
You could prepare a Map<Int, Int>, holding the number of the set bit for each possible value, but I'm not sure if it is really faster than a loop.
Maybe bit shifting is faster than dividing by 2.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm looking for an algorithm to combine two values;
Where the first value indicates a positive result when the value is higher. For example, 0.98 is 'good' and 0.15 is 'bad'.
Where the second value indicates a positive result when the value is lower. For example, 10,000 is 'bad', whereas 1000 is 'good'.
I need a method of determining a value that can represent both of these scales with one number, so that I can sort my findings on my application from high to low accordingly. I'm not sure if anyone knows of such an algorithm, or any advice, but any help is greatly appreciated. Thank you.
P.S. I am aware I can 'negate' one of the two values, to have them appear on a similar scale, however I'm not sure how this would work in Java.
EDIT: Sorry, so to elaborate, I'm sorting images based on similarity to a user input image. Each of my algorithms that I'm using to return a value of similarity, function on a different scale. The first being a value between 0.00 and 1.00, with numbers being closer to 1.00, indicating the image is more similar to the original. Whereas, my second algorithm returns values from 1000+, with higher values indicating the image is less similar to the original. I need to take these two values and combine them to allow me to sort the resulting images in order of similarity, with the most similar image being shown at the top of my list, and the least similar at the bottom. Hopefully this helps clear up any confusion. Thanks again.
If your only goal is sorting, you need to come up with a function g(x,y) that represents the "goodness" of your pair of values. A pair (x1,y1) is better than (x2,y2) if and only if g(x1,y1) > g(x2,y2).
The function must represent what you consider "good". A simple example would be:
g(x,y) = x - y / 10000
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Why do we need two-dimensional or multi-dimensional array?
If we want a series of continuous elements of the same type, it can be achieved with simple 1-D array. Like int[] a = new int[15625] will allocate space 15625 elements, and
int [][][] b = new int[25][25][25] will also allocate space 15625 elements
Why do we need them if things can be achieved with 1-d?
You don't need them, but doing grid[x][y] is nicer than grid[x + y*width]
Because sometimes data isn't one dimensional.
For example, the classic game breakout. You could organize your bricks into a one dimensional array and each turn process which row they are in, however it makes more sense to make it a two-dimensional array where the first array is the rows, and the second array is the bricks.
i.e:
bricks[0][3] is the fourth brick of the rirst row
While this could still be done with a one dimensional array, there are math applications where multi-dimensional arrays are necessary.
In addition to this it allows you to have arrays of an infinite1. size, whereas arrays are limited to 230
1. mileage may vary
As mentioned in the comments, multi-dimensional arrays in Java are actually nested arrays. Your nested arrays don't all need to be the same size which is one reason you would use them instead of a single array as you have proposed.
The only other way to declare such an array (off the top of my head) is to declare an array of type Object and then put arrays in it but you lose the basic array type safety. You can access the elements in nested arrays without the [][]... syntax, however.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
First time poster, sorry if I break any etiquette.
I'm studying for my Data structures and algorithms midterm and I have a practice problem I don't really understand.
Suppose you are given a sorted list of N elements
followed by f(N) randomly-order elements.
How would you sort the entire list if f(N) = O(1)?
How would you sort the entire list if
f(N) = O(log N)?
We have gone over lots of sorting algorithms but the exam focuses on Insertion, Quick and Merge Sort. I don't really understand what it means by if F(N) = O(log N). Is that using Big oh notation to say that the most amount of random elements on the end would be either a constant or log(N) in each respect case.
Thanks for any insight.
Edited: Fixed my obvious mistake in terms of Big Oh notation, not sure where to go from here though.
In the first example you are given a problem, where a constant number of non-ordered elements follow the sorted sequence. In essence this means that you may implement an algorithm to insert a single non-ordered element and then repeat it several times. The overall complexity of inserting all f(N) = O(1) elements will be the same. One of the algorithms you mention is best to perform this operation.
In the second case you have number of elements to be inserted in the order of log(n). In this case you can not ignore this number as it is dependent on the input size. Thus you need to think of a smarter way to merge it with the remaining part that is already sorted. (TIP: maybe the operation you need to perform will help you choose an algorithm?)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Given a sorted array of both positive and negative numbers (Example:-9, -7, -4, -0.9, 1, 2, 3, 8) i need to output the elements in the array in sorted order of their absolute values in less than O(N^2) without using any inbuilt function.
Does anyone know any acceptable solution for this simple problem?
I was thinking of modifying the quicksort algorithm to make it check the abs values for elements.
I would binary search for 0, conceptually split the list into 2 parts, and merge the two lists (the negative value one as the positive value of itself) into a single new one by walking the negative in reverse and the positive forward.
O(log n) for binary search, O(n) for the merge of 2 sorted lists.
Pick any 2-way comparison sorting algorithm of your pleasure with runtime bounds less than O(N^2). Quicksort is a valid choice.
When doing comparisons (which will show up in the 2-way comparison sorting algorithm), instead of comparing the values a and b compare abs(a) and abs(b). Just make sure that you don't replace a and b with abs(a) and abs(b), just use the latter two when doing comparasons
Just have two pointers at the end of the arrays then compare the absolute value with the end.
start == array[0]
end == array.length
while start != end
if abs(start) => end
put start in front of end
start++
end--
else
end--
I might be missing some pieces but that's the idea.
0(n) solution.