日撸代码300行学习笔记 Day 32

 1.图的连通性检测

在这个连通性检测在中,大致原理是这样的:令图的连通矩阵为M,M^{_{0}}=I为单位矩阵,只需计算M_{a}=M^{^{1}}+M^{^{2}}+M^{^{3}}+\cdots +M^{^{n-1}},其中n是节点个数,M^{_{a}}中第i行第j列元素为0的话,表示从节点i到节点j不可达。

该方法,参考离散数学的图论基础,还在学习之中。

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.总结

今天代码部分还是不难吧,主要是其中涉及到的离散数学的一些基础理论知识,需要去学一学。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值