图的遍历分两种一种是深度 是纵向的,另外 一种是广度 是横向的。
main方法测试及深度和广度方法
public class Graph {
private ArrayList<String> vertexList;//存储顶点集合
private int[][] edges;//存储图对应的邻结矩阵
private int numOfEdges;//表示边的数目
//定义给数组boolean[],记录某个节点是否被访问
private boolean[] isVisited;
public static void main(String[] args) {
//测试一把图是否创建ok
int n = 5;//节点的个数
String VertexValue[] = {
"A", "B", "C", "D", "E"};
//创建图对象
Graph graph = new Graph(n);
//循环的添加顶点
for (String value : VertexValue) {
graph.insertVertex(value);
}
//添加边
graph.insertEdge(0, 1, 1);
graph.insertEdge(0, 2, 1);
graph.insertEdge(1, 2, 1);
graph.insertEdge(1, 3, 1);
graph.insertEdge(1, 4, 1);
//显示邻结
graph.showGraph();
//深度遍历dfs
System.out.println("深度遍历");
graph.dfs();
System.out.println();
System.out.println("广度优先");
graph.bfs();
}
public Graph