图的建立及两种优先搜索实现

利用邻接矩阵法建立一个简单的图,然后利用广度优先搜索(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 + '\t' + 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')); } }

 

转载于:https://www.cnblogs.com/lasclocker/p/4861814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值