图的表示:给定图数据文件(
tinyG.txt),计算得到图的邻接矩阵,并把邻接矩阵保存到文件(tinyG_matrix.txt)中。类名:
GraphRepresentation。摘自《
Algorithms, 4th Edition》P
522。
package graph;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class GraphRepresentation {
public static void main(String[] args) {
try { FileReader r=new FileReader("F:/Arithmetic/tinyG.txt");
BufferedReader o=new BufferedReader(r);
int tc;
String str="";
str=o.readLine();
int a=Integer.parseInt(str);
str=o.readLine();
int E=Integer.parseInt(str);
int[][] arry=new int[a][a];
str="";
int v=0,e=0;
while((tc=o.read())!=-1)
{ str=str+(char)tc;
if ((char)tc==' ')
{ v=Integer.parseInt(str.trim());
str="";
}
if((char)tc=='\n')
{ e=Integer.parseInt(str.trim());
arry[v][e]=1;
arry[e][v]=1;
str="";
}
}
o.close();
r.close();
FileOutputStream f=new FileOutputStream("F:/Arithmetic/tinyG_matrix.txt");
OutputStreamWriter O=new OutputStreamWriter(f);
BufferedWriter w=new BufferedWriter(O);
str="";
System.out.println("图的邻接矩阵为 :");
for(int i=0;i
