1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| package graph;
import java.util.LinkedList;
public class DirectedGraphs<T extends Comparable<T>> {
public static void main(String[] args) { Integer[] vertex = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[][] matrix = new int[][] { { 0, 3, MAX_WEIGHT, MAX_WEIGHT, 6, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT }, { 3, 0, 4, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT }, { MAX_WEIGHT, 4, 0, 7, MAX_WEIGHT, MAX_WEIGHT, 17, MAX_WEIGHT, MAX_WEIGHT }, { MAX_WEIGHT, MAX_WEIGHT, 7, 0, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT }, { 6, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, 0, 10, MAX_WEIGHT, 1, MAX_WEIGHT }, { MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, 10, 0, 12, 9, MAX_WEIGHT }, { MAX_WEIGHT, MAX_WEIGHT, 17, MAX_WEIGHT, MAX_WEIGHT, 12, 0, MAX_WEIGHT, 2 }, { MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, 1, 9, MAX_WEIGHT, 0, 14 }, { MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, MAX_WEIGHT, 2, MAX_WEIGHT, 14, 0 }, }; DirectedGraphs<Integer> graphs = new DirectedGraphs<>(vertex, matrix); graphs.broadFirstSearch(); graphs.depthFirstSearch(); }
public static final int MAX_WEIGHT = 100000; private T[] vertex; private int[][] matrix; private boolean[] isVisited;
public DirectedGraphs(T[] vertex, int[][] matrix) { this.vertex = vertex; this.matrix = matrix; }
private void initVisited() { this.isVisited = new boolean[this.vertex.length]; }
public void broadFirstSearch() { initVisited(); for (int i = 0; i < this.vertex.length; i++) { if (!isVisited[i]) { broadFirstSearch(i); } } initVisited(); }
public void depthFirstSearch() { initVisited(); for (int i = 0; i < this.vertex.length; i++) { if (!isVisited[i]) { System.out.println("位置: " + i + " 数据: " + this.vertex[i]); depthFirstSearch(i); } } initVisited(); }
private void broadFirstSearch(int index) { T u, w; LinkedList<T> queue = new LinkedList<>(); System.out.println("位置: " + index + " 数据: " + vertex[index]); isVisited[index] = true; queue.add(vertex[index]); while (!queue.isEmpty()) { u = queue.removeFirst(); w = getFirstNeighbor(u); while (w != null) { int i = getVertexIndex(w); if (!isVisited[i]) { System.out.println("位置: " + i + " 数据: " + vertex[i]); isVisited[i] = true; queue.add(w); } w = getNextNeighbor(u, w); } } }
private void depthFirstSearch(int index) { this.isVisited[index] = true; T t = getFirstNeighbor(this.vertex[index]); while (t != null) { int i = getVertexIndex(t); if (!isVisited[i]) { this.isVisited[i] = true; System.out.println("位置: " + i + " 数据: " + this.vertex[i]); depthFirstSearch(getVertexIndex(t)); } t = getNextNeighbor(this.vertex[index], t); } }
public T getFirstNeighbor(T vertex) { int index = getVertexIndex(vertex); if (index != -1) { int[] arr = this.matrix[index]; for (int i = 0; i < arr.length; i++) { if (arr[i] != 0 && arr[i] != MAX_WEIGHT) { return this.vertex[i]; } } } return null; }
public T getNextNeighbor(T vertex, T start) { int vertexIndex = getVertexIndex(vertex); if (vertexIndex != -1) { int startIndex = getVertexIndex(start); int[] arr = this.matrix[vertexIndex]; for (int i = startIndex + 1; i < arr.length; i++) { if (arr[i] != 0 && arr[i] != MAX_WEIGHT) { return this.vertex[i]; } } } return null; }
public int getVertexIndex(T vertex) { for (int i = 0; i < this.vertex.length; i++) { if (this.vertex[i].compareTo(vertex) == 0) { return i; } } return -1; }
}
|