图的广度优先遍历

33.1 与树的广度优先遍历类似。

33.2 为每个核心方法写一个测试方法,这叫单元测试。

代码:

/***************** Breadth first traversal.* * @param paraStartIndex The start index.* @return The sequence of the visit.***************/public String breadthFirstTraversal(int paraStartIndex) {CircleObjectQueue tempQueue = new CircleObjectQueue();String resultString = "";int tempNumNodes = connectivityMatrix.getRows();boolean[] tempVisitedArray = new boolean[tempNumNodes];// Initialize the queue.// visit before enqueue.tempVisitedArray[paraStartIndex] = true;resultString += paraStartIndex;tempQueue.enqueue(new Integer(paraStartIndex));// Now visit the rest of the graph.int tempIndex;Integer tempInteger = (Integer) tempQueue.dequeue();while (tempInteger != null) {tempIndex = tempInteger.intValue();// Enqueue all its unvisited neighbors.for (int i = 0; i < tempNumNodes; i++) {if (tempVisitedArray[i]) {continue;// Already visited.} // Of ifif (connectivityMatrix.getData()[tempIndex][i] == 0) {continue;// Not directly connected.} // Of if// Visit before enqueue.tempVisitedArray[i] = true;resultString += i;tempQueue.enqueue(new Integer(i));} // Of for i// Take out one from the head.tempInteger = (Integer) tempQueue.dequeue();} // Of whilereturn resultString;}// Of breadthFirstTraversal/************** Unit test for breadthFirstTraversal.************/public static void breadthFirstTraversalTest() {// Test an undirected graph.int[][] tempMatrix = { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 1 }, { 0, 1, 1, 0 } };Graph tempGraph = new Graph(tempMatrix);System.out.println(tempGraph);String tempSequence = "";try {tempSequence = tempGraph.breadthFirstTraversal(2);} catch (Exception ee) {System.out.println(ee);} // Of try.System.out.println("The breadth first order of visit: " + tempSequence);}// Of breadthFirstTraversalTest/*************** The entrance of the program.* * @param args Not used now.*************/public static void main(String[] args) {// TODO 自动生成的方法存根System.out.println("Hello!");Graph tempGraph = new Graph(3);System.out.println(tempGraph);// Unit test.getConnectivityTest();breadthFirstTraversalTest();}// Of main

结果:

33.3 如果图不一定是连通的, 就需要用到张星移师兄代码

代码:

tempSequence = tempGraph.BreadthFirstTraversalForAllNodes();} catch (Exception ee) {System.out.println(ee);} // Of try.System.out.println("The breadth first order of visit: " + tempSequence);}// Of breadthFirstTraversalTest/*************** The entrance of the program.* * @param args Not used now.*************/public static void main(String[] args) {// TODO 自动生成的方法存根System.out.println("Hello!");Graph tempGraph = new Graph(3);System.out.println(tempGraph);// Unit test.getConnectivityTest();breadthFirstTraversalTest();}// Of main

结果:

