图的广度优先遍历

广度优先遍历类似于二叉树的层次遍历,采用的是队列的数据结构。区别是 图有多个连通分量,所以得先遍历所有顶点(有可能各个顶点之间都没有边,独立的),然后将顶点入队列,然后出队列,再将顶点相连的其他顶点入队列,依此循环。。

1)邻接矩阵的广度优先遍历

数据结构

public class Graph {
    String[] vertex = new String[5];    //顶点类型可自定义
    int[][] edge = new int[5][5];       //边的类型可自定义

     //省略get.set方法

}

public class BFSTraverseGraph {
    private static boolean[] visited = new boolean[5];   //假定为5个顶点

    /**
     * 邻接矩阵的广度优先遍历  类似于二叉树的层次遍历,使用队列完成
     * @param g
     */
    public static void BFSTraverseMatrix(Graph g){

        //初始化顶点访问状态为未访问
        for(int i = 0; i < visited.length; i++){
            visited[i] = false;
        }

        LinkedList queue = new LinkedList();   //此处使用java中的LinkedList代替队列
        for(int i = 0; i < visited.length; i++){
            if(!visited[i]){
                queue.push(g.getVertex()[i]);
                visited[i] = true;  //设该顶点状态为已访问
                System.out.println("打印顶点信息:" + g.getVertex()[i]);
                while(queue.size() != 0){       //不能判断null 因为局部变量queue必须实例化(java相关)
                    queue.pop();
                    for(int j = 0; j < g.getVertex().length; j++){
                        if(g.getEdge()[i][j] == 1 && !visited[j]){
                            visited[j] = true;
                            System.out.println("打印顶点信息:" + g.getVertex()[j]);
                            queue.push(g.getVertex()[j]);
                        }
                    }
                }
            }
        }
    }

}

2.邻接表的广度优先遍历

数据结构

public class AdjacentTable {
    private static final int MAX_SIZE = 100;        //能存放的最大顶点数
    private VertexNode[] vertexNodes = new VertexNode[MAX_SIZE];   //存放顶点的数组
    private int vertexNum;      //顶点数   不能超过能存放顶点数的最大值
    private int edgeNum;          //边数   不能超过无向图的最大边数 即  n(n-1)/2   n为顶点数

//省略get.set方法

}

public class VertexNode {
    private String data;   //存放顶点结点信息   比如名称
    private EdgeNode firstEdge;  //指向与该结点相连的 顶点集合 的第一个结点
//省略get.set方法}
public class EdgeNode {
    private int adjvex;  //存放该顶点在顶点列表中的下标(位置)
    private int weight;  //权值  对于非网图可以不需要  所谓网图:就是带权的图
    private EdgeNode next; //下一个邻接点

//省略get.set方法}

public class BFSTraverseGraph {
    private static boolean[] visited = new boolean[5];   //假定为5个顶点

    

    public static void BFSTraverseAdjacent(AdjacentTable g){
        //初始化顶点状态为未访问状态
        for(int i = 0; i < visited.length; i++){
            visited[i] = false;
        }

        LinkedList queue = new LinkedList();  //使用LinkedList代替队列
        for(int i = 0; i < g.getVertexNum(); i++){
            if(!visited[i]){
                visited[i] = true;
                System.out.println("打印顶点信息:" + g.getVertexNodes()[i].getData());
                queue.push(g.getVertexNodes()[i]);
                while(queue.size() != 0){       //不能判断null 因为局部变量queue必须实例化(java相关)
                    queue.pop();
                    EdgeNode node = g.getVertexNodes()[i].getFirstEdge();
                    while(node != null){
                        if(!visited[node.getAdjvex()]){
                            visited[node.getAdjvex()] = true;
                            System.out.println("打印顶点信息:" + g.getVertexNodes()[node.getAdjvex()].getData());
                            queue.push(node);
                        }
                        node = node.getNext();
                    }
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值