java找图最短路径_Java-在距离加权图中的2点之间找到最短路径

小编典典

就像SplinterReality所说: There's no reason not to use Dijkstra's algorithm here.

下面的代码我从这里开始进行了昵称,并对其进行了修改以解决问题中的示例。

import java.util.PriorityQueue;

import java.util.List;

import java.util.ArrayList;

import java.util.Collections;

class Vertex implements Comparable

{

public final String name;

public Edge[] adjacencies;

public double minDistance = Double.POSITIVE_INFINITY;

public Vertex previous;

public Vertex(String argName) { name = argName; }

public String toString() { return name; }

public int compareTo(Vertex other)

{

return Double.compare(minDistance, other.minDistance);

}

}

class Edge

{

public final Vertex target;

public final double weight;

public Edge(Vertex argTarget, double argWeight)

{ target = argTarget; weight = argWeight; }

}

public class Dijkstra

{

public static void computePaths(Vertex source)

{

source.minDistance = 0.;

PriorityQueue vertexQueue = new PriorityQueue();

vertexQueue.add(source);

while (!vertexQueue.isEmpty()) {

Vertex u = vertexQueue.poll();

// Visit each edge exiting u

for (Edge e : u.adjacencies)

{

Vertex v = e.target;

double weight = e.weight;

double distanceThroughU = u.minDistance + weight;

if (distanceThroughU < v.minDistance) {

vertexQueue.remove(v);

v.minDistance = distanceThroughU ;

v.previous = u;

vertexQueue.add(v);

}

}

}

}

public static List getShortestPathTo(Vertex target)

{

List path = new ArrayList();

for (Vertex vertex = target; vertex != null; vertex = vertex.previous)

path.add(vertex);

Collections.reverse(path);

return path;

}

public static void main(String[] args)

{

// mark all the vertices

Vertex A = new Vertex("A");

Vertex B = new Vertex("B");

Vertex D = new Vertex("D");

Vertex F = new Vertex("F");

Vertex K = new Vertex("K");

Vertex J = new Vertex("J");

Vertex M = new Vertex("M");

Vertex O = new Vertex("O");

Vertex P = new Vertex("P");

Vertex R = new Vertex("R");

Vertex Z = new Vertex("Z");

// set the edges and weight

A.adjacencies = new Edge[]{ new Edge(M, 8) };

B.adjacencies = new Edge[]{ new Edge(D, 11) };

D.adjacencies = new Edge[]{ new Edge(B, 11) };

F.adjacencies = new Edge[]{ new Edge(K, 23) };

K.adjacencies = new Edge[]{ new Edge(O, 40) };

J.adjacencies = new Edge[]{ new Edge(K, 25) };

M.adjacencies = new Edge[]{ new Edge(R, 8) };

O.adjacencies = new Edge[]{ new Edge(K, 40) };

P.adjacencies = new Edge[]{ new Edge(Z, 18) };

R.adjacencies = new Edge[]{ new Edge(P, 15) };

Z.adjacencies = new Edge[]{ new Edge(P, 18) };

computePaths(A); // run Dijkstra

System.out.println("Distance to " + Z + ": " + Z.minDistance);

List path = getShortestPathTo(Z);

System.out.println("Path: " + path);

}

}

上面的代码产生:

Distance to Z: 49.0

Path: [A, M, R, P, Z]

2020-09-23

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值