package org.iaiai.suanfa;
import java.util.ArrayList;
/**
*
* <p>
* Title: Dijkstra.java
* </p>
* <p>
* E-Mail: 176291935@qq.com
* </p>
* <p>
* QQ: 176291935
* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-8-5
* </p>
*
* @author 丸子
* @version 0.0.1
*/
public class Dijkstra {
static ArrayList<Side> map = null;
static ArrayList<Integer> redAgg = null;
static ArrayList<Integer> blueAgg = null;
static Side[] parents = null;
public static void main(String[] args) {
// 初始化顶点集
int[] nodes = { 0, 1, 3, 2, 4, 5, 6 };
// 初始化有向权重图
map = new ArrayList<Side>();
map.add(new Side(0, 1, 10));
map.add(new Side(0, 3, 30));
map.add(new Side(0, 4, 100));
map.add(new Side(1, 2, 50));
map.add(new Side(2, 4, 10));
map.add(new Side(3, 2, 20));
map.add(new Side(3, 4, 60));
map.add(new Side(4, 5, 50));
map.add(new Side(3, 5, 60));
map.add(new Side(5, 6, 10));
map.add(new Side(3, 6, 80));
// 初始化已知最短路径的顶点集,即红点集,只加入顶点0
redAgg = new ArrayList<Integer>();
redAgg.add(nodes[0]);
// 初始化未知最短路径的顶点集,即蓝点集
blueAgg = new ArrayList<Integer>();
for (int i = 1; i < nodes.length; i++)
blueAgg.add(nodes[i]);
// 初始化每个顶点在最短路径中的父结点,及它们之间的权重,权重-1表示无连通
parents = new Side[nodes.length];
parents[0] = new Side(-1, nodes[0], 0);
for (int i = 0; i < blueAgg.size(); i++) {
int n = blueAgg.get(i);
parents[i + 1] = new Side(nodes[0], n, getWeight(nodes[0], n));
}
// 找从蓝点集中找出权重最小的那个顶点,并把它加入到红点集中
while (blueAgg.size() > 0) {
MinShortPath msp = getMinSideNode();
if (msp.getWeight() == -1)
msp.outputPath(nodes[0]);
else
msp.outputPath();
int node = msp.getLastNode();
redAgg.add(node);
// 如果因为加入了新的顶点,而导致蓝点集中的顶点的最短路径减小,则要重要设置
setWeight(node);
}
}
/** */
/**
* 得到一个节点的父节点
*
* @param parents
* @param node
* @return
*/
public static int getParent(Side[] parents, int node) {
if (parents != null) {
for (Side nd : parents) {
if (nd.getNode() == node) {
return nd.getPreNode();
}
}
}
return -1;
}
/** */
/**
* 重新设置蓝点集中剩余节点的最短路径长度
*
* @param preNode
* @param map
* @param blueAgg
*/
public static void setWeight(int preNode) {
if (map != null && parents != null && blueAgg != null) {
for (int node : blueAgg) {
MinShortPath msp = getMinPath(node);
int w1 = msp.getWeight();
if (w1 == -1)
continue;
for (Side n : parents) {
if (n.getNode() == node) {
if (n.getWeight() == -1 || n.getWeight() > w1) {
n.setWeight(w1);
n.setPreNode(preNode);// 重新设置顶点的父顶点
break;
}
}
}
}
}
}
/** */
/**
* 得到两点节点之间的权重
*
* @param map
* @param preNode
* @param node
* @return
*/
public static int getWeight(int preNode, int node) {
if (map != null) {
for (Side s : map) {
if (s.getPreNode() == preNode && s.getNode() == node)
return s.getWeight();
}
}
return -1;
}
/** */
/**
* 从蓝点集合中找出路径最小的那个节点
*
* @param map
* @param blueAgg
* @return
*/
public static MinShortPath getMinSideNode() {
MinShortPath minMsp = null;
if (blueAgg.size() > 0) {
int index = 0;
for (int j = 0; j < blueAgg.size(); j++) {
MinShortPath msp = getMinPath(blueAgg.get(j));
if (minMsp == null || msp.getWeight() != -1
&& msp.getWeight() < minMsp.getWeight()) {
minMsp = msp;
index = j;
}
}
blueAgg.remove(index);
}
return minMsp;
}
/** */
/**
* 得到某一节点的最短路径(实际上可能有多条,现在只考虑一条)
*
* @param node
* @return
*/
public static MinShortPath getMinPath(int node) {
MinShortPath msp = new MinShortPath(node);
if (parents != null && redAgg != null) {
for (int i = 0; i < redAgg.size(); i++) {
MinShortPath tempMsp = new MinShortPath(node);
int parent = redAgg.get(i);
int curNode = node;
while (parent > -1) {
int weight = getWeight(parent, curNode);
if (weight > -1) {
tempMsp.addNode(parent);
tempMsp.addWeight(weight);
curNode = parent;
parent = getParent(parents, parent);
} else
break;
}
if (msp.getWeight() == -1 || tempMsp.getWeight() != -1
&& msp.getWeight() > tempMsp.getWeight())
msp = tempMsp;
}
}
return msp;
}
}
package org.iaiai.suanfa;
import java.util.ArrayList;
/**
*
* <p>
* Title: MinShortPath.java
* </p>
* <p>
* E-Mail: 176291935@qq.com
* </p>
* <p>
* QQ: 176291935
* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-8-5
* </p>
*
* @author 丸子
* @version 0.0.1
*/
public class MinShortPath {
private ArrayList<Integer> nodeList;// 最短路径集
private int weight;// 最短路径
public MinShortPath(int node) {
nodeList = new ArrayList<Integer>();
nodeList.add(node);
weight = -1;
}
public ArrayList<Integer> getNodeList() {
return nodeList;
}
public void setNodeList(ArrayList<Integer> nodeList) {
this.nodeList = nodeList;
}
public void addNode(int node) {
if (nodeList == null)
nodeList = new ArrayList<Integer>();
nodeList.add(0, node);
}
public int getLastNode() {
int size = nodeList.size();
return nodeList.get(size - 1);
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void outputPath() {
outputPath(-1);
}
public void outputPath(int srcNode) {
String result = "[";
if (srcNode != -1)
nodeList.add(srcNode);
for (int i = 0; i < nodeList.size(); i++) {
result += "" + nodeList.get(i);
if (i < nodeList.size() - 1)
result += ",";
}
result += "]:" + weight;
System.out.println(result);
}
public void addWeight(int w) {
if (weight == -1)
weight = w;
else
weight += w;
}
}
package org.iaiai.suanfa;
/**
*
* <p>
* Title: Side.java
* </p>
* <p>
* E-Mail: 176291935@qq.com
* </p>
* <p>
* QQ: 176291935
* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-8-5
* </p>
*
* @author 丸子
* @version 0.0.1
*/
public class Side {
private int preNode; // 前向节点
private int node;// 后向节点
private int weight;// 权重
public Side(int preNode, int node, int weight) {
this.preNode = preNode;
this.node = node;
this.weight = weight;
}
public int getPreNode() {
return preNode;
}
public void setPreNode(int preNode) {
this.preNode = preNode;
}
public int getNode() {
return node;
}
public void setNode(int node) {
this.node = node;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
输出:
引用
[0,1]:10
[0,3]:30
[0,3,2]:50
[0,3,2,4]:60
[0,3,5]:90
[0,3,5,6]:100
分享到:
相关推荐
Dijkstra算法是最常用的单源最短路径算法,用于找到图中一个顶点到其他所有顶点的最短路径。在无向图中,Dijkstra算法通过维护一个优先队列(通常是二叉堆)来逐步扩展最短路径树。每次从队列中取出距离源点最近的...
总之,这个Java实现的迷宫最短路径算法项目是一个很好的学习资源,它涵盖了数据结构(如坐标位置类)、路径搜索算法以及如何在Java中编写清晰的代码。通过研究这些代码,开发者可以提升自己的算法理解和编程技能。
基于java的开发源码-最短路径算法实现 k-shortest-paths.zip 基于java的开发源码-最短路径算法实现 k-shortest-paths.zip 基于java的开发源码-最短路径算法实现 k-shortest-paths.zip 基于java的开发源码-最短路径...
7. 源码软件:在实际应用中,k-最短路径算法的实现通常涉及到编程,可能用C++, Java, Python等语言编写。源码软件应当包含数据结构(如邻接矩阵或邻接表)、算法实现以及可能的优化策略,例如使用优先队列(如二叉堆...
根据给定的信息,本文将详细解释Java实现的最短路径问题动态规划算法。该程序的主要目的是寻找图中各个节点到指定终点的最短路径,并输出每个节点到终点的最短距离以及达到这些最短距离时的决策路径。 ### 1. 问题...
Java编写的单元最短路径算法,是解决图论问题中的一种经典方法,主要用来寻找网络中的最短路径。在这个场景中,我们关注的是Dijkstra算法,这是一种贪心算法,由荷兰计算机科学家艾兹格·迪科斯彻在1956年提出。它能...
图的结构建立与最短路径算法是图论中两个核心概念,主要应用于网络优化、路径规划等领域。本文将深入探讨这两个主题。 首先,我们来理解什么是图的结构建立。图是由顶点(或节点)和边(或弧)构成的数据结构,用来...
【最短路径算法】 在计算机科学中,最短路径算法是一种用于寻找图中两点之间最短路径的方法。这里提到的算法是Dijkstra算法,一种贪心算法,它能有效地找到加权无向图中单个起点到所有其他顶点的最短路径。Dijkstra...
最短路径算法是图论中的一个经典问题,用于寻找图中两点之间路径的最小成本或时间。在Java中实现这样的算法可以帮助我们解决多种实际问题,比如网络路由、交通规划等。这里我们将深入探讨单源点最短路径的Dijkstra...
"基于Java的最短路径算法实现 k-shortest-paths.zip" 是一个压缩包,其中包含了用Java编程语言实现的一种算法,该算法旨在找到图中两个节点之间的多条最短路径,即k最短路径(K-Shortest Paths, KSP)。在这个讨论中...
**Floyd最短路径算法详解** Floyd-Warshall算法是一种经典的解决图中所有顶点对最短路径问题的算法,由美国计算机科学家Robert W. Floyd于1962年提出。该算法的核心思想是逐步考虑更多的中间节点,通过动态规划的...
Java实现的Dijkstra最短路径算法是图论中的一个经典算法,用于寻找图中两个节点间的最短路径。这个算法由荷兰计算机科学家艾兹格·迪科斯彻于1956年提出,广泛应用于路由选择、网络规划、任务调度等领域。在Java编程...
计算机毕业设计中最短路径算法的实现是计算科学与信息技术领域中的一个重要课题,特别是在网络路由、地理信息系统、游戏设计等多个场景中都有广泛应用。本项目聚焦于使用Java编程语言实现一种受限制的最短路径优先...
首先,我们需要了解最短路径算法。其中,Dijkstra算法和Floyd-Warshall算法是两种常用的方法。Dijkstra算法适用于单源最短路径问题,而Floyd-Warshall则可解决所有顶点对之间的最短路径。在这个项目中,具体使用哪种...
Floyd算法,也称为Floyd-Warshall算法,是一种用于解决所有对之间的最短路径问题的有效方法。这个算法的核心思想是动态规划,它通过逐步增加中间节点来探索可能的最短路径。 Floyd算法的基本步骤如下: 1. 初始化...
总结起来,Java最短路径软件结合了Swing GUI技术和最短路径算法,为用户提供了一个直观易用的解决方案来处理图的最短路径问题。通过理解和运用这些知识点,开发者可以创建出更高效、更友好的工具,服务于各类应用...
在这个"基于java的最短路径算法实现 k-shortest-paths.zip"压缩包中,很显然包含了一个用Java实现的求解最短路径问题的程序,尤其是K-Shortest Paths(K条最短路径)算法。 K-Shortest Paths算法是一种扩展了...