Euler depth first search algorithm - java

I've coded a depth first search from a graph using euler algorithm of getting a cycle and splice subcicles into the result.
The problem is, to very large data, it isn't fast enough to find the correct path, namely on the dfs worst case scenario.
I've ordered the adjacency list and start at a given point, to finish at the same starting point. My idea to improve was to make the search bidirectional but that adds alot of complexity dealing with the dead ends when I want to add order to the result.
My question is basically if there is some other way to get around the worst case scenario or how to properly deal with dead ends on bidirectional search so the result will stay numericly ordered?
Any input is welcome.

Related

Ice Sliding Puzzle Path Finding Shortest Distance

Consider a problem similar to this: Ice Sliding Puzzle Path Finding
Except that I wish to find the optimal shortest path algorithm. I've looked into it and found that BFS, A* and Djikstra's are all viable algorithms, but I need some advice on which would be best for my scenario or if there's a better choice.
My scenario being where an ice maze similar to the one in the stackoverflow question exists and is in a txt file. For the data structure holding the grid I thought to use a 2D char array since I thought it would perform best but maybe simplicity isn't the way to go here.But I'm clueless as to whether more fitting data structures exist
With that taken into account which of the three algorithms do you guys think would perform best on one of these mazes given that they'd be say max 2560x250 in size with varying complexities in the possible paths.
Concerns with BFS - non-optimal path storage approach, considers each tile moved a step so one path could be sliding along step by step while another is taking steps in different directions with shorter slides ( don't know the performance impact it will have)
Concerns with A* & Dijkstra's - Forming all possible nodes and edges with weights could be a heavy process
simple example maze txt (0 - wall, S - Start, F - Finish)
.0...00...
.0F0......
..0..0....
.0.0.....0
..........
..........
....0.0.0.
.0.0......
0........0
S.0.....00

Java Path-finding puzzle game

I need some advise in an approach I may need to take to solve a gaming problem which is a puzzle (NxN), the puzzle consists of positive numbers and stored in a two dimensional array. For simplistic reasons is i´ll list a simple example
2 1 2 2
1 3 2 1
1 0 2 1
3 1 2 0
So the starting point is at (0,0) => 2 and the goal location is to (3,3) => 0
The number in the array location tells you how far to move. (0,0)=> 2 can move to either (0,2) or (2,0) and so on (moves allowed left, right, up, or down)
So you end up with a solution like this for example (0,2)=>(2,2)=>(2,0)=>(3,0)=>(3,3).
so my question is what sort of algorithm i should be looking into and whether any of you have done something similar to this?
You have plenty of solutions here:
A* algorithm
Dijkstra
Depth-first
Breadth-first
The first two will give you an optimal solution if one exists. A* is typically faster than Dijkstra if the heuristic is well chosen. Breadth- first will also give you an optimal implementation. Depth-first may give you non-optimal solutions in this problem.
The main difference between A* and Djisktra is that A* defines a heuristic, namely a function that tries to estimate if a move is better than another one.
The main difference between depth-first and breadth-first is the order in which they explore the space of solutions. Breadth-first will start by looking for all solutions of length 1 then all solutions of length 2, etc, while depth-first will fully explore an entire path until it either cannot go any further or finds a solution.
A* and Dijkstra are typically implemented in imperative style and are probably more sophisticated than the other two, especially A*. Breadth-first is also naturally expressed in imperative style. Depth-first is generally expressed recursively, which can be a problem if your solutions can exceed a length of several thousands moves (depending on the size of your stack, you will generally only be able to make 7-10k recursive calls before you get a StackOverflowError).
To sum up:
A* is generally the most efficient of the algorithms listed below
A* is the most difficult to implement
Dijkstra is a special case of A* with similar performance but potentially less efficient
Breadth-first is straightforward to implement and is resilient to long solutions
Depth-first is straightforward to implement but it is limited by the length of the longest possible path if it is implemented recursively
All these algorithms except depth-first guarantee an optimal solution
Code example:
I found this Scala implementation of A* in one of my repositories. Might help.
I have some grid search algorithms (Best-First, Breadth-First, Depth-First) implemented here in Object Pascal (Delphi), that you could easily adapt to Java if this was a classic grid search:
https://github.com/Zoomicon/GridSearchDemo/tree/master/Object%20Pascal/algorithms
You can try the GridSearchDemo application here to see how those algorithms behave when searching in a grid with start and target point and obstacles in various grid cells (you can set them):
https://github.com/Zoomicon/GridSearchDemo/releases
In general, I prefer the A* algorithm, which is an example of a Best-First algorithm (https://en.wikipedia.org/wiki/Best-first_search)
In your case, this is not a grid really, but a graph, since you seem to have jump links to other cells (or at least this is how you explain the number in your question, although you call it "how far" at first)
I have written a program in java to solve this problem. It uses A*-algorithm with heuristic functions Manhatten and hamming. It depends on the person whether he uses hamming or manhatten distance but Manhatten is better.
Here is my code in java: 8-puzzle
Btw it's not an easy approach and many problems can't be solved.

Quadtree performance

Anyone knows where i can find some documentation on, or know how many operations insertion and queries takes in a quadtree?
wiki says O(logn) but i found another source saying O(nlogn) and i need to know which is true.
I'm working with a point quadtree
http://www.codeproject.com/Articles/30535/A-Simple-QuadTree-Implementation-in-C
http://en.wikipedia.org/wiki/Quadtree
Search: O(logn): it must traverse down the entire tree to find the element. To be specific the log in this case is log_4, as there are 4 children.
Insert(single point): O(logn): You must traverse the tree place to find the insertion location, then some small constant amount of work to split the points in that quadrant.
Insert(n points): O(nlogn), every point must inserted, leading to nlogn. I hope this is what the other site you read meant be nlogn, otherwise they would be very wrong.
The original paper is called "Quad trees a data structure for retrieval on composite keys" by Finkel and Bentley.

Depth first search with loops

I'm making a robot maze in java in which the robot uses the depth-first search algorithm to traverse the maze and reach the target. This works fine in a maze with no loops, but when those are introduced the algorithm fails. Is there any way to make depth-first search work with loopy mazes? If so, how does one go about doing that?
I have two separate implementations of this maze - one records each junction and stores it in an array while the other one uses a stack to push a new junction and pop it off when it has finished exploring that junction. A solution using any one of these implementations is acceptable.
You need to mark visited nodes and treat them as "additional" walls.
That way, you can avoid searching loops. It will no longer find the shortest path though.
See Dijkstra's algorithm for details. For an even more advanced - directed - version, look at A* search. On difficult mazes, it shouldn't gain you a lot though. A* is more interesting for open areas.

Dealing with identical data in a binary search tree

Im in the process of teaching myself data structures and I am currently working on a binary search tree. I was curious how you would sort the tree if you had identical data. For example say that my data consists of [4,6,2,8,4,5,7,3].
I set 4 as the root element
put 6 to the right of it
put 2 to the left of 4
put 8 to the right of 6
Then I get to 4 where do I put it since 4=4? To the left or the right?
Option #1
Option #2
Are either one of these correct or are they both wrong? If they are both wrong could you show me how they should be sorted. Thanks!
Usually binary trees do not allow data duplication. If you make a custom implementation you can store a count of elements. TreeSet in Java is an example - it contains only unique elements.
Actually the cases you listed broke the whole structure of the tree. Search operations will look weird now and couldn't be performed with O(ln n). It will take O(n) in worst case so you loose all the benefits of this data structure.
If this is a sort-tree, then what you have will work fine, either way; in the end you'll do a tree-walk and dump the data.
If this is a search-tree, then I'd just drop the extra (redundant) data once it's been encountered; "it exists". You did say this is a search-tree, and while not ideal, it's not actually broken - if you search for "4" you'll simply catch the root node (in this case), and never decend below that to see any other "4". It isn't optimal, having all the extra #'s around.
There will be best-case and worst-case situations regardless of which way you choose; don't worry too much about left/right decisions - generally just doesn't matter. IF you have a solid grasp of details in a known data-stream you'd be able to make an optimal decision for that specific case.

Categories

Resources