最大流(四)----單源最短路徑: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));
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章