这里用深度优先遍历存在矩阵里面的图。

  深度优先利用的是栈的FIFO特性。为此遍历到底后,可以找到最相邻的节点继续遍历。实现深度优先,还需要在节点加上一个访问标识,来确定该节点是否已经被访问过了。

源码:

package mygraph;

import java.util.Stack;

public class DFS_Vertex {
   //创建一个我们需要的节点类
class Vertex {
private char lable;
private int val;
private boolean wasvisited;
Vertex(char lable) {
this.lable = lable;
}
Vertex() { }
}
private char lable; // 矩阵元素
private Vertex[][] list = new Vertex[20][20];
private Vertex[] vertexList = new Vertex[20];
private int nVerts; // 当前顶点下标
DFS_Vertex() {
this.nVerts = 0;
for(int i = 0; i < 20; i ++) {
for(int j = 0; j < 20; j ++) {
list[i][j] = new Vertex();
}
}
} // 增加一个顶点
public void addVertex(char lable) {
vertexList[nVerts++] = new Vertex(lable);
} // 增加一条边
public void addEdge(int start, int end) {
list[start][end].val = 1;
list[end][start].val = 1;
} // 打印矩阵
public void printMatrix() {
for (int i = 0; i < nVerts; i++) {
for (int j = 0; j < nVerts; j++) {
System.out.print(list[i][j].val);
}
System.out.println();
}
}
//显示字符
public void showVertex(int v) {
System.out.print(vertexList[v].lable + "\t");
}
//获得邻接未访问节点
public int getAdjUnvisitedVertex(int v) {
for(int j = 0; j < nVerts; j ++) {
if((list[v][j].val == 1) && (vertexList[j].wasvisited == false)) {
return j;
}
}
return -1;
}
//DFS
public void DFS() {
Stack<Integer> s = new Stack();
vertexList[0].wasvisited = true;
showVertex(0);
s.push(0);
int v;
while(s.size() > 0) {
v = getAdjUnvisitedVertex(s.peek());
if(v == -1) {
s.pop();
}else {
vertexList[v].wasvisited = true;
showVertex(v);
s.push(v);
}
}
for(int j = 0; j < nVerts; j ++) {
vertexList[j].wasvisited = false;
}
} }

测试程序:

    public static void main(String[] args) {
DFS_Vertex ds = new DFS_Vertex();
ds.addVertex('A'); //
ds.addVertex('B'); //
ds.addVertex('C'); //2
ds.addVertex('D'); //
ds.addVertex('E'); //
ds.addEdge(0, 1); //A-B
ds.addEdge(0, 3); //A-D
ds.addEdge(1, 4); //B-E
ds.addEdge(3, 4); //D-E
ds.addEdge(4, 2); //E-C
ds.printMatrix();
ds.DFS();
}

测试结果:

10001
00001
10001
01110
A B E C D

最新文章

  1. C#开发微信门户及应用(42)--使用Autofac实现微信接口处理的控制反转处理
  2. 关于JS的编码转换问题
  3. 联不上网 Unable to initialize Windows Sockets interface. General failure.
  4. 关于Xcode的项目文件夹
  5. Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心
  6. Python3 IO
  7. UVA 465 (13.08.02)
  8. Bootstrap_Javascript_按钮插件
  9. Josephus2
  10. Java学习的随笔(一)对象概念、this指针、权限修饰符
  11. c# 学习笔记(二)
  12. ssh远程登录linux live系统
  13. Mediawiki.org的PHP编码约定
  14. UGUI Button控件
  15. ViewPager和View组合 实现页面的切换
  16. 黑马程序员:3分钟带你读懂C/C++学习路线
  17. centos ios镜像文件 安装详细
  18. Spring - BeanPostProcessor接口(后处理器)讲解
  19. Linux系列教程(十三)——Linux软件包管理之源码包、脚本安装包
  20. 剑指Offer——网易校招内推笔试题+模拟题知识点总结

热门文章

  1. go语言实现分布式id生成器
  2. OS UIButton之UIButtonType详解-转
  3. c#生成高清字体图片
  4. CLR、CIL、CTS、CLS、CLI、BCL和FCL,JIT,IL,GC
  5. 编译安装php服务报错问题:configure: error: Cannot find libmysqlclient under /usr.
  6. Oracle IMP-00010 不是有效的导出文件,标头验证失败 解决方法
  7. zabbix Server 4.0 监控TCP的12种状态
  8. C++(四十四) — 数组模板类(vector工具)
  9. 163data.com.cn data
  10. A Beginner’s Guide to Webpack 4 and Module Bundling