学习来源:日撸 Java 三百行(31-40天,图)
第 31 天: 整数矩阵及其运算输入:输入多个整数构造整数矩阵tempMatrix1
输出:输出tempMatrix1、tempMatrix1自乘运算得到的新矩阵tempMatrix2、tempMatrix2的单位矩阵tempMatrix3再加上tempMatrix1所得到的矩阵
优化目标:无
package my_java;
import java.util.Arrays;
public class IntMatrix {
int[][] data;
public IntMatrix(int paraRows, int paraColumns) {
data = new int[paraRows][paraColumns];
}// Of the first constructor
public IntMatrix(int[][] paraMatrix) {
data = new int[paraMatrix.length][paraMatrix[0].length];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = paraMatrix[i][j];
} // Of for j
} // Of for i
}// Of the second constructor
public IntMatrix(IntMatrix paraMatrix) {
this(paraMatrix.getData());//用 this调用其它的构造方法以减少冗余代码
}// Of the third constructor
public static IntMatrix getIdentityMatrix(int paraRows) {
IntMatrix resultMatrix = new IntMatrix(paraRows, paraRows);
for (int i = 0; i < paraRows; i++) {
resultMatrix.data[i][i] = 1;
} // Of for i
return resultMatrix;
}// Of getIdentityMatrix
public String toString() {
return Arrays.deepToString(data);
}// Of toString
public int[][] getData() {
return data;
}// Of getData
public int getRows() {
return data.length;
}// Of getRows
public int getColumns() {
return data[0].length;
}// Of getColumns
public void setValue(int paraRow, int paraColumn, int paraValue) {
data[paraRow][paraColumn] = paraValue;
}// Of setValue
public int getValue(int paraRow, int paraColumn) {
return data[paraRow][paraColumn];
}// Of getValue
public void add(IntMatrix paraMatrix) throws Exception {
int[][] tempData = paraMatrix.getData();
if (data.length != tempData.length) {
throw new Exception("Cannot add matrices. Rows not match: " + data.length + " vs. "
+ tempData.length + ".");
} // Of if
if (data[0].length != tempData[0].length) {
throw new Exception("Cannot add matrices. Rows not match: " + data[0].length + " vs. "
+ tempData[0].length + ".");
} // Of if
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] += tempData[i][j];
} // Of for j
} // Of for i
}// Of add
public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
IntMatrix resultMatrix = new IntMatrix(paraMatrix1);
resultMatrix.add(paraMatrix2);
return resultMatrix;
}// Of add
public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2)throws Exception {
int[][] tempData1 = paraMatrix1.getData();
int[][] tempData2 = paraMatrix2.getData();
if (tempData1[0].length != tempData2.length) {
throw new Exception("Cannot multiply matrices: " + tempData1[0].length + " vs. "+ tempData2.length + ".");
} // Of if
int[][] resultData = new int[tempData1.length][tempData2[0].length];
for (int i = 0; i < tempData1.length; i++) {
for (int j = 0; j < tempData2[0].length; j++) {
for (int k = 0; k < tempData1[0].length; k++) {
resultData[i][j] += tempData1[i][k] * tempData2[k][j];
} // Of for k
} // Of for j
} // Of for i
IntMatrix resultMatrix = new IntMatrix(resultData);
return resultMatrix;
}// Of multiply
public static void main(String args[]) {
IntMatrix tempMatrix1 = new IntMatrix(3, 3);
tempMatrix1.setValue(0, 1, 1);
tempMatrix1.setValue(1, 0, 1);
tempMatrix1.setValue(1, 2, 1);
tempMatrix1.setValue(2, 1, 1);
System.out.println("The original matrix is: " + tempMatrix1);
IntMatrix tempMatrix2 = null;
try {
tempMatrix2 = IntMatrix.multiply(tempMatrix1, tempMatrix1);
} catch (Exception ee) {
System.out.println(ee);
} // Of try
System.out.println("The square matrix is: " + tempMatrix2);
IntMatrix tempMatrix3 = new IntMatrix(tempMatrix2);
try {
tempMatrix3.add(tempMatrix1);
} catch (Exception ee) {
System.out.println(ee);
} // Of try
System.out.println("The connectivity matrix is: " + tempMatrix3);
}// Of main
}// Of class IntMatrix
运行截图:
输入:输入整型二维矩阵构造图
输出:输出tempConnected判断图是否连通
优化目标:无
package my_java;
import my_java.IntMatrix;
public class Graph {
IntMatrix connectivityMatrix;
public Graph(int paraNumNodes) {
connectivityMatrix = new IntMatrix(paraNumNodes, paraNumNodes);
}// Of the first constructor
public Graph(int[][] paraMatrix) {
connectivityMatrix = new IntMatrix(paraMatrix);
}// Of the second constructor
public String toString() {
String resultString = "This is the connectivity matrix of the graph.rn"+ connectivityMatrix;
return resultString;
}// Of toString
public boolean getConnectivity() throws Exception {
IntMatrix tempConnectivityMatrix = IntMatrix.getIdentityMatrix(connectivityMatrix.getData().length);
IntMatrix tempMultipliedMatrix = new IntMatrix(connectivityMatrix);
for (int i = 0; i < connectivityMatrix.getData().length - 1; i++) {
tempConnectivityMatrix.add(tempMultipliedMatrix);
tempMultipliedMatrix = IntMatrix.multiply(tempMultipliedMatrix, connectivityMatrix);
} // Of for i
System.out.println("The connectivity matrix is: " + tempConnectivityMatrix);
int[][] tempData = tempConnectivityMatrix.getData();
for (int i = 0; i < tempData.length; i++) {
for (int j = 0; j < tempData.length; j++) {
if (tempData[i][j] == 0) {
System.out.println("Node " + i + " cannot reach " + j);
return false;
} // Of if
} // Of for j
} // Of for i
return true;
}// Of getConnectivity
public static void getConnectivityTest() {
int[][] tempMatrix = { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 } };
Graph tempGraph2 = new Graph(tempMatrix);
System.out.println(tempGraph2);
boolean tempConnected = false;
try {
tempConnected = tempGraph2.getConnectivity();
} catch (Exception ee) {
System.out.println(ee);
} // Of try.
System.out.println("Is the graph connected? " + tempConnected);
tempGraph2.connectivityMatrix.setValue(1, 0, 0);
tempConnected = false;
try {
tempConnected = tempGraph2.getConnectivity();
} catch (Exception ee) {
System.out.println(ee);
} // Of try.
System.out.println("Is the graph connected? " + tempConnected);
}// Of getConnectivityTest
public static void main(String args[]) {
System.out.println("Hello!");
Graph tempGraph = new Graph(3);
System.out.println(tempGraph);
getConnectivityTest();
}// Of main
}// Of class Graph
运行截图:



