迪克斯塔';s算法Java-如何从对象的ArrayList中找到距离值最短的顶点
我试图用Java实现Dijkstra的算法。我使用的是ArrayList,我知道还有其他数据结构可能更适合此任务。但是,我想熟悉ArrayList,所以我将使用它 我有一个对象的数组列表。这些对象包含多个值,其中一个值是与源节点的距离。如何选择具有最小距离值的节点 然后我想将具有最小距离值的顶点的名称设置为字符串“minimate” 这是我到目前为止所拥有的迪克斯塔';s算法Java-如何从对象的ArrayList中找到距离值最短的顶点,java,arraylist,dijkstra,Java,Arraylist,Dijkstra,我试图用Java实现Dijkstra的算法。我使用的是ArrayList,我知道还有其他数据结构可能更适合此任务。但是,我想熟悉ArrayList,所以我将使用它 我有一个对象的数组列表。这些对象包含多个值,其中一个值是与源节点的距离。如何选择具有最小距离值的节点 然后我想将具有最小距离值的顶点的名称设置为字符串“minimate” 这是我到目前为止所拥有的 import java.io.File; import java.io.FileNotFoundException; import jav
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
public class Graph
{
ArrayList<Vertex> vertexObjects = new ArrayList<Vertex>();
ArrayList<Edge> edgeObjects = new ArrayList<Edge>();
ArrayList<Vertex> visitedObjects = new ArrayList<Vertex>();
ArrayList<Vertex> unvisitedObjects = new ArrayList<Vertex>();
int numVertices = 0;
public void readFile()
{
try {
Scanner s = new Scanner(new File("graph.txt"));
String sameVertex = "";
while (s.hasNext()) {
String preVertex = s.next();
String postVertex = s.next();
String distance = s.next();
Edge temp = new Edge(preVertex, postVertex, distance);
edgeObjects.add(temp);
if (!(preVertex.equals(sameVertex)))
{
Vertex herp = new Vertex(preVertex, Double.POSITIVE_INFINITY, false, null);
vertexObjects.add(herp);
sameVertex = preVertex;
numVertices++;
}
}
} catch (FileNotFoundException e) {
System.out.println("I can't find that file!");
}
}
public void Dijkstra(String startVertex, String endVertex)
{
// Change the distance of the startVertex to 0 and all others to infinity.
for (int i = (numVertices-1); i >= 0; i--)
{
if (vertexObjects.get(i).vertexName.equals(startVertex))
{
vertexObjects.get(i).distance = 0;
} else {
vertexObjects.get(i).distance = Double.POSITIVE_INFINITY;
}
unvisitedObjects.add(vertexObjects.get(i));
}
String currentVertex = startVertex;
//Set the node with lowest distance value to Current Status
while( unvisitedObjects.size() != 0) {
{
//set current node to vertex with shortest distance
string smallest;
for (int j = (vertexObjects.size()-1); j >= 0; j++) {
if (vertexObjects.get(j).distance < 0) {
smallest = vertexObjects.get(j).distance;
}
}
}
}
}
public class Vertex
{
public String vertexName;
public double distance;
public boolean visitedStatus;
public String previousVertex;
public Vertex(String vertexName, double distance, boolean visitedStatus, String previousVertex)
{
this.vertexName = vertexName;
this.distance = distance;
this.visitedStatus = visitedStatus;
this.previousVertex = previousVertex;
}
public String toString()
{
String d;
if (this.visitedStatus = false)
{
d = "unvisited";
} else {
d = "visited";
}
String s = "Vertex " + vertexName + " is " + d + "and is " + distance + " away from " + previousVertex + ".";
return s;
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
导入java.util.ArrayList;
公共类图
{
ArrayList VertexObject=新的ArrayList();
ArrayList EdgeObject=新的ArrayList();
ArrayList visitedObjects=新建ArrayList();
ArrayList unvisitedObjects=新建ArrayList();
int数值=0;
公共void readFile()
{
试一试{
扫描仪s=新扫描仪(新文件(“graph.txt”);
字符串sameVertex=“”;
而(s.hasNext()){
字符串preVertex=s.next();
字符串postVertex=s.next();
字符串距离=s.next();
边温度=新边(顶点前、顶点后、距离);
边对象添加(温度);
如果(!(preVertex.equals(sameVertex)))
{
顶点herp=新顶点(preVertex,Double.POSITIVE_∞,false,null);
顶点对象添加(herp);
sameVertex=preVertex;
numVertices++;
}
}
}catch(filenotfounde异常){
System.out.println(“我找不到那个文件!”);
}
}
公共void Dijkstra(字符串startVertex、字符串endVertex)
{
//将startVertex的距离更改为0,将所有其他距离更改为无穷大。
对于(int i=(num比值-1);i>=0;i--)
{
if(vertexObjects.get(i).vertexName.equals(startVertex))
{
顶点对象.get(i).distance=0;
}否则{
顶点对象.get(i).distance=Double.POSITIVE_无穷大;
}
添加(vertexObjects.get(i));
}
字符串currentVertex=startVertex;
//将距离值最小的节点设置为当前状态
while(unvisitedObjects.size()!=0){
{
//将当前节点设置为距离最短的顶点
弦最小;
对于(int j=(vertexObjects.size()-1);j>=0;j++){
if(顶点对象获取(j)距离<0){
最小=顶点对象。获取(j)。距离;
}
}
}
}
}
公共类顶点
{
公共字符串vertexName;
公共双距离;
公共布尔访问状态;
公共字符串优先顶点;
公共顶点(字符串vertexName、双距离、布尔visitedStatus、字符串previousVertex)
{
this.vertexName=vertexName;
这个距离=距离;
this.visitedStatus=visitedStatus;
this.previousVertex=previousVertex;
}
公共字符串toString()
{
字符串d;
如果(this.visitedStatus=false)
{
d=“未访问”;
}否则{
d=“参观”;
}
字符串s=“Vertex”+vertexName+”是“+d+”并且距离“+previousVertex+”是“+distance+”;
返回s;
}
}你想过PriorityQueue吗?它会自动对需要用于探索可能路径的边进行排序。否则,你应该在编辑列表后对列表进行排序。我真的不明白。你问的是如何从ArrayList中获取最小值?这是我能想到的最容易实现的算法之一。这不应该是个问题m如果你试图实现Dijkstra,这比那复杂得多。确实,不要试图用香蕉来修复汽车引擎,这也没有意义。如果它是一个整数数组列表,我可以很容易地想到如何去做,但是因为值只是一个对象的属性,我不知道如何去做。我想出来了,但是我无法发布代码,因为我的代表太低,无法回答我自己的问题。