# 基于无向图且权重单一的最短路径Dijkstra算法——JAVA实现

http://blog.csdn.net/javaman_chen/article/details/8254309

Coordinate.java

Coordinate中包含相邻坐标的List，以及距离起始点的距离。

package com.harlan.dijkstra;

/**
* 坐标点的数据结构
*
* @author Harlan
*
*/
public class Coordinate {
//x坐标
public int x;
//y坐标
public int y;
//相邻坐标
//距离
public int steps;
// 最短路径中的前一个顶点
public Coordinate lastPoint;
;

public Coordinate(){

}

public Coordinate(int newX, int newY) {
x = newX;
y = newY;
reset();
}

public void reset(){
steps=Integer.MAX_VALUE;
lastPoint=null;
}

@Override
public 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;
}

@Override
public int hashCode() {
return x*10000+y;
}

/**
* 以JSON格式展示坐标
*/
@Override
public String toString() {
return "{\"x\":" + x + ",\"y\":" + y + "}";
}
}

PathInfo.java

import java.util.List;

/**
* 路径信息
* @author Harlan
*
*/
public class PathInfo {

//目标点的坐标
private Coordinate targetCd;

//去往目标点的最佳路径
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 style="white-space:pre">	</span> /**
* 和周围的四个点建立关系
*
* @param node
*/
private void getContactWithF(Coordinate node) {
Coordinate coordinate = getCoordinate(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);
EAST = getCoordinate(EAST);
}
SOUTH = getCoordinate(SOUTH);
}
WEST = getCoordinate(WEST);
}
NORTH = getCoordinate(NORTH);
}
}

/**
* 判断周围的位子是不是道路
*
* @return
*/
public boolean isCellSafe(Coordinate park, Set<Coordinate> roads) {
boolean isSafe = false;
// 在道路集合里面，就是安全的，否则，不安全
for (Coordinate info : roads) {
if (info.equals(park)) {
isSafe = true;
}
}
return isSafe;
}

<span style="white-space:pre">	</span>// 无权最短路径计算
public void unweighted(Coordinate enter) {

if (enter == null)

clearAll();
enter = vertexMap.get(enter.toString());

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;
}
}
}
}

<span style="white-space:pre">	</span>private List<Coordinate> getPath(Coordinate dest, List<Coordinate> cdList) {
if (dest.lastPoint != null) {
cdList = (getPath(dest.lastPoint, cdList));
}
return cdList;
}

<span style="white-space:pre">	</span>// 显示一条路径
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();
}
}

// 显示实际最短路径
private void printPath(Coordinate dest) {

if (dest.lastPoint != null) {
printPath(dest.lastPoint);
System.out.print(",");
}
System.out.print(dest.toString());
}

GetDijkstraPath.java

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();

/**
* 主函数，测试类
* @param args
*/
public static void main(String[] args) {
Coordinate enter = new Coordinate(2, 0);

Set<Coordinate> trags = new HashSet<Coordinate>();
System.out.println("nearest : "+nearest.getCdList());
}

/**
* 对外的接口(如果计算多入口的最短路径的时候使用)
* 获取多入口的最佳路径
* @param trags
* @param enters
* @return
*/
Set<Coordinate> trags, Set<Coordinate> enters){
List<PathInfo> list = new ArrayList<>();
for(Coordinate enter:enters){
}
//每条路径的步长
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;

}

/**
* 对外的接口(如果计算单一入口的最短路径时候使用)
* 获取单一入口的最佳路径
*
* @param trags
* @param enter
* @return
*/
Set<Coordinate> trags, Coordinate enter){
//		 for(PathInfo info:list){
//	    		System.out.println("getNearestPathInfo targ:"+info.getTargetCd());
//	    		System.out.println("getNearestPathInfo route:"+info.getCdList());
//	    		System.out.println("getNearestPathInfo *********************");
//	    	}
//
//每条路径的步长
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;
}

/**
* 获取到达所有目标点的所有可用路径
*
* @param trags
* @param enter
* @return
*/
Set<Coordinate> trags, Coordinate enter) {

//计算出起始点到各个可达点的距离
test.unweighted(enter);

//得出到停车位的可达点的最短路径
List<PathInfo> availableList = new ArrayList<>();
PathInfo pathInfo = test.getPathInfo(info.toString());

}
return availableList;
}

/**
* 获取通往所有目标的有效道路点(一个目标的临近道路点的集合)
* @param tragSet
* @return
*/
Set<Coordinate> tragSet) {
Set<Coordinate> allOfNearList = new HashSet<>();
for (Coordinate targ : tragSet) {
}
return allOfNearList;
}

/**
* 获取通往一个目标的临近道路点
*
* @param targ
* @return
*/
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);
for (Coordinate info : roads) {
if (EAST.equals(info)) {
}
if (SOUTH.equals(info)) {
}
if (WEST.equals(info)) {
}
if (NORTH.equals(info)) {
}
}
return nearList;
}

}

nearest : [{"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}]

• 本文已收录于以下专栏：

举报原因： 您举报文章：基于无向图且权重单一的最短路径Dijkstra算法——JAVA实现 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)