第四周作业:图的表示
来源:互联网 发布:淘宝破损补寄怎么设置 编辑:程序博客网 时间:2024/06/11 11:20
图数据文件:
计算得到图的邻接矩阵,并把邻接矩阵保存到文件(tinyG_matrix.txt)中
源程序:
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;import java.util.Scanner;public class GraphRepresentation {public static void main(String[] args) throws FileNotFoundException{//从文件(tinyG.txt)中读取数据并保存在二维数组中Scanner sc= new Scanner(new File("tinyG.txt"));int vertex = sc.nextInt();int edge = sc.nextInt();int[][] toGraphic = new int[vertex][edge];int length = toGraphic.length;for(int i=0;i<length;i++)for(int j=0;j<length;j++)toGraphic[i][j] = 0;int vertex1,vertex2;while(sc.hasNextInt()){vertex1 = sc.nextInt();vertex2 = sc.nextInt();toGraphic[vertex1][vertex2] = 1;toGraphic[vertex2][vertex1] = 1;}sc.close();//邻接矩阵保存到文件(tinyG_matrix.txt)中PrintWriter pw = new PrintWriter(new File("tinyG_matrix.txt"));for(int n=0;n<length;n++){for(int m=0;m<length;m++){pw.printf(toGraphic[n][m]+" ");}pw.println();}pw.close();//输出邻接矩阵System.out.println("生成的邻接矩阵为:");for(int n=0;n<length;n++){for(int m=0;m<length;m++){System.out.print(toGraphic[n][m]+" ");}System.out.println();}}}
结果如图:
0 0
- 第四周作业 -- 图的表示
- 第四周作业--图的表示
- 第四周作业—图的表示
- 第四周作业---图的表示
- 第四周作业-图的表示
- 第四周作业-图的表示
- 第四周作业---图的表示
- 第四周作业—图的表示
- 第四周作业--图的表示
- 第四周作业—图的表示
- 第四周java作业,图的表示
- 第四周作业—图的表示
- 第四周作业,图的表示
- 第四周作业:图的表示
- 第四周作业-----图的表示
- 第四周作业-----图的表示
- 第四周作业 图的表示
- 第四周作业 -- 图的表示
- UML
- 小凡实验室卧龙老师CCIE学习方法:CCIE涅槃五剑
- sql简单语句
- 安卓开发之清理手机应用程序缓存
- wikioi天梯之1475 m进制转十进制
- 第四周作业:图的表示
- MySQL 5.0 游标基础
- jsp 用户上传头像、上传图片、邮件上传附件代码
- win8推荐浏览器
- nc.exe(Netcat) 瑞士军刀
- 算法实验题 车皮编序问题(转化为在全排列的基础上的堆栈问题)
- iOS view 自适应屏幕问题
- Cocos2dx入门小游戏---Runner教程
- 设计模式一日一练:单件模式(Singleton)