-
Notifications
You must be signed in to change notification settings - Fork 2
/
AdjListsGraph.java
343 lines (286 loc) · 9.29 KB
/
AdjListsGraph.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* AdjListsGraph.java
* AUTHORS: Lily Orth-Smith and Emma Shumadine (for lab 8)
* MODIFIED BY: Lily Orth-Smith for Assignment 7 (mostly just added comments)
* This class defines a graph by implementing vectors defining its vertices and arcs.
* The user can add or remove vertices and arcs. The class can also print the graph
* to a file using tgf format.
*
*/
import java.util.*;
import java.io.*;
public class AdjListsGraph<T> implements Graph<T> {
private Vector<T> vertices;
private Vector<LinkedList<Integer>> arcs;
/**
* Constructor
*
*/
public AdjListsGraph() {
vertices = new Vector<T>(10,10);
arcs = new Vector<LinkedList<Integer>>(10, 10);
}
/**
* Creates a TGF file representing the graph
* @params fileName name of TGF file to be created
*
*/
public void saveToTGF(String fileName) {
try {
File f = new File(fileName);
PrintWriter write = new PrintWriter(f);
//add vertices
for(int i=0; i<vertices.size(); i++) {
write.println("" + (i+1) + " " + vertices.get(i));
}
write.println("#");
//add edges
for(int i=0; i<arcs.size(); i++) {
for(int j=0; j<arcs.get(i).size(); j++) {
write.println("" + (i+1) + " " + (j+1));
}
}
write.close();
} catch (IOException e) {
System.out.println(e);
}
}
/**
* Tells you whether the graph is empty
* @returns isEmpty true if is empty, else false
*
*/
public boolean isEmpty() {
return vertices.size() == 0;
}
/**
* Gets the number of vertices
* @returns numVertices number of vertices in the graph
*/
public int getNumVertices() {
return vertices.size();
}
/**
* Gets the number of arcs
* @returns numArcs number of arcs in the graph
*/
public int getNumArcs() {
int count = 0;
for (int i = 0; i < arcs.size(); i++) {
/*for (int j = 0; j < arcs.get(i).size(); j++) {
count++;
}*/
count += arcs.get(i).size();
}
return count;
}
/**
* Adds a vertex to the graph
* @param v value of the vertex you want to add
*/
public void addVertex(T v) {
vertices.add(v);
arcs.add(new LinkedList<Integer>());
}
/**
* Removes a vertex from the graph
* @param v value of the vertex you want to remove
*/
public void removeVertex(T v) {
arcs.remove(vertices.indexOf(v));
for(int i=0; i<arcs.size(); i++) {
for(int j=0; j<arcs.get(i).size(); j++) {
if (arcs.get(i).get(j) == vertices.indexOf(v)) {
arcs.get(i).remove(j);
}
}
}
vertices.remove(v); //what if v is not in vertices?
}
/**
* Removes an arc from a graph
* @param v1 starting vertex of arc to remove
* @param v2 ending vertex of arc to remove
*/
public void removeArc(T v1, T v2) {
if (isArc(v1, v2)) {
arcs.get(vertices.indexOf(v1)).remove((Integer)(vertices.indexOf(v2)));
}
}
/**
* Adds an arc to graph
* @param v1 starting vertex of arc to add
* @param v2 ending vertex of arc to add
*/
public void addArc(T v1, T v2) {
if (vertices.contains(v1) && vertices.contains(v2)) {
LinkedList<Integer> v1List = arcs.get(vertices.indexOf(v1));
if(v1List.indexOf(vertices.indexOf(v2)) == -1) { //if v2 is not in v1's arc list
arcs.get(vertices.indexOf(v1)).add(vertices.indexOf(v2));
}
/*
LinkedList<Integer> v1List = arcs.get(vertices.indexOf(v1));
System.out.println(arcs.get(vertices.indexOf(v1)));
System.out.println(v1List);
//DOESNT WORK
System.out.println(v1List.indexOf(vertices.indexOf(v2)) + 1); //index of element
System.out.println(v2);
if (v1List.indexOf(v2) == -1) { //if arcs doesn't contain v2
arcs.get(vertices.indexOf(v1)).add(vertices.indexOf(v2));
}
*/
}
}
/**
* Gets a new vertex
* @returns vertex at index
*/
public T getVertex(int index) {
return vertices.get(index);
}
/**
* Determines whether there is an edge between two vertices (edge is two way arc)
* @param v1 starting vertex of edge to check
* @param v2 ending vertex of dedge to check
*/
public boolean isEdge(T v1, T v2) {
return isArc(v1, v2) && isArc(v2, v1);
}
/**
* Determines whether there is an arc between two vertices
* @param v1 starting vertex of arc to check
* @param v2 ending vertex of arc to check
*/
public boolean isArc(T v1, T v2) {
return arcs.get(vertices.indexOf(v1)).contains(vertices.indexOf(v2));
}
/**
* Determines whether the graph is undirected
*@returns isUndirected true if undirected, else false
*/
public boolean isUndirected() {
for (int i = 0; i < vertices.size(); i++) {
for (int j = i + 1; j < vertices.size(); j++) {
if (isArc(vertices.get(i), vertices.get(j)) && !(isEdge(vertices.get(i), vertices.get(j)))) {
return false;
}
}
}
return true;
}
/**
* Adds an edge to the graph
* @param v1 starting vertex of edge to add
* @param v2 ending vertex of edge to add
*/
public void addEdge(T v1, T v2) {
addArc(v1, v2);
addArc(v2, v1);
}
/**
* Removes an edge from the graph
* @param v1 starting vertex of edge to remove
* @param v2 ending vertex of edge to remove
*/
public void removeEdge(T v1, T v2) {
if (isEdge(v1, v2)) {
removeArc(v1, v2);
removeArc(v2, v1);
}
}
/**
* Gets successors of a vertex
* @param v vertex whose successors you are getting
* @returns successors a linked list containing all the predecessors of vertex v
*/
public LinkedList<T> getSuccessors(T v) {
LinkedList<T> successors = new LinkedList<T>();
LinkedList<Integer> arcList = arcs.get(vertices.indexOf(v));
for (int i = 0; i < arcList.size(); i++) {
successors.add(vertices.get(arcList.get(i)));
}
return successors;
}
/**
* Gets predecessors of a vertex
* @param v vertex whose successors you are getting
* @returns successors a linked list containing all the predecessors of vertex v
*/
public LinkedList<T> getPredecessors(T v) {
int vIndex = vertices.indexOf(v);
LinkedList<T> predecessors = new LinkedList<T>();
for(int i=0; i<arcs.size(); i++) {
if (arcs.get(i).contains(vIndex)) {
predecessors.add(vertices.get(i));
}
}
return predecessors;
}
/**
* Returns string representation of this graph
* @return toString string representation of the graph
*/
public String toString() {
String s = "Vertices:\n" + vertices + "\n\nArcs:";
for (int i = 0; i<arcs.size(); i++) {
s += "\nfrom " + vertices.get(i) + ": ";
Vector<T> storage = new Vector<T>(arcs.get(i).size());
for (int j=0; j<arcs.get(i).size(); j++) {
storage.add(vertices.get(arcs.get(i).get(j)));
}
s += storage.toString();
}
return s;
}
/*
* Main
* tests the AdjListsGraph class
*/
public static void main(String[] args) {
/* The testing code below was for Assignment 7 -- it doesn't work here because the person class isn't included in the game.
* However, AdjListsGraph has been thouroghly tested, and does work.
*/
/*
AdjListsGraph<Person> hi = new AdjListsGraph<Person>();
Person p1 = new Person("bob", "1");
Person p2 = new Person("sally", "2");
Person p3 = new Person("dorothea", "3");
//System.out.println(hi);
//System.out.println(hi.isEmpty());
hi.addVertex(p1);
hi.addVertex(p2);
hi.addVertex(p3);
System.out.println("Adding arcs and edges: ");
System.out.println("Empty graph: \n" + hi);
System.out.println();
hi.addEdge(p1, p2);
hi.addEdge(p2, p3);
System.out.println("After adding 2 edges: \n" + hi);
System.out.println("Is hi undirected? (should be true): " + hi.isUndirected());
System.out.println();
hi.addArc(p1, p3);
System.out.println("After adding an arc: \n" + hi);
System.out.println("Is hi undirected? (should be false): " + hi.isUndirected());
System.out.println();
hi.addArc(p1, p3);
System.out.println("After adding an arc already in the graph: \n" + hi);
System.out.println("Testing successors and predecessors: \n");
System.out.println("Predecessors of p1: " + hi.getPredecessors(p1));
System.out.println("Successors of p1: " + hi.getSuccessors(p1));
System.out.println();
hi.saveToTGF("hiToTGF.tgf");
System.out.println("Hi was saved to tgf file at this point");
System.out.println("Removing edges and arcs: \n");
hi.removeEdge(p1, p2);
System.out.println("After removing an edge: \n" + hi);
System.out.println();
hi.removeArc(p1, p3);
System.out.println("After removing an arc: \n" + hi);
System.out.println();
System.out.println("Num vertices: " + hi.getNumVertices());
System.out.println("Num arcs: " + hi.getNumArcs());
System.out.println("Is arc? p1->p2 (false): " + hi.isArc(p1, p2));
System.out.println("Is arc? p2->p3 (true): " + hi.isArc(p2, p3));
System.out.println("Is edge? p2->p2 (true): " + hi.isArc(p2, p3));
*/
}
}