备注:遍历顶点合集时,使用原生的方法可能存在下标错位的情况,使用时用迭代器遍历即可。
package exercise.data.structure.graph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 无向图
* @Author quanfa.liu
* @Date 2021年12月11日 上午9:13:08
* @Version 1.0
*/
public class Graph {
private List<String> vertexList;// 顶点
int edge[][] = null;// 边,0
int sumedges;// 边的总数,默认为0
int sparseMatrix[][] = null; // 稀疏数组
/**
* 构造器
* @param sumOfVertex 顶点总数
*/
Graph(int sumOfVertex) {
vertexList = new ArrayList<String>();
edge = new int[sumOfVertex][sumOfVertex];
}
/**
* 加入顶点
* @author quanfa.liu
* @date 2021年12月11日 上午9:16:29
* @return: void
*/
public void insertVertex(String vertex) {
vertexList.add(vertex);
}
/**
* 插入边
* @author quanfa.liu
* @date 2021年12月11日 上午9:20:29
* @param x 矩阵横坐标
* @param y 矩阵纵坐标
* @param weight 权重,这里用0,1标识 0标识没有通路 1标识存在通路
* @return: void
*/
public void insertEdge(int x, int y, int weight) {
//如果从 x,y坐标存在通路,则无向图中 y,x坐标一定存在通路
edge[x][y] = weight;
edge[y][x] = weight;
sumedges++;
}
/**
* 打印矩阵
* @author quanfa.liu
* @date 2021年12月11日 上午9:20:31
* @return: void
*/
public void printMatrix() {
for (int i[] : edge) {
System.out.println(Arrays.toString(i));
}
}
/**
* 删除顶点
* @author quanfa.liu
* @date 2021年12月11日 上午9:36:54
* @param vertex 顶点
* @return: void
*/
public void delateVertex(String vertex) {
if (!vertexList.contains(vertex)) {
System.err.println("删除的顶点不存在");
}
// 找待删除节点索引index -- 该索引对应矩阵的index行列
int index = vertexList.indexOf(vertex);
// 初始化稀疏数组
sparseMatrix = new int[edge.length - 1][3];
// 待删除的边的个数
int delEdgecount = 0;
// 稀疏数组行数
int x = 0;
String ij = "";
for (int i = 0, len = edge.length; i < len; i++) {
for (int j = 0; j < len; j++) {
if (index == i || j == index) {
if (1 == edge[i][j]) {
delEdgecount++;
}
continue;
} else {
if (edge[i][j] == 1) {
// 判断字数数组中的值,如果edge[i][j]已经存进去,则edge[j][i]可以不存
if (ij.contains(j + "" + i + ",")) {
continue;
}
if (i > index) {
sparseMatrix[x][0] = i - 1;
} else {
sparseMatrix[x][0] = i;
}
if (j > index) {
sparseMatrix[x][1] = j - 1;
} else {
sparseMatrix[x][1] = j;
}
sparseMatrix[x][2] = edge[i][j];
ij += i + "" + j + ",";
x++;
}
}
}
}
sumedges = sumedges - delEdgecount / 2;
vertexList.remove(vertex);
/**
* 稀疏矩阵转化为边矩阵
*/
edge = new int[sparseMatrix.length][sparseMatrix.length];
for (int i = 0, len = sparseMatrix.length; i < len; i++) {
for (int j = 0; j < len; j++) {
if (i > index) {
if (j > index) {
edge[sparseMatrix[i][0]][sparseMatrix[i][1]] = sparseMatrix[i][2];
edge[sparseMatrix[i][1]][sparseMatrix[i][0]] = sparseMatrix[i][2];
} else {
edge[sparseMatrix[i][0]][sparseMatrix[i][1]] = sparseMatrix[i][2];
edge[sparseMatrix[i][1]][sparseMatrix[i][0]] = sparseMatrix[i][2];
}
} else {
if (j > index) {
edge[sparseMatrix[i][0]][sparseMatrix[i][1]] = sparseMatrix[i][2];
edge[sparseMatrix[i][1]][sparseMatrix[i][0]] = sparseMatrix[i][2];
} else {
edge[sparseMatrix[i][1]][sparseMatrix[i][0]] = sparseMatrix[i][2];
edge[sparseMatrix[i][0]][sparseMatrix[i][1]] = sparseMatrix[i][2];
}
}
}
}
}
public static void main(String[] args) {
Graph graph = new Graph(5);
// 插入顶点
graph.insertVertex("A");
graph.insertVertex("B");
graph.insertVertex("C");
graph.insertVertex("D");
graph.insertVertex("E");
// 插入边
graph.insertEdge(0, 1, 1);
graph.insertEdge(0, 2, 1);
graph.insertEdge(0, 3, 1);
graph.insertEdge(1, 4, 1);
graph.insertEdge(2, 4, 1);
graph.insertEdge(3, 4, 1);
System.out.println("原邻接矩阵");
graph.printMatrix();
System.out.println("删除某节点的邻接矩阵");
graph.delateVertex("C");
graph.printMatrix();
System.out.println("删除后的边数:" + graph.sumedges);
}
}