The shortest path algorithm is a single-weighted one for the non-graph.
Simulate parking options for the nearest car park.
First of all, refer to Bo friend Javaman_chen's Blog
http://blog.csdn.net/javaman_chen/article/details/8254309
But this algorithm is problematic.
Algorithm, if point A is the current point, it is the point at which the lowest weight of a point is selected as the next path.
This brings up the problem that, if the weights are the same, the 2 points from point A are randomly selected.
So, when the amount of data is a little bit larger, it makes a mistake.
The use of the Dijkstra algorithm here is using the open, Close table method.
First, the data structure of the coordinate point is defined
Coordinate.java
The coordinate contains the list of adjacent coordinates, and the distance from the starting point.
In the algorithm, all the path points are initially associated.
Then, the step of all points is calculated by spreading from the starting point.
<span Style= "FONT-SIZE:18PX;" >package com.harlan.dijkstra;import java.util.linkedlist;/** * Coordinate point data structure * * @author Harlan * */public class Coordinat e {//x coordinates public int x;//y coordinates public int y;//neighboring coordinates public linkedlist<coordinate> adj;//distance public int steps;// The previous vertex in the shortest path public coordinate lastpoint;; public coordinate () {}public coordinate (int newx, int newy) {x = Newx;y = Newy;adj=new linkedlist<coordinate> (); Rese T ();} public void Reset () {steps=integer.max_value;lastpoint=null;} @Overridepublic boolean equals (Object obj) {if (!) ( obj instanceof coordinate)) return false; coordinate other = (coordinate) obj;if (x = = Other.x && y = = other.y) {return true;} return false;} @Overridepublic int hashcode () {return x*10000+y;} /** * Display coordinates in JSON format */@Overridepublic String toString () {return "{\" x\ ":" + x + ", \" y\ ":" + y + "}";}} </span>
and defines the path data structure
Pathinfo.java
<span style= "FONT-SIZE:18PX;" >package Com.harlan.dijkstra;import java.util.list;/** * path information * @author Harlan * */public class PathInfo {//target point coordinates priva Te coordinate targetcd;//the best path to the target point private list<coordinate> cdlist;public coordinate Gettargetcd () {return TARGETCD;} public void settargetcd (coordinate targetcd) {THIS.TARGETCD = TARGETCD;} Public list<coordinate> getcdlist () {return cdlist;} public void Setcdlist (list<coordinate> cdlist) {this.cdlist = Cdlist;}} </span>
In the algorithm, the associated method for the path point:
<span style= "FONT-SIZE:18PX;" >/** * and the surrounding four points to establish a relationship * * @param node */private void getcontactwithf (coordinate node) {Coordinate coordinate = Getcoordin Ate (node); Coordinate EAST = new Coordinate (node.x + 1, node.y); Coordinate south = new coordinate (node.x, node.y + 1); Coordinate WEST = new coordinate (node.x-1, node.y); coordinate north = new coordinate (node.x, node.y-1), if (Iscellsafe (east, Mroads)) {east = Getcoordinate (east); coordinate . Adj.add (EAST);} if (Iscellsafe (south, Mroads)) {south = getcoordinate [South]; Coordinate.adj.add (south);} if (Iscellsafe (west, Mroads)) {west = Getcoordinate (west); Coordinate.adj.add (west);} if (Iscellsafe (north, Mroads)) {n = getcoordinate (north); Coordinate.adj.add (north);}} /** * Judge if the seat around is not the road * * @param head * @return */public boolean iscellsafe (Coordinate Park, set<coordinate> roads) {b Oolean Issafe = false;//in the road collection is safe, otherwise, unsafe for (coordinate info:roads) {if (Info.equals (Park)) {Issafe = true;}} return issafe;} </span>
The algorithm for the least-authorized path is as follows:
<span style= "FONT-SIZE:18PX;" >//not authorized Shortest path calculation public void unweighted (coordinate enter) {if (enter = = null) throw new Nosuchelementexception ("Start vertex Not found! "); linkedlist<coordinate> q = new linkedlist<coordinate> (); ClearAll (); enter = Vertexmap.get (enter.tostring ( )); System.out.println ("unweighted Harlan:" + enter.adj.toString ()); Q.addlast (enter); enter.steps = 0;while (!q.isempty () {Coordinate v = q.removefirst (); for (iterator<coordinate> ITR = V.adj.iterator (); Itr.hasnext ();) {Coordinate w = itr.next (); if (w.steps = = integer.max_value) {w.steps = v.steps + 1;w.lastpoint = v;q.addlast (w);}}}} </span>
Traversal gets the actual shortest path
<span style= "FONT-SIZE:18PX;" >private list<coordinate> getpath (coordinate dest, list<coordinate> cdlist) {if (dest.lastPoint! = null) {cdlist = (GetPath (dest.lastpoint, cdlist));} Cdlist.add (dest); return cdlist;} </span>
Show Shortest Path:
<span style= "FONT-SIZE:18PX;" >//displays a path public void Printpath (String coodrstr) throws nosuchelementexception {coordinate coord = Vertexmap.get ( COODRSTR); if (coord = = null) throw new Exception (No path found! "); else if (coord.steps = = Integer.max_value) System.out.println (coord.tostring () + "is unreachable!"); else {Printpath (coord); System.out.println ();}} Shows the actual shortest path private void printpath (coordinate dest) {if (dest.lastpoint! = null) {Printpath (dest.lastpoint); System.out.print (",");} System.out.print (Dest.tostring ());} </span>
Finally, write an external usage class that you can use on Android or anywhere else.
Getdijkstrapath.java
<span style= "FONT-SIZE:18PX;" >package com.harlan.dijkstra;import java.util.arraylist;import Java.util.hashset;import java.util.List;import Java.util.set;public class Getdijkstrapath {private static final String TAG = GetDijkstraPath.class.getSimpleName ();/** * Main function, test class * @param args */public static void main (string[] args) {Coordinate enter = new coordinate (2, 0); Set<coordinate> roads = new Hashset<coordinate> (); Roads.add (New Coordinate (3, 10)); Roads.add (new Coordinate (3,11)); Roads.add (New Coordinate (3, 8)); Roads.add (New Coordinate (3, 9)); Roads.add (New Coordinate (3, 6)); Roads.add (New Coordinate (3, 7)); Roads.add (New Coordinate (3, 4)); Roads.add (New Coordinate (3, 5)); Roads.add (New Coordinate (3, 2)); Roads.add (New Coordinate (3, 3)); Roads.add (New Coordinate (3, 1)); Roads.add (New Coordinate (6, 1)); Roads.add (New Coordinate (1, 9)); Roads.add (New coordinate (1, 8)), Roads.add (new coordinate (1, one)); Roads.add (New COordinate (1)); Roads.add (new Coordinate (1, 5)); Roads.add (new Coordinate (1, 4)); Roads.add (new Coordinate (1, 7)); Roads.add (New Coordinate (1, 6)), Roads.add (new coordinate (1, 1)), Roads.add (new coordinate (1, 3)), Roads.add (new Coordinate (1, 2)); Roads.add (new Coordinate (4, 1)), Roads.add (new coordinate (4, one)), Roads.add (new coordinate (7, 5)); Roads.add (New Coordinate (7, 4)), Roads.add (new coordinate (2, 11)); Roads.add (New Coordinate (7, 7)); Roads.add (new Coordinate (7,6)); Roads.add (New Coordinate (7, 1)); Roads.add (New Coordinate (7, 3)); Roads.add (new Coordinate (7,2)); Roads.add (New Coordinate (2, 1)); Roads.add (New Coordinate (7, 9)); Roads.add (new Coordinate (7,8)); Roads.add (New Coordinate (7, 11)); Roads.add (New Coordinate (7, 10)); Roads.add (new Coordinate (5,11)); Roads.add (New Coordinate (5, 10)); Roads.add (New Coordinate (5, 9)); Roads.add (new Coordinate (5,8)); Roads.add (new Coordinate (5,7)); Roads.add (new Coordinate (5,6)); Roads.add (New CoordInate (5,5)); Roads.add (new Coordinate (5,4)); Roads.add (new Coordinate (5,3)); Roads.add (new Coordinate (5,2)); Roads.add (new Coordinate (5,1)); System.out.println ("Nearest roads.size ():" +roads.size ()); set<coordinate> trags = new hashset<coordinate> (); Trags.add (New Coordinate (5, 4)); Trags.add (New Coordinate (5, 5)); PathInfo nearest = Getnearestpathinfo (Roads,trags,enter); System.out.println ("Nearest:" +nearest.getcdlist ()); /** * External interface (used when calculating the shortest path of multiple portals) * Get the best path for multiple portals * @param roads * @param trags * @param enters * @return * * Public Static PathInfo Getnearestpathinfofromdiffenter (set<coordinate> roads,set<coordinate> trags, Set< Coordinate> enters) {list<pathinfo> List = new arraylist<> (); for (coordinate enter:enters) {List.add ( Getnearestpathinfo (Roads,trags,enter)); }//step of each path int steps = Integer.max_value; PathInfo nearste = new PathInfo (); for (PathInfo pathinfo:list) {if (pathinfo.getcdlist(). Size () <steps) {steps = Pathinfo.getcdlist (). Size (); nearste = PathInfo;}} return nearste; /** * External interface (used when calculating the shortest path of a single entry) * Get the best path for a single entry * * @param roads * @param trags * @param Enter * @return */Publ IC Static PathInfo getnearestpathinfo (set<coordinate> roads,set<coordinate> trags, coordinate enter) {List <PathInfo> list = Getallavailablepathinfo (roads,trags,enter);//For (PathInfo info:list) {//System.out.println ( "Getnearestpathinfo Targ:" +info.gettargetcd ());//System.out.println ("Getnearestpathinfo Route:" +info.getcdlist () );//System.out.println ("Getnearestpathinfo *********************");//}////The step size of each path int steps = Integer.max_value; PathInfo nearste = new PathInfo (); for (PathInfo pathinfo:list) {if (Pathinfo.getcdlist (). Size () <steps) {steps = Pathinfo.getcdlist (). Size (); nearste = PathInfo; }} return nearste; /** * Get all available paths to reach all target points * * @param roads * @param trags * @param enter * @return */private static list<pathinfo> Getallavailablepathinfo (set<coordinate> roads,set<coordinate> trags, Coordinate Enter) {Set< coordinate> Availableroadtar = Getallroadneartarg (roads,trags); Calculate the distance from the starting point to each available point Harlandijkstra test=new Harlandijkstra (roads,enter); test.unweighted (enter);//The shortest path to the point where the parking space can be reached list<pathinfo> availablelist = new arraylist<> (); for ( Coordinate Info:availableroadtar) {PathInfo PathInfo = Test.getpathinfo (info.tostring ()); Availablelist.add (PathInfo );} return availablelist;} /** * Get effective road points to all targets (a collection of nearby road points for a target) * @param roads * @param tragset * @return */private static set<coordinate> getal Lroadneartarg (set<coordinate> roads,set<coordinate> tragset) {set<coordinate> allOfNearList = new Hashset<> (); for (coordinate targ:tragset) {//system.out.println ("Getallroadneartarg targ:" +targ); set<coordinate> Childset = Getroadneartarg (Roads,targ); Allofnearlist.addall (Childset);} return allofnearlist;} /** * Get adjacent road points to a target * * @param roads * @parAm Targ * @return */private static set<coordinate> getroadneartarg (set<coordinate> roads,coordinate targ) { set<coordinate> nearlist = new hashset<> (); Coordinate EAST = new Coordinate (targ.x + 1, targ.y); Coordinate south = new coordinate (targ.x, TARG.Y + 1); Coordinate WEST = new coordinate (targ.x-1, targ.y); coordinate north = new coordinate (targ.x, targ.y-1) and for (coordinate info:roads) {if (East.equals (info)) {Nearlist.add ( EAST);} if (South.equals (info)) {Nearlist.add (south);} if (West.equals (info)) {nearlist.add (WEST);} if (North.equals (info)) {nearlist.add (north);}} return nearlist;}} </span>
In the main method, the distance to the target point (5, 4) or (5, 5) is output as follows:
<span style= "FONT-SIZE:18PX;" >nearest roads.size (): 49nearest: [{"X": 2, "Y": 0}, {"X": 2, "Y": 1}, {"X": 3, "Y": 1}, {"X": 4, "Y": 1}, {"X": 5, "Y": 1}, {"X" : 5, "Y": 2}, {"X": 5, "Y":3}]</span>
The output path is in JSON format and is easy to parse.
in Android, the source of the snake is adapted for random generation of related "maps".
After various tests, the algorithm is correct.
--java implementation of Shortest path Dijkstra algorithm based on graph without direction and single weight