圖的最短路徑和拓撲排序

當前比較行:0余素,shortTablePath結果:[0, 1, 5, 1000, 1000, 1000, 1000, 1000, 1000]
當前比較行:1,shortTablePath結果:[0, 1, 4, 8, 6, 1000, 1000, 1000, 1000]
當前比較行:2键科,shortTablePath結果:[0, 1, 4, 8, 5, 11, 1000, 1000, 1000]
當前比較行:4域携,shortTablePath結果:[0, 1, 4, 7, 5, 8, 11, 14, 1000]
當前比較行:3蛋叼,shortTablePath結果:[0, 1, 4, 7, 5, 8, 10, 14, 1000]
當前比較行:5堕绩,shortTablePath結果:[0, 1, 4, 7, 5, 8, 10, 13, 1000]
當前比較行:6,shortTablePath結果:[0, 1, 4, 7, 5, 8, 10, 12, 17]
當前比較行:7肺缕,shortTablePath結果:[0, 1, 4, 7, 5, 8, 10, 12, 16]
當前比較行:8左医,shortTablePath結果:[0, 1, 4, 7, 5, 8, 10, 12, 16]

拓撲排序

2、圖的最短路徑


public class DnjavaDijstra {
    private final static int MAXVEX = 9;
    private final static int MAXWEIGHT = 65535;
    private int shortTablePath[] = new int[MAXVEX];// 記錄的是v0到某頂點的最短路徑和

     /**
     * 獲取一個圖的最短路徑
     * 首先在第一行找出一個最小的值(下標1)同木,表示v0到v1(k=1)的最短路徑浮梢。
     * 其次在遍歷第k行數據,滿足沒有被標記并且graph[k][j]+min<shortTablePath[j],表示v0到vj存在更小的路徑彤路。
     * 重復以上步驟秕硝,循環(huán)頂點個數減1次。
     */
    public void shortestPathDijstra(Graph graph) {
        int min;//最小值且沒確定是最短路徑
        int k = 0;// 記錄下標
        //與shortTablePath對應洲尊,表示每個節(jié)點最短路徑有沒有被確定
        boolean isgetPath[] = new boolean[MAXVEX];
        //獲取v0這一行的權值數組
        for (int v = 0; v < graph.getVertexSize(); v++) {
            shortTablePath[v] = graph.getMatrix()[0][v];// 
        }
        shortTablePath[0] = 0;
        isgetPath[0] = true;
        System.out.println("當前比較行:0远豺,shortTablePath結果:"+Arrays.toString(shortTablePath));
        //遍歷行,除過第0行(數組)遍歷剩余graph.getVertexSize()-1行,不是按照順序遍歷坞嘀,跟k有關
        for (int l = 1; l < graph.getVertexSize(); l++) {
            
            min = MAXWEIGHT;
            //在shortTablePath數組中查找最小值躯护,且沒有被標記過   
            for (int w = 0; w < graph.getVertexSize(); w++) {
                if (!isgetPath[w] && shortTablePath[w] < min) {
                    k = w;
                    min = shortTablePath[w];
                }
            }
            isgetPath[k] = true;
            //當前行的每一列j數據與min之和如果小于shortTablePath[j],說明v0到vv
            for (int j = 0; j < graph.getVertexSize(); j++) {
                if(!isgetPath[j]&&(min+graph.getMatrix()[k][j]<shortTablePath[j])){
                    shortTablePath[j] = min + graph.getMatrix()[k][j];
                }
            }
            System.out.println("當前比較行:"+k+",shortTablePath結果:"
+Arrays.toString(shortTablePath));
        }
        for(int i = 0;i<shortTablePath.length;i++){
            System.out.println("V0到V"+i+"的最短路徑為:"+shortTablePath[i]+"\n");
        }
        
    }
    
    public static void main(String[] args){
        Graph graph = new Graph(MAXVEX);
        graph.createGraph();
        DnjavaDijstra dijstra = new DnjavaDijstra();
        dijstra.shortestPathDijstra(graph);
    }
}

3丽涩、圖的拓撲排序

package com.dn.dijstra;

import java.util.Stack;

