-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightedGraph.java
270 lines (241 loc) · 6 KB
/
WeightedGraph.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//----------------------------------------------------------------------------
// WeightedGraph.java by Dale/Joyce/Weems Chapter 9
//
// Implements a directed graph with weighted edges.
// Vertices are objects of class T and can be marked as having been visited.
// Edge weights are integers.
// Equivalence of vertices is determined by the vertices' equals method.
//
// General precondition: Except for the addVertex and hasVertex methods,
// any vertex passed as an argument to a method is in this graph.
//----------------------------------------------------------------------------
import Queue.*;
public class WeightedGraph<T> implements WeightedGraphInterface<T>
{
public static final int NULL_EDGE = 0;
private static final int DEFCAP = 500; // default capacity
private int numVertices;
private int maxVertices;
private T[] vertices;
private int[][] edges;
private boolean[] marks; // marks[i] is mark for vertices[i]
public WeightedGraph()
// Instantiates a graph with capacity DEFCAP vertices.
{
numVertices = 0;
maxVertices = DEFCAP;
vertices = (T[]) new Object[DEFCAP];
marks = new boolean[DEFCAP];
edges = new int[DEFCAP][DEFCAP];
}
public WeightedGraph(int maxV)
// Instantiates a graph with capacity maxV.
{
numVertices = 0;
maxVertices = maxV;
vertices = (T[]) new Object[maxV];
marks = new boolean[maxV];
edges = new int[maxV][maxV];
}
public boolean isEmpty()
// Returns true if this graph is empty; otherwise, returns false.
{
if(numVertices == 0){
return true;
}
else{
return false;
}
}
public boolean isFull()
// Returns true if this graph is full; otherwise, returns false.
{
if(numVertices >= maxVertices || numVertices >= DEFCAP){
return true;
}
else{
return false;
}
}
public void addVertex(T vertex)
// Preconditions: This graph is not full.
// Vertex is not already in this graph.
// Vertex is not null.
//
// Adds vertex to this graph.
{
vertices[numVertices] = vertex;
for (int index = 0; index < numVertices; index++)
{
edges[numVertices][index] = NULL_EDGE;
edges[index][numVertices] = NULL_EDGE;
}
numVertices++;
}
public void addVertex(T vertex, int index) {
if(isFull()){
throw new RuntimeException();
} else{
this.vertices[index] = vertex;
} this.numVertices ++;
}
public void removeVertex(int index) {
for(int i = index; i < this.numVertices; i++) {
this.vertices[index] = this.vertices[index + 1];
}
this.vertices[this.numVertices] = null;
this.numVertices--;
}
public void removeVertex(T vertex) {
boolean sigueCambiando = false;
for(int i = 0; i < this.numVertices; i++) {
if(this.vertices[i].equals(vertex)) {
sigueCambiando = true;
}
if(sigueCambiando) {
this.vertices[i] = this.vertices[i + 1];
}
}
this.vertices[this.numVertices] = null;
this.numVertices--;
}
public boolean hasVertex(T vertex)
// Returns true if this graph contains vertex; otherwise, returns false.
{
int index = 0;
while(vertices[index] != vertex){
index++;
}
if(vertices[index] == vertex){
return true;
}
else{
return false;
}
}
public int indexIs(T vertex)
// Returns the index of vertex in vertices.
{
int index = 0;
while (!vertex.equals(vertices[index]))
index++;
return index;
}
public void addEdge(T fromVertex, T toVertex, int weight)
// Adds an edge with the specified weight from fromVertex to toVertex.
{
int row;
int column;
row = indexIs(fromVertex);
column = indexIs(toVertex);
edges[row][column] = weight;
}
public void removeEdge(T fromVertex, T toVertex)
{
int row;
int column;
row = indexIs(fromVertex);
column = indexIs(toVertex);
edges[row][column] = 0;
}
public int weightIs(T fromVertex, T toVertex)
// If edge from fromVertex to toVertex exists, returns the weight of edge;
// otherwise, returns a special “null-edge” value.
{
int row;
int column;
row = indexIs(fromVertex);
column = indexIs(toVertex);
return edges[row][column];
}
public UnboundedQueueInterface<T> getToVertices(T vertex)
// Returns a queue of the vertices that are adjacent from vertex.
{
UnboundedQueueInterface<T> adjVertices = new LinkedUnbndQueue<T>();
int fromIndex;
int toIndex;
fromIndex = indexIs(vertex);
for (toIndex = 0; toIndex < numVertices; toIndex++)
if (edges[fromIndex][toIndex] != NULL_EDGE)
adjVertices.enqueue(vertices[toIndex]);
return adjVertices;
}
public void clearMarks()
// Sets marks for all vertices to false.
{
for(int i = 0; i < maxVertices; i++){
marks[i] = false;
}
}
public void markVertex(T vertex)
// Sets mark for vertex to true.
{
for(int i = 0; i < numVertices; i++){
if(vertices[i] == vertex){
marks[i] = true;
break;
}
}
}
public boolean isMarked(T vertex)
// Returns true if vertex is marked; otherwise, returns false.
{
int i = 0;
for(i = 0; i < numVertices; i++){
if(vertices[i] == vertex){
break;
}
}
if(marks[i] == true){
return true;
}
else{
return false;
}
}
public T getUnmarked()
// Returns an unmarked vertex if any exist; otherwise, returns null.
{
boolean found = false;
int index = 0;
while ((index < numVertices) && !found)
{
if (!marks[index])
found = true;
else
index++;
}
if (found)
return vertices[index];
else
return null;
}
public boolean breadthPath(T startVertex, T endVertex) {
UnboundedQueueInterface<T> queue = new LinkedUnbndQueue<>();
UnboundedQueueInterface<T> vertexQueue = new LinkedUnbndQueue<>();
boolean found = false;
T vertex;
T element;
clearMarks();
queue.enqueue(startVertex);
do{
vertex = (T) queue.dequeue();
if(vertex==endVertex){
found=true;
}else{
if(!isMarked(vertex)){
markVertex(vertex);
vertexQueue = getToVertices(vertex);
while(!vertexQueue.isEmpty()){
element=(T) vertexQueue.dequeue();
if(!isMarked(element)){
queue.enqueue(element);
}
}
}
}
}
while(!queue.isEmpty()&&!found);
return found;
}
}