用JAVA实现广度优先搜索

深度优先与广度优先搜索代码实现很相似,前者是利用了栈这种数据结构;而后者是利用了队列这种数据结构。下面看看广度优先搜索代码是怎么实现的

/*****************************广度优先***********************************/    

    基本思路:1、找到初始结点,标记,入队;

                      2、找到与初始结点相邻的所有未被访问结点,标记,入队;

                      3、当没有相邻的未被访问的结点被找到时,出队,然后找这时出队的

                           这个结点的所有相邻的未被访问的结点。重复2、;

                      4、只要队列不为空,就一直循环这个过程;

    相比深度优先,广度优先多了一个循环:外层循环是控制队列是否为空,内层循环

                             判断是否还有相邻的未被访问的结点。

    广度优先代码如下:

  package bfs;

import dfs.Vertex;

public class Graph
{
 private int max = 20;
 private Vertex[] vertexs;
 private int[][] adjMat; //邻接矩阵
 private int nVertes;
 private Queue queue;
 public Graph(){
adjMat = new int[max][max];
nVertes=0;
vertexs = new Vertex[max];
queue = new Queue();
for (int i = 0; i < max; i++)
{
for (int k = 0; k < max; k++)
{
adjMat[i][k]=0;
}
}
 }
 //广度优先搜索
 public void bfs(){
//初始节点,访问,标记,入队
vertexs[0].wasVisited=true;
displayVertex(0);
queue.enqueue(0);
 
//外层循环,控制队列不为空
while (!queue.isEmpty())
{
//当找到所有相邻的节点后 ,没有相邻的节点时取出队首
int v2 = queue.dequeue();
//内循环,
int v1;
while ((v1=getUnvisitedVertex(v2))!=-1)
{
vertexs[v1].wasVisited=true;
displayVertex(v1);
queue.enqueue(v1);
}

}
for (int i = 0; i < nVertes; i++)
{
vertexs[i].wasVisited = false;
}
 }
private int getUnvisitedVertex(int v)
{
for (int i = 0; i < nVertes; i++)

if (adjMat[v][i]==1&&vertexs[i].wasVisited==false)

return i;

return -1;


}
private void displayVertex(int v)
{
System.out.print(vertexs[v].label+” “);

}
public void addEdge(int start ,int end){
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}
public  void addVertex(char lab){
vertexs[nVertes++] = new  Vertex(lab);
}
}

用到的数据结构如下:

//**********************************************//

package bfs;

public class Queue
{
private int []a;
private int  max=20;
private int head;
private int tail;
public Queue(){
a = new int[max];
head = 0;
tail = -1;
}
 public void enqueue(int j){
if (tail == max-1)
{
tail = -1;
}else {
a[++tail] = j;
}
 
 }
 public int dequeue(){
int temp = a[head++];
if (temp==max)
head = 0;
return temp;

 }
 public boolean isEmpty(){
return (head+max-1==tail)||(tail+1==head); 
 }
}

//*************************************//

package bfs;

public class Vertex
{
  public char label;
  public  boolean wasVisited ;

 public Vertex(char lab){
this.label = lab;
wasVisited = false;

 }
 
}

//***********************************************************//

package bfs;

public class Vertex
{
  public char label;
  public  boolean wasVisited ;

 public Vertex(char lab){
this.label = lab;
wasVisited = false;

 }
 
}

//************************************//

测试代码如下:

package bfs;

public class test
{
 public static void main(String[] args)
{
Graph graph = new Graph();
graph.addVertex(‘A’);
graph.addVertex(‘B’);
graph.addVertex(‘F’);
graph.addVertex(‘H’);
graph.addVertex(‘C’);
graph.addVertex(‘D’);
graph.addVertex(‘G’);
graph.addVertex(‘I’);
graph.addVertex(‘E’);
graph.addEdge(0,1);
graph.addEdge(1,2);
graph.addEdge(2,3);
graph.addEdge(0,4);
graph.addEdge(0,5);
graph.addEdge(5,6);
graph.addEdge(6,7);
graph.addEdge(7,8);
graph.bfs();
}
}

//*************************************//

运行结果如下:

A B C D F G H I E 


点赞