DFS和BFS实现
DFS有递归实现和非递归实现,非递归实现就是用栈来模拟递归的过程,BFS类似二叉树的层序遍历,使用一个LILF的简单队列即可以实现
图的表示用邻接矩阵,DFS和BFS
package com.cn.jiang;
import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;
public class GraphDfsBfs {
private int total;
private String[] nodes;
private int[][] matrix;
public GraphDfsBfs(int total, String[] nodes){
this.total = total;
this.nodes = nodes;
this.matrix = new int[total][total];
}
private void printMatrix(){
System.out.println("---------------matrix----------------");
System.out.println("---0-1-2-3-4-5-6-7-8--");
System.out.println("---A-B-C-D-E-F-G-H-I--");
for(int i = 0; i < total; i++)
{
System.out.print(" "+nodes[i]+"|");
for(int j = 0; j < total; j++)
{
System.out.print(matrix[i][j]+"-");
}
System.out.print("\n");
}
System.out.println("---------------matrix----------------");
}
private void resetVisited(){
for(int i = 0; i < total; i++)
matrix[i][i] = 0;
}
//图的深度优先遍历方法(递归方法)
private void dfsRecursive(int i){
if(matrix[i][i] == 1)
return;
matrix[i][i] = 1;
System.out.print(nodes[i]);
for(int j = 0; j < total; j++)
{
if(i == j)
continue;
if(matrix[i][j] == 1)
dfsRecursive(j);
}
}
//图的深度优先遍历通过栈的方式实现
private void dfsStack(ArrayDeque<Integer> stack){
int k = -1;
while(!stack.isEmpty())
{
k = stack.peek().intValue();
boolean needpop = true;
for(int i = 0; i < total; i++)
{
if(matrix[k][i] == 1 && matrix[i][i] == 0)
{
stack.push(i);
matrix[i][i] = 1;
System.out.print(nodes[i]);
needpop = false;
break;
}
}
if(needpop)
stack.pop();
}
}
private void bfsQueue(Queue<Integer> ls){
if(ls == null || ls.size() == 0)
return;
int i = ls.poll().intValue();
if(matrix[i][i] == 1)
{
bfsQueue(ls);
return;
}
matrix[i][i] = 1;
System.out.print(""+nodes[i]);
for(int j = 0; j < total; j++)
{
if(i == j)
continue;
if(matrix[i][j] == 1 && matrix[j][j] != 1)
ls.offer(j);
}
bfsQueue(ls);
}
private void initGrf() {
// A-B, A-D, A-E
matrix[0][1] = 1;
matrix[1][0] = 1;
matrix[0][3] = 1;
matrix[3][0] = 1;
matrix[0][4] = 1;
matrix[4][0] = 1;
// B-C
matrix[1][2] = 1;
matrix[2][1] = 1;
// C-F
matrix[2][5] = 1;
matrix[5][2] = 1;
// D-E, D-G
matrix[3][4] = 1;
matrix[4][3] = 1;
matrix[3][6] = 1;
matrix[6][3] = 1;
// E-F, E-H
matrix[4][5] = 1;
matrix[5][4] = 1;
matrix[4][7] = 1;
matrix[7][4] = 1;
// F-H, F-I
matrix[5][7] = 1;
matrix[7][5] = 1;
matrix[5][8] = 1;
matrix[8][5] = 1;
// G-H
matrix[6][7] = 1;
matrix[7][6] = 1;
// H-I
matrix[7][8] = 1;
matrix[8][7] = 1;
}
public static void main(String[] args) {
String[] nodes = new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
GraphDfsBfs grf = new GraphDfsBfs(9, nodes);
grf.initGrf();
grf.printMatrix();
System.out.println("------ 深度优先遍历(递归)开始 ------");
grf.resetVisited();
grf.dfsRecursive(0);
System.out.println();
System.out.println("------ 深度优先遍历(递归)结束 ------");
System.out.println("------ 深度优先遍历(栈)开始 ------");
grf.resetVisited();
ArrayDeque<Integer> stack = new ArrayDeque<>();
stack.push(0);
grf.matrix[0][0] = 1;
System.out.print(grf.nodes[0]);
grf.dfsStack(stack);
System.out.println();
System.out.println("------ 深度优先遍历(栈)结束 ------");
System.out.println("------ 广度优先遍历开始 ------");
grf.resetVisited();
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(0);
grf.bfsQueue(queue);
System.out.println();
System.out.println("------ 广度优先遍历结束 ------");
}
}
该方法参考博客冰冻火山
图的表示用邻接表,DFS和BFS
package com.cn.jiang;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
//通过邻接表进行遍历操作
class VNode{
public int from;//顶点标号
public Edge first;
public VNode(int from){
this.from = from;
first = null;
}
}
class Edge{
public int to; //边的终点的标号
public Edge next;
public Edge(int to){
this.to = to;
next = null;
}
}
public class GraphDfsBfs1 {
public int k;
public VNode[] V;
public boolean[] visited;
public GraphDfsBfs1(int k, VNode[] v){
this.k = k;
V = v;
visited = new boolean[k];
}
private void bfs(int v){
Queue<Integer> queue = new LinkedList<>();
queue.add(v);
while(!queue.isEmpty())
{
int v1 = queue.poll();
if(!visited[v1])
{
System.out.print(v1+" ");
visited[v1] = true;
}
Edge edge = V[v1].first;
while(edge != null)
{
if(!visited[edge.to]) queue.add(edge.to);
edge = edge.next;
}
}
}
private void dfs(int v){
visited[v] = true;
System.out.print(v+" ");
Edge edge = V[v].first;
while(edge != null)
{
if(!visited[edge.to])
dfs(edge.to);
edge = edge.next;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
int m = scanner.nextInt();
VNode[] V = new VNode[k];
for(int i = 0; i < k; i++)
V[i] = new VNode(i);
Edge e = null; Edge e1 = null;
int u = 0, v = 0;
for(int i = 0; i < m; i++)
{
u = scanner.nextInt();
v = scanner.nextInt();
e = new Edge(v);
e.next = V[u].first; //将每条边表头插入
V[u].first = e;
//对于无向图做对称处理 无向图一条边相当于两条边
e1 = new Edge(u);
e1.next = V[v].first;
V[v].first = e1;
}
GraphDfsBfs1 graphDfsBfs1 = new GraphDfsBfs1(k, V);
graphDfsBfs1.dfs(0);
//graphDfsBfs1.bsf(0);
}
}