1 public interface Graph<V> {
2
3 public int getSize();
4
5 public List<V> getVertices();
6
7 public V getVertex(int index);
8
9 public int getIndex(V v);
10
11 public List<Integer> getNeighbors(int index);
12
13 public int getDegree(int v);
14
15 public int[][] getAdjacencyMatrix();
16
17 public void printAdjacencyMatrix();
18
19 public void printEdges();
20
21 public AbstractGraph<V>.Tree dfs(int v);
22
23 public AbstractGraph<V>.Tree bfs(int v);
24
25 }
1 /**
2 *
3 */
4 package datastructure;
5
6 import java.util.*;
7
8
9 /**
10 * <p>Description:</p>
11 *
12 * @author Administrator
13 * @date 2019年1月14日上午11:36:55
14 * @version 1.0
15 */
16 public abstract class AbstractGraph<V> implements Graph<V> {
17
18 protected List<V> vertices;
19 protected List<List<Integer>> neighbors;
20
21 /**
22 *
23 * @param edges
24 * @param vertices
25 */
26 protected AbstractGraph(int[][] edges, V[] vertices){
27 this.vertices = new ArrayList<V>();
28 for (int i = 0; i < vertices.length; i++) {
29 this.vertices.add(vertices[i]);
30 }
31
32 createAdjacencyLists(edges, vertices.length);
33 }
34
35 /**
36 *
37 * @param edges
38 * @param vertices
39 */
40 protected AbstractGraph(List<Edge> edges, List<V> vertices) {
41 this.vertices = vertices;
42 createAdjacencyLists(edges, vertices.size());
43 }
44
45 /**
46 *
47 * @param edges
48 * @param numberOfVertices
49 */
50 protected AbstractGraph(List<Edge> edges, int numberOfVertices) {
51 vertices = new ArrayList<V>();
52 for (int i = 0; i < numberOfVertices; i++) {
53 vertices.add((V)(new Integer(i))); // vertices is {0,1,...}
54 }
55
56 createAdjacencyLists(edges, numberOfVertices);
57 }
58
59
60 /**
61 *
62 * @param edges
63 * @param numberOfVertices
64 */
65 protected AbstractGraph(int[][] edges, int numberOfVertices) {
66 vertices = new ArrayList<V>();
67 for (int i = 0; i < numberOfVertices; i++) {
68 vertices.add((V)(new Integer(i)));
69 }
70
71 createAdjacencyLists(edges, numberOfVertices);
72 }
73
74
75 /**
76 * create adjacency lists fore each vertex
77 * @param edges
78 * @param numberOfVertices
79 */
80 private void createAdjacencyLists(int[][] edges, int numberOfVertices) {
81 neighbors = new ArrayList<List<Integer>>();
82 for (int i = 0; i < numberOfVertices; i++) {
83 neighbors.add(new ArrayList<Integer>());
84 }
85
86 for (int i = 0; i < edges.length; i++) {
87 int u = edges[i][0];
88 int v = edges[i][1];
89 neighbors.get(u).add(v);
90 }
91 }
92
93 private void createAdjacencyLists(List<Edge> edges, int numberOfVertices) {
94 neighbors = new ArrayList<List<Integer>>();
95 for (int i = 0; i < numberOfVertices; i++) {
96 neighbors.add(new ArrayList<Integer>());
97 }
98
99 for (Edge edge : edges) {
100 neighbors.get(edge.u).add(edge.v);
101 }
102 }
103
104
105 public int getSize() {
106 return vertices.size();
107 }
108
109 public List<V> getVertices() {
110 return vertices;
111 }
112
113 public V getVertex(int index) {
114 return vertices.get(index);
115 }
116
117 public int getIndex(V v) {
118 return vertices.indexOf(v);
119 }
120
121 public List<Integer> getNeighbors(int index) {
122 return neighbors.get(index);
123 }
124
125 public int getDegree(int v) {
126 return neighbors.get(v).size();
127 }
128
129 public int[][] getAdjacencyMatrix() {
130 int[][] adjacencyMatrix = new int[getSize()][getSize()];
131 for (int i = 0; i < neighbors.size(); i++) {
132 for (int j = 0; j < neighbors.get(i).size(); j++) {
133 int v = neighbors.get(i).get(j);
134 adjacencyMatrix[i][v] = 1;
135 }
136 }
137
138 return adjacencyMatrix;
139 }
140
141 public void printAdjacencyMatrix() {
142 int[][] adjacencyMatrix = getAdjacencyMatrix();
143 for (int i = 0; i < adjacencyMatrix.length; i++) {
144 for (int j = 0; j < adjacencyMatrix[0].length; j++) {
145 System.out.print(adjacencyMatrix[i][j] + " ");
146 }
147
148 System.out.println();
149
150 }
151 }
152
153 public void printEdges() {
154 for (int u = 0; u < neighbors.size(); u++) {
155 System.out.print("Vertex" + u + ":");
156 for (int j = 0; j < neighbors.get(u).size(); j++) {
157 System.out.print("(" + u + "." + neighbors.get(u).get(j) + ")");
158 }
159
160 System.out.println();
161 }
162 }
163
164 public static class Edge {
165 public int u;
166 public int v;
167
168 public Edge(int u, int v) {
169 this.u = u;
170 this.v = v;
171 }
172 }
173
174 public Tree dfs(int v) {
175 List<Integer> searchOrders = new ArrayList<Integer>();
176 int[] parent = new int[vertices.size()];
177
178 for (int i = 0; i < parent.length; i++) {
179 parent[i] = -1;
180 }
181
182 boolean[] isVisited = new boolean[vertices.size()];
183
184 dfs(v, parent, searchOrders, isVisited);
185
186 return new Tree(v, parent, searchOrders);
187 }
188
189 private void dfs(int v, int[] parent, List<Integer> searchOrders, boolean[] isVisited) {
190 searchOrders.add(v);
191 isVisited[v] = true;
192 for (int i : neighbors.get(v)) {
193 if(!isVisited[i]) {
194 parent[i] = v;
195 dfs(i, parent, searchOrders, isVisited);
196 }
197 }
198 }
199
200
201 public Tree bfs(int v) {
202 List<Integer> searchOrders = new ArrayList<Integer>();
203 int[] parent = new int[vertices.size()];
204
205 for (int i = 0; i < parent.length; i++) {
206 parent[i] = -1;
207 }
208
209 LinkedList<Integer> queue = new LinkedList<Integer>();
210 boolean[] isVisted = new boolean[vertices.size()];
211
212 queue.offer(v);
213 isVisted[v] = true;
214
215 while(!queue.isEmpty()) {
216 int u = queue.poll();
217 searchOrders.add(u);
218 for (int w : neighbors.get(u)) {
219 if(!isVisted[w]) {
220 queue.offer(w);
221 parent[w] = u;
222 isVisted[w] = true;
223 }
224 }
225 }
226
227 return new Tree(v, parent, searchOrders);
228
229 }
230
231 public class Tree {
232 private int root;
233 private int[] parent;
234 private List<Integer> searchOrders;
235
236 public Tree(int root, int[] parent, List<Integer> searchOrders) {
237 this.root = root;
238 this.parent = parent;
239 this.searchOrders = searchOrders;
240 }
241
242 public Tree(int root, int[] parent) {
243 this.root = root;
244 this.parent = parent;
245 }
246
247 public int getRoot() {
248 return root;
249 }
250
251 public int getParent(int v) {
252 return parent[v];
253 }
254
255 public List<Integer> getSearchOrders() {
256 return searchOrders;
257 }
258
259 public int getNumberOfVerticesFound() {
260 return searchOrders.size();
261 }
262
263
264 public List<V> getPath(int index) {
265 ArrayList<V> path = new ArrayList<V>();
266 do {
267 path.add(vertices.get(index));
268 index = parent[index];
269 }while(index != -1);
270
271 return path;
272 }
273
274 public void printPath(int index) {
275 List<V> path = getPath(index);
276 System.out.print("A path from " + vertices.get(root) + " to " + vertices.get(index) + ": ");
277 for(int i = path.size()-1 ; i >= 0; i--){
278 System.out.print(path.get(i) + " ");
279 }
280 }
281
282 public void printTree() {
283 System.out.println("Root is:" +vertices.get(root));
284 System.out.print("Edges: ");
285 for (int i = 0; i < parent.length; i++) {
286 if(parent[i] != -1) {
287 System.out.print("(" + vertices.get(parent[i]) + "," + vertices.get(i) + ")");
288 }
289 }
290 System.out.println();
291 }
292
293 }
294
295
296
297 }
1 /**
2 *
3 */
4 package datastructure;
5
6 import java.util.List;
7
8 /**
9 * <p>Description:</p>
10 *
11 * @author Administrator
12 * @date 2019年1月14日下午7:40:01
13 * @version 1.0
14 */
15 public class UnweightedGraph<V> extends AbstractGraph<V> {
16 public UnweightedGraph(int[][] edges, V[] vertices) {
17 super(edges, vertices);
18 }
19
20 public UnweightedGraph(List<Edge> edges, List<V> vertices) {
21 super(edges, vertices);
22 }
23
24 public UnweightedGraph(List<Edge> edges, int numberOfVertices) {
25 super(edges, numberOfVertices);
26 }
27
28 public UnweightedGraph(int[][] edges, int numberOfVertices) {
29 super(edges, numberOfVertices);
30 }
31 }