https://blog.csdn.net/u011893609/article/details/81232124
package graphModel;
import java.util.Arrays;
public class BellmanFord {
public static int inf = 1000;// 用于申请大数组,防止越界,同时也表示两点之间不连接
public static int N = 6;// 图的节点个数
public static int S = 0;// 源节点
public static int T = N - 1;// 目的节点
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1.初始化图结构
int[][] dag = { { 0, 50, 10, inf, 70, inf }, { inf, 0, 15, inf, 10, inf }, { 20, inf, 0, 15, inf, inf },
{ inf, 20, inf, 0, 35, inf }, { inf, inf, inf, 30, 0, inf }, { inf, inf, inf, 3, inf, 0 } };
Graph graph = new Graph();
graph.init(dag);
Graph.Edge[] edges = graph.getEdges();
int[] head = graph.getHead();
int[] distance = new int[N];
Arrays.fill(distance, inf);
distance[0] = 0;
boolean isChange = true;
while (isChange) {
isChange = false;
for (int i = 0; head[i] != -1; i++) {
for (int j = head[i]; j != -1; j = edges[j].next) {
if (distance[edges[j].to] > distance[i] + edges[j].w) {
distance[edges[j].to] = distance[i] + edges[j].w;
isChange = true;
}
}
}
}
System.out.println(Arrays.toString(distance));
}
}
加队列进行优化
package graphModel;
import java.util.Arrays;
import java.util.LinkedList;
public class SPFA {
public static int inf = 1000;//用于申请大数组,防止越界,同时也表示两点之间不连接
public static int N = 6;//图的节点个数
public static int S = 0;//源节点
public static int T = N - 1;//目的节点
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1.初始化图结构
int[][] dag= { { 0, 50, 10, inf, 70, inf }, { inf, 0, 15, inf, 10, inf }, { 20, inf, 0, 15, inf, inf },
{ inf, 20, inf, 0, 35, inf }, { inf, inf, inf, 30, 0, inf }, { inf, inf, inf, 3, inf, 0 } };
Graph graph = new Graph();
graph.init(dag);
Graph.Edge[] edges = graph.getEdges();
int[] head = graph.getHead();
int[] distance = new int[N];
Arrays.fill(distance, inf);
distance[0] = 0;
LinkedList<Integer> queue = new LinkedList<>();
queue.add(S);
while(!queue.isEmpty()) {
int u = queue.poll();
for (int j = head[u]; j != -1; j = edges[j].next) {
if(distance[edges[j].to] > distance[u]+edges[j].w) {
distance[edges[j].to] = distance[u]+edges[j].w;
queue.add(edges[j].to);
}
}
}
System.out.println(Arrays.toString(distance));
}
}
缺点:
package graphModel;
import java.util.Arrays;
import java.util.LinkedList;
public class SPFA {
public static int inf = 1000;//用于申请大数组,防止越界,同时也表示两点之间不连接
public static int N = 6;//图的节点个数
public static int S = 0;//源节点
public static int T = N - 1;//目的节点
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1.初始化图结构
int[][] dag = { { 0, 50, 10, inf, 70, inf }, { inf, 0, 15, inf, 10, inf }, { 20, inf, 0, 15, inf, inf },
{ inf, 20, inf, 0, 35, inf }, { inf, inf, inf, 30, 0, inf }, { inf, inf, inf, 3, inf, 0 } };
Graph graph = new Graph();
graph.init(dag);
Graph.Edge[] edges = graph.getEdges();
int[] head = graph.getHead();
int[] distance = new int[N];
Arrays.fill(distance, inf);
distance[0] = 0;
LinkedList<Integer> queue = new LinkedList<>();
queue.add(S);
visit[S]=true;
while(!queue.isEmpty()) {
int u = queue.poll();
visit[u]=false;
for (int j = head[u]; j != -1; j = edges[j].next) {
if(distance[edges[j].to] > distance[u]+edges[j].w ) {
distance[edges[j].to] = distance[u]+edges[j].w;
if(visit[edges[j].to]==false) {
queue.add(edges[j].to);
visit[edges[j].to]=true;
}
}
}
}
System.out.println(Arrays.toString(distance));
}
}