public class DnGraphTopologic {
    private int numVertexes;
    private VertexNode []  adjList;//鄰接頂點的一維數組
    public DnGraphTopologic(int numVertexes){
        this.numVertexes = numVertexes;
    }
    private void createGraph(){
        VertexNode node0 = new VertexNode(0,"v0");
        VertexNode node1 = new VertexNode(0,"v1");
        VertexNode node2 = new VertexNode(2,"v2");
        VertexNode node3 = new VertexNode(0,"v3");
        VertexNode node4 = new VertexNode(2,"v4");
        VertexNode node5 = new VertexNode(3,"v5");
        VertexNode node6 = new VertexNode(1,"v6");
        VertexNode node7 = new VertexNode(2,"v7");
        VertexNode node8 = new VertexNode(2,"v8");
        VertexNode node9 = new VertexNode(1,"v9");
        VertexNode node10 = new VertexNode(1,"v10");
        VertexNode node11 = new VertexNode(2,"v11");
        VertexNode node12 = new VertexNode(1,"v12");
        VertexNode node13 = new VertexNode(2,"v13");
        adjList = new VertexNode[numVertexes];
        adjList[0] =node0;
        adjList[1] =node1;
        adjList[2] =node2;
        adjList[3] =node3;
        adjList[4] =node4;
        adjList[5] =node5;
        adjList[6] =node6;
        adjList[7] =node7;
        adjList[8] =node8;
        adjList[9] =node9;
        adjList[10] =node10;
        adjList[11] =node11;
        adjList[12] =node12;
        adjList[13] =node13;
        node0.firstEdge = new EdgeNode(11);node0.firstEdge.next = new EdgeNode(5);node0.firstEdge.next.next = new EdgeNode(4);
        node1.firstEdge = new EdgeNode(8);node1.firstEdge.next = new EdgeNode(4);node1.firstEdge.next.next = new EdgeNode(2);
        node2.firstEdge = new EdgeNode(9);node2.firstEdge.next = new EdgeNode(6);node2.firstEdge.next.next = new EdgeNode(5);
        node3.firstEdge = new EdgeNode(13);node3.firstEdge.next = new EdgeNode(2);
        node4.firstEdge = new EdgeNode(7);
        node5.firstEdge = new EdgeNode(12);node5.firstEdge.next = new EdgeNode(8);
        node6.firstEdge = new EdgeNode(5);
        node8.firstEdge = new EdgeNode(7);
        node9.firstEdge = new EdgeNode(11);node9.firstEdge.next = new EdgeNode(10);
        node10.firstEdge = new EdgeNode(13);
        node12.firstEdge = new EdgeNode(9);
    }
    /**
     * 拓撲排序
     * @author Administrator
     * @throws Exception 
     *
     */
    private void topologicalSort() throws Exception{
        Stack<Integer> stack = new Stack<>();
        int count = 0;
        int k = 0;
        for(int i = 0;i<numVertexes;i++ ){
            if(adjList[i].in == 0){
                stack.push(i);
            }
        }
        while(!stack.isEmpty()){
            int pop = stack.pop();
            System.out.println("頂點:"+adjList[pop].data);
            count++;
            for(EdgeNode node = adjList[pop].firstEdge;node!=null;node = node.next){
                k = node.adjVert;//下標
                if(--adjList[k].in == 0){
                    stack.push(k);//入度為0,入棧
                }
            }
        }
        if(count<numVertexes){
            throw new Exception("完犢子了棺滞,拓撲排序失敗");
        }
    }
    
    //邊表頂點
    class EdgeNode{
        private int adjVert;
        private EdgeNode next;
        private int weight;
        public EdgeNode(int adjVert){
            this.adjVert = adjVert;
        }
        public int getAdjVert() {
            return adjVert;
        }
        public void setAdjVert(int adjVert) {
            this.adjVert = adjVert;
        }
        public EdgeNode getNext() {
            return next;
        }
        public void setNext(EdgeNode next) {
            this.next = next;
        }
        public int getWeight() {
            return weight;
        }
        public void setWeight(int weight) {
            this.weight = weight;
        }
        
    }
    
    //鄰接頂點
    class VertexNode{
        private int in;//入度
        private String data;
        private EdgeNode firstEdge;
        
        public VertexNode(int in,String data){
            this.in = in;
            this.data = data;
        }

        public int getIn() {
            return in;
        }

        public void setIn(int in) {
            this.in = in;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public EdgeNode getFirstEdge() {
            return firstEdge;
        }

        public void setFirstEdge(EdgeNode firstEdge) {
            this.firstEdge = firstEdge;
        }
        
    }
    
    public static void main(String [] args){
        DnGraphTopologic dnGraphTopologic = new DnGraphTopologic(14);
        dnGraphTopologic.createGraph();
        try {
            dnGraphTopologic.topologicalSort();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市矢渊,隨后出現的幾起案子检眯,更是在濱河造成了極大的恐慌,老刑警劉巖昆淡,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異刽严,居然都是意外死亡昂灵,警方通過查閱死者的電腦和手機避凝,發(fā)現死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來眨补,“玉大人管削,你說我怎么就攤上這事〕怕荩” “怎么了含思?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長甘晤。 經常有香客問我含潘,道長,這世上最難降的妖魔是什么线婚? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任遏弱,我火速辦了婚禮,結果婚禮上塞弊,老公的妹妹穿的比我還像新娘漱逸。我一直安慰自己,他們只是感情好游沿,可當我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布饰抒。 她就那樣靜靜地躺著,像睡著了一般诀黍。 火紅的嫁衣襯著肌膚如雪袋坑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天蔗草,我揣著相機與錄音咒彤,去河邊找鬼。 笑死咒精,一個胖子當著我的面吹牛镶柱,可吹牛的內容都是我干的。 我是一名探鬼主播模叙,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼歇拆,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了范咨?” 一聲冷哼從身側響起故觅,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎渠啊,沒想到半個月后输吏,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡替蛉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年贯溅,在試婚紗的時候發(fā)現自己被綠了拄氯。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡它浅,死狀恐怖译柏,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情姐霍,我是刑警寧澤鄙麦,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站镊折,受9級特大地震影響胯府,放射性物質發(fā)生泄漏。R本人自食惡果不足惜腌乡,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一盟劫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧与纽,春花似錦侣签、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至僚碎,卻和暖如春猴娩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背勺阐。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工卷中, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人渊抽。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓蟆豫,卻偏偏與公主長得像,于是被迫代替她去往敵國和親懒闷。 傳聞我的和親對象是個殘疾皇子十减,可洞房花燭夜當晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內容