|
1 | 1 | package com.examplehub.datastructures.graph; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | | -import java.util.List; |
5 | | -import java.util.Stack; |
6 | | -import java.util.StringJoiner; |
| 3 | +import java.util.*; |
7 | 4 |
|
8 | 5 | public class AdjacencyMatrix<E> { |
9 | 6 | private final E[] vertex; |
10 | 7 | private final int[][] adj; |
11 | | - private final boolean[] visited; |
12 | 8 | int numOfVertex; |
13 | 9 |
|
14 | 10 | public AdjacencyMatrix(E[] vertex) { |
15 | 11 | this.vertex = vertex; |
16 | 12 | this.numOfVertex = vertex.length; |
17 | 13 | this.adj = new int[numOfVertex][numOfVertex]; |
18 | | - this.visited = new boolean[numOfVertex]; |
19 | 14 | } |
20 | 15 |
|
21 | 16 | public AdjacencyMatrix(E[] vertex, int[][] adj) { |
22 | 17 | this.vertex = vertex; |
23 | 18 | this.adj = adj; |
24 | 19 | this.numOfVertex = vertex.length; |
25 | | - this.visited = new boolean[numOfVertex]; |
26 | 20 | } |
27 | 21 |
|
28 | 22 | public void addEdge(int i, int j) { |
29 | 23 | adj[i][j] = adj[j][i] = 1; |
30 | 24 | } |
31 | 25 |
|
32 | | - public void deepFirstSearch(StringJoiner joiner, int start) { |
| 26 | + public void removeEdge(int i, int j) { |
| 27 | + adj[i][j] = adj[j][i] = 0; |
| 28 | + } |
| 29 | + |
| 30 | + public void deepFirstSearch(StringJoiner joiner, int start, boolean[] visited) { |
33 | 31 | joiner.add(vertex[start].toString()); |
34 | 32 | visited[start] = true; |
35 | 33 | for (int i = 0; i < numOfVertex; ++i) { |
36 | 34 | if (adj[start][i] == 1 && !visited[i]) { |
37 | | - deepFirstSearch(joiner, i); |
| 35 | + deepFirstSearch(joiner, i, visited); |
38 | 36 | } |
39 | 37 | } |
40 | 38 | } |
41 | 39 |
|
42 | | - public int getVertexDegree(E v) { |
43 | | - int row = -1; |
44 | | - for (int i = 0; i < numOfVertex; i++) { |
| 40 | + private int getVertexIndex(E v) { |
| 41 | + for (int i = 0; i < numOfVertex; ++i) { |
45 | 42 | if (vertex[i].equals(v)) { |
46 | | - row = i; |
| 43 | + return i; |
47 | 44 | } |
48 | 45 | } |
| 46 | + return -1; |
| 47 | + } |
| 48 | + |
| 49 | + public int getVertexDegree(E v) { |
| 50 | + int index = getVertexIndex(v); |
49 | 51 | int sum = 0; |
50 | | - for (int i = 0; i < numOfVertex; ++i) { |
51 | | - sum += adj[row][i]; |
| 52 | + for (int j = 0; j < numOfVertex; ++j) { |
| 53 | + sum += adj[index][j]; |
52 | 54 | } |
53 | 55 | return sum; |
54 | 56 | } |
55 | 57 |
|
56 | 58 | public String dfsPath() { |
57 | 59 | StringJoiner joiner = new StringJoiner("->"); |
58 | | - deepFirstSearch(joiner, 0); |
| 60 | + boolean[] visited = new boolean[numOfVertex]; |
| 61 | + deepFirstSearch(joiner, 0, visited); |
59 | 62 | return joiner.toString(); |
60 | 63 | } |
61 | 64 |
|
62 | | - List<String> findAllPath(E startVertex, E endVertex) { |
63 | | - int start = -1; |
64 | | - int end = -1; |
65 | | - for (int i = 0; i < numOfVertex; ++i) { |
66 | | - if (startVertex == vertex[i]) { |
67 | | - start = i; |
68 | | - } |
69 | | - if (endVertex == vertex[i]) { |
70 | | - end = i; |
| 65 | + public String bfsPath() { |
| 66 | + StringJoiner joiner = new StringJoiner("->"); |
| 67 | + boolean visited[] = new boolean[numOfVertex]; |
| 68 | + breadthFirstSearch(joiner, 0); |
| 69 | + return joiner.toString(); |
| 70 | + } |
| 71 | + |
| 72 | + private void breadthFirstSearch(StringJoiner joiner, int start) { |
| 73 | + boolean[] visited = new boolean[numOfVertex]; |
| 74 | + Queue<Integer> queue = new LinkedList<>(); |
| 75 | + queue.offer(start); |
| 76 | + visited[start] = true; |
| 77 | + while (!queue.isEmpty()) { |
| 78 | + int popIndex = queue.poll(); |
| 79 | + joiner.add(vertex[popIndex].toString()); |
| 80 | + for (int j = 0; j < numOfVertex; ++j) { |
| 81 | + if (adj[popIndex][j] == 1 && !visited[j]) { |
| 82 | + queue.offer(j); |
| 83 | + visited[j] = true; |
| 84 | + } |
71 | 85 | } |
72 | 86 | } |
| 87 | + } |
| 88 | + |
| 89 | + List<String> findAllPath(E startVertex, E endVertex) { |
| 90 | + int start = getVertexIndex(startVertex); |
| 91 | + int end = getVertexIndex(endVertex); |
73 | 92 | return (start == -1 || end == -1) ? null : findAllPath(start, end); |
74 | 93 | } |
75 | 94 |
|
|
0 commit comments