package upload;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
public class DfsandBfs {
private ArrayList<String> vertex;// 顶点
private int[][] edges;// 边,实际上是邻接矩阵
private int number_of_edge;// 边的数目
private boolean[] isVisited;
// 构造器
public DfsandBfs(int n) {
vertex = new ArrayList<String>(n);
edges = new int[n][n];
isVisited = new boolean[n];
number_of_edge = 0;
}
// 添加点的方法
public void addvertex(String point) {
vertex.add(point);// 后面可以通过增强for循环添加
}
// 添加边的方法
public void addedge(int w1, int w2, int weight) {
edges[w1][w2] = weight;
edges[w2][w1] = weight;
number_of_edge++;
}
// 返回下标所对应字符
public String showsign(int index) {
return vertex.get(index);
}
// 返回顶点数目
public int shownumber_vertex() {
return vertex.size();
}
// 返回边的数目
public int shownumber_edge() {
return number_of_edge;
}
// 返回边的权值
public int showweight(int w1, int w2) {
return edges[w1][w2];
}
// 返回图的邻接矩阵
public void showgraph() {
for (int[] arr : edges) {
System.out.println(Arrays.toString(arr));
}
}
// 寻找邻接点的下标,给定这个元素的下标index情况下
public int seekadjacentpoint(int index) {
for (int i = 0; i < shownumber_vertex(); i++) {
if (edges[index][i] != 0) {
return i;
}
}
return -1;
}
// 当要寻找邻接点的下一个邻接点下标,这里是为深度遍历和广度遍历做铺垫
public int seeknextadjacentpoint(int index, int next) {
for (int j = next + 1; j < shownumber_vertex(); j++) {
if (edges[index][j] != 0) {
return j;
}
}
return -1;
}
// 开始深度遍历一个结点
public void dfs(boolean[] isVisited, int index) {
System.out.print(showsign(index) + "->");// 访问当前结点
isVisited[index] = true;
int adjacentpoint = seekadjacentpoint(index);
while (adjacentpoint != -1) {
if (!isVisited[adjacentpoint]) {
dfs(isVisited, adjacentpoint);//直接以这个点为基础继续深度遍历
}
adjacentpoint = seeknextadjacentpoint(index, adjacentpoint);
}
}
// 深度遍历所有结点
public void dfs() {
for (int i = 0; i < shownumber_vertex(); i++) {
if (!isVisited[i]) {
dfs(isVisited, i);
}
}
System.out.println();
}
// 重置一下布尔数值
public void resetbool() {
for (int i = 0; i < isVisited.length; i++) {
isVisited[i] = false;
}
}
// 广度遍历一个结点
public void bfs(boolean[] isVisited, int index) {
LinkedList queue = new LinkedList();// 用一个队列存放遍历时的结点
System.out.print(showsign(index) + "->");
isVisited[index] = true;
queue.add(index);
while (!queue.isEmpty()) {
int point = (Integer) queue.removeFirst();
int adjacentpoint = seekadjacentpoint(point);
while (adjacentpoint != -1) {
if (!isVisited[adjacentpoint]) {
System.out.print(showsign(adjacentpoint) + "->");
isVisited[adjacentpoint] = true;
queue.add(adjacentpoint);
} else {
adjacentpoint = seeknextadjacentpoint(index, adjacentpoint);
}
}
}
}
// 广度遍历所有结点
public void bfs() {
for (int i = 0; i < shownumber_vertex(); i++) {
if (!isVisited[i]) {
bfs(isVisited, i);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DfsandBfs db = new DfsandBfs(5);
String[] str = { "A", "B", "C", "D", "E"};
for (String s : str) {
db.addvertex(s);
}
//图的连接方式为:A-B A-C A-E B-D C-E
db.addedge(0, 1, 1);
db.addedge(0, 2, 1);
db.addedge(0, 4, 1);
db.addedge(1, 3, 1);
db.addedge(2, 4, 1);
db.showgraph();
System.out.println("深度遍历的结果是:");
db.dfs();
db.resetbool();
System.out.println("广度遍历的结果是:");
db.bfs();
}
}
记录Java实现图的深度优先遍历与广度优先遍历练习
最新推荐文章于 2025-04-16 21:47:09 发布