一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java實現Dijkstra最短路徑算法

java實現Dijkstra最短路徑算法

2021-07-10 10:29javaman_chen Java教程

這篇文章主要為大家詳細介紹了java實現Dijkstra最短路徑算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

任務描述:在一個無向圖中,獲取起始節點到所有其他節點的最短路徑描述

dijkstra(迪杰斯特拉)算法是典型的最短路徑路由算法,用于計算一個節點到其他所有節點的最短路徑。主要特點是以起始點為中心向外層層擴展,直到擴展到終點為止。

dijkstra一般的表述通常有兩種方式,一種用永久和臨時標號方式,一種是用open, close表方式
用open,close表的方式,其采用的是貪心法的算法策略,大概過程如下:

1.聲明兩個集合,open和close,open用于存儲未遍歷的節點,close用來存儲已遍歷的節點
2.初始階段,將初始節點放入close,其他所有節點放入open
3.以初始節點為中心向外一層層遍歷,獲取離指定節點最近的子節點放入close并從新計算路徑,直至close包含所有子節點

代碼實例如下:

node對象用于封裝節點信息,包括名字和子節點

java" id="highlighter_373167">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class node {
 private string name;
 private map<node,integer> child=new hashmap<node,integer>();
 public node(string name){
 this.name=name;
 }
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
 public map<node, integer> getchild() {
 return child;
 }
 public void setchild(map<node, integer> child) {
 this.child = child;
 }
}

mapbuilder用于初始化數據源,返回圖的起始節點

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class mapbuilder {
 public node build(set<node> open, set<node> close){
 node nodea=new node("a");
 node nodeb=new node("b");
 node nodec=new node("c");
 node noded=new node("d");
 node nodee=new node("e");
 node nodef=new node("f");
 node nodeg=new node("g");
 node nodeh=new node("h");
 nodea.getchild().put(nodeb, 1);
 nodea.getchild().put(nodec, 1);
 nodea.getchild().put(noded, 4);
 nodea.getchild().put(nodeg, 5);
 nodea.getchild().put(nodef, 2);
 nodeb.getchild().put(nodea, 1);
 nodeb.getchild().put(nodef, 2);
 nodeb.getchild().put(nodeh, 4);
 nodec.getchild().put(nodea, 1);
 nodec.getchild().put(nodeg, 3);
 noded.getchild().put(nodea, 4);
 noded.getchild().put(nodee, 1);
 nodee.getchild().put(noded, 1);
 nodee.getchild().put(nodef, 1);
 nodef.getchild().put(nodee, 1);
 nodef.getchild().put(nodeb, 2);
 nodef.getchild().put(nodea, 2);
 nodeg.getchild().put(nodec, 3);
 nodeg.getchild().put(nodea, 5);
 nodeg.getchild().put(nodeh, 1);
 nodeh.getchild().put(nodeb, 4);
 nodeh.getchild().put(nodeg, 1);
 open.add(nodeb);
 open.add(nodec);
 open.add(noded);
 open.add(nodee);
 open.add(nodef);
 open.add(nodeg);
 open.add(nodeh);
 close.add(nodea);
 return nodea;
 }
}

圖的結構如下圖所示:

java實現Dijkstra最短路徑算法

dijkstra對象用于計算起始節點到所有其他節點的最短路徑

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class dijkstra {
 set<node> open=new hashset<node>();
 set<node> close=new hashset<node>();
 map<string,integer> path=new hashmap<string,integer>();//封裝路徑距離
 map<string,string> pathinfo=new hashmap<string,string>();//封裝路徑信息
 public node init(){
 //初始路徑,因沒有a->e這條路徑,所以path(e)設置為integer.max_value
 path.put("b", 1);
 pathinfo.put("b", "a->b");
 path.put("c", 1);
 pathinfo.put("c", "a->c");
 path.put("d", 4);
 pathinfo.put("d", "a->d");
 path.put("e", integer.max_value);
 pathinfo.put("e", "a");
 path.put("f", 2);
 pathinfo.put("f", "a->f");
 path.put("g", 5);
 pathinfo.put("g", "a->g");
 path.put("h", integer.max_value);
 pathinfo.put("h", "a");
 //將初始節點放入close,其他節點放入open
 node start=new mapbuilder().build(open,close);
 return start;
 }
 public void computepath(node start){
 node nearest=getshortestpath(start);//取距離start節點最近的子節點,放入close
 if(nearest==null){
  return;
 }
 close.add(nearest);
 open.remove(nearest);
 map<node,integer> childs=nearest.getchild();
 for(node child:childs.keyset()){
  if(open.contains(child)){//如果子節點在open中
  integer newcompute=path.get(nearest.getname())+childs.get(child);
  if(path.get(child.getname())>newcompute){//之前設置的距離大于新計算出來的距離
   path.put(child.getname(), newcompute);
   pathinfo.put(child.getname(), pathinfo.get(nearest.getname())+"->"+child.getname());
  }
  }
 }
 computepath(start);//重復執行自己,確保所有子節點被遍歷
 computepath(nearest);//向外一層層遞歸,直至所有頂點被遍歷
 }
 public void printpathinfo(){
 set<map.entry<string, string>> pathinfos=pathinfo.entryset();
 for(map.entry<string, string> pathinfo:pathinfos){
  system.out.println(pathinfo.getkey()+":"+pathinfo.getvalue());
 }
 }
 /**
 * 獲取與node最近的子節點
 */
 private node getshortestpath(node node){
 node res=null;
 int mindis=integer.max_value;
 map<node,integer> childs=node.getchild();
 for(node child:childs.keyset()){
  if(open.contains(child)){
  int distance=childs.get(child);
  if(distance<mindis){
   mindis=distance;
   res=child;
  }
  }
 }
 return res;
 }
}

main用于測試dijkstra對象

?
1
2
3
4
5
6
7
8
public class main {
 public static void main(string[] args) {
 dijkstra test=new dijkstra();
 node start=test.init();
 test.computepath(start);
 test.printpathinfo();
 }
}

打印輸出如下:

d:a->d
e:a->f->e
f:a->f
g:a->c->g
b:a->b
c:a->c
h:a->b->h

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/javaman_chen/article/details/8254309

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女用手扒开粉嫩的屁股 | 风间由美被义子中文字幕 | 乌克兰成人性色生活片 | 亚洲福利视频在线观看 | 99久久免费国内精品 | 波多野结衣不卡 | 国产精品第一区揄拍 | 亚洲码在线观看 | 亚洲一区二区三区免费视频 | 男人把大ji巴放进女人小说 | 变态 另类 人妖小说 | 国产免费好大好硬视频 | 免费观看韩剧网站在线观看 | 亚洲欧美日韩综合在线 | 丰满的闺蜜2中文字幕 | 超91在线 | 校草太大了h | 99久久国产亚洲综合精品 | 亚洲精品中文字幕久久久久久 | 亚洲夜色夜色综合网站 | 欧洲男同直粗无套播放视频 | 日韩欧美中文字幕一区二区三区 | 欧美女人p| 国产区小视频 | 四虎www| 操b图片| 美女校花被调教出奶水 | cao逼视频| 亚洲精品在看在线观看 | 亚洲精品电影天堂网 | 亚洲精品无码不卡在线观看 | 色多多在线观看视频 | 强迫高h| 8天堂资源在线官网 | 性欧美高清理论片 | 91嫩草私人成人亚洲影院 | 日本免费在线观看视频 | 色啪啪888.com| 爆操女友 | 欧美另类老女人 | 久久久无码精品无码国产人妻丝瓜 |