- package algorithm;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.util.StringTokenizer;
- public class GraphReverse {
- private static int vNum; //点的个数
- private static int eNum; //边的个数
- private static int[][] getData() {
- int[][] matrix = null;
- try {
- BufferedReader reader = new BufferedReader (new FileReader(new File("src/dataFile/tinyDG.txt")));
- vNum = Integer.valueOf(reader.readLine().trim());
- eNum = Integer.valueOf(reader.readLine().trim());
- matrix = new int[eNum][2];
- String temp = "";
- int count = 0;
- while((temp = reader.readLine()) != null) {
- int index = 0;
- StringTokenizer str = new StringTokenizer(temp);
- while (str.hasMoreElements()) {
- matrix[count][index] = Integer.valueOf(str.nextToken());
- index++;
- }
- count++;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return matrix;
- }
- /**
- * function:
- * 0:有序表的邻接表
- * 1:反向图的邻接表
- */
- private static void getSortedList(int[][] data, int function) {
- String str = "";
- int begin=0, end=0;
- if (function == 0) {
- str = "有序表的邻接表:";
- begin = 0;
- end = 1;
- } else if (function == 1) {
- str = "反向图的邻接表:";
- begin = 1;
- end = 0;
- }
- System.out.println(str);
- for (int i=0; i<vNum; i++) {
- System.out.print(i + ": ");
- for (int j=0; j<data.length; j++) {
- if (data[j][begin] == i) {
- System.out.print("\t" + data[j][end]);
- }
- }
- System.out.println("");
- }
- }
- public static void main(String[] args) {
- getSortedList(getData(), 0);
- getSortedList(getData(), 1);
- }
- }
-
第五周作业
最新推荐文章于 2023-04-08 09:39:53 发布