根据图的形状建立图的邻接矩阵,并进行初始化
建立代码如下
public class Graph{
//存储图中结点信息
private List<String> vertexList;
//存储途中结点与结点之间关系
private int[][] edges;
//存储边的数量
private int numOffEdges;
//存储结点是否访问过
private boolean[] booleanList;
//初始化图
public Graph(int n){
vertexList = new ArrayList<String>();
edges = new int[n][n];
numOffEdges = 0;
booleanList = new boolean[n];
}
//添加结点信息
/**
* node 当前结点
*/
public void insertNode(String node){
vertexList.add(node);
}
/**
* 插入结点与结点之间的关系
* m 代表横坐标
* n 代表纵坐标
*/
//由于是无向图,两边都需添加对应关系
public void insertRelationship(int m, int n){
edges[m][n] = 1;
edges[n][m] = 1;
numOffEdges++;
}
//查看结点与结点之间关系
public void show(){
for(int i = 0; i < edges.length; i++){
System.out.println(Arrays.toString(edges[i]));
}
}
}
图的深度优先遍历
/**
* 查看当前结点的下一节点
* v1 代表横坐标
*/
public int getNextNode(int v1, int v2){
for(int i = v2 +1; i < edges.length; i++){
if(edges[v1][i] > 0){
return i;
}
}
return -1;
}
//图的深度优先遍历
/**
*ver 存储访问过的结点信息
* i 当前结点的下标
*/
public void dfs(boolean[] ver, int i){
//输出当前结点
System.out.printf(vertexList.get(i) + "-->");
//将此节点置为已访问
ver[i] = true;
//访问下一个结点
int nextNode = getNextNode(i , -1);
while(nextNode != -1){
//判断该结点是否已经访问过
if(!ver[nextNode]){
//没有访问过递归调用输出当前结点
dfs(ver, nextNode);
}
//结点后移
nextNode = getNextNode(i, nextNode);
}
}
图的广度优先遍历
/**
* ver 标记访问过的结点
* i 当前结点
*/
public void bfs(boolean[] ver, int i){
//创建队列,保存该结点的后续结点
LinkedList<Integer> list = new LinkedList();
//判断结点是否访问过,没有访问过进行输出
System.out.printf(vertexList.get(i) + "-->");
ver[i] = true;
list.addLast(i);
while(!list.isEmpty()){
//查到该结点的所有后续结点
Integer node = list.removeFirst();
int nextNode = getNextNode(node, -1);
while(nextNode != -1){
if(!ver[nextNode]){
System.out.printf(vertexList.get(nextNode ) + "-->");
ver[nextNode] = true;
list.addLast(nextNode);
}
nextNode = getNextNode(node, nextNode);
}
}
}
主函数的调用
//主函数调用
public static void main(String[] args) {
Graph graph = new Graph(5);
graph.insertNode("A");
graph.insertNode("B");
graph.insertNode("C");
graph.insertNode("D");
graph.insertNode("E");
//插入节点与节点之间的关系
// A-B A-C B-C B-D B-E
graph.insertRelationship(0, 1, 1);
graph.insertRelationship(0, 2, 1);
graph.insertRelationship(1, 2, 1);
graph.insertRelationship(1, 3, 1);
graph.insertRelationship(1, 4, 1);
String[] str = {"A", "B", "C", "D", "E"};
System.out.println(Arrays.toString(str));
graph.show();
//图的深度优先遍历
graph.dfs();
//图的广度优先遍历
graph.bfs();
注意
在该代码中,如果使用深度优先遍历,就不能使用广度优先遍历,二者只能选其一,如果想同时使用,请修改部分代码,以下代码是可以同时进行两种遍历
**
//主函数调用
public static void main(String[] args) {
//图的结点个数
int n = 5;
Graph graph = new Graph(n);
graph.insertNode("A");
graph.insertNode("B");
graph.insertNode("C");
graph.insertNode("D");
graph.insertNode("E");
//插入节点与节点之间的关系
// A-B A-C B-C B-D B-E
graph.insertRelationship(0, 1);
graph.insertRelationship(0, 2);
graph.insertRelationship(1, 2);
graph.insertRelationship(1, 3);
graph.insertRelationship(1, 4);
String[] str = {"A", "B", "C", "D", "E"};
System.out.println(Arrays.toString(str));
graph.show();
graph.dfs(graph.booleanList, 0);
graph.booleanList = new boolean[n];
System.out.println();
graph.bfs(graph.booleanList, 0);
}