利用邻接矩阵法建立一个简单的图,然后利用广度优先搜索(BFS)和深度优先搜索(DFS)测试代码,并实现了深度优先搜索的非递归形式。需要注意的是,由于每次测试前都要初始化图,故每种方法只能单独测试。
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
class GraphVertex { //图的顶点。
char vertex;
boolean isVisited;
public GraphVertex(char vt) {
vertex = vt; isVisited = false;
}
@Override
public boolean equals(Object obj) {
return this.vertex == ((GraphVertex)obj).vertex;
}
@Override
public int hashCode() {
return (this.vertex - '0');
}
@Override
public String toString() {
return "" + vertex + ' ' + isVisited;
}
}
class Graph {
int vertexNum;
GraphVertex[] gVertex; //顶点
int[][] adjMat; //邻接矩阵
public Graph(int vertexNum) {
this.vertexNum = vertexNum;
gVertex = new GraphVertex[vertexNum];
adjMat = new int[vertexNum][vertexNum]; //全部元素初始化为0
}
public void addVertexs(GraphVertex[] nodes) { //添加顶点
System.arraycopy(nodes, 0, gVertex, 0, nodes.length);
}
public void addEdge(GraphVertex ch1, GraphVertex ch2) { //构造邻接矩阵
int index1 = getIndex(ch1);
int index2 = getIndex(ch2);
adjMat[index1][index2] = 1;
adjMat[index2][index1] = 1;
}
public void BFS(GraphVertex ch) { //广度优先搜索
Queue<GraphVertex> queue = new LinkedList<GraphVertex>();
int index = getIndex(ch);
GraphVertex gv = gVertex[index];
System.out.print(gv.vertex);
gv.isVisited = true;
queue.offer(gv);
while (!queue.isEmpty()) {
GraphVertex tmp = queue.poll();
int i = getIndex(tmp);
for (int j = 0; j < adjMat[i].length; j++) {
if(adjMat[i][j] == 1 && gVertex[j].isVisited == false) {
System.out.println(gVertex[j].vertex);
gVertex[j].isVisited = true;
queue.offer(gVertex[j]);
}
}
}
}
public void DFS(GraphVertex ch) { // 深度优先搜索(递归实现)
int index = getIndex(ch);
GraphVertex gv = gVertex[index];
System.out.println(gv.vertex);
gv.isVisited = true;
for (int j = 0; j < adjMat[index].length; j++) {
if(adjMat[index][j] == 1 && gVertex[j].isVisited == false) {
DFS(gVertex[j]);
}
}
}
public void NonDFS(GraphVertex ch) { //深度优先搜索(非递归实现)
Stack<GraphVertex> stack = new Stack<GraphVertex>();
int index = getIndex(ch);
GraphVertex gv = gVertex[index];
System.out.println(gv.vertex);
gv.isVisited = true;
stack.push(gv);
while (!stack.isEmpty()) {
GraphVertex tmp = stack.peek();
int nextIndex = nextNotVisitVertex(tmp);
if(nextIndex == -1)
stack.pop();
else {
GraphVertex vtex = gVertex[nextIndex];
System.out.println(vtex.vertex);
vtex.isVisited = true;
stack.push(vtex);
}
}
}
public int nextNotVisitVertex(GraphVertex tmp) {
int index = getIndex(tmp);
for (int j = 0; j < adjMat[index].length; j++) {
if(adjMat[index][j] == 1 && gVertex[j].isVisited == false) {
return j;
}
}
return -1;
}
public int getIndex(GraphVertex gv) {
for (int i = 0; i < gVertex.length; i++) {
if(gv.equals(gVertex[i]))
return i;
}
return Integer.MAX_VALUE;
}
}
public class TestClass { //测试类
public static void addEdges(Graph graph, String[] edges) {
for (int i = 0; i < edges.length; i++) {
char ch1 = edges[i].charAt(0);
char ch2 = edges[i].charAt(1);
graph.addEdge(new GraphVertex(ch1), new GraphVertex((ch2)));
}
}
public static void main(String[] args) {
char[] vertexs = {'A', 'B', 'C', 'D', 'E'};
String[] edges = {"AB", "AE", "BC", "BD", "BE", "CD", "DE"};
Graph graph = new Graph(vertexs.length);
GraphVertex[] nodes = new GraphVertex[vertexs.length];
for (int i = 0; i < nodes.length; i++) {
nodes[i] = new GraphVertex(vertexs[i]);
}
graph.addVertexs(nodes);
addEdges(graph, edges);
// graph.BFS(new GraphVertex('A'));
// graph.DFS(new GraphVertex('A'));
graph.NonDFS(new GraphVertex('A'));
}
}