- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.StringTokenizer;
- public class GraphRepresentation {
- private static int vNum;
- private static int eNum;
- private static void showMatrix(int matrix[][]) {
- for (int m=0; m<vNum; m++) {
- System.out.print("\t" + m);
- }
- System.out.println("");
- for (int i=0; i<matrix.length; i++) {
- System.out.print(i + "\t");
- for (int j=0; j<matrix[i].length; j++) {
- System.out.print(matrix[i][j] + "\t");
- }
- System.out.println("");
- }
- }
- private static int[][] getData() {
- int[][] matrix = null;
- try {
- BufferedReader reader = new BufferedReader (new FileReader(new File("src/tinyG.txt")));
- vNum = Integer.valueOf(reader.readLine().trim());
- eNum = Integer.valueOf(reader.readLine().trim());
- matrix = new int[vNum][vNum];
- String temp = "";
- while((temp = reader.readLine()) != null) {
- StringTokenizer str = new StringTokenizer(temp);
- int[] points = new int[2];
- int index = 0;
- while (str.hasMoreElements()) {
- points[index] = Integer.valueOf(str.nextToken());
- index++;
- }
- matrix[points[0]][points[1]] = matrix[points[1]][points[0]] = 1;
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (NumberFormatException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return matrix;
- }
- public static void main(String[] args) {
- showMatrix(getData());
- }
- }
第四周的作业
最新推荐文章于 2024-08-03 13:35:06 发布