图的表示

来源:互联网 发布:自定义动态壁纸软件 编辑:程序博客网 时间:2024/06/05 19:11
1. 图的表示:给定图数据文件(tinyG.txt),计算得到图的邻接矩阵,并把邻接矩阵保存到文件(tinyG_matrix.txt)中。类名:GraphRepresentation。摘自《Algorithms, 4th Edition》P522博文标题:第四周作业——图的表示



package Graph;
import java.io.*;  
import java.util.ArrayList;  


public class GraphRepresentation {    
  public static void main(String[] args) {  
      // TODO 自动生成的方法存根  
      String path = "tinyG.txt";  
      ArrayList<Integer> list = read(path);  
      // 调用产生邻接矩阵  
      int A[][] = MGraph(list);  
      // 控制台打印邻接矩阵  
      for (int i = 0; i < A.length; i++) {  
          for (int j = 0; j < A[0].length; j++) {  
              System.out.print(A[i][j]);  
          }  
          System.out.println(" ");  
      }  
      //写入tinyG_matrix.txt  
      write(A);  
  }  
  // 构造图的邻接矩阵  
  public static int[][] MGraph(ArrayList<Integer> list) {  


      // list数组中的前两个数据分别是图的顶点数目和边的数目  
      int v = list.get(0);  
      int e = list.get(1);  
      // 创建一个二维数组arc来存储邻接矩阵  
      int B[][] = new int[v][e];  
      // 初始化邻接矩阵  
      for (int i = 0; i < v; i++) {  
          for (int j = 0; j < v; j++) {  
              B[i][j] = 0;  
          }  
      }  
      //给邻接矩阵赋值,1表示边存在关系  
      for (int k = 0; k < e; k++) {  
          for (int q = 2; q < list.size() - 2; q = q + 2) {  
              B[list.get(q)][list.get(q + 1)] = 1;  
              B[list.get(q + 1)][list.get(q)] = 1;  
          }  
      }  
      return B;  
  }  


  // 写入tinyG_matrix.txt  
  public static void write(int[][] C) {  
      File f = new File("tinyG_matrix.txt");  
      FileOutputStream fou = null;  
      String a="";  
      try {  


          fou = new FileOutputStream(f, false);// true,设置可追加  
          for (int i = 0; i < C.length; i++) {  
              for (int j = 0; j < C[0].length; j++) {  
                  a =a+String.valueOf(C[i][j]);  
              }  
              a=a+"\t\n";  
          }  
          fou.write(a.getBytes());  
            
      } catch (Exception e) {    
          e.printStackTrace();  
      } finally {  
          try {  
              fou.close();  
          } catch (Exception e) {             
              e.printStackTrace();  
          }  
      }  
  }  


  // 读取文件到Arraylist 数组  
  public static ArrayList<Integer> read(String path) {  
      ArrayList<Integer> list = new ArrayList<Integer>();  
      BufferedReader input = null;  
      try {  
          FileReader in = new FileReader(path);  
          input = new BufferedReader(in);  
          String ss;  
          try {  
              while ((ss = input.readLine()) != null) {  
                  String[] s = (ss.split(" "));  
                  for (int i = 0; i < s.length; i++) {  
                      list.add(Integer.parseInt(s[i].trim()));   
                  }  
              }  
          } catch (IOException e) {         
              e.printStackTrace();  
          }  
          in.close();  
          input.close();  
      } catch (Exception e) {         
          e.printStackTrace();  
      }  
      return list;  
  }  


}  





0 0
原创粉丝点击