java广度优先遍历


一 广度优先遍历介绍

java广度优先遍历_第1张图片

java广度优先遍历_第2张图片


二 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 

你可能感兴趣的:(java广度优先遍历)