第四周作业——图的表示
来源:互联网 发布:琅琊榜细节 知乎 编辑:程序博客网 时间:2024/06/06 03:09
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)
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 第四周作业——图的表示
- 如何鉴别snidel真假
- SharePoint 中调用传出电子邮件中的邮件服务器及地址发送邮件
- 字符串与正则表达式
- C++学习过程中的一些笔记
- ActiveX控件中加载BCB编写的dll的注意事项
- 第四周作业——图的表示
- ios Sqlite数据库增删改查基本操作
- 发送TCP数据包
- 搜狐云景客户端工具评测之WordPress的搭建
- google开源protobuf
- vc中release和debug的区别
- JAVA 通过HtmlParser获取指定的tag
- 使用迭代法对二叉树进行后序遍历——Leetcode系列(六)
- 网站建设10个最好的响应的HTML5滑块插件