|
@@ -18,11 +18,11 @@ public class IPMMSVertexServiceImpl implements IIPMMSVertexService {
|
|
|
@Override
|
|
|
public ArrayList<IPMMSVertex> getObtainTheOptimalPath(String startpoint,String endPoint,Map<String, List<IPMMSVertexEdge>> vertexEdgeList, Map<String, IPMMSVertex> vertexList) throws VertexAngEdgeException {
|
|
|
//初始化数据
|
|
|
- int[] startPoin=new int[]{0,-1};
|
|
|
+ int[] startPoin=new int[]{0,-1,0};
|
|
|
//创建S战队(放入的是已经遍历过边的顶点)
|
|
|
List<String> obtainTheOptimalPath=new ArrayList();
|
|
|
//obtainTheOptimalPath.add(statpoint);
|
|
|
- //创建 dist[] (起点到集合点中的最短路径) key:指向的点 new int[]{0,-1}; {权重、中间点}
|
|
|
+ //创建 dist[] (起点到集合点中的最短路径) key:指向的点 new int[]{0,-1}; {累计权重、中间点、自己权重}
|
|
|
HashMap<String,int[]> currentBestDinstance=new HashMap<>();
|
|
|
currentBestDinstance.put(startpoint,startPoin);
|
|
|
while (obtainTheOptimalPath.size()!=vertexList.size()){
|
|
@@ -36,9 +36,6 @@ public class IPMMSVertexServiceImpl implements IIPMMSVertexService {
|
|
|
if (island(currentBestDinstance,stringMinimumValue,vertexEdgeList)){
|
|
|
continue;
|
|
|
}
|
|
|
- if (stringMinimumValue.equals("0")||stringMinimumValue.equals("13")){
|
|
|
- System.out.println();
|
|
|
- }
|
|
|
|
|
|
//借东风
|
|
|
List<IPMMSVertexEdge> ipmmsVertexEdges = vertexEdgeList.get(stringMinimumValue);
|
|
@@ -66,17 +63,37 @@ public class IPMMSVertexServiceImpl implements IIPMMSVertexService {
|
|
|
if(historyBestPaht==null){
|
|
|
int distance=starBestPath[0]+ipmmsVertexEdge.getWeigh();
|
|
|
int outVertex=outVertexID.intValue();
|
|
|
- int[] bestPath=new int[]{distance,outVertex};
|
|
|
+ int[] bestPath=new int[]{distance,outVertex,ipmmsVertexEdge.getWeigh()};
|
|
|
currentBestDinstance.put(inVertexID.toString(),bestPath);
|
|
|
}else if ((starBestPath[0]+ipmmsVertexEdge.getWeigh())<historyBestPaht[0]){
|
|
|
//更新和此顶点有关的数据
|
|
|
+ int distance=starBestPath[0]+ipmmsVertexEdge.getWeigh();
|
|
|
+ int outVertex=outVertexID.intValue();
|
|
|
+ int[] bestPath=new int[]{distance,outVertex,ipmmsVertexEdge.getWeigh()};
|
|
|
+ currentBestDinstance.put(inVertexID.toString(),bestPath);
|
|
|
+ ArrayList<String> updateList = new ArrayList<>();
|
|
|
+ updateRelactionVertex(updateList, stringMinimumValue, currentBestDinstance);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+ if (!currentBestDinstance.containsKey(endPoint)){
|
|
|
+ throw new VertexAngEdgeException("没有该条路径,请检查是否缺少边!");
|
|
|
+ }
|
|
|
return startPointToEndPointPaht(endPoint,currentBestDinstance,vertexList);
|
|
|
}
|
|
|
+ //更新相关的值
|
|
|
+ public void updateRelactionVertex(List<String> result,String currentVertex,HashMap<String,int[]> currentBestDinstance){
|
|
|
+ Set<String> keys = currentBestDinstance.keySet();
|
|
|
+ for (String key: keys){
|
|
|
+ int[] value=currentBestDinstance.get(key);
|
|
|
+ if (value[1]==Integer.valueOf(currentVertex).intValue()){
|
|
|
+ value[0]=value[2]+currentBestDinstance.get(currentVertex)[0];
|
|
|
+ currentBestDinstance.put(key,value);
|
|
|
+ updateRelactionVertex(result,key,currentBestDinstance);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
//判断是否有进入这个顶点的边,如果没有这个顶点就是一个孤岛
|
|
|
public Boolean island(HashMap<String,int[]> currentBestDinstance,String stringMinimumValue,Map<String, List<IPMMSVertexEdge>> vertexEdgeList){
|
|
|
if(!currentBestDinstance.containsKey(stringMinimumValue)){
|
|
@@ -91,10 +108,10 @@ public class IPMMSVertexServiceImpl implements IIPMMSVertexService {
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
- //通过所有最优解集合选择我们要的
|
|
|
+ //通过所有最优解集合选择我们要的路径
|
|
|
public ArrayList<IPMMSVertex> startPointToEndPointPaht(String point,HashMap<String,int[]> currentBestDinstance,Map<String, IPMMSVertex> vertexList){
|
|
|
ArrayList<IPMMSVertex> obtainOptimalPath=new ArrayList();
|
|
|
- int beforeDistance=100;
|
|
|
+ int beforeDistance=0;
|
|
|
do {
|
|
|
int[] ints = currentBestDinstance.get(point);
|
|
|
beforeDistance=ints[1];
|
|
@@ -105,6 +122,7 @@ public class IPMMSVertexServiceImpl implements IIPMMSVertexService {
|
|
|
return obtainOptimalPath;
|
|
|
}
|
|
|
|
|
|
+ //获得目前最短距离
|
|
|
public String getStringMinimumValue(HashMap<String,int[]> currentBestDinstance,List<String> obtainTheOptimalPath){
|
|
|
|
|
|
HashMap<String,int[]> bestDinstance = (HashMap<String, int[]>) SerializationUtils.clone(currentBestDinstance);
|