Java 对于Dijkstra';s算法?
我有一个Dijkstra算法的版本,我试图确定它的效率。我不太确定我的回答是否正确,我想知道我的回答是否正确 我知道优先级队列版本有更好的效率。我将放下伪代码,然后是下面的java代码,以防我遗漏了什么 请参阅伪代码中的注释以了解其来源:Java 对于Dijkstra';s算法?,java,performance,big-o,dijkstra,Java,Performance,Big O,Dijkstra,我有一个Dijkstra算法的版本,我试图确定它的效率。我不太确定我的回答是否正确,我想知道我的回答是否正确 我知道优先级队列版本有更好的效率。我将放下伪代码,然后是下面的java代码,以防我遗漏了什么 请参阅伪代码中的注释以了解其来源: O(E^2*V^2) dijkstra算法伪码类 执行方法 输入:图形的源顶点 输出:无 while(未访问的顶点!=0){/--->O(V) 节点:=getMinimum Unvisted visitedNodes.add(节点) 未访问的节点。删除(节点)
O(E^2*V^2)
dijkstra算法伪码类
执行方法
输入:图形的源顶点
输出:无
while(未访问的顶点!=0){/--->O(V)
节点:=getMinimum Unvisted
visitedNodes.add(节点)
未访问的节点。删除(节点)
findminimaldistance(节点)}
findMinimalDistances方法
输入:顶点
输出:无
adjacentNodes:=GetNextant(节点)
for(目标到相邻节点){
if(getShortestDistance(目标)>getShortestDistance(节点)+getDistance(节点,目标)){
距离。放置(目标,距离)
put(目标、节点)
unvisitedNodes.add(目标)}
}
getDistance方法
输入:正在计算的顶点和目标顶点
输出:整数距离
对于(边到边){/------------->O(E)
if(边缘源(节点)和边缘目标(目标)){
返回edge.getWeight}
}
获取相邻方法
输入:查找相邻顶点的顶点
输出:相邻顶点的ArrayList
对于(边到边){/-------------->O(E)
if(未访问edge.getSource.equals(节点)&)
添加(节点)
}
getMinimum方法
输入:未访问顶点的ArrayList
输出:最小顶点
对于(顶点到顶点){/----------->O(V)
如果(最小值=null)
最小值:=顶点
其他的
if(getShortestDistance(顶点)
代码如下:
package shortestPath;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DijkstraAlgorithm {
private final List<Vertex> nodes;
private final List<Edge> edges;
private Set<Vertex> visitedNodes;
private Set<Vertex> unvisitedNodes;
private Map<Vertex, Vertex> predecessors;
private Map<Vertex, Integer> distance;
public DijkstraAlgorithm(Graph graph) {
// create a copy of the array so that we can operate on this array
this.nodes = new ArrayList<Vertex>(graph.getVertexes());
this.edges = new ArrayList<Edge>(graph.getEdges());
}
public void execute(Vertex source) {
visitedNodes = new HashSet<Vertex>();
unvisitedNodes = new HashSet<Vertex>();
// Create hashmap for shortest distance to each node
distance = new HashMap<Vertex, Integer>();
predecessors = new HashMap<Vertex, Vertex>();
// Start with the source node
distance.put(source, 0);
unvisitedNodes.add(source);
// While there are still unvisited nodes
while (unvisitedNodes.size() > 0) {
// Hold minimum unvisited node
Vertex node = getMinimum(unvisitedNodes);
visitedNodes.add(node);
unvisitedNodes.remove(node);
// Put the node's distance in the distance map
findMinimalDistances(node);
}
}
// Find shortest distance from current minimum node to neighbors
private void findMinimalDistances(Vertex node) {
// Initialize list to find adjacent nodes
List<Vertex> adjacentNodes = getAdjacent(node);
// Cycle through neighbors
for (Vertex target : adjacentNodes) {
// If i is greater than the node current shortest distance + the distance from node to i
if (getShortestDistance(target) > getShortestDistance(node) + getDistance(node, target)) {
// Update i node to shorter distance in distance map
distance.put(target, getShortestDistance(node) + getDistance(node, target));
predecessors.put(target, node);
unvisitedNodes.add(target);
}
}
}
// Gets distance of node to target vertex
private int getDistance(Vertex node, Vertex target) {
// Cycle through all edges
for (Edge edge : edges) {
// If edge begins at node and ends at target get the distance
if (edge.getSource().equals(node) && edge.getDestination().equals(target)) {
return edge.getWeight();
}
}
throw new RuntimeException("Should not happen");
}
// Find adjacent nodes to input node and return arraylist of neighbors
private List<Vertex> getAdjacent(Vertex node) {
List<Vertex> neighbors = new ArrayList<Vertex>();
// Cycle through all edges
for (Edge edge : edges) {
// If edge source is the input node and destination node of edge is not in visited list
// add as neighbor
if (edge.getSource().equals(node) && !isVisited(edge.getDestination())) {
neighbors.add(edge.getDestination());
}
}
return neighbors;
}
// Returns minimum of unvisited nodes
private Vertex getMinimum(Set<Vertex> vertexes) {
// First pass initialize minimum node to null
Vertex minimum = null;
// Cycle through entire unvisited list for minimum unvisited node
// --> Note: Minimum unvisited node on first iteration is (source node, 0 distance)
for (Vertex vertex : vertexes) {
// If minimum is set to null (first pass), set minimum to first unvisited node
if (minimum == null) {
minimum = vertex;
// Else if next node is less, set it to minimum
} else {
if (getShortestDistance(vertex) < getShortestDistance(minimum)) {
minimum = vertex;
}
}
}
return minimum;
}
private boolean isVisited(Vertex vertex) {
return visitedNodes.contains(vertex);
}
// Get shortest distance of node from source
private int getShortestDistance(Vertex destination) {
// Set d equal to the distance assigned to the node currently in distance map, if available
Integer d = distance.get(destination);
// If node not yet added to distance map
if (d == null) {
// Return infinity
return Integer.MAX_VALUE;
// Else return distance assigned to that node
} else {
return d;
}
}
/*
* This method returns the path from the source to the selected target and
* NULL if no path exists
*/
public LinkedList<Vertex> getPath(Vertex target) {
LinkedList<Vertex> path = new LinkedList<Vertex>();
Vertex step = target;
// check if a path exists
if (predecessors.get(step) == null) {
return null;
}
path.add(step);
while (predecessors.get(step) != null) {
step = predecessors.get(step);
path.add(step);
}
// Put it into the correct order
Collections.reverse(path);
return path;
}
}
包最短路径;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.HashSet;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
公共类dijkstra算法{
私有最终列表节点;
私人最终名单边缘;
私有设置访问节点;
私有集未访问节点;
私有地图;
私有地图距离;
公共Dijkstra算法(图){
//创建阵列的副本,以便我们可以在此阵列上操作
this.nodes=newArrayList(graph.getVertexs());
this.edges=newarraylist(graph.getEdges());
}
公共void执行(顶点源){
visitedNodes=新HashSet();
unvisitedNodes=新HashSet();
//为到每个节点的最短距离创建hashmap
距离=新HashMap();
前辈=新的HashMap();
//从源节点开始
距离。放置(源,0);
未访问的节点。添加(源);
//但仍然存在未访问的节点
而(unvisitedNodes.size()>0){
//保持最小未访问节点
顶点节点=获取最小值(未访问的节点);
visitedNodes.add(节点);
未访问的节点。删除(节点);
//在距离贴图中放置节点的距离
findminimaldistance(节点);
}
}
//查找从当前最小节点到邻居的最短距离
专用void findminimaldistance(顶点节点){
//初始化列表以查找相邻节点
List adjacentNodes=GetNextant(节点);
//周游邻居
对于(顶点目标:相邻节点){
//如果i大于节点当前最短距离+节点到i的距离
if(getShortestDistance(目标)>getShortestDistance(节点)+getDistance(节点,目标)){
//将i节点更新为距离贴图中的较短距离
距离.put(目标,getShortestDistance(节点)+getDistance(节点,目标));
put(目标、节点);
未访问的节点。添加(目标);
}
}
}
//获取节点到目标顶点的距离
私有int getDistance(顶点节点、顶点目标){
//循环遍历所有边
用于(边:边){
//如果边开始于节点,结束于目标,则获取距离
if(edge.getSource().equals(节点)和&edge.getDestination().equals(目标)){
返回edge.getWeight();
}
}
抛出新的RuntimeException(“不应该发生”);
}
//查找输入节点的相邻节点并返回相邻节点的arraylist
私有列表(顶点节点){
列表邻居=新的ArrayList();
//循环遍历所有边
用于(边:边){
//如果边缘源是输入节点,而边缘的目标节点不在已访问列表中
//添加为邻居
if(edge.getSource().equals(node)&&!isvisted(edge.getDestination()){
add(edge.getDestination());
}
}
回归邻居;
}
//返回未访问节点的最小值
私有顶点getMinimum(设置顶点){
//第一次通过将最小节点初始化为null
顶点最小值=零;
//在整个未访问列表中循环查找最小未访问节点
//-->注意:第一次迭代时未访问的最小节点为(源节点,0距离)
用于(顶点:顶点){
//如果最小值设置为null(第一次通过),则将最小值设置为第一个未访问的节点
如果(最小值==null){
最小值=顶点;
//否则,如果下一个节点较少,请将其设置为最小值
}否则{
if(getShortestDistance(顶点)package shortestPath;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DijkstraAlgorithm {
private final List<Vertex> nodes;
private final List<Edge> edges;
private Set<Vertex> visitedNodes;
private Set<Vertex> unvisitedNodes;
private Map<Vertex, Vertex> predecessors;
private Map<Vertex, Integer> distance;
public DijkstraAlgorithm(Graph graph) {
// create a copy of the array so that we can operate on this array
this.nodes = new ArrayList<Vertex>(graph.getVertexes());
this.edges = new ArrayList<Edge>(graph.getEdges());
}
public void execute(Vertex source) {
visitedNodes = new HashSet<Vertex>();
unvisitedNodes = new HashSet<Vertex>();
// Create hashmap for shortest distance to each node
distance = new HashMap<Vertex, Integer>();
predecessors = new HashMap<Vertex, Vertex>();
// Start with the source node
distance.put(source, 0);
unvisitedNodes.add(source);
// While there are still unvisited nodes
while (unvisitedNodes.size() > 0) {
// Hold minimum unvisited node
Vertex node = getMinimum(unvisitedNodes);
visitedNodes.add(node);
unvisitedNodes.remove(node);
// Put the node's distance in the distance map
findMinimalDistances(node);
}
}
// Find shortest distance from current minimum node to neighbors
private void findMinimalDistances(Vertex node) {
// Initialize list to find adjacent nodes
List<Vertex> adjacentNodes = getAdjacent(node);
// Cycle through neighbors
for (Vertex target : adjacentNodes) {
// If i is greater than the node current shortest distance + the distance from node to i
if (getShortestDistance(target) > getShortestDistance(node) + getDistance(node, target)) {
// Update i node to shorter distance in distance map
distance.put(target, getShortestDistance(node) + getDistance(node, target));
predecessors.put(target, node);
unvisitedNodes.add(target);
}
}
}
// Gets distance of node to target vertex
private int getDistance(Vertex node, Vertex target) {
// Cycle through all edges
for (Edge edge : edges) {
// If edge begins at node and ends at target get the distance
if (edge.getSource().equals(node) && edge.getDestination().equals(target)) {
return edge.getWeight();
}
}
throw new RuntimeException("Should not happen");
}
// Find adjacent nodes to input node and return arraylist of neighbors
private List<Vertex> getAdjacent(Vertex node) {
List<Vertex> neighbors = new ArrayList<Vertex>();
// Cycle through all edges
for (Edge edge : edges) {
// If edge source is the input node and destination node of edge is not in visited list
// add as neighbor
if (edge.getSource().equals(node) && !isVisited(edge.getDestination())) {
neighbors.add(edge.getDestination());
}
}
return neighbors;
}
// Returns minimum of unvisited nodes
private Vertex getMinimum(Set<Vertex> vertexes) {
// First pass initialize minimum node to null
Vertex minimum = null;
// Cycle through entire unvisited list for minimum unvisited node
// --> Note: Minimum unvisited node on first iteration is (source node, 0 distance)
for (Vertex vertex : vertexes) {
// If minimum is set to null (first pass), set minimum to first unvisited node
if (minimum == null) {
minimum = vertex;
// Else if next node is less, set it to minimum
} else {
if (getShortestDistance(vertex) < getShortestDistance(minimum)) {
minimum = vertex;
}
}
}
return minimum;
}
private boolean isVisited(Vertex vertex) {
return visitedNodes.contains(vertex);
}
// Get shortest distance of node from source
private int getShortestDistance(Vertex destination) {
// Set d equal to the distance assigned to the node currently in distance map, if available
Integer d = distance.get(destination);
// If node not yet added to distance map
if (d == null) {
// Return infinity
return Integer.MAX_VALUE;
// Else return distance assigned to that node
} else {
return d;
}
}
/*
* This method returns the path from the source to the selected target and
* NULL if no path exists
*/
public LinkedList<Vertex> getPath(Vertex target) {
LinkedList<Vertex> path = new LinkedList<Vertex>();
Vertex step = target;
// check if a path exists
if (predecessors.get(step) == null) {
return null;
}
path.add(step);
while (predecessors.get(step) != null) {
step = predecessors.get(step);
path.add(step);
}
// Put it into the correct order
Collections.reverse(path);
return path;
}
}