I'm sorry if the title is misleading, but I am not really sure how else to describe it. I am trying to write a program in Java that would convert data, given only some of the conversion factors. For example, if I have these conversion factors:
Convert a to b by multiplying a by 10
Convert b to c by multiplying b by 20
Convert c to a by dividing c by 200
How would I write a method that would convert a to c? I know that it would have to first convert a to b and then b to c but I do not know how to teach Java to recognize patterns like that. I know that I could just write if statements to check for these directly, however, I have a lot of these conversions and all those if statements seem terribly inefficient. Is there a better way?
You could build a Graph. Every arc between two nodes are a convertion. Finally, you need to run an algorithm like Dijkstra's algorithm to find the shortest way to convert x to y.
This is a StackOverFlow question, with answers providing links and implementation of Graphs and Dijkstra's algorithm in Java.
Edit: it seems somebody else has suggested the same idea :)
You can model this problem as graph search problem. Model each a, b, and c, into a node. Create a directed edge between node u and v if u can be converted into v.
Using this graph, you can find method to convert x to y by simply finding a path from x to y. You can use standard shortest path algorithm like Dijkstra algorithm to find such path.
Related
I've done a fair bit of reading around this, and know that discussions regarding this algorithm in Java have been semi-frequent. My issue with implementing Dijkstra's algorithm in Java is simply that I'm not sure how to prepare my data.
I have a set of coordinates within an array, and a set of 1s and 0s in a matrix that represent whether there is a path between the points that the coordinates represent. My question is, how do I present this information so that I can search for the best path with Dijkstra? I have seen many people create a "Node" class, but they never seem to store the coordinates within that Node. Is there some standardized way of creating this kind of structure (I suppose it's a graph?) that I am simply missing?
Any help would be appreciated.
There are two main options:
1. You can use an adjacency matrix in which rows an columns represent your nodes. The value matrix[x, y] must be the weight(e.g. distance/cost etc.) to travel from x to y. You could use the Euclidian distance to calculate these values from your coordinate array;
2. You can implement a couple of classes (Node, Edge - or just Node with a internal Map to another node and the weight as a map value) - it is a graph indeed.
I'm trying to make a program that calculates the distance between one point to another in a 3D array, and then returns the point's distance to the origin. What kind of method(s) could I use for this? How should I think? Are there any good tutorials about this (that aren't too basic)? I'm a beginner and very new to methods and would really appreciate some help. :)
Here's the math basics about calculating distance between two points in 3D: https://math.stackexchange.com/questions/42640/calculate-distance-in-3d-space
The relevant Java functions that will help you are:
Math.sqrt: square root (e.g. Math.sqrt(6) => 2.44948974278)
Math.pow: raise first argument to the power of the second (e.g. Math.pow(2,4) => 16)
Give it a try and post your code if you want more specific help.
First off, I apologize for my bad paint drawing of the graph. The weights are obviously not scaled. I am having a hard time coming up with the algorithms to solve a few problems.
First, I want to find all paths that take 3 "stops" or less from C to C (just an example... can be any two vertexes). After researching, I found that BFS might be what I'm looking for to solve this problem. Am I correct in this assumption?
There are two paths that have 3 stops or less:
C -> D -> C
C -> E -> B -> C
I also want to find the shortest path from A to C (just an example.. can be any two vertexes). After doing a little bit of research, I came to the conclusion that I should use Dijkstra's algorithm. Am I correct in this assumption? If so, I saw that there are various implementations. Does it matter if I use binary heap, fibonacci heap, or queue?
Thank you and please let me know if you need any more information!
First, I want to find all paths that take 3 "stops" or less from C to
C (just an example... can be any two vertexes). After researching, I
found that BFS might be what I'm looking for to solve this problem. Am
I correct in this assumption?
Yes, you are. The property of BFS is that processes nodes in level-order, therefore you first process all nodes that are neighbors of the source node, then the nodes that are neighbors of neighbors of the source node etc.
I also want to find the shortest path from A to C (just an example..
can be any two vertexes). After doing a little bit of research, I came
to the conclusion that I should use Dijkstra's algorithm. Am I correct
in this assumption? If so, I saw that there are various
implementations. Does it matter if I use binary heap, fibonacci heap,
or queue?
Again, yes, Dijkstra's algorithm is a classic solution for such problems. There are other algorithms better suited for some special situations (e.g. Bellman-Ford if you have negative weights in your graph), but in most cases (yours as well), go with Dijkstra. Regarding the implementation, theoretically the best implementation is based on a min-priority queue implemented by a Fibonacci heap. The running time of this implementation is O(|E|+|V|/log|V|) (where |V| is the number of vertices and |E| is the number of edges). However, in practice, binary heaps often outperform Fibonacci heaps, see this interesting thread for more information.
I am trying to find a function to perform Lagrange Interpolation in java. I have 3 (x,y) pairs, where x and y are BigInteger objects, and would like to use some interpolation function to determine f(0) for the polynomial f used to calculate my x,y these pairs. Something like this seems perfect, except that this class doesn't seem to actually belong to a package I can import: http://nssl.eew.technion.ac.il/files/Projects/thresholddsaimporvement/doc/javadoc/Lagrange.html
Forgive me if my question is naive, any help I can get would really be appreciated.
A similar class seems to be in Apache Commons Math. This is probably going to be much more reliable than what you found.
http://commons.apache.org/math/apidocs/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionLagrangeForm.html
It would appear that you construct it with PolynomialFunctionLagrangeForm(double[] x, double[] y), then call value(0) to get the value at x = 0.
Situation:
I am currently developing a Java application based on rules. Every rule has 3 numeric parameters to influence a database communication. I am measuring a value, that is affected by this rules and calculate the standard deviation of the measured values. The standard deviation should be as small as possible.
Question:
I am wondering if it is possible to do this automated? I can already start a test scenario automatically and I can calculate the standard deviation automatically. So, now I am looking for mechanism to adjust the parameters according to the measured values. Any ideas?
Thanx.
PS: I know, it's a very general question...
As Peter says, you have to minimize a function f(a,b,c). There are a lot of elaborate methods for well behaving functions. Eg for functions which can be differentiated, or for so called convex functions. In your case you have a function where we do not know very much about. So f could have different local minima which kills many established minimization methods.
If a simple evaluation of a parameter set a,b,c is fast, you can try some kind of coordinate descent. This is not the best method, brute force but easy for you to implement. I will name the standard deviation achieved by (a,b,c) as s(a,b,c):
I give you some python style pseudo code, which should be easy to read:
def improve(a,b,c):
eps = .01
s1 = s(a*(1+eps), b, c)
s2 = s(a, b*(1+eps), b, c)
s3 = s(a, b, c*(1+eps))
s4 = s(a*(1-eps), b, c)
s5 = s(a, b*(1-eps), c)
s6 = s(a, b, c*(1-eps))
# determine minimal of (s1....s6) and take index:
i = argmin (s1....s6)
# take parameters which lead to miminal si:
if i==1:
a = a*(1+eps)
if i==2:
b = b*(1+eps)
...
if i==6:
c = c*(1-eps)
return a,b ,c
You have to start with some values (a,b,c) and this function should give you a new triple (a,b,c) which leads to less variation. Now you can apply this step as often as you want.
Maybe you have to adapt eps, that depends on how fast s(a,b,c) changes if you make little modifications on a, b, or c.
This is not the best solution, but an easy to try hands-on approach.
Fortunately there are a number of general solutions. It should be a matter of solving the function to minimise the result. If you have a function x= f(a,b,c) you want to find a, b, c which gives a minimal x. The simplest approach is trial an error, but you can improve on this by using a binary search and linear interpolation (assuming the topology is relatively simple) There are more complex approaches but you may not need them.
Do you know what the actual function is? If its a pure standard deviation you can make a,b,c the same e.g. the average, and your standard deviation will be 0.
If you don't know anything about f, I think I would run random samples for some time and have a look at the results. Then you can decide whether you want to do a gradient descent or something else.