深度优先搜索与广度优先搜索

有两种常用的方法可用来搜索图:即深度优先搜索和广度优先搜索。它们最终都会到达所有连通的顶点。深度优先搜索通过栈来实现,而广度优先搜索通过队列来实现。 

深度优先搜索:
下面图中的数字显示了深度优先搜索顶点被访问的顺序。

为了实现深度优先搜索,首先选择一个起始顶点并需要遵守三个规则:
(1) 如果可能,访问一个邻接的未访问顶点,标记它,并把它放入栈中。
(2) 当不能执行规则1时,如果栈不空,就从栈中弹出一个顶点。
(3) 如果不能执行规则1和规则2,就完成了整个搜索过程。

广度优先搜索:
在深度优先搜索中,算法表现得好像要尽快地远离起始点似的。相反,在广度优先搜索中,算法好像要尽可能地靠近起始点。它首先访问起始顶点的所有邻接点,然后再访问较远的区域。它是用队列来实现的。
下面图中的数字显示了广度优先搜索顶点被访问的顺序。

实现广度优先搜索,也要遵守三个规则:
(1) 访问下一个未来访问的邻接点,这个顶点必须是当前顶点的邻接点,标记它,并把它插入到队列中。
(2) 如果因为已经没有未访问顶点而不能执行规则1时,那么从队列头取一个顶点,并使其成为当前顶点。
(3) 如果因为队列为空而不能执行规则2,则搜索结束。

下面是一个图类的java代码,dfs()为深度优先搜索算法,bfs()为广度优先搜索算法:
// 用于实现深度优先搜索的栈类
class  StackX {
    
private final int SIZE=20;
    
private int[] st;
    
private int top;
    
public StackX(){
        st
=new int[SIZE];
        top
=-1;
    }

    
public void push(int j){
        st[
++top]=j;
    }

    
public int pop(){
        
return st[top--];
    }

    
public int peek(){
        
return st[top];
    }

    
public boolean isEmpty(){
        
return top==-1;
    }

}

// 用于实现广度优先搜索的队列类
class  Queue {
    
private final int SIZE=20;
    
private int[] queArray;
    
private int front;
    
private int rear;
    
public Queue(){
        queArray
=new int[SIZE];
        front
=0;
        rear
=-1;
    }

    
public void insert(int j){
        
if(rear==SIZE-1)
            rear
=-1;
        queArray[
++rear]=j;
    }

    
public int remove(){
        
int temp=queArray[front++];
        
if(front==SIZE)
            front
=0;
        
return temp;
    }

    
public boolean isEmpty(){
        
return ((rear+1==front)||(front+SIZE-1==rear));
    }

}

// 顶点类
class  Vertex {
    
public char label;
    
public boolean wasVisited;
    
public Vertex(char lab){
        label
=lab;
        wasVisited
=false;
    }

}

// 图类
public   class  Graph  {
    
    
private final int MAX_VERTS=20;
    
private Vertex vertexList[];
    
private int adjMat[][];
    
private int nVerts;
    
private StackX theStack;
    
private Queue theQueue;
    
    
/** Creates a new instance of Graph */
    
public Graph() {
        vertexList
=new Vertex[MAX_VERTS];
        adjMat
=new int[MAX_VERTS][MAX_VERTS];
        nVerts
=0;
        
for (int j = 0; j < MAX_VERTS; j++{
            
for (int k = 0; k < MAX_VERTS; k++{
                adjMat[j][k]
=0;
            }

        }

        theStack
=new StackX();
        theQueue
=new Queue();
    }

    
//增加一个顶点
    public void addVertex(char lab){
        vertexList[nVerts
++]=new Vertex(lab);
    }

    
//增加一条边
    public void addEdge(int start,int end){
        adjMat[start][end]
=1;
        adjMat[end][start]
=1;
    }

    
public void displayVertex(int v){
        System.out.print(vertexList[v].label);
    }

    
//深度优先搜索
    public void dfs(){
        vertexList[
0].wasVisited=true;
        displayVertex(
0);
        theStack.push(
0);
        
while(!theStack.isEmpty()){
            
int v=getAdjUnvisitedVertex(theStack.peek());
            
if(v==-1)
                theStack.pop();
            
else{
                vertexList[v].wasVisited
=true;
                displayVertex(v);
                theStack.push(v);
            }

        }

        
for(int j=0;j<nVerts;j++)
            vertexList[j].wasVisited
=false;
    }

    
//得到与v顶点邻接且未访问过的顶点标号
    public int getAdjUnvisitedVertex(int v){
        
for (int j = 0; j < nVerts; j++{
            
if(adjMat[v][j]==1&&vertexList[j].wasVisited==false)
                
return j;
        }

        
return -1;
    }

    
//广度优先搜索
    public void bfs(){
        vertexList[
0].wasVisited=true;
        displayVertex(
0);
        theQueue.insert(
0);
        
int v2;
        
while(!theQueue.isEmpty()){
            
int v1=theQueue.remove();
            
while((v2=getAdjUnvisitedVertex(v1))!=-1){
                vertexList[v2].wasVisited
=true;
                displayVertex(v2);
                theQueue.insert(v2);
            }

        }

        
for (int j = 0; j < nVerts; j++{
            vertexList[j].wasVisited
=false;
        }

    }

    
}

  • 11
    点赞
  • 109
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值