Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am currently in the process of going through old programming olympiad questions, and found question 5 rather difficult. The problem is based in some category of graph theory and involves computing the most inexpensive path and visiting all nodes. Full details can be found here: problem
Would it be suitable to use A* search algorithm? What kind of algorithm would you use to solve the problem, which is fast to implement and can solve the problem in the given time period?
As #kiheru stated, A* won't work.
This is the traveling salesman problem, and it's an NP Complete problem. Replace tolls for distance traveled, and you get the same problem. The Traveling Salesman link has several of such algorithms.
Traveling Salesman
You'll find different algorithms depending on the number of cities, but it gets much more computationally expensive as you add cities to where a computer isn't the best choice for an exact solution. There are many different techniques for getting an approximation, but it's not a solvable problem.
If I were to code it, I'd use something called Linguistic Geometry (something I learned in grad school). Basically you treat the nodes as a game board, and you take one step at a time towards the answer you want and evaluate it. This won't solve it, but it will give you a good approximation in a very short amount of time.
This is known as the travelling salesman problem, and is NP-Complete. That means there is no generally-faster method of solving this problem than brute-forcing (well, there is actually a O(2^n*n^2) solution based on dynamic programming). Since you are dealing with only 6 nodes, which is 6! = 720 total possible paths to check, the simplest solution would be to just try every different ordering of cities and record which is fastest.
(Also, contrary to #kiheru's comment above, A* is not a heuristic. It uses a heuristic, but still finds an exact solution to the shortest-path problem. However, either way it does not apply to your problem)
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I am trying to find a way to calculate the strength of a hand, specifically how many unique hands (of the 169 starting hands) have a higher chance of winning at showdown given a complete or incomplete board (holdem).
I've tried to do this a few ways and have been somewhat successful but it takes an obnoxious amount of time for my program to run given that i'm essentially branching out for every possible hand combo, and comparing aggregate results for every scenario to find how many hands are better than hero's. TLDR it's terribly inefficient and it takes an unrealistic amount of time to run.
However there are tools like this one http://www.cardplayer.com/poker-tools/odds-calculator/texas-holdem that seem to do the calculation a lot faster. The above program seems to do calculations for all possible future board combinations, so it can give you the better hand for incomplete boards. Although, for my particular program i'd like to find the number of hands ahead of hero's at any given point, which would require me to run the program above for each of the 52*51 non-unique starting hands, and find my hand's place among the rest and once the number of better hands have been gotten, i'll have to reduce those to unqique starting hands (ie 8c7h and 8h7c would be reduced to 87o)
So my question is, are there any tools/frameworks/references (preferably in Java) out there for calculating the strength of hero's hand vs an anonymous hand given any complete or incomplete board that also doesn't take a day to run?
I am not much of a poker kind of guy, but you may find ThePokerBank site interesting, what about a whole course dedicated at poker theory from MIT, a bonus infographic to help you out too.
There are different strategies that you can take to try to tackle this issue, all of them involving quite some knowledge on Statiscal analysis, I would say that one of the reason other poker algorithm work a bit better is that they are using a form of vectorization math instead of a series of for loop. I know that language like octave/MatLab/R take this strategy to do bulk operation.
Good luck and have fun!!
This thread has much information Stack Overflow Evaluation Algorithms
Also at Code Project and a tutorial on an algorithm and Java source: at Github and in different languages at rosettacode.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to generate random roads on a swing application. However, I am unable to create the roads such that they do not over-intersect with each other. I mean one road should not cross over another unless they have a common intersection point.
I tried generating random points and connecting them using minimum spanning trees but it does not seem to work.
Do you have any ideas?
I would try the easy thing first: generate a bunch of random roads and keep only those that don't intersect any you've found so far. This has O(n^2) runtime, but it's easy to understand and implement.
In pseudocode:
points = (lots of random points)
roads = empty list
repeat n times:
r = road(pick_random(points), pick_random(points))
if r does not intersect anything in roads list:
add r to roads
This has the advantage over minimum spanning trees that it may generate cycles for you, which would make for a more interesting road network.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
I'm hoping someone can point me in the right direction.
We have millions of records flowing/streaming through where we need to do a quick lookup to determine which country polygon they fall into.
Could someone recommend a complete JAVA based approach to do this. From what I can tell I would use JTS and/or geotools? My thoughts are to take all of the country polygons and possibly split them up using like a FishNet or Grid to make them smaller for better performance. I would then load these into a java based in-memory spatial index...As the records stream through I would do a lookup into the java spatial index to see what country they fall into. (Maybe like a Spatial Feature collection).
Does this approach sound resonable for lots of data? From a java standpoint how would I implement this? Is this just using JTS and/or Geotools? What type of index would I create. (The polygon data will be static after loading as it will just contain country borders)
From reading geotools documentation its hard to decipher the spatial index and how well it performs and whether it should be used?
Any help or guidance would be appreciated.
Thanks
The problem you're trying to solve is called the point in polygon problem. A previous answer gave an example of testing if one point is in one polygon, using JTS (Java Topology Suite).
I don't know if there's a more efficient solution for multiple polygons. You might want to test the polygons in turn, sorted by distance from your point.
Short answer: Building up a trapezoidal map from your countries and point location is O(log n) where n is the number of line segments.
Reference: Chapter 6 of Computational Geometry: Algorithms and Applications, Mark de Berg, Otfried Cheong, Marc van Kreveld, Mark Overmars
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
I have to do a project about shortest path algorithm. I am not really a professional I am just a normal student. I have to pick up first a problem ( train route, bus route, any travel route) then choose a suitable algorithm to solve it. Then I have to program it (using coding language java, python..) Then after that I should evaluate it(how speed is the algorithm, time complexity, etc) and if there is a better algorithm that I can be implement instead of what I chose first.
Choosing the problem is the hardest part as I
Don't know what kind of problem I should explore to use of the algorithm!
Regards
Let's take the city where you live in. Take any two bus stations, namely A and B, and the entire commuter network. This network is a graph. Stations are nodes, commuter connections between stations are edges (edge weight = time it takes the bus to travel between two stations).
Say, you want to travel from A to B in the shortest amount of time possible (you wont want to waste time, would you?).
Dijkstras Algorithm has as input the graph representing the commuter network and the node A and is able to deduce the shortest path from A to any other station (which includes the shortest path from A to B).
Does that help you? Wikipedia on Dijkstra has more detailed examples.
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 9 years ago.
Improve this question
This is kind of unusual question for developers but for some reason i want to post it here and hope to get adequate answer.
Here is a simple example:
I wrote a java function that calculates distance between two geo points. The function is not more than 50 lines of code. I decided to download a source code from ibm that does the same thing but when i opened it i saw that it looks very complicated and is almost thousand lines of code.
What kind of people write such source code? Are they just very good programmers? Should i use their source code or my own?
I have noticed this kind of thing lots of times and i from time to time i start to wonder if it is just me who do not know how exactly to program or maybe i am wrong?
Do you guys have the same kind of feeling when you browse throught some other peoples source code?
The code you found, does it do the exact same calculation? Perhaps it takes into account some edge cases you didn't think of, or uses an algorithm that has better numerical stability, lower asymptotic complexity, or is written to take advantage of branch prediction or CPU caches. Or it could be just over-engineered.
Remember the saying: "For every complex problem there is a solution that is simple, elegant, and wrong." If you are dealing with numerical software, even the most basic problems like adding a bunch of numbers can turn out to be surprisingly complex.