java constructed adjacency matrix

Java constructed using the adjacency matrix

"code show as below"

FileUtil,java

package com.gongjiao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 用来读取文件和保存文件
 * @author Administrator
 *
 */
public class FileUtil {

	// //////读取文件
	public static ArrayList<String> getFile(String filename) {

		BufferedReader br = null;

		ArrayList<String> list = new ArrayList<String>();

		try {

			br = new BufferedReader(new InputStreamReader(new FileInputStream(
					new File(filename)), "GBK"));

			String str = br.readLine();

			while (str != null) {

				list.add(str);

				str = br.readLine();
			}

			br.close();

		} catch (Exception e) {

			e.printStackTrace();
		}
		return list;
	}

	// ////保存文件
	public static void savefile(List<String> list, String filename) {

		BufferedWriter bout = null;
		try {
			bout = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(filename), "GBK"));
			for (String str : list) {
				if (str != null && !str.equals("")) {
					bout.write(str);
					bout.newLine();
				}
			}
			bout.flush();
			bout.close();

		} catch (Exception e) {

			e.printStackTrace();
		}

	}

}

AdjacencyMatrix.java

package com.gongjiao;

import java.util.ArrayList;

/**
 * 利用节点和边来构造邻接矩阵
 * @author Administrator
 *
 */
public class AdjacencyMatrix
{  
	public static void main(String[] args)
	{
		ArrayList<String> strList = new ArrayList<String>();
		
		//边
		ArrayList<String> xianlu = FileUtil.getFile("edge.txt");
		
		//节点
		ArrayList<String> zhandian = FileUtil.getFile("node.txt");
		int[][] matrix = new int[zhandian.size()][zhandian.size()];

		for (int i = 0; i < zhandian.size(); i++)
		{
			for (int j = 0; j < zhandian.size(); j++)
			{
				matrix[i][j] = 0;
			}
		}
		
		for (int i = 0; i < zhandian.size(); i++)
		{
			for (int j = 0; j < zhandian.size(); j++)
			{
				for (int k = 0; k < xianlu.size(); k++)
				{

					if (xianlu.get(k).contains(zhandian.get(i))
							&& xianlu.get(k).contains(zhandian.get(j))& i != j)
					{

						matrix[i][j] = 1;
						matrix[j][i] = 1;
					}
				}
			}
		}
		
		for (int i = 0; i < zhandian.size(); i++)
		{
			StringBuffer sb = new StringBuffer();
		    
			if( i == 0 ){
				
				sb.append("[[");
			
			}else {
				
				sb.append("[");
			}

			for (int j = 0; j < zhandian.size(); j++)
			{
				sb.append(matrix[i][j] + ",");
			}
			
			String str = sb.toString().substring(0,sb.toString().length() - 1);
			
			if( i == zhandian.size() - 1){
				
				str = str + "]]";
			
			}else{
				
				str = str + "],";
			}
			strList.add(str);

		}
		
		FileUtil.savefile(strList,"result.txt");
		
		System.out.println("成功!!");

	}
}

edge.txt follows

1 3 5
2 3 4
1 2 3
2
3 4

node.txt follows

1
2
3
4
5

Program results are as follows

[[0,1,1,0,1],
[1,0,1,1,0],
[1,1,0,1,1],
[0,1,1,0,0],
[1,0,1,0,0]]

Published 28 original articles · won praise 2 · Views 1369

Guess you like

Origin blog.csdn.net/qq_31960623/article/details/103773965