import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class GraphRepresentation {
/**
* 计算得到图的邻接矩阵,并把邻接矩阵保存到文件
*/
static int arc[][];
static int vertexNum;
static int arcNum;
static int vartex[];
public static void main(String[] args) {
String Filein="D:/tinyG.txt";
String Fileout="D:/Result.txt";
try{
FileReader fr=new FileReader(new File(Filein));
BufferedReader br=new BufferedReader(fr);
PrintWriter pw=new PrintWriter(new File(Fileout));
String content="";
int i=1;
while((content=br.readLine())!=null){
if(i==1){
vertexNum=Integer.parseInt(content.trim());
i++;
continue;
}else if(i==2){
arcNum=Integer.parseInt(content.trim());
i++;
continue;
}else if(i==3){
arc=new int[vertexNum][arcNum];
i++;
continue;
}
addEdge(content);
i++;
}
for(int j=0;j<vertexNum;j++){
for(int k=0;k<vertexNum;k++){
pw.print(arc[j][k]+"\t");
System.out.print(arc[j][k]+"\t");
}
pw.flush();
pw.println();
System.out.println();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void addEdge(String content) {
String result[]=content.trim().split(" ");
int x=Integer.parseInt(result[0]);
int y=Integer.parseInt(result[1]);
for(int i=0;i<arcNum;i++){
arc[x][y]=1;
arc[y][x]=1;
}
}
}
算法第四周作业
最新推荐文章于 2019-05-04 17:00:34 发布