java數據結構----圖的遍歷應用舉例:編程實現判斷一個有向圖中任意給定的兩個頂點之間是否存在一條長度爲k的簡單路徑

數據結構系列是我學習做的筆記,會持續更新,源碼分享在github:數據結構,當然你也可以從下面的代碼片中獲取
注:github代碼更新會有延遲,關注不迷路😄

圖的遍歷應用舉例2

題目:編程實現判斷一個有向圖中任意給定的兩個頂點之間是否存在一條長度爲k的簡單路徑

簡單路徑:指圖G(V,E)中路徑上的頂點都不相同的路徑

其中,涉及到:接口 IGraph 和類 ALGraph 詳見博文圖的存儲結構之鄰接表
當然也可以訪問我的github,獲取完整項目代碼(注:更新有延遲),地址見博文開頭

package code.Graph;
/**
 * 圖的遍歷方法應用舉例2
 *
 * 題目:
 *      編程實現判斷一個有向圖中任意給定的兩個頂點之間是否存在一條長度爲k的簡單路徑
 *
 *      簡單路徑:指圖G(V,E)中路徑上的頂點都不相同的路徑
 * */
public class GraphSearchExample2 {
    private boolean [] visited; //訪問標誌數組

    private int i = 0;  //輔助變量,在遍歷過程中用於記錄從起點出發的路徑長度

    private boolean find = false;   //標誌着是否已經找到了指定長度的路徑

    /**
     * @param G 圖
     * @param v 第一個任意頂點
     * @param u 第二個任意頂點
     * @param k 長度
     * */
    public void findPath(IGraph G, int u, int v, int k)throws Exception{
        visited = new boolean[G.getVexNum()];
        for (int w = 0;w<G.getVexNum();w++){
            //訪問數組初始化
            visited[w] = false;
        }
        findDFS(G,u,v,k);
        if (find){
            System.out.println(G.getVex(u)+"和"+G.getVex(v)+"之間存在一條長度爲"+k+"的簡單路徑");
        }else {
            System.out.println(G.getVex(u)+"和"+G.getVex(v)+"之間不存在一條長度爲"+k+"的簡單路徑");
        }

    }

    /**
     * @param G 圖
     * @param v 第一個任意頂點
     * @param u 第二個任意頂點
     * @param k 長度
     * */
    public void findDFS(IGraph G, int u, int v, int k)throws Exception{
        if (i == k && u == v){
            find = true;
        }else if (!find){   //如果還沒有找到
            visited[u] = true;
            for (int w = G.firstAdjVex(u);w >= 0;w = G.nextAdjVex(u,w)){
                if (!visited[w]){
                    if (i<k){
                        ++i;
                        //對v的尚未訪問的鄰接頂點w遞歸調用findDFS(G,u,v,k);
                        findDFS(G,w,v,k);
                    }else{
                      //若路徑長度已經達到k值而仍未找到簡單路徑,則不再繼續對當前頂點進行深度優先搜索
                        break;
                    }
                }

            }
            --i;//回退一個頂點
        }
    }

    public static ALGraph getGraphSearch_G(){
        /*
         * 構建一個有向圖
         * 其中:
         *
         *       A--->B--->C--->D--->E-->F--->A
         *
         *       F--->B--->E
         *
         * */

        ArcNode ab = new ArcNode(1);
        VNode A = new VNode("A",ab);

        ArcNode bc = new ArcNode(2);
        ArcNode be = new ArcNode(4,0,bc);
        VNode B = new VNode("B",be);

        ArcNode cd = new ArcNode(3);
        VNode C = new VNode("C",cd);

        ArcNode de = new ArcNode(4);
        VNode D = new VNode("D",de);

        ArcNode ef = new ArcNode(5);
        VNode E = new VNode("E",ef);

        ArcNode fa = new ArcNode(0);
        ArcNode fb = new ArcNode(1,0,fa);
        VNode F = new VNode("F",fb);

        VNode [] vexs = {A,B,C,D,E,F};
        return new ALGraph(GraphKind.DG,6,8,vexs);
    }

    public static void main(String [] args)throws Exception{
        GraphSearchExample2 test = new GraphSearchExample2();
        test.findPath(getGraphSearch_G(),0,5,3);

    }

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