第四周作业——图的表示
来源:互联网 发布:造字软件手机版 编辑:程序博客网 时间:2024/06/10 21:15
1. 图的表示:给定图数据文件(tinyG.txt),计算得到图的邻接矩阵,并把邻接矩阵保存到文件(tinyG_matrix.txt)中。类名:GraphRepresentation。
package suanfa3;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.PrintWriter;public class GraphRepresentation {static int arc[][];static int vertextNum;static int arcNum;static int vartex[];public static void main(String[] args) {String path1="src/tinyG.txt";String path2="src/tinyGResult.txt";try {FileReader read=new FileReader(new File(path1));BufferedReader reader=new BufferedReader(read);PrintWriter out=new PrintWriter(new File(path2)); String line=""; int index=1;while((line=reader.readLine())!=null){if(index==1){vertextNum=Integer.parseInt(line.trim());index++;continue;}else if(index==2){arcNum=Integer.parseInt(line.trim());index++;continue;} else if(index==3){arc=new int[vertextNum][arcNum];index++;continue;}addEadge(line);}for(int i=0;i<vertextNum;i++){for(int j=0;j<vertextNum;j++){out.print(arc[i][j]+"\t");}out.flush();out.println();}} catch (Exception e) {e.printStackTrace();}}//传入格式为"x y"的值分解为x和y,并使arc[x][y]=1和arc[y][x]=1 public static void addEadge(String line){ String []result=line.trim().split(" "); int v1=Integer.parseInt(result[0]); int v2=Integer.parseInt(result[1]); for(int i=0;i<arcNum;i++){ arc[v1][v2]=1; arc[v2][v1]=1; } }}
0 0
- 第四周作业—图的表示
- 第四周作业—图的表示
- 第四周作业—图的表示
- 第四周作业—图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示(GraphRepresentation)
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- jdbc 访问 sql server 2008
- 【玩转cocos2d-x之三十九】Cocos2d-x 3.0截屏功能集成
- c++常用小工具的函数
- arch qtcreator解决无法输入中文
- windows server 2008看安装IIS的方法(已解决)
- 第四周作业——图的表示
- 算法学习十三----和为n连续正数序列
- Oracle 表连接方式(内连接/外连接/自连接) 详解
- javascript学习笔记 面向对象 2014-04-17
- javascript_dom简单表单校验
- 第六周作业——利用哈夫曼编码英文字母表
- 【HTML/CSS】总结web app项目实践经验
- 科目三并不难 盘点科目三技巧
- javascript_dom表单校验