數據結構——圖的鄰接矩陣創建(java版本)


鄰接矩陣的概念:

所謂鄰接矩陣,就是用兩個數組來表示圖的相關信息,其中用一個一維的頂點數組來表示圖的頂點信息,用一個二維的邊數組來表示圖的邊或者弧信息。 

如下圖是一個無向圖的鄰接矩陣表示,兩個頂點之間若聯通則二維數組對應位置為1,否則為0。

 下圖是一個有向圖的鄰接矩陣表示。

 下圖是一個帶權值的有向圖(又稱為有向網)的鄰接矩陣表示,兩個頂點之間若連通則二維數組      對應位置為邊上的權值,否則為無窮大,並且二維矩陣的對角線為0(當然也可以設置為無窮大)

 代碼部分:

 該代碼用一個類去實現鄰接矩陣,其中類中含有一個一維頂點數組和一個二維邊數組字段,並且   包含無向圖、有向圖、帶權值的無向圖(無向網)、帶權值的有向圖(有向網)的創建方法

public class MGraph01 {
    public int numNodes;      //圖的頂點數目
    public int numEdges;      //圖的邊數
    public Object[] vexs;     //一維頂點數組
    public int[][] arcs;      //二維邊數組
    public static final int INF = Integer.MAX_VALUE; //無窮大

    /**
     * 創建無向圖的鄰接矩陣
     */
    public void createUDG() {
        Scanner sc = null;
        try {
            sc = new Scanner(System.in);
            System.out.println("請輸入圖的頂點數、圖的邊數:");
            numNodes = Integer.parseInt(sc.nextLine());
            numEdges = Integer.parseInt(sc.nextLine());
            vexs = new Object[numNodes];
            //錄入頂點信息
            System.out.println("請輸入圖中的頂點:");
            vexs = sc.nextLine().split(" ");
            //錄入邊的信息
            arcs = new int[numNodes][numNodes];
            for (int i = 0; i < numEdges; i++) {
                System.out.println("請輸入第" + (i + 1) + "條邊上的一個頂點:");
                //locate方法用來定位某個頂點在數組中的索引
                int index1 = locate(sc.nextLine());
                System.out.println("請輸入第" + (i + 1) + "條邊上的另一個頂點:");
                int index2 = locate(sc.nextLine());
                //無向圖是個對稱矩陣
                arcs[index1][index2] = arcs[index2][index1] = 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sc.close();
        }
    }

    /**
     * 創建有向圖的鄰接矩陣
     */
    public void createDG() {
        Scanner sc = null;
        try {
            sc = new Scanner(System.in);
            System.out.println("請輸入圖的頂點數、圖的邊數:");
            numNodes = Integer.parseInt(sc.nextLine());
            numEdges = Integer.parseInt(sc.nextLine());
            vexs = new Object[numNodes];
            //錄入頂點信息
            System.out.println("請輸入圖中的頂點:");
            String str = sc.nextLine();
            vexs = str.split(" ");
            //錄入邊的信息
            arcs = new int[numNodes][numNodes];
            for (int i = 0; i < numEdges; i++) {
                System.out.println("請輸入第" + (i + 1) + "條弧上的弧尾:");
                int index1 = locate(sc.nextLine());
                System.out.println("請輸入第" + (i + 1) + "條弧上的弧頭:");
                int index2 = locate(sc.nextLine());
                arcs[index1][index2] = 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sc.close();
        }
    }

    /**
     * 創建無向網的鄰接矩陣
     */
    public void createUDN() {
        Scanner sc = null;
        try {
            sc = new Scanner(System.in);
            System.out.println("請輸入圖的頂點數、圖的邊數:");
            numNodes = Integer.parseInt(sc.nextLine());
            numEdges = Integer.parseInt(sc.nextLine());
            vexs = new Object[numNodes];
            //錄入頂點信息
            System.out.println("請輸入圖中的頂點:");
            String str = sc.nextLine();
            vexs = str.split(" ");
            //矩陣初始化,有向網中
            arcs = new int[numNodes][numNodes];
            for (int i = 0; i < numNodes; i++) {
                for (int j = 0; j < numNodes; j++) {
                    arcs[i][j] = INF;
                }
            }
            //錄入邊的信息
            for (int i = 0; i < numEdges; i++) {
                System.out.println("請輸入第" + (i + 1) + "條邊上的一個頂點:");
                int index1 = locate(sc.nextLine());
                System.out.println("請輸入第" + (i + 1) + "條邊上的另一個頂點:");
                int index2 = locate(sc.nextLine());
                System.out.println("請輸入第" + (i + 1) + "條邊上的權值:");
                arcs[index1][index2] = arcs[index2][index1] = Integer.parseInt(sc.nextLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sc.close();
        }
    }

    /**
     * 創建有向網的鄰接矩陣
     */
    public void createDN() {
        Scanner sc = null;
        try {
            sc = new Scanner(System.in);
            System.out.println("請輸入圖的頂點數、圖的邊數:");
            numNodes = Integer.parseInt(sc.nextLine());
            numEdges = Integer.parseInt(sc.nextLine());
            vexs = new Object[numNodes];
            //錄入頂點信息
            System.out.println("請輸入圖中的頂點:");
            String str = sc.nextLine();
            vexs = str.split(" ");
            //錄入邊的信息
            arcs = new int[numNodes][numNodes];
            for (int i = 0; i < numEdges; i++) {
                System.out.println("請輸入第" + (i + 1) + "條弧上的弧尾:");
                int index1 = locate(sc.nextLine());
                System.out.println("請輸入第" + (i + 1) + "條弧上的弧頭:");
                int index2 = locate(sc.nextLine());
                System.out.println("請輸入第" + (i + 1) + "條弧上的權值:");
                arcs[index1][index2] = Integer.parseInt(sc.nextLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sc.close();
        }
    }

    /**
     * 通過頂點信息來定位其在頂點數組中的索引
     *
     * @param s
     * @return
     */
    public int locate(Object s) {
        for (int i = 0; i < vexs.length; i++) {
            if (s.equals(vexs[i])) {
                return i;
            }
        }
        return -1;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM