鄰接矩陣有向圖(三)之 Java詳解
阿新 • • 發佈:2018-12-30
/* * 建立圖(自己輸入資料) */ public MatrixDG() { // 輸入"頂點數"和"邊數" System.out.printf("input vertex number: "); int vlen = readInt(); System.out.printf("input edge number: "); int elen = readInt(); if ( vlen < 1 || elen < 1 || (elen > (vlen*(vlen - 1)))) { System.out.printf("input error: invalid parameters!\n"); return ; } // 初始化"頂點" mVexs = new char[vlen]; for (int i = 0; i < mVexs.length; i++) { System.out.printf("vertex(%d): ", i); mVexs[i] = readChar(); } // 初始化"邊" mMatrix = new int[vlen][vlen]; for (int i = 0; i < elen; i++) { // 讀取邊的起始頂點和結束頂點 System.out.printf("edge(%d):", i); char c1 = readChar(); char c2 = readChar(); int p1 = getPosition(c1); int p2 = getPosition(c2); if (p1==-1 || p2==-1) { System.out.printf("input error: invalid edge!\n"); return ; } mMatrix[p1][p2] = 1; } }