java广度优先遍历
来源:互联网 发布:淘宝cos道具定制店 编辑:程序博客网 时间:2024/05/29 13:07
一 广度优先遍历介绍
二 java代码
package leaning.graph;import java.util.LinkedList;import java.util.Queue;public class BreadthFirstSearch {private String[] nodes = {"A","B","C","D","E","F","G","H","I"};private boolean [] isVisited = new boolean[9]; private int maxValue = Integer.MAX_VALUE;private int[][] map; Queue<String> queue=new LinkedList<String>(); /* * * 初始化地图 ( * 不连通 , 0 连通未访问 , 1 连通已访问 ) * A B C D E F G H I * 0 A * 0 * * * 0 * * * * 1 B 0 * 0 * * * 0 * 0 * 2 C * 0 * 0 * * * * 0 * 3 D * * 0 * 0 * 0 0 0 * 4 E * * * 0 * 0 * 0 * * 5 F 0 * * * 0 * 0 * * * 6 G * 0 * 0 * 0 * 0 * * 7 H * * * 0 0 * 0 * * * 8 I * 0 0 0 * * * * * * * */ public void initMap(){ this.map = new int[9][9]; this.map[0] = new int[]{maxValue, 0,maxValue,maxValue,maxValue, 0,maxValue,maxValue,maxValue}; this.map[1] = new int[]{ 0,maxValue, 0,maxValue,maxValue,maxValue, 0,maxValue, 0}; this.map[2] = new int[]{maxValue, 0,maxValue, 0,maxValue,maxValue,maxValue,maxValue, 0}; this.map[3] = new int[]{maxValue,maxValue, 0,maxValue, 0,maxValue, 0, 0, 0}; this.map[4] = new int[]{maxValue,maxValue,maxValue, 0,maxValue, 0,maxValue, 0,maxValue}; this.map[5] = new int[]{ 0,maxValue,maxValue,maxValue, 0,maxValue, 0,maxValue,maxValue}; this.map[6] = new int[]{maxValue, 0,maxValue, 0,maxValue, 0,maxValue, 0,maxValue}; this.map[7] = new int[]{maxValue,maxValue,maxValue, 0, 0,maxValue, 0,maxValue,maxValue}; this.map[8] = new int[]{maxValue, 0, 0, 0,maxValue,maxValue,maxValue,maxValue,maxValue}; } /* * 广度遍历入口 * */ public void BFS(){ this.queue.add(this.nodes[0]);//入队 this.BFSMain(); } /* * 广度遍历算法核心 * */ public void BFSMain(){ // 1 : 出队 String currentNode = this.queue.poll(); int yValue = this.getYValue(currentNode); if(!this.isVisited[yValue]){ System.out.print(currentNode+" "); this.isVisited[yValue] = true; } // 2 : 该节点下所有能访问的节点入队 for(int i = 0; i<this.nodes.length ;i++){ if(this.map[yValue][i]==0&&!this.isVisited[i]){ this.queue.add(this.nodes[i]); } } // 3 : 如果队列不为空 ,则继续出队 while(this.queue.size()!=0){ this.BFSMain(); } } //得到节点纵坐标的值 public int getYValue(String node){ int yValue = -1; for(int i = 0 ; i < this.nodes.length ;i++){ if(this.nodes[i].equals(node)){ yValue = i; break; } } return yValue; } public static void main(String[] args) {BreadthFirstSearch breadthFirstSearch = new BreadthFirstSearch();breadthFirstSearch.initMap();breadthFirstSearch.BFS();}}
A B F C G I E D H
0 0
- java广度优先遍历
- 深度优先遍历 广度优先遍历JAVA
- Java图以及BFS广度优先遍历
- Java 二叉树广度优先遍历
- JAVA-图的广度优先遍历
- java算法:图遍历(深度优先和广度优先)
- Java遍历树(深度优先+广度优先)
- 深度优先和广度优先遍历及其 Java 实现
- Java遍历树(深度优先+广度优先)
- 深度优先和广度优先遍历及其 Java 实现
- Java 实现深度优先和广度优先遍历
- Java遍历树(深度优先+广度优先)
- 广度优先遍历BFS
- 广度优先遍历
- 图--广度优先遍历
- 图广度优先遍历
- 广度优先遍历
- 广度优先遍历-示例
- Hibernate配置文件
- 【STL】next_permutation的实现
- caffe源码阅读——Blob类
- tomcat启动一闪而过
- 记一次花费一天时间debug的过程
- java广度优先遍历
- bzoj3926 广义后缀自动机
- Collection Data Structures In Swift
- Valid Palindrome
- 多线程 工厂 > 商店(仓库) > 客户模式 消费者模式 实现多仓库 多产品 多工厂 多客户
- 产品需求管理
- BZOJ-1066 蜥蜴 最大流+拆点+超级源超级汇
- 在CentOS 6.6 64bit上升级GCC到4.8.2版本
- POJ 3046 Ant Counting(dp—多重集组合数问题)