package tu;
public class Graph {
// 存储节点信息
private Object[] vertices;
// 存储边的信息
private int[][] arcs;
// 图的顶点个数
private int vexnum;
// 记录地i个节点是否被访问过
private boolean[] visited;
// 构造方法
public Graph(int n) {
// 顶点个数
vexnum = n;
// 初始化顶点和边
vertices = new Object[n];
arcs = new int[n][n];
visited = new boolean[n];
for (int i = 0; i < vexnum; i++) {
for (int j = 0; j < vexnum; j++) {
arcs[i][j] = 0;
}
}
}
// 初始化节点数组
public void addVertex(Object[] obj) {
this.vertices = obj;
}
// 添加边的方法
public void addEdge(int i, int j) {
if (i == j)
return;
arcs[i][j] = 1;
arcs[j][i] = 1;
}
// 得到一个初始顶点位置
public int firstAdjVex(int i) {
for (int j = 0; j < vexnum; j++) {
if (arcs[i][j] > 0)
return j;
}
return -1;
}
// 下一个临界点
public int nextAdjVex(int i, int k) {
for (int j = k + 1; j < vexnum; j++) {
if (arcs[i][j] > 0)
return j;
}
return -1;
}
// 显示访问的节点
public void visit(int i) {
System.out.print(vertices[i] + " ");
}
// 一个连通图的深度递归遍历
public void traverse(int i) {
visited[i] = true;
visit(i);
for (int j = this.firstAdjVex(i); j >= 0; j = this.nextAdjVex(i, j)) {
if (!visited[j])
this.traverse(j);
}
}
// 深度优先遍历
public void depthTraverse() {
for (int i = 0; i < vexnum; i++) {
visited[i] = false;
}
for (int i = 0; i < vexnum; i++) {
if (!visited[i])
traverse(i);
}
}
public static void main(String[] args) {
Graph g = new Graph(8);
Character[] vertices = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
g.addVertex(vertices);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(3, 7);
g.addEdge(4, 7);
g.addEdge(2, 5);
g.addEdge(2, 6);
System.out.println("深度优先遍历:");
g.depthTraverse();
System.out.println();
}
}
public class Graph {
// 存储节点信息
private Object[] vertices;
// 存储边的信息
private int[][] arcs;
// 图的顶点个数
private int vexnum;
// 记录地i个节点是否被访问过
private boolean[] visited;
// 构造方法
public Graph(int n) {
// 顶点个数
vexnum = n;
// 初始化顶点和边
vertices = new Object[n];
arcs = new int[n][n];
visited = new boolean[n];
for (int i = 0; i < vexnum; i++) {
for (int j = 0; j < vexnum; j++) {
arcs[i][j] = 0;
}
}
}
// 初始化节点数组
public void addVertex(Object[] obj) {
this.vertices = obj;
}
// 添加边的方法
public void addEdge(int i, int j) {
if (i == j)
return;
arcs[i][j] = 1;
arcs[j][i] = 1;
}
// 得到一个初始顶点位置
public int firstAdjVex(int i) {
for (int j = 0; j < vexnum; j++) {
if (arcs[i][j] > 0)
return j;
}
return -1;
}
// 下一个临界点
public int nextAdjVex(int i, int k) {
for (int j = k + 1; j < vexnum; j++) {
if (arcs[i][j] > 0)
return j;
}
return -1;
}
// 显示访问的节点
public void visit(int i) {
System.out.print(vertices[i] + " ");
}
// 一个连通图的深度递归遍历
public void traverse(int i) {
visited[i] = true;
visit(i);
for (int j = this.firstAdjVex(i); j >= 0; j = this.nextAdjVex(i, j)) {
if (!visited[j])
this.traverse(j);
}
}
// 深度优先遍历
public void depthTraverse() {
for (int i = 0; i < vexnum; i++) {
visited[i] = false;
}
for (int i = 0; i < vexnum; i++) {
if (!visited[i])
traverse(i);
}
}
public static void main(String[] args) {
Graph g = new Graph(8);
Character[] vertices = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
g.addVertex(vertices);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(3, 7);
g.addEdge(4, 7);
g.addEdge(2, 5);
g.addEdge(2, 6);
System.out.println("深度优先遍历:");
g.depthTraverse();
System.out.println();
}
}