迪杰斯特拉java代码 迪杰斯特拉算法c++代码

寻求大神帮忙写Java代码,要用Dijkstra’s algorithm(迪杰斯特拉算法)

package minRoad.no;

创新互联成都网站建设专业公司,是成都网站开发公司,为服务器租用提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站改版热线:028-86922220

import java.util.Arrays;

//这个程序用来求得一个图的最短路径矩阵

public class ShortestDistance_V4 {

private static final int inf = Integer.MAX_VALUE;// 表示两个点之间无法直接连通

public static int[][] dijkstra(int[][] graph) {

int min, v, u = 0, n = graph.length;

int[] path = new int[n];

int[] dist = new int[n];

boolean[] s = new boolean[n];

Arrays.fill(s, false);

Arrays.fill(dist, inf);

for (int i = 0; i n; i++) {

dist[i] = graph[u][i];

if (i != u dist[i] inf)

path[i] = u;

else

path[i] = -1;

}

s[u] = true;

while (true) {

min = inf;

v = -1;

// 找到最小的dist

for (int i = 0; i n; i++) {

if (!s[i]) {

if (dist[i] min) {

min = dist[i];

v = i;

}

}

}

if (v == -1) break;// 找不到更短的路径了

// 更新最短路径

s[v] = true;

for (int i = 0; i n; i++) {

if (!s[i] graph[v][i] != inf dist[v] + graph[v][i] dist[i]) {

dist[i] = dist[v] + graph[v][i];

path[i] = v;

}

}

}

// 输出路径

int[] shortest = new int[n];

for (int i = 1; i n; i++) {

Arrays.fill(shortest, 0);

int k = 0;

shortest[k] = i;

while (path[shortest[k]] != 0) {

k++;

shortest[k] = path[shortest[k - 1]];

}

k++;

shortest[k] = 0;

}

int[] tmp = new int[shortest.length];

for (int i = 0; i tmp.length; i++) {

tmp[i] = shortest[tmp.length - i - 1];

}

return new int[][] { dist, tmp };

}

/**

* pre

* v0

* 1, v1

* 4, 2, v2

* inf, 7, -1, v3

* inf, 5, 1, 3, v4

* inf, inf, inf, 2, 6, v5

* /pre

*

* *

*

* pre

* A--------30-------D

* |\ ∧|

* | \ / |

* | \ / |

* | 10 10 |

* | \ / 20

* | \ / |

* | \ / |

* | ∨ / ∨

* 20 B E

* | / ∧

* | / /

* | / /

* | 5 /

* | / 30

* | / /

* | / /

* ∨∠ /

* C

* /pre

*

* @param args

*/

public static void main(String[] args) {

int[][] W1 = {

{ 0, 10, 20, 30, inf },

{ 10, 0, 5, 10, inf },

{ 20, 5, 0, inf, 30 },

{ 30, 10, inf, 0, 20 },

{ inf, inf, 30, 20, 0 },

};

//

//

// int[][] W = {

// { 0, 1, 4, inf, inf, inf },

// { 1, 0, 2, 7, 5, inf },

// { 4, 2, 0, inf, 1, inf },

// { inf, 7, inf, 0, 3, 2 },

// { inf, 5, 1, 3, 0, 6 },

// { inf, inf, inf, 2, 6, 0 }};

int[][] distAndShort = dijkstra(W1);

System.out.println(Arrays.toString(distAndShort[0]));

System.out.println(Arrays.toString(distAndShort[1]));

// distance: { 0, 1, 3, 7, 4, 9};

}

}

求PROMETHEE-II算法代码

Dijkstra算法

1.定义概览

Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法是很有代表性的最短路径算法,在很多专业课程中都作为基本内容有详细的介绍,如数据结构,图论,运筹学等等。注意该算法要求图中不存在负权边。

问题描述:在无向图 G=(V,E) 中,假设每条边 E[i] 的长度为 w[i],找到由顶点 V0 到其余各点的最短路径。(单源最短路径)

2.算法描述

1)算法思想:设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , 就将加入到集合S中,直到全部顶点都加入到S中,算法就结束了),第二组为其余未确定最短路径的顶点集合(用U表示),按最短路径长度的递增次序依次把第二组的顶点加入S中。在加入的过程中,总保持从源点v到S中各顶点的最短路径长度不大于从源点v到U中任何顶点的最短路径长度。此外,每个顶点对应一个距离,S中的顶点的距离就是从v到此顶点的最短路径长度,U中的顶点的距离,是从v到此顶点只包括S中的顶点为中间顶点的当前最短路径长度。

2)算法步骤:

