1.图的连通性检测
在这个连通性检测在中,大致原理是这样的:令图的连通矩阵为M,为单位矩阵,只需计算,其中n是节点个数,中第行第列元素为0的话,表示从节点到节点不可达。
该方法,参考离散数学的图论基础,还在学习之中。
package graph;
import tree.IntMatrix;//不小心扔到树里面去了...
/**
*
* @author leeyz_1@163.com
*/
public class graph1 {
IntMatrix connectivityMatrix;
/**
*
* @param paraNumNodes the number of nodes in the graph
*/
public graph1(int paraNumNodes) {
connectivityMatrix = new IntMatrix(paraNumNodes, paraNumNodes);
}//Of the first constructor
/**
*
* @param paraMatrix the number of nodes in the graph
*/
public graph1(int [][] paraMatrix) {
connectivityMatrix = new IntMatrix(paraMatrix);
}// Of the second constructor
public String toString() {
String resultString = "This is the connectivity matrix of the graph.\r\n"+ connectivityMatrix;
return resultString;
}// Of resultString
/**
*
***********************************
* @Title: getConnectivity
* @Description:
* @param: @return
* @param: @throws Exception
* @return: boolean
***********************************
*/
public boolean getConnectivity() throws Exception{
// step 1:Initialize accumulated matrix: M_a = I
IntMatrix tempConnectivityMatrix = IntMatrix.getIdentityMatrix(connectivityMatrix.getData().length);
// step 2:Initialize M^1
IntMatrix tempMultipliedMatrix = new IntMatrix(connectivityMatrix);
//step 3:Determine the actual connectivity
for(int i = 0; i < connectivityMatrix.getData().length-1; i++) {
// M^k
tempMultipliedMatrix = IntMatrix.multiply(tempConnectivityMatrix, connectivityMatrix);
// M_a = M_a + M^k
tempConnectivityMatrix.add(tempMultipliedMatrix);
}// Of for i;
// step 4:Check the connectivity
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
/**
*
***********************************
* @Title: getConnectivityTest
* @Description: Unit test for getConnectivity
* @param:
* @return: void
***********************************
*/
public static void getConnectivityTest() {
// Test an undirected graph.
int[][] tempMatrix = { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 } };
graph1 tempGraph2 = new graph1(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);
// Test a directed graph.
// Remove one arc to form a directed graph.
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
/**
*
***********************************
* @Title: main
* @Description:
* @param: @param args not used now
* @return: void
***********************************
*/
public static void main(String args[]) {
System.out.println("Hello!");
graph1 tempGraph = new graph1(3);
System.out.println(tempGraph);
// Unit test.
getConnectivityTest();
}// Of main
}
2.总结
今天代码部分还是不难吧,主要是其中涉及到的离散数学的一些基础理论知识,需要去学一学。