-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphMatrix.java
More file actions
192 lines (162 loc) · 5.09 KB
/
GraphMatrix.java
File metadata and controls
192 lines (162 loc) · 5.09 KB
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
186
187
188
189
190
191
192
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
public class GraphMatrix<T> {
// Attributes
private T[] vertices;
private int[][] edges;
private int maxVertices;
private boolean isDirected;
private boolean isWeighted;
// Constructors
@SuppressWarnings("unchecked")
public GraphMatrix(int maxVertices, boolean isDirected, boolean isWeighted) {
this.maxVertices = maxVertices;
this.isDirected = isDirected;
this.isWeighted = isWeighted;
this.vertices = (T[]) new Object[maxVertices];
this.edges = new int[maxVertices][maxVertices];
}
// Methods
public int addVertex(T vertex) {
for (int i = 0; i < maxVertices; i++) {
if (vertices[i] == null) {
vertices[i] = vertex;
return i;
}
}
return -1;
}
public int getVertexIndex(T vertex) {
for (int i = 0; i < maxVertices; i++) {
if (vertices[i] == vertex) {
return i;
}
}
return -1;
}
public void addEdge(T source, T destination, int weight) {
int sourceIndex = getVertexIndex(source);
int destinationIndex = getVertexIndex(destination);
if (sourceIndex == -1) {
sourceIndex = addVertex(source);
}
if (destinationIndex == -1) {
destinationIndex = addVertex(destination);
}
if (sourceIndex != -1 && destinationIndex != -1) {
if (isWeighted) {
edges[sourceIndex][destinationIndex] = weight;
} else {
edges[sourceIndex][destinationIndex] = 1;
}
if (!isDirected) {
edges[destinationIndex][sourceIndex] = edges[sourceIndex][destinationIndex];
}
}
}
public void removeVertex(T vertex) {
int vertexIndex = getVertexIndex(vertex);
if (vertexIndex != -1) {
for (int i = 0; i < maxVertices; i++) {
edges[i][vertexIndex] = 0;
edges[vertexIndex][i] = 0;
}
vertices[vertexIndex] = null;
}
}
public int getVertexCount() {
int count = 0;
for (int i = 0; i < maxVertices; i++) {
if (vertices[i] != null) {
count++;
}
}
return count;
}
public int getEdgeCount() {
int count = 0;
for (int i = 0; i < maxVertices; i++) {
for (int j = 0; j < maxVertices; j++) {
if (edges[i][j] != 0) {
count++;
}
}
}
if (!isDirected) {
count /= 2;
}
return count;
}
public boolean hasVertex(T vertex) {
return getVertexIndex(vertex) != -1;
}
public boolean hasEdge(T source, T destination) {
int sourceIndex = getVertexIndex(source);
int destinationIndex = getVertexIndex(destination);
if (sourceIndex != -1 && destinationIndex != -1) {
return edges[sourceIndex][destinationIndex] != 0;
}
return false;
}
public int getWeight(T source, T destination) {
int sourceIndex = getVertexIndex(source);
int destinationIndex = getVertexIndex(destination);
if (sourceIndex != -1 && destinationIndex != -1) {
return edges[sourceIndex][destinationIndex];
}
return -1;
}
// Methods - Traversal
public List<T> getNeighbors(T vertex) {
List<T> neighbors = new ArrayList<>();
int vertexIndex = getVertexIndex(vertex);
if (vertexIndex != -1) {
for (int i = 0; i < maxVertices; i++) {
if (edges[vertexIndex][i] != 0) {
neighbors.add(vertices[i]);
}
}
}
return neighbors;
}
public List<T> depthFirstSearch(T start) {
List<T> visited = new ArrayList<>();
Stack<T> stack = new Stack<>();
stack.push(start);
while (!stack.isEmpty()) {
T vertex = stack.pop();
if (!visited.contains(vertex)) {
visited.add(vertex);
List<T> neighbors = getNeighbors(vertex);
for (T neighbor : neighbors) {
stack.push(neighbor);
}
}
}
return visited;
}
public List<T> breadthFirstSearch(T start) {
List<T> visited = new ArrayList<>();
Queue<T> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
T vertex = queue.poll();
if (!visited.contains(vertex)) {
visited.add(vertex);
List<T> neighbors = getNeighbors(vertex);
for (T neighbor : neighbors) {
queue.add(neighbor);
}
}
}
return visited;
}
@Override
public String toString() {
T start = vertices[0];
return breadthFirstSearch(start).toString();
}
}