import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class GraphRepresentation {
public static void main(String[] args) {
try {
int vertexNum = 0, arcNum = 0;
FileReader f = new FileReader("C:/Users/Y470/Desktop/tinyG.txt");
BufferedReader br = new BufferedReader(f);
vertexNum = Integer.parseInt(br.readLine().trim());
arcNum = Integer.parseInt(br.readLine().trim());
int vertex[] = new int[vertexNum];
// 初始化顶点信息
for (int i = 0; i < vertexNum; i++) {
vertex[i] = i;
}
int arc[][] = new int[vertexNum][vertexNum];// 初始化边的信息
for (int i = 0; i < vertexNum; i++) {
for (int j = 0; j < vertexNum; j++) {
arc[i][j] = 0;
}
}
String str = br.readLine().trim();
String textStr[] = new String[2];
while (str != null) {
int pointFirst, pointEnd;// 定义初始顶点,末顶点
if (str.trim().contains(" ")) {
textStr = str.split(" "); // 用空格键分割获取的每一行
pointFirst = Integer.parseInt(textStr[0]);
pointEnd = Integer.parseInt(textStr[1]);
arc[pointFirst][pointEnd] = 1;// 给有连接的两条边赋值
arc[pointEnd][pointFirst] = 1;
}
str = br.readLine();
}
FileWriter w = new FileWriter(
"C:/Users/Y470/Desktop/tinyG_matrix.txt");
BufferedWriter wt = new BufferedWriter(w);
System.out.println("该图的顶点有:" + vertexNum);
System.out.println("该图的边有:" + arcNum);
System.out.println("该图的邻接矩阵:");
for (int i = 0; i < arc.length; i++) {
for (int j = 0; j < arc.length; j++) {
wt.write(arc[i][j] + " ");
wt.newLine();
System.out.print(arc[i][j] + " ");
}
System.out.println();
}
wt.flush();
wt.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class GraphRepresentation {
public static void main(String[] args) {
try {
int vertexNum = 0, arcNum = 0;
FileReader f = new FileReader("C:/Users/Y470/Desktop/tinyG.txt");
BufferedReader br = new BufferedReader(f);
vertexNum = Integer.parseInt(br.readLine().trim());
arcNum = Integer.parseInt(br.readLine().trim());
int vertex[] = new int[vertexNum];
// 初始化顶点信息
for (int i = 0; i < vertexNum; i++) {
vertex[i] = i;
}
int arc[][] = new int[vertexNum][vertexNum];// 初始化边的信息
for (int i = 0; i < vertexNum; i++) {
for (int j = 0; j < vertexNum; j++) {
arc[i][j] = 0;
}
}
String str = br.readLine().trim();
String textStr[] = new String[2];
while (str != null) {
int pointFirst, pointEnd;// 定义初始顶点,末顶点
if (str.trim().contains(" ")) {
textStr = str.split(" "); // 用空格键分割获取的每一行
pointFirst = Integer.parseInt(textStr[0]);
pointEnd = Integer.parseInt(textStr[1]);
arc[pointFirst][pointEnd] = 1;// 给有连接的两条边赋值
arc[pointEnd][pointFirst] = 1;
}
str = br.readLine();
}
FileWriter w = new FileWriter(
"C:/Users/Y470/Desktop/tinyG_matrix.txt");
BufferedWriter wt = new BufferedWriter(w);
System.out.println("该图的顶点有:" + vertexNum);
System.out.println("该图的边有:" + arcNum);
System.out.println("该图的邻接矩阵:");
for (int i = 0; i < arc.length; i++) {
for (int j = 0; j < arc.length; j++) {
wt.write(arc[i][j] + " ");
wt.newLine();
System.out.print(arc[i][j] + " ");
}
System.out.println();
}
wt.flush();
wt.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}