Java 图上的Floyd-Warshall算法
我正在尝试使用floyd warshall算法为一个类寻找到图中所有节点的最短路径。我得到了图形的基本代码,并尝试通过伪代码实现该算法,但还没有弄清楚如何根据im中的迭代选择特定的边 这是算法的代码。我尝试更改的部分是“使用邻接矩阵权重值初始化”,我不确定如何选择特定的边权重Java 图上的Floyd-Warshall算法,java,graph,edges,floyd-warshall,Java,Graph,Edges,Floyd Warshall,我正在尝试使用floyd warshall算法为一个类寻找到图中所有节点的最短路径。我得到了图形的基本代码,并尝试通过伪代码实现该算法,但还没有弄清楚如何根据im中的迭代选择特定的边 这是算法的代码。我尝试更改的部分是“使用邻接矩阵权重值初始化”,我不确定如何选择特定的边权重 public static void FloydWarshall(Graph g) { int V = g.getvCount(); // to store the calculated
public static void FloydWarshall(Graph g) {
int V = g.getvCount();
// to store the calculated distances
float dist[][] = new float[V][V];
// initialize with adjacency matrix weight values
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
for(int k = 0; k<=g.edgeList.size(); k++){
if(g.edgeList.get(i).head.equals(i) && g.edgeList.get(j).tail.equals(j)){
int label = Integer.parseInt(g.edgeList.get(k).label);
dist[i][j] = label;
}}
}
}
// loop through all vertices one by one
for (int k = 0; k < V; k++) {
// pick all as source
for (int i = 0; i < V; i++) {
// pick all as destination
for (int j = 0; j < V; j++) {
// If k is on the shortest path from i to j
if (dist[i][k] + dist[k][j] < dist[i][j]) {
// update the value of dist[i][j]
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
// shortest path matrix
for (int z = 0; z < V; z++) {
for (int j = 0; j < V; j++) {
// if value is infinity
if (dist[z][j] == Float.MAX_VALUE)
System.out.print("INF ");
else
System.out.print(dist[z][j] + " ");
}
System.out.println();
}
公共静态空隙絮状物(图g){
int V=g.getvCount();
//存储计算的距离
浮动区[][]=新浮动[V][V];
//使用邻接矩阵权重值初始化
对于(int i=0;i
您可以将所有单元格初始化为无穷大,然后对于从顶点i到顶点j有边的每个单元格[i][j],将其距离设置为边权重。您可以通过迭代所有边来完成此操作
// initialize with adjacency matrix weight values
// set all to be initially infinity
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
dist[i][j] = Float.MAX_VALUE;
}
}
// set all edge weights
for(int k = 0; k <= g.edgeList.size(); k++){
Edge e = g.edgeList.get(k);
int i = Integer.parseInt(e.getHead().label);
int j = Integer.parseInt(e.getTail().label);
dist[i][j] = Integer.parseInt(e.getLabel());
}
//使用邻接矩阵权重值初始化
//将all设置为初始无穷大
对于(int i=0;iimport java.util.*;
public class Graph {
ArrayList<Node> nodeList;
ArrayList<Edge> edgeList;
public Graph() {
nodeList = new ArrayList<Node>();
edgeList = new ArrayList<Edge>();
}
public ArrayList<Node> getNodeList() {
return nodeList;
}
public ArrayList<Edge> getEdgeList() {
return edgeList;
}
public void addNode(Node n) {
nodeList.add(n);
}
public void addEdge(Edge e) {
edgeList.add(e);
}
public String toString() {
String s = "Graph g.\n";
if (nodeList.size() > 0) {
for (Node n : nodeList) {
// Print node info
String t = "\nNode " + n.getName() + ", abbrev " + n.getAbbrev() + ", value " + n.getVal() + "\n";
s = s.concat(t);
}
s = s.concat("\n");
}
return s;
}
public void sortNodes(){
Collections.sort(nodeList);
}
private int vCount;
private float[][] adj;
public int getvCount() {
return vCount;
}
public float[][] getAdj() {
return adj;
}
public Graph(int vCount) {
this.vCount = vCount;
adj = new float[vCount][vCount];
for (int i = 0; i < vCount; i++) {
for (int j = 0; j < vCount; j++) {
if (i != j) {
adj[i][j] = Float.MAX_VALUE;
}
}
}
}
public void addEdge(int i, int j, float weight) {
adj[i][j] = weight;
}
public void removeEdge(int i, int j) {
adj[i][j] = Float.MAX_VALUE;
}
public boolean hasEdge(int i, int j) {
if (adj[i][j] != Float.MAX_VALUE && adj[i][j] != 0) {
return true;
}
return false;
}
public List<Integer> neighbours(int vertex) {
List<Integer> edges = new ArrayList<Integer>();
for (int i = 0; i < vCount; i++)
if (hasEdge(vertex, i))
edges.add(i);
return edges;
}
}
// initialize with adjacency matrix weight values
// set all to be initially infinity
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
dist[i][j] = Float.MAX_VALUE;
}
}
// set all edge weights
for(int k = 0; k <= g.edgeList.size(); k++){
Edge e = g.edgeList.get(k);
int i = Integer.parseInt(e.getHead().label);
int j = Integer.parseInt(e.getTail().label);
dist[i][j] = Integer.parseInt(e.getLabel());
}