Day 33 图的广度优先遍历相关推荐

  1. [C] 图的广度优先遍历

    图的广度优先遍历 我一直觉得图的遍历没有地图类型的题目难,遍历嘛,每个点都走一遍就行了. 但是给定地图求面积啊,数量啊的那种题目,花样挺多的. 图的遍历真挺难把人绕晕的,关于广度优先,理解好层层递进这 ...

  2. 邻接表存储图的广度优先遍历

    试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ); 其中LGraph是邻接表存储 ...

  3. 数据结构 图的广度优先遍历 C++

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Bool ...

  4. 二十三、图的广度优先遍历

    二十三.图的广度优先遍历 文章目录 二十三.图的广度优先遍历 题目描述 解题思路 上机代码 题目描述 程序的输入是无向图的顶点序列和边序列(顶点序列以*为结束标志,边序列以-1,-1为结束标志).程序 ...

  5. 数据结构与算法:终于可以用三种语言(C,C#,JavaScript)把图的广度优先遍历讲清楚了(推荐收藏)

    文章目录 邻接矩阵存储图的广度优先遍历过程分析 C语言实现队列编程 程序中加入图的处理函数 结果的再次分析 C#语言实现图的广度优先遍历.并显示广度优先遍历生成树 JavaScript语言实现图的广度 ...

  6. 数据结构之栈的应用:树的层次遍历、图的广度优先遍历、OS的FCFS策略

    栈的应用:树的层次遍历.图的广度优先遍历.OS的FCFS策略 树的层次遍历: 图的广度优先遍历 OS的FCFS策略: 树的层次遍历: 算法思想: 1.先遍历头节点1,头节点1入队 2.在遍历头节点的孩 ...

  7. 数据结构笔记(二十八)-- 图的广度优先遍历

    图的广度优先遍历 一.基本思想 二.图的广度优先遍历举例 三.伪代码实现 邻接点的访问的实现需要根据图的存储方式来进行实现

  8. 图论算法(5):图的广度优先遍历 BFS

    本章节内容使用 java 实现,Github 代码仓:https://github.com/ZhekaiLi/Code/tree/main/Graph/src 查看文章内的图片可能需要科学上网! 因为 ...

  9. 获取图顶点的入度、出度;获取图的两个顶点之间的权值; 图的深度优先算法、图的广度优先遍历

    广度优先结果: 深度优先结果: 代码整理: public class Graph {private int vertexSize;//顶点数量private int[] vertexs;//顶点数组p ...

最新文章

  1. 2021谷歌学术指标出炉:CVPR总榜第4,仅次于Science,ECCV超过ICCV......
  2. python求解方程组_python如何解方程组
  3. HDU-4850 Wow! Such String!(模拟) ——26行代码AC
  4. InfoQ —— 腾讯游戏大数据服务场景与应用
  5. java读取文件并显示_JAVA读取本地文件并显示到页面中
  6. hiho1257 Snake Carpet
  7. 远程桌面无法连接服务器,启动Terminal Services 服务报1053错误
  8. centOS下安装tomcat详解
  9. 【转】RegSetValueEx运行时失败,返回代码为5,解决方法
  10. 聊聊互联网秋招总结 经验分享
  11. 西瓜书+实战+吴恩达机器学习(五)监督学习之线性判别分析 Linear Discriminant Analysis
  12. JavaScript继承详解
  13. comsol分析时总位移代表什么_通过仿真分析球形盖的变形问题
  14. 【转】运维DBA的4大纪律9项注意
  15. Oracle用Start with...Connect By prior子句递归查询(转)
  16. 《JDBC与Java数据库编程》pdf 附下载链接
  17. pic单片机汇编 c语言,PIC单片机汇编语言指令详解
  18. LDAP统一认证服务解决方案
  19. 2020.10.16 web前端 盒子模型border-box 图片模糊处理(filter)clac的使用 CSS的过渡(trancsion hover)
  20. PTA 求链式线性表的倒数第K项 给定一系列正整数,请设计一个尽可能高效的算法,查找倒数第K个位置上的数字。

热门文章

  1. oracle中的orddata,【Oracle Database 12c新特性】ORACLE_MAINTAINED
  2. mybatisPlus-mapper的注入值得思考
  3. S355NL-Z35欧标低合金期货定轧,S355NL-Z35钢板性能介绍
  4. 8086cpu 可编程接口技术(一)
  5. wincc怎么做一个弹出画面_在wincc画面上如何弹出小窗口
  6. 计算机在医学领域的最新应用领域,计算机技术在医学领域的具体应用及发展研究...
  7. 粗粒度和细粒度入门知识
  8. 必易微电子科创板上市:市值39亿 小米长江是股东
  9. 如何计算机硬盘恢复默认设置,恢复bios出厂默认值怎么操作【图解】
  10. Firefox和IE中浏览一些网页字体模糊的解决方法