首页 > 其他 > 详细

图最短路径之BellmanFord

时间:2021-08-30 12:10:55      阅读:9      评论:0      收藏:0      [点我收藏+]

定义

贝尔曼-福特算法,可以从给定一个图和图中的源顶点src,找到从src到给定图中所有顶点的最短路径。该图可能包含负权重边。相对于Dijkstra算法的优势是可以处理负权重边,缺点则是复杂度高于Dijkstra 。具体算法的详细解析请参考https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/,以下代码也是参考https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/,只是根据自己的需要增加了一些东西。

package graph.bellman_ford;
?
import lombok.Data;
?
public class Graph {
   private final int vertexCount;
   private final int edgeCount;
   private final Edge[] edge;
?
   public Graph(int vertexCount, int edgeCount, Edge[] edge) {
       this.vertexCount = vertexCount;
       this.edgeCount = edgeCount;
       this.edge = edge;
  }
?
   @Data
   public static class Edge {
       Vertex source;
       Vertex destination;
       int weight;
  }
?
   @Data
   public static class Vertex {
       int sequence;
       String code;
       String name;
  }
?
?
   public void bellmanFord(Graph graph, int src) {
       int[] distance = new int[vertexCount];
       
       for (int i = 0; i < vertexCount; ++i) {
           distance[i] = Integer.MAX_VALUE;
      }
       distance[src] = 0;
?
 
       for (int i = 1; i < vertexCount; ++i) {
           for (int j = 0; j < edgeCount; ++j) {
               int u = graph.edge[j].source.sequence;
               int v = graph.edge[j].destination.sequence;
               int weight = graph.edge[j].weight;
               if (distance[u] != Integer.MAX_VALUE && distance[u] + weight < distance[v]) {
                   distance[v] = distance[u] + weight;
              }
          }
      }
?
   
       for (int j = 0; j < edgeCount; ++j) {
           int u = graph.edge[j].source.sequence;
           int v = graph.edge[j].destination.sequence;
           int weight = graph.edge[j].weight;
           if (distance[u] != Integer.MAX_VALUE && distance[u] + weight < distance[v]) {
               return;
          }
      }
       printArr(distance, vertexCount);
  }
?
   public void printArr(int[] distance, int vertexCount) {
       for (int i = 0; i < vertexCount; ++i) {
           System.out.println(i + "\t\t" + distance[i]);
      }
  }
}

测试

package graph.bellman_ford;
?
import java.util.ArrayList;
import java.util.List;
?
public class ShortestPathOfBellmanFord {
?
  public static void main(String[] args) {
?
?
      List<Graph.Vertex> vertexList = new ArrayList<>();
      for (int i = 0; i < 5; i++) {
          Graph.Vertex vertex = new Graph.Vertex();
          vertex.code = "code" + i;
          vertex.name = "name" + i;
          vertex.sequence = i;
          vertexList.add(vertex);
      }
      Graph.Edge[] edges = new Graph.Edge[8];
      for (int i = 0; i < edges.length; i++) {
          edges[i] = new Graph.Edge();
      }
?
      // edge 0 --> 1
      edges[0].source = vertexList.get(0);
      edges[0].destination = vertexList.get(1);
      edges[0].weight = -1;
?
?
      // edge 0 --> 2
      edges[1].source = vertexList.get(0);
      edges[1].destination = vertexList.get(2);
      edges[1].weight = 4;
?
      // edge 1 --> 2
      edges[2].source = vertexList.get(1);
      edges[2].destination = vertexList.get(2);
      edges[2].weight = 3;
?
      // edge 1 --> 3
      edges[3].source = vertexList.get(1);
      edges[3].destination = vertexList.get(3);
      edges[3].weight = 2;
?
      // edge 1 --> 4
      edges[4].source = vertexList.get(1);
      edges[4].destination = vertexList.get(4);
      edges[4].weight = 2;
?
      // edge 3 --> 2
      edges[5].source = vertexList.get(3);
      edges[5].destination = vertexList.get(2);
      edges[5].weight = 5;
?
      // edge 3 --> 1
      edges[6].source = vertexList.get(3);
      edges[6].destination = vertexList.get(1);
      edges[6].weight = 1;
?
      // edge 4--> 3
      edges[7].source = vertexList.get(4);
      edges[7].destination = vertexList.get(3);
      edges[7].weight = -3;
?
?
      Graph graph = new Graph(5, 8, edges);
      graph.bellmanFord(graph, 0);
  }
}

 

图最短路径之BellmanFord

原文:https://www.cnblogs.com/wkynf/p/15202669.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!