-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdjacencyMatrixGraph.java
185 lines (157 loc) · 5.27 KB
/
AdjacencyMatrixGraph.java
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package dsa_dp.data_structures.graph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
public class AdjacencyMatrixGraph {
private final static int VERTICES_NUMBER = 100;
private final int[][] matrix;
public int numberOfEdges = 0;
/**
* Default constructor
*/
public AdjacencyMatrixGraph() {
this(VERTICES_NUMBER);
}
/**
* Constructor with a given number of vertices
*
* @param verticesNumber The starting vertices number of the Graph
*/
public AdjacencyMatrixGraph(int verticesNumber) {
matrix = new int[verticesNumber][verticesNumber];
}
/**
* Adds a new edge from the given 'from' vertex to the 'to' vertex
*
* @param from Starting vertex's edge
* @param to Ending vertex's edge
* @return false if the given vertices outbounds the matrix size, true otherwise
*/
public boolean addEdge(int from, int to) {
return this.addEdge(from, to, false);
}
/**
* Adds a new edge from the given 'from' vertex to the 'to' vertex, it also
* adds the reverse edge if the 'isUndirected' param is given true
*
* @param from Starting vertex's edge
* @param to Ending vertex's edge
* @param isUndirected Decides whether to add the reverse edge or not
* @return false if the given vertices outbounds the matrix size, true otherwise
*/
public boolean addEdge(int from, int to, boolean isUndirected) {
if (from > matrix.length || to > matrix.length || from == to)
return false;
if (matrix[from][to] == 0) {
numberOfEdges++;
matrix[from][to] = 1;
}
if (isUndirected && matrix[to][from] == 0) {
numberOfEdges++;
matrix[to][from] = 1;
}
return true;
}
/**
* Removes and existing edge from the graph
*
* @param from Starting vertex's edge
* @param to Ending vertex's edge
* @return false if the given edge does not exist, true otherwise
*/
public boolean removeEdge(int from, int to) {
if (matrix[from][to] > 0) {
numberOfEdges--;
matrix[from][to] = 0;
return true;
}
return false;
}
/**
* Updates the weight of an existing edge
*
* @param from Starting vertex's edge
* @param to Ending vertex's edge
* @param weight Edge's weight
* @return false if the given edge does not exist, true otherwise
*/
public boolean putEdgeWeight(int from, int to, int weight) {
if (matrix[from][to] > 0) {
matrix[from][to] = weight;
return true;
}
return false;
}
/**
* Performs a depth first search on the graph starting from the given vertex
*
* @param v Starting vertex
* @return List of connected vertices in order of visit
*/
public ArrayList<Integer> iterativeDfs(int v) {
if (v > matrix.length)
throw new IndexOutOfBoundsException();
ArrayList<Integer> res = new ArrayList<>(matrix.length);
boolean[] visited = new boolean[matrix.length];
Stack<Integer> stack = new Stack<>();
stack.push(v);
while (!stack.isEmpty()) {
v = stack.pop();
if (!visited[v]) {
visited[v] = true;
res.add(v);
for (int i = 0; i < matrix[v].length; i++) {
if (matrix[v][i] > 0 && !visited[i])
stack.add(i);
}
}
}
return res;
}
/**
* Performs a depth first search using recursion starting from the given vertex
*
* @param v Starting vertex
* @return List of connected vertices in order of visit
*/
public ArrayList<Integer> recursiveDfs(int v) {
if (v > matrix.length)
throw new IndexOutOfBoundsException();
ArrayList<Integer> res = new ArrayList<>(matrix.length);
boolean[] visited = new boolean[matrix.length];
depthFirstSearch(v, visited, res);
return res;
}
/**
* Prints the graph in a visually comprehensible format
*
* @return The string conversion of the graph
*/
public String print() {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] > 0) {
System.out.println(i + " ---> " + j + " weight: " + matrix[i][j]);
}
}
}
return Arrays.deepToString(matrix);
}
/**
* Internal method that performs the DFS in a recursive fashion
*
* @param v Starting vertex
* @param visited Array of booleans needed to check whether a vertex has already been visited or not
* @param res Array containing the resulting visited vertices in order of visit
*/
private void depthFirstSearch(int v, boolean[] visited, ArrayList<Integer> res) {
if (!visited[v]) {
visited[v] = true;
res.add(v);
for (int i = 0; i < matrix.length; i++) {
if (matrix[v][i] > 0 && !visited[i])
depthFirstSearch(i, visited, res);
}
}
}
}