- package bbb;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- public class GraphRepresentation {
- /**
- * 计算得到图的邻接矩阵,并把邻接矩阵保存到文件
- */
- static int arc[][];
- static int vertextNum;
- static int arcNum;
- static int vartex[];
- public static void main(String[] args) {
- String path1="src/tinyG.txt";
- String path2="src/tinyGResult.txt";
- try {
- FileReader read=new FileReader(new File(path1));
- BufferedReader reader=new BufferedReader(read);
- PrintWriter out=new PrintWriter(new File(path2));
- String line="";
- int index=1;
- while((line=reader.readLine())!=null){
- if(index==1){
- vertextNum=Integer.parseInt(line.trim());
- index++;
- continue;
- }else if(index==2){
- arcNum=Integer.parseInt(line.trim());
- index++;
- continue;
- } else if(index==3){
- arc=new int[vertextNum][arcNum];
- index++;
- continue;
- }
- addEadge(line);
- index++;
- }
- for(int i=0;i<vertextNum;i++){
- for(int j=0;j<vertextNum;j++){
- out.print(arc[i][j]+"\t");
- System.out.print(arc[i][j]+"\t");
- }
- out.flush();
- out.println();
- System.out.println();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void addEadge(String line){
- String []result=line.trim().split(" ");
- int v1=Integer.parseInt(result[0]);
- int v2=Integer.parseInt(result[1]);
- for(int i=0;i<arcNum;i++){
- arc[v1][v2]=1;
- arc[v2][v1]=1;
- }
- }
- }