I have a 2D array with various entries at different positions. However some positions have same value (say 5). I need to find the nearest block with value 5 from any other positioned element.!
Image is in this link :)
This is the image to understand the problem better
In this pic above. We can use the concept of Digital Image Processing to find the m-distance between each blocks. But if the problem space is too big ( suppose an array of 100X100 or 200X200) then the solution in this way will be time taking.
In way to solution I found out these links.
Wikipedia Link for Nearest Neighbour
Apart from this how to map this whole thing in programming...?
You can try any PL/SQL code for this, then you can get the nearest point from there.
The simplest (maybe not most efficient) way is if you use the Wikipedia method #1 which is as follows:
Loop though all the coordinate pairs, finding the distance between them. Formula: sqrt((x2-x1)^2+(y2-y1)^2)
Keep track of which pair are closest to the point you are testing, and the closest distance.
After each calculation, test if the distance is shorter; if so, then overwrite the distance and closest-pair variables.
I can expand this if you like.
Related
I have distance matrix as two-dimensional array, like this:
So, I need to find clusters, of elements with its help. I can do it, using hierarchic clusterization, like k-means. I have found such example here PHP K-Means
How can I convert my two-dimensional array into array of points, listed in this example?
$points = [
[80,55],[86,59],[19,85],[41,47],[57,58],
[76,22],[94,60],[13,93],[90,48],[52,54],
[62,46],[88,44],[85,24],[63,14],[51,40],
[75,31],[86,62],[81,95],[47,22],[43,95],
[71,19],[17,65],[69,21],[59,60],[59,12],
[15,22],[49,93],[56,35],[18,20],[39,59],
[50,15],[81,36],[67,62],[32,15],[75,65],
[10,47],[75,18],[13,45],[30,62],[95,79],
[64,11],[92,14],[94,49],[39,13],[60,68],
[62,10],[74,44],[37,42],[97,60],[47,73],
];
First: a nitpick: k-Means is not a hierarchical clustering algorithm, see https://www.quora.com/What-is-the-difference-between-k-means-and-hierarchical-clustering for details o the difference.
Second: you don't want to convert a distance matrix back to the points it originated from as you take a step back. Sadly the k-Means implementation you linked only has an API that allows you to enter raw coordinates and assumes Euclidean distance, therefore you have some possibilities, depending on your requirements:
Where do you get the distance matrix from? If it is possible, get the raw coordinates (and make sure the distance measure is euclidean distance) and use the library you linked.
Override the Point class in the library you linked: specifically the getDistanceWith method to return values from your matrix
If you only need to calculate the cluster once, use python and sklearn. This library does exactly what you want. Especially: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.cluster.hierarchy.linkage.html
Write your own code: clustering is quite an easy topic and therefore it is a nice coding exercise.
I'm making a PHP trigonometry tool using PHP that can analyze how best to solve a given problem.
For instance, I know angle A and sides b and c, and I need the computer to calculate which formulas to use in which order to make the best mathematical solution to finding an unknown value.
Right now, I have created an array with numerous options on how to find the unknown value:
Image of array
The array is made like this:[formula: xxx] is a suggestion on what formula to use to find a previous value.
[target: xxx] is the name of the value we're looking for in order to satisfy the need of the previous formula.
There might be more than one target attached to a formula, because a formula might need more than one information in order to be complete.
At the end of each path there is an array showing a mathematical function which can be solved with the information we have, and if you track backwards from there you have enough information to solve the task which was initially given (which was angle B (cannot be seen on the image, but it is))
So, out of all of these solution options I need to find the shortest solution, the solution which requires the fewest steps.
Keep in mind that a formula might have two unknown variables, which means that we need to calculate the total sum of the shortest path and end up with the an array containing the optimal path.
Bonus question: I'm aware that this only solves the problem using the information provided by the user, maybe at some point along the way we find a variable, which is needed at some other point. Maybe if a function needs two variables, it is faster to find A before B before A calculates a "target" which might be needed in B.
I would really like to have a solution for the first part, but I will need to solve the "bonus question" later on.
I have a set of numbers:
1,22
1,46
32,1
1,9
32,22
1,14
1,45
1,33
33,22
45,22
32,46
32,9
3,1
3,9
3,22
3,32
3,46
9,22
46,22
46,45
46,33
15,1
15,46
15,6
15,22
15,3
15,9
15,45
15,33
15,32
15,14
I need to get combinations from them with a rule that each new pair can only be appended if the latter number is the same as the first in the pair.
For example if I have a pair {15,1}, the next on can be only {1,46} and the next {46,45}, and the final pair must end with the first number of the whole set. In this case it could be for example {45,1}.
So the end result of sets with 4 set limit would be
{15,1,1,46,46,45,45,1}
I can do basic power sets and generate all possible combinations from set of numbers but this seems to be too advanced for me.
I can do C, Javascript or PHP so all the help or solutions to this are highly appreciated. And for clarification, this is not a homework, this is just something I would like to learn and understand.
This looks as if some graph data structure, and some graph algorithms, would be appropriate. Your graph would comprise nodes (each of which is a number) and edges (each of which represents one of your pairs). Then write the appropriate routine for walking round the graph. It's not entirely clear from your question what the rules for the walk are, but I guess you know.
EDIT
Of course, I should point out that what you have is already a graph data structure, it's called an adjacency list. Google around for algorithms and representations.
I am looking for an algorithm (php would be most ideal) that can, given two sets of coordinates (start and end), calculate the geographical coordinates along that path at given intervals (say every mile). Note that I am not looking for something like Bresenham's algorithm - I want the exact coordinates along the path.
You need to find the latitude/longitude of a point at a given distance along a great circle passing through your start- and end-point. You'll find the formulae worked out here, which you should be able to adapt to your use case.
I'm wondering what the best method would be for me to approach a problem where I need to find adjacent (horizontal, vertical, diagonal) X's in a grid which is provided.
I wanted to know what the recursive way, and the nonrecursive way would be. I tried a recursive method of checking each column, and then iterating rows - that gives me X's in one direction - should I write seperate recursive functions for the other directions?
Example grid:
XXX0X
0000X
00X00
XXXX0
0000X
output should be :
(0,0),(1,0),(2,0)
(4,0),(4,1)
(2,2),(0,3),(1,3),(2,3)(3,3)
You may want to check out the Flood Fill algorithm. You can find it on Wikipedia.
I think what you're describing is more or less it. What you do is basically:
For a given position:
If it is of the desired color (in your case 'O'):
mark it (say, re-color it to a color 'M'),
recurse on all desirable directions (run the same algorithm
on new positions, which are +/-1 away);
else
do nothing.
In your case, the result are the positions marked 'M'. If you want to find additional adjacencies, you can always reset the ones marked 'M' and start the algorithm on a different position.
EDIT: According to your examples, it seems you're looking for adjacent 'X's. :)