a.初始时,S只包含源点,即S={v},v的距离为0。U包含除v外的其他顶点,即:U={其余顶点},若v与U中顶点u有边,则u,v正常有权值,若u不是v的出边邻接点,则u,v权值为∞。

b.从U中选取一个距离v最小的顶点k,把k,加入S中(该选定的距离就是v到k的最短路径长度)。

c.以k为新考虑的中间点,修改U中各顶点的距离;若从源点v到顶点u的距离(经过顶点k)比原来距离(不经过顶点k)短,则修改顶点u的距离值,修改后的距离值的顶点k的距离加上边上的权。

d.重复步骤b和c直到所有顶点都包含在S中。

执行动画过程如下图

3.算法代码实现:

复制代码

const int MAXINT = 32767;

const int MAXNUM = 10;

int dist[MAXNUM];

int prev[MAXNUM];

int A[MAXUNM][MAXNUM];

void Dijkstra(int v0)

{

bool S[MAXNUM]; // 判断是否已存入该点到S集合中

int n=MAXNUM;

for(int i=1; i=n; ++i)

{

dist[i] = A[v0][i];

S[i] = false; // 初始都未用过该点

if(dist[i] == MAXINT)

prev[i] = -1;

else

prev[i] = v0;

}

  dist[v0] = 0;

  S[v0] = true;

for(int i=2; i=n; i++)

{

int mindist = MAXINT;

int u = v0; // 找出当前未使用的点j的dist[j]最小值

for(int j=1; j=n; ++j)

if((!S[j]) dist[j]mindist)

{

u = j; // u保存当前邻接点中距离最小的点的号码

    mindist = dist[j];

}

S[u] = true;

for(int j=1; j=n; j++)

if((!S[j]) A[u][j]MAXINT)

{

   if(dist[u] + A[u][j] dist[j]) //在通过新加入的u点路径找到离v0点更短的路径

   {

dist[j] = dist[u] + A[u][j]; //更新dist

prev[j] = u; //记录前驱顶点

}

   }

}

}

用java怎么用迪杰斯特拉算有向图有权值的最短路径

 Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。

Dijkstra一般的表述通常有两种方式,一种用永久和临时标号方式,一种是用OPEN, CLOSE表方式

用OPEN,CLOSE表的方式,其采用的是贪心法的算法策略,大概过程如下:

1.声明两个集合,open和close,open用于存储未遍历的节点,close用来存储已遍历的节点

2.初始阶段,将初始节点放入close,其他所有节点放入open

3.以初始节点为中心向外一层层遍历,获取离指定节点最近的子节点放入close并从新计算路径,直至close包含所有子节点

代码实例如下:

Node对象用于封装节点信息,包括名字和子节点

[java] view plain copy

public class Node {

private String name;

private MapNode,Integer child=new HashMapNode,Integer();

public Node(String name){

this.name=name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public MapNode, Integer getChild() {

return child;

}

public void setChild(MapNode, Integer child) {

this.child = child;

}

}

MapBuilder用于初始化数据源,返回图的起始节点

[java] view plain copy

public class MapBuilder {

public Node build(SetNode open, SetNode 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;

}

}

图的结构如下图所示:

Dijkstra对象用于计算起始节点到所有其他节点的最短路径

[java] view plain copy

public class Dijkstra {

SetNode open=new HashSetNode();

SetNode close=new HashSetNode();

MapString,Integer path=new HashMapString,Integer();//封装路径距离

MapString,String pathInfo=new HashMapString,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);

MapNode,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(){

SetMap.EntryString, String pathInfos=pathInfo.entrySet();

for(Map.EntryString, String pathInfo:pathInfos){

System.out.println(pathInfo.getKey()+":"+pathInfo.getValue());

}

}

/**

* 获取与node最近的子节点

*/

private Node getShortestPath(Node node){

Node res=null;

int minDis=Integer.MAX_VALUE;

MapNode,Integer childs=node.getChild();

for(Node child:childs.keySet()){

if(open.contains(child)){

int distance=childs.get(child);

if(distanceminDis){

minDis=distance;

res=child;

}

}

}

return res;

}

}

Main用于测试Dijkstra对象

[java] view plain copy

public class Main {

public static void main(String[] args) {

Dijkstra test=new Dijkstra();

Node start=test.init();

test.computePath(start);

test.printPathInfo();

}

}

分享文章:迪杰斯特拉java代码 迪杰斯特拉算法c++代码
URL地址:https://www.cdcxhl.com/article8/dddgoip.html

成都网站建设公司_创新互联,为您提供静态网站网站维护商城网站定制网站域名注册建站公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站优化排名