算法第四次作業

 学习需要不断练习。请大家至少完成下面的练习1,希望有志于走编程道路的同学完成1、2、3。现在的每一分付出,都可以得到10倍以上的回报。计算机科学专业学生比职业培训的学生优势在哪里,在眼界、思想。

1. 图的表示:给定图数据文件(tinyG.txt),计算得到图的邻接矩阵,并把邻接矩阵保存到文件(tinyG_matrix.txt)中。类名:GraphRepresentation。摘自《Algorithms, 4th Edition》P522博文标题:第四周作业——图的表示

 
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.util.ArrayList;  
  7.   
  8. public class GraphRepresentation {  
  9.   
  10.   
  11.     public static void main(String[] args) {  
  12.         // TODO 自动生成的方法存根  
  13.         String path = "txt/tinyG.txt";  
  14.         ArrayList<Integer> list = read(path);  
  15.         // 调用产生邻接矩阵的函数  
  16.         int[][] arc = MGraph(list);  
  17.         // 控制台打印邻接矩阵  
  18.         for (int i = 0; i < arc.length; i++) {  
  19.             for (int j = 0; j < arc[0].length; j++) {  
  20.                 System.out.print(arc[i][j]);  
  21.             }  
  22.             System.out.println("");  
  23.         }  
  24.         //写入tinyG_matrix.txt  
  25.         write(arc);  
  26.     }  
  27.   
  28.     // 构造图的邻接矩阵函数  
  29.     public static int[][] MGraph(ArrayList<Integer> list) {  
  30.   
  31.         // list数组中的前两个数据分别是图的顶点数目和边的数目  
  32.         int v = list.get(0);  
  33.         int e = list.get(1);  
  34.         // 创建一个二维数组arc来存储邻接矩阵  
  35.         int arc[][] = new int[v][e];  
  36.         // 初始化邻接矩阵  
  37.         for (int i = 0; i < v; i++) {  
  38.             for (int j = 0; j < v; j++) {  
  39.                 arc[i][j] = 0;  
  40.             }  
  41.         }  
  42.         //给邻接矩阵赋值,1表示边存在关系  
  43.         for (int k = 0; k < e; k++) {  
  44.             for (int q = 2; q < list.size() - 2; q = q + 2) {  
  45.                 arc[list.get(q)][list.get(q + 1)] = 1;  
  46.                 arc[list.get(q + 1)][list.get(q)] = 1;  
  47.             }  
  48.         }  
  49.         return arc;  
  50.   
  51.     }  
  52.   
  53.     // 写入tinyG_matrix.txt  
  54.     public static void write(int[][] arc) {  
  55.         File f = new File("txt/tinyG_matrix.txt");  
  56.         FileOutputStream fou = null;  
  57.         String a="",b="";  
  58.         try {  
  59.   
  60.             fou = new FileOutputStream(f, false);// true,设置可追加  
  61.             for (int i = 0; i < arc.length; i++) {  
  62.                 for (int j = 0; j < arc[0].length; j++) {  
  63.                     a =a+String.valueOf(arc[i][j]);  
  64.                 }  
  65.                 a=a+"\t\n";  
  66.             }  
  67.             fou.write(a.getBytes());  
  68.               
  69.         } catch (Exception e) {  
  70.             // TODO: handle exception  
  71.             e.printStackTrace();  
  72.         } finally {  
  73.             try {  
  74.                 fou.close();  
  75.             } catch (Exception e) {  
  76.                 // TODO Auto-generated catch block  
  77.                 e.printStackTrace();  
  78.             }  
  79.         }  
  80.     }  
  81.   
  82.     // 读取文件到Arraylist 数组  
  83.     public static ArrayList read(String path) {  
  84.         ArrayList<Integer> list = new ArrayList<Integer>();  
  85.         BufferedReader input = null;  
  86.         try {  
  87.             FileReader in = new FileReader(path);  
  88.             input = new BufferedReader(in);  
  89.             String ss;  
  90.             try {  
  91.                 while ((ss = input.readLine()) != null) {  
  92.                     String[] s = (ss.split(" "));  
  93.                     for (int i = 0; i < s.length; i++) {  
  94.                         list.add(Integer.parseInt(s[i].trim())); // 将String  
  95.                                                                     // s中的内容添加到动态数组中  
  96.                     }  
  97.   
  98.                 }  
  99.             } catch (IOException e) {  
  100.                 // TODO 自动生成的 catch 块  
  101.                 e.printStackTrace();  
  102.             }  
  103.             in.close();  
  104.             input.close();  
  105.         } catch (Exception e) {  
  106.             // TODO 自动生成的 catch 块  
  107.             e.printStackTrace();  
  108.         }  
  109.   
  110.         return list;  
  111.     }  
  112.   
  113. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值