Java实现dijkstra算法: 地图中任意起点寻找最佳路径

import java.util.*;


public class Dijkstra {


ArrayList <Node>Collection = new ArrayList<Node>(); //储存所有的点
ArrayList <Node>Route = new ArrayList<Node>(); //记录路径


ArrayList <Node>Waiting = new ArrayList<Node>();//此轮候选的点


String current = null;


int length = 0;


boolean addNodes(){
Collection.add(new Node("A","B",20));
Collection.add(new Node("A","D",80));
Collection.add(new Node("A","G",90));
Collection.add(new Node("B","F",10));
Collection.add(new Node("C","F",50));
Collection.add(new Node("C","H",20));
Collection.add(new Node("C","D",10));
Collection.add(new Node("D","G",20));
Collection.add(new Node("D","C",10));
Collection.add(new Node("E","B",50));
Collection.add(new Node("E","G",30));
Collection.add(new Node("F","C",10));
Collection.add(new Node("F","D",40));
Collection.add(new Node("G","A",20));
return true;
}


void setNext(String nt){
current = nt;
}


void update(ArrayList<Node> wl){


ArrayList<Node> update = new ArrayList<Node>();


for(Node a:wl){
for(Node b:wl){
if(a.getDest() == b.getDest()&&a.getDist()<b.getDist()){
update.add(b);
}}}


for(Node c:update){
wl.remove(c);
}
}


void Remove(ArrayList<Node> coll,String nt){
ArrayList<Node> move = new ArrayList<Node>();
for(Node a:coll){
if(nt == a.getDest())
move.add(a);
}


for(Node a:move){
if(coll.contains(a))
coll.remove(a);
}


}


void linkRoute(ArrayList<Node> rt){


ArrayList<Node> linkroute = new ArrayList<Node>();


int last = rt.size()-1;


for(int i=rt.size()-2;i>-1;i--){
if(rt.get(last).getDep() != rt.get(i).getDest()){
linkroute.add(rt.get(i));
}else if(rt.get(last).getDep() == rt.get(i).getDest()){
last = i;
}
}


for(Node a:linkroute){
rt.remove(a);
}


}


void toWait(String nt){


Remove(Collection,current);


for(Node a:Collection){


if(nt == a.getDep()){
Waiting.add(new Node(a.getDep(),a.getDest(),a.getDist()+length));
}
}
for(Node b:Waiting){
if(Collection.contains(b))
Collection.remove(b);
}


update(Waiting);
}


boolean selectNext(ArrayList<Node> wl){
if(wl.size() == 0){
System.out.println("done");
return false;
}else if(wl.size() == 1){
current = wl.get(0).getDest();
length = wl.get(0).getDist();
Route.add(Route.size(), wl.get(0));
wl.remove(wl.get(0));


}else{
Node s = wl.get(0);


for(Node a:wl){
if(a.getDist()<s.getDist()){
s = a;
}
}
current = s.getDest();
length = s.getDist();
Route.add(Route.size(), s);


wl.remove(s);//将waiting list中的这一点删去


}
return true;
}


public void wrap(String start, String stop){
current = start;


if(addNodes()){


for(int i=0;;i++){
if(stop == current){
//Route.add();
break;}
toWait(current);
if(selectNext(Waiting))
continue;
else
break;
}


if(Route.size()!=0&&Route.get(Route.size()-1).getDest()==stop){
linkRoute(Route);


System.out.print(Route.get(0).getDep()+" -> ");


for(Node a:Route){
System.out.print(a.getDest()+" ");
}
System.out.print("distance: "+Route.get(Route.size()-1).getDist()+"\n");
}
else
System.err.println("no fucking way");


}
}


public static void main(String[] args){
new Dijkstra().wrap("A","G");
}


}


class Node{
String From;
String To;
int distance;


Node(String f, String t, int d){
this.From = f;
this.To = t;
this.distance = d;
}


public String getDep(){
return this.From;
}


public String getDest(){
return this.To;
}


public int getDist(){
return this.distance;
}



}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值