一、BFS思想:
二、算法步骤:
无向图的广度优先遍历可以理解为层次遍历。从A开始,先从第一层找B、C都可以,找不到了,再往下走把B这一层都给访问了,找到D、E。
核心代码:
测试:
完整代码:
package com.huey.graph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
public class Graph {
private ArrayList<String> vertexList;// 存储顶点的集合
private int[][] edges;// 存储图对应的邻接矩阵
private int numOfEdges;// 表示边的数目
// 定义一个数组boolean[],记录某个结点是否被访问过
private boolean[] isVisited;
public static void main(String[] args) {
// 测试图是否创建成功
int n = 5;// 结点的个数
String Vertexs[] = { "A", "B", "C", "D", "E" };
// 创建图对象
Graph graph = new Graph(n);
// 循环添加结点
for (String vertex : Vertexs) {
graph.insertVertex(vertex);
}
// 添加边
// A-B A-C B-C B-D B-E
graph.insertEdge(0, 1, 1);// A-B
graph.insertEdge(0, 2, 1);
graph.insertEdge(1, 2, 1);
graph.insertEdge(1, 3, 1);
graph.insertEdge(1, 4, 1);
// 显示邻接矩阵
graph.showGraph();
// 测试dfs遍历
System.out.println("深度遍历:");
graph.dfs();
System.out.println();
System.out.println("广度优先:");
graph.bfs();
}
// 构造器
public Graph(int n) {
// 初始化矩阵和vertexList
edges = new int[n][n];
vertexList = new ArrayList<String>(n);
numOfEdges = 0;
}
// 得到第一个邻接结点的下标 w
/**
* @param index
* @return 如果存在就返回对应的下标,否则返回-1
*/
public int getFirstNeighbor(int index) {
for (int j = 0; j < vertexList.size(); j++) {
if (edges[index][j] > 0) {
return j;
}
}
return -1;
}
// 根据前一个邻接结点的下标来获取下一个邻接结点
public int getNextNeighbor(int v1, int v2) {
for (int j = v2 + 1; j < vertexList.size(); j++) {
if (edges[v1][j] > 0) {
return j;
}
}
return -1;
}
// 深度优先遍历算法
// i第一次是0
public void dfs(boolean[] isVisited, int i) {
// 首先我们访问该节点,输出
System.out.print(getValueByIndex(i) + "->");
// 将结点设置为已经访问过
isVisited[i] = true;
// 查找结点i的第一个邻接结点w
int w = getFirstNeighbor(i);
while (w != -1) {// 说明有
if (!isVisited[w]) {
dfs(isVisited, w);
}
// 如果w结点已经被访问过
w = getNextNeighbor(i, w);// i是正在被访问的结点
}
}
// 对dfs 进行一个重载,遍历我们所有的结点,并进行 dfs
public void dfs() {
isVisited = new boolean[5];
// 遍历所有的结点,进行dfs(回溯)
for (int i = 0; i < getNumOfVertex(); i++) {
if (!isVisited[i]) {
dfs(isVisited, i);
}
}
}
// 对一个结点进行广度优先遍历的方法
private void bfs(boolean[] visited, int i) {
int u;// 表示队列的头节点对应的下标
int w;// 邻接结点w
// 队列,结点访问的顺序
LinkedList queue = new LinkedList();// 有两个方法,可以当作队列使用:addLast()和removeFirst()
// 访问节点
System.out.print(getValueByIndex(i) + "=>");
// 标记为已访问
isVisited[i] = true;
// 将结点加入队列
queue.addLast(i);
while (!queue.isEmpty()) {
// 取出队列的头节点下标
u = (Integer) queue.removeFirst();// 自动拆箱,queue取出来之后是个Integer类型,u是int类型。
// 就通过这个u找到其第一个邻接结点的下标
// 得到第一个邻接结点的下标 w
w = getFirstNeighbor(u);
while (w != -1) {// 找到
// 是否访问过
if (!isVisited[w]) {
System.out.print(getValueByIndex(w) + "=>");
// 标记已经访问
isVisited[w] = true;
// 入队列
queue.addLast(w);
}
// 以u 为前驱点,找w 后面的下一个邻接点。(已经访问过了?没事!继续往后面访问)
w = getNextNeighbor(u, w);// 查找u这一行的,w的邻接点【体现出广度优先】
// 不通的时候,while循环就不走了,到上面去判断队列是否为空。只要不为空,就从队列头部弹一个出来
}
}
}
// 遍历所有的结点,都进行广度优先搜索
public void bfs() {
isVisited = new boolean[5];
for (int i = 0; i < getNumOfVertex(); i++) {
if (!isVisited[i]) {
bfs(isVisited, i);
}
}
}
// 图中常用的方法
// 返回结点的个数
public int getNumOfVertex() {
return vertexList.size();
}
// 显示图对应的矩阵
public void showGraph() {
for (int[] link : edges) {
System.err.println(Arrays.toString(link));
}
}
// 得到边的个数
public int getNumOfEdges() {
return numOfEdges;
}
// 返回结点i对应的数据 0->"A" 1->"B" 2->"C"
public String getValueByIndex(int i) {
return vertexList.get(i);
}
// 返回V1和V2的权值
public int getWeight(int v1, int v2) {
return edges[v1][2];
}
// 插入顶点
public void insertVertex(String vertex) {
vertexList.add(vertex);
}
// 添加边
/**
* @param v1 表示点的下标,即第几个顶点 "A"-"B" "A"->0 "B"->1
* @param v2 第二个顶点对应的下标
* @param weight 值
*/
public void insertEdge(int v1, int v2, int weight) {
edges[v1][v2] = weight;
edges[v2][v1] = weight;
numOfEdges++;
}
}