`

图的深搜和广搜

阅读更多
DFS(深度优先遍历)
图的深度优先搜索和树的深度优先搜索思想很相似,都是从一个顶点v出发尽可能像纵深方向搜索,访问了顶点v,下一个访问与v相邻的顶点v1,然后以v1作为初始节点,依次访问后面的节点。对于深度优先搜索的实现,我们采用堆栈来实现。
递归实现DFS的伪代码:
void dfs(vertex v) {
	if(v == null) return;
	v.visited = true;
	foreach(vertex v.adjancent){
		if(v.adjancent.visted == false)
			dfs(v.adjancent);
	}
}


BFS(广度优先遍历)
广度优先遍历也很容易理解,就是从一个顶点v出发,我们首先访问完所有与v相邻的顶点后,然后在以和v第一个相邻的节点作为初始节点以同样的方式搜索。我们采用队列来实现图的广度优先搜索。
迭代实现BFS的伪代码:
void bfs(Queue queue) {
	while(!queue.isEmpty()){
		Vertex v = queue.poll();
		while(foreach vertex : v.adjancent == false){
			queue.offer(v.adjancent);
			v.adjancent == true;
		}
	}
}


BFS和DFS的具体实现
下面我们用java实现DFS和BFS,然后建立一个无向图进行测试。代码如下:
//图的顶点类
class GraphVertex{
    public int label;
    public boolean wasVisited;
    public GraphVertex(int label){
        this.label=label;
        wasVisited=false;
    }
}
public class GraphDemo {
	 private  GraphVertex[] vertexList;
	 private  int[][] ajmx;
	 private  int numVerts;
	 Stack<Integer> theStack = new Stack<Integer>();
	 Queue<Integer> theQueue = new LinkedList<Integer>();
	 
	 //创建一个图的实例
	 public GraphDemo(int vertexNum) {
		 //构建顶点集合
		 vertexList = new GraphVertex[vertexNum];
	     //构建邻接矩阵
		 ajmx = new int[vertexNum][vertexNum];
	     numVerts = 0;
	}
	 
	//添加顶点
	public  void addVertex(int label) {
	        vertexList[numVerts++] = new GraphVertex(label);
	}
	//添加边
    public  void addEdge(int start,int end){
        //如果两个顶点之间有边,则初始化为1;
    	ajmx[start][end]=1;
        ajmx[end][start]=1;
    } 
    
    //深度优先遍历
    public void dfs(){
        //从标号为0的点开始遍历
    	vertexList[0].wasVisited=true;
        System.out.print(0 + " ");
        theStack.push(0);
        while(!theStack.isEmpty()){
            int vertex = getAdjUnvisitedVertex(theStack.peek());
            if(vertex == -1)
                theStack.pop();
            else{
                vertexList[vertex].wasVisited=true;
                System.out.print(vertex + " ");
                theStack.push(vertex);
            }
        }
        setDefaultVertex();
    }
    
    //广度优先遍历
    public void bfs(){
        vertexList[0].wasVisited = true;
        System.out.print(0 + " ");
        theQueue.offer(0);
        int vertex1;
        while(!theQueue.isEmpty()){
            int temVertex = theQueue.remove();
            while((vertex1 = getAdjUnvisitedVertex(temVertex)) != -1){
                vertexList[vertex1].wasVisited = true;
                System.out.print(vertex1 + " ");
                theQueue.offer(vertex1);
            }
        }
        setDefaultVertex();
    }
    
    //遍历结束后,恢复顶点的默认值
    public void setDefaultVertex(){
    	for (int j = 0; j < numVerts; j++) {
            vertexList[j].wasVisited=false;
        }
    }
    
    //得到未访问过的相连的顶点
    public int getAdjUnvisitedVertex(int v){
        for (int j = 0; j < numVerts; j++) {
            if(ajmx[v][j] == 1 && vertexList[j].wasVisited == false)
                return j;
        }
        return -1;
    } 
    
    public static void main(String[] args) {
    	GraphDemo graph = new GraphDemo(5);
    	
    	for(int i = 0; i < 5; i++)
    		graph.addVertex(i);
    	
    	graph.addEdge(0,1);
    	graph.addEdge(0,2);
    	graph.addEdge(1,4);
    	graph.addEdge(2,3);
    	
    	graph.dfs();
    	System.out.println();
    	graph.bfs();
    		
    }
}


输出结果为:
0 1 4 2 3
0 1 2 4 3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics