单源最短路径:Bellman-Ford-算法及改进后的SPFA算法

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));
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值