码迷,mamicode.com
首页 > 编程语言 > 详细

Java用邻接矩阵实现广度优先

时间:2015-08-27 18:39:55      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:java      广度优先搜索   

定义节点类:

//一个节点
class Vertex{
    char label;
    boolean wasVisited;
    public Vertex(char label){
        this.label = label;
        wasVisited = false; 
    }
}

图:

class Graph{
    private final int MAX_VERTS = 20;
    private Vertex vertexList[];//节点列表
    private int adjMat[][];//邻接矩阵
    private int nVerts;//节点数

    private Queue theQueue;//协助队列
    public Graph(){
        vertexList = new Vertex[MAX_VERTS];
        adjMat = new int[MAX_VERTS][MAX_VERTS];
        nVerts = 0;
        for(int i = 0; i < MAX_VERTS; i++){
            for(int j = 0; j < MAX_VERTS; j++)
                adjMat[i][j] = 0;
        }
        theQueue = new ArrayDeque();
    }

    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);
    }

    //获取为v节点邻接的未被访问的节点
    public int getAdjUnvisiedVertex(int v){
        for(int i = 0; i < nVerts; i++){
            if(adjMat[v][i] == 1 && vertexList[i].wasVisited == false){
                return i;
            }
        }
        return -1;
    }


    //广度优先搜索
    /**
     * 规则1:访问下一个未来访问的邻接点(如果存在),这个顶点必须是当前顶点的邻接点,标记为已访问,并把它插入到队列中
     * 
     * 
     * 规则2:如果因为已经没有未访问顶点而不能执行规则1,那么从队列头取一个顶点(如果存在),并使其
     * 称为当前顶点
     * 
     * 规则3:如果因为队列为空而不能执行规则2,则搜索结束
     */
    public void bfs(){
        vertexList[0].wasVisited = true;
        displayVertex(0);;
        theQueue.offer(0);
        int v2;

        while( !theQueue.isEmpty()){
            int v1 = (int) theQueue.remove();
            while((v2 = getAdjUnvisiedVertex(v1)) != -1){
                vertexList[v2].wasVisited = true;
                displayVertex(v2);
                theQueue.offer(v2);
            }
        }

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

测试:

        Graph theGraph = new Graph();
        theGraph.addVertex(‘A‘);
        theGraph.addVertex(‘B‘);
        theGraph.addVertex(‘C‘);
        theGraph.addVertex(‘D‘);
        theGraph.addVertex(‘E‘);
        theGraph.addVertex(‘F‘);
        theGraph.addVertex(‘G‘);
        theGraph.addVertex(‘H‘);
        theGraph.addVertex(‘I‘);
        theGraph.addEdge(0, 1);//AB
        theGraph.addEdge(0, 2);//AC
        theGraph.addEdge(0, 3);//AD
        theGraph.addEdge(0, 4);//AE
        theGraph.addEdge(1, 5);//BF
        theGraph.addEdge(5, 6);//FG
        theGraph.addEdge(3, 6);//DG
        theGraph.addEdge(6, 7);//GI

        theGraph.bfs();

技术分享

广度优先搜索:ABCDEFGH

版权声明:本文为博主原创文章,未经博主允许不得转载。

Java用邻接矩阵实现广度优先

标签:java      广度优先搜索   

原文地址:http://blog.csdn.net/u011102153/article/details/48031525

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!