import java.util.LinkedList; public class Graph { private int vertexSize;//顶点的数量 private int[] vertexs;//顶点数组 private int[][] matrix;//矩阵 private boolean[] isVisited;//是否访问过 private static final int MAX_WEIGHT = 1000; public Graph(int vertexSize){ this.vertexSize = vertexSize; matrix = new int[vertexSize][vertexSize]; vertexs = new int[vertexSize]; for(int i = 0;i < vertexSize;i++){ vertexs[i] = i; } isVisited = new boolean[vertexSize]; } public int getVertexSize() { return vertexSize; } public void setVertexSize(int vertexSize) { this.vertexSize = vertexSize; } public int[] getVertexs() { return vertexs; } public void setVertexs(int[] vertexs) { this.vertexs = vertexs; } public int[][] getMatrix() { return matrix; } public void setMatrix(int[][] matrix) { this.matrix = matrix; } //获取某个顶点的出度 public int getOutDegree(int index){ int[] arr = matrix[index]; int degree = 0; for(int i = 0;i < arr.length;i++){ if(arr[i] !=0 && arr[i] < MAX_WEIGHT){ degree++; } } return degree; } //获取两个顶点之间的权值 public int getWeight(int v1,int v2){ return matrix[v1][v2] == 0 ? 0 : (matrix[v1][v2] == MAX_WEIGHT ? -1 : matrix[v1][v2]); } //深度优先遍历 private void depthFirstSearch(int i){ isVisited[i] = true; int w = getFirstNeighor(i); while(w != -1){ if (!isVisited[w]){//需要遍历该顶点 System.out.println("访问到了 " + w + "顶点"); depthFirstSearch(w); } w = getNextNeighor(i,w);//第一个相对于w的邻接点 } } //对外提供深度优先遍历 public void depthFirstSearch(){ isVisited = new boolean[vertexSize]; for(int i = 0;i < vertexSize;i++){ if(!isVisited[i]){ System.out.println("访问到了 " + i + "顶点"); depthFirstSearch(i); } } isVisited = new boolean[vertexSize]; } //广度优先遍历(使用到了队列) public void broadFirstSearch(){ isVisited = new boolean[vertexSize]; for(int i = 0;i < vertexSize;i++){ if(!isVisited[i]){ broadFirstSearch(i); } } isVisited = new boolean[vertexSize]; } private void broadFirstSearch(int i){ int u;//获取队列的头结点 int w;//获取第一个邻接点 LinkedList<Integer> queue = new LinkedList<>(); System.out.println("访问到了 " + i + "结点"); isVisited[i] = true; queue.add(i); while(!queue.isEmpty()){ u = (Integer)(queue.removeFirst()).intValue(); w = getFirstNeighor(u); while(w != -1){ if(!isVisited[w]) { System.out.println("访问到了 " + w + "结点"); isVisited[w] = true; queue.add(w); } w = getNextNeighor(u,w); } } } //获取某个结点的第一个邻接点 public int getFirstNeighor(int index){ for(int i = 0;i < vertexSize;i++){ if(matrix[index][i] > 0 && matrix[index][i] < MAX_WEIGHT){ return i; } } return -1; } //根据前一个邻接点的下标来获取下一个邻接点(就是根据相对于第一个邻接点获取下一个邻接点) /* * @param v1表示要找的顶点 * @param v2表示该顶点相对于哪个邻接点去获取下一个邻接点 * **/ public int getNextNeighor(int v1,int v2){ for(int i = v2 + 1;i < vertexSize;i++){ if(matrix[v1][i] > 0 && matrix[v1][i] < MAX_WEIGHT){ return i; } } return -1; } //普里母算法 public void prim(){ int[] lowcost = new int[vertexSize]; //最小代价顶点权值的数组,为0表示已经获取到了最小的权值 int[] adjvex = new int[vertexSize]; //放顶点权值 int min = 0;//最小值 int minId = 0;//最小的下标 int sum = 0; //总和 for(int i = 1;i < vertexSize;i++){ lowcost[i] = matrix[0][i]; } for(int i = 1;i < vertexSize;i++){ min = MAX_WEIGHT; minId = 0; for(int j = 1;j < vertexSize;j++){ if(lowcost[j] < min && lowcost[j] > 0){ min = lowcost[j]; minId = j; } } // for(int a = 0;a < vertexSize;a++){ // System.out.print(adjvex[a] + " "); // } System.out.println("顶点为:" + adjvex[minId] + ",权值为:" + min + "最小值的下标为:" + minId); sum += min; lowcost[minId] = 0; for(int j = 1;j < vertexSize;j++){ if(lowcost[j] != 0 && matrix[minId][j] < lowcost[j]){ lowcost[j] = matrix[minId][j]; adjvex[j] = minId; } } } System.out.println("最小生成树的权值和为:" + sum); } public static void main(String[] args){ Graph graph = new Graph(9); int[] a1 = new int[]{0,10,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,11,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT}; int[] a2 = new int[]{10,0,18,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,16,MAX_WEIGHT,12}; int[] a3 = new int[]{MAX_WEIGHT,MAX_WEIGHT,0,22,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,8}; int[] a4 = new int[]{MAX_WEIGHT,MAX_WEIGHT,22,0,20,MAX_WEIGHT,24,16,21}; int[] a5 = new int[]{MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,20,0,26,MAX_WEIGHT,7,MAX_WEIGHT}; int[] a6 = new int[]{11,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,26,0,17,MAX_WEIGHT,MAX_WEIGHT}; int[] a7 = new int[]{MAX_WEIGHT,16,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,17,0,19,MAX_WEIGHT}; int[] a8 = new int[]{MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,16,7,MAX_WEIGHT,19,0,MAX_WEIGHT}; int[] a9 = new int[]{MAX_WEIGHT,12,8,21,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,0}; graph.matrix[0] = a1; graph.matrix[1] = a2; graph.matrix[2] = a3; graph.matrix[3] = a4; graph.matrix[4] = a5; graph.matrix[5] = a6; graph.matrix[6] = a7; graph.matrix[7] = a8; graph.matrix[8] = a9; // System.out.println("出度为:" + graph.getOutDegree(4)); // System.out.println("权值:" + graph.getWeight(0,4)); } }
————————————————
版权声明:本文为CSDN博主「StrongHYQ」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/StrongHYQ/article/details/81324928