激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - java實(shí)現(xiàn)Dijkstra最短路徑算法

java實(shí)現(xiàn)Dijkstra最短路徑算法

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

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

任務(wù)描述:在一個(gè)無向圖中,獲取起始節(jié)點(diǎn)到所有其他節(jié)點(diǎn)的最短路徑描述

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

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

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

代碼實(shí)例如下:

node對(duì)象用于封裝節(jié)點(diǎn)信息,包括名字和子節(jié)點(diǎn)

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用于初始化數(shù)據(jù)源,返回圖的起始節(jié)點(diǎn)

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

圖的結(jié)構(gòu)如下圖所示:

java實(shí)現(xiàn)Dijkstra最短路徑算法

dijkstra對(duì)象用于計(jì)算起始節(jié)點(diǎn)到所有其他節(jié)點(diǎn)的最短路徑

?
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)設(shè)置為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");
 //將初始節(jié)點(diǎn)放入close,其他節(jié)點(diǎn)放入open
 node start=new mapbuilder().build(open,close);
 return start;
 }
 public void computepath(node start){
 node nearest=getshortestpath(start);//取距離start節(jié)點(diǎn)最近的子節(jié)點(diǎn),放入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)){//如果子節(jié)點(diǎn)在open中
  integer newcompute=path.get(nearest.getname())+childs.get(child);
  if(path.get(child.getname())>newcompute){//之前設(shè)置的距離大于新計(jì)算出來的距離
   path.put(child.getname(), newcompute);
   pathinfo.put(child.getname(), pathinfo.get(nearest.getname())+"->"+child.getname());
  }
  }
 }
 computepath(start);//重復(fù)執(zhí)行自己,確保所有子節(jié)點(diǎn)被遍歷
 computepath(nearest);//向外一層層遞歸,直至所有頂點(diǎn)被遍歷
 }
 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最近的子節(jié)點(diǎn)
 */
 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對(duì)象

?
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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 在线免费小视频 | 久操国产| 久草视频福利在线观看 | 伊人手机在线观看 | 黄视频免费在线观看 | 在线播放一区二区三区 | 天天草天天干天天 | 国产福利不卡一区二区三区 | 精品中文字幕久久久久四十五十骆 | 久久综合入口 | 日韩美香港a一级毛片 | 欧美成人激情在线 | 美女毛片在线观看 | 久久思思爱 | 黄色片视频观看 | 久色视频网站 | 国产一区二区影视 | mmmwww| 国产99精品视频 | 亚洲国产精品久久久久久久久久久 | 羞羞答答影院 | 中文字幕国产日韩 | 国产视频在线一区 | 一区二区视 | 精品久久久久久久久久久久久久 | 黄色欧美精品 | 色污视频在线观看 | h视频在线播放 | av在线久草 | 日日碰日日操 | av免费在线免费观看 | 久久精品欧美一区 | 久久吊 | 56av国产精品久久久久久久 | 91美女视频在线观看 | 黄色aaa视频 | 亚洲午夜精选 | 一级视频在线播放 | 蜜桃日韩 | 黑人一级片| 一本大道av|