44import java .util .Collection ;
55import java .util .List ;
66import java .util .Set ;
7- import java .util .concurrent .CopyOnWriteArrayList ;
87
98/**
109 * Graph. Could be directed or undirected depending on the TYPE enum. A graph is
1716 */
1817public class Graph <T extends Comparable <T >> {
1918
20- private List <Vertex <T >> verticies = new CopyOnWriteArrayList <Vertex <T >>();
21- private List <Edge <T >> edges = new CopyOnWriteArrayList <Edge <T >>();
19+ private List <Vertex <T >> verticies = new ArrayList <Vertex <T >>();
20+ private List <Edge <T >> edges = new ArrayList <Edge <T >>();
2221
2322 public enum TYPE {
2423 DIRECTED , UNDIRECTED
25- };
24+ }
2625
2726 private TYPE type = TYPE .UNDIRECTED ;
2827
2928 public Graph () {
3029 }
3130
31+ public Graph (TYPE type ) {
32+ this ();
33+ this .type = type ;
34+ }
35+
3236 public Graph (Graph <T > g ) {
3337 // Deep copies
3438
39+ type = g .getType ();
40+
3541 // Copy the vertices (which copies the edges)
36- for (Vertex <T > v : g .getVerticies ()) {
42+ for (Vertex <T > v : g .getVerticies ())
3743 this .verticies .add (new Vertex <T >(v ));
38- }
3944
4045 // Update the object references
4146 for (Vertex <T > v : this .verticies ) {
@@ -49,13 +54,6 @@ public Graph(Graph<T> g) {
4954 this .edges .add (e );
5055 }
5156 }
52-
53- type = g .getType ();
54- }
55-
56- public Graph (TYPE type ) {
57- this ();
58- this .type = type ;
5957 }
6058
6159 public Graph (Collection <Vertex <T >> verticies , Collection <Edge <T >> edges ) {
@@ -129,9 +127,8 @@ public Vertex(T value, int weight) {
129127 public Vertex (Vertex <T > vertex ) {
130128 this (vertex .value , vertex .weight );
131129 this .edges = new ArrayList <Edge <T >>();
132- for (Edge <T > e : vertex .edges ) {
130+ for (Edge <T > e : vertex .edges )
133131 this .edges .add (new Edge <T >(e ));
134- }
135132 }
136133
137134 public T getValue () {
@@ -166,10 +163,9 @@ public boolean pathTo(Vertex<T> v) {
166163 * {@inheritDoc}
167164 */
168165 @ Override
169- public int compareTo (Vertex <T > v ) {
170- if (this .value == null || v .value == null )
171- return -1 ;
172- return this .value .compareTo (v .value );
166+ public int hashCode () {
167+ int code = this .value .hashCode () + this .weight ;
168+ return 31 * code ;
173169 }
174170
175171 /**
@@ -194,6 +190,16 @@ public boolean equals(Object v1) {
194190 return true ;
195191 }
196192
193+ /**
194+ * {@inheritDoc}
195+ */
196+ @ Override
197+ public int compareTo (Vertex <T > v ) {
198+ if (this .value == null || v .value == null )
199+ return -1 ;
200+ return this .value .compareTo (v .value );
201+ }
202+
197203 /**
198204 * {@inheritDoc}
199205 */
@@ -232,7 +238,6 @@ public int getCost() {
232238
233239 public void setCost (int cost ) {
234240 this .cost = cost ;
235- ;
236241 }
237242
238243 public Vertex <T > getFromVertex () {
@@ -243,24 +248,12 @@ public Vertex<T> getToVertex() {
243248 return to ;
244249 }
245250
246- /**
247- * {@inheritDoc}
248- */
249- @ Override
250- public int compareTo (Edge <T > e ) {
251- if (this .cost < e .cost )
252- return -1 ;
253- if (this .cost > e .cost )
254- return 1 ;
255- return 0 ;
256- }
257-
258251 /**
259252 * {@inheritDoc}
260253 */
261254 @ Override
262255 public int hashCode () {
263- return this .cost * (this .getFromVertex ().value .hashCode () * this .getToVertex ().value .hashCode ());
256+ return 31 * ( this .cost * (this .getFromVertex ().value .hashCode () * this .getToVertex ().value .hashCode () ));
264257 }
265258
266259 /**
@@ -289,14 +282,26 @@ public boolean equals(Object e1) {
289282 return true ;
290283 }
291284
285+ /**
286+ * {@inheritDoc}
287+ */
288+ @ Override
289+ public int compareTo (Edge <T > e ) {
290+ if (this .cost < e .cost )
291+ return -1 ;
292+ if (this .cost > e .cost )
293+ return 1 ;
294+ return 0 ;
295+ }
296+
292297 /**
293298 * {@inheritDoc}
294299 */
295300 @ Override
296301 public String toString () {
297302 StringBuilder builder = new StringBuilder ();
298303 builder .append ("edge:" ).append (" [" ).append (from .value ).append ("]" ).append (" -> " ).append ("[" )
299- .append (to .value ).append ("]" ).append (" = " ).append (cost ).append ("\n " );
304+ .append (to .value ).append ("]" ).append (" = " ).append (cost ).append ("\n " );
300305 return builder .toString ();
301306 }
302307 }
@@ -326,6 +331,33 @@ public Vertex<T> getVertex() {
326331 return vertex ;
327332 }
328333
334+ /**
335+ * {@inheritDoc}
336+ */
337+ @ Override
338+ public int hashCode () {
339+ return 31 * (this .cost * this .vertex .hashCode ());
340+ }
341+
342+ /**
343+ * {@inheritDoc}
344+ */
345+ @ SuppressWarnings ("rawtypes" )
346+ @ Override
347+ public boolean equals (Object e1 ) {
348+ if (!(e1 instanceof CostVertexPair ))
349+ return false ;
350+
351+ CostVertexPair pair = (CostVertexPair )e1 ;
352+ if (this .cost != pair .cost )
353+ return false ;
354+
355+ if (!this .vertex .equals (pair ))
356+ return false ;
357+
358+ return true ;
359+ }
360+
329361 /**
330362 * {@inheritDoc}
331363 */
@@ -389,4 +421,4 @@ public String toString() {
389421 return builder .toString ();
390422 }
391423 }
392- }
424+ }
0 commit comments