22
33import java .util .ArrayList ;
44import java .util .Collection ;
5+ import java .util .Iterator ;
56import java .util .List ;
6- import java .util .Set ;
77
88/**
99 * Graph. Could be directed or undirected depending on the TYPE enum. A graph is
@@ -26,11 +26,9 @@ public enum TYPE {
2626
2727 private TYPE type = TYPE .UNDIRECTED ;
2828
29- public Graph () {
30- }
29+ public Graph () { }
3130
3231 public Graph (TYPE type ) {
33- this ();
3432 this .type = type ;
3533 }
3634
@@ -45,42 +43,32 @@ public Graph(Graph<T> g) {
4543
4644 // Update the object references
4745 for (Vertex <T > v : this .verticies ) {
48- for (Edge <T > e : v .getEdges ()) {
49- Vertex <T > fromVertex = e .getFromVertex ();
50- Vertex <T > toVertex = e .getToVertex ();
51- int indexOfFrom = this .verticies .indexOf (fromVertex );
52- e .from = this .verticies .get (indexOfFrom );
53- int indexOfTo = this .verticies .indexOf (toVertex );
54- e .to = this .verticies .get (indexOfTo );
46+ for (Edge <T > e : v .getEdges ())
5547 this .edges .add (e );
56- }
5748 }
5849 }
5950
60- public Graph (Collection <Vertex <T >> verticies , Collection <Edge <T >> edges ) {
51+ public Graph (Collection <Vertex <T >> verticies , List <Edge <T >> edges ) {
6152 this (TYPE .UNDIRECTED , verticies , edges );
6253 }
6354
64- public Graph (TYPE type , Collection <Vertex <T >> verticies , Collection <Edge <T >> edges ) {
55+ public Graph (TYPE type , Collection <Vertex <T >> verticies , List <Edge <T >> edges ) {
6556 this (type );
57+
6658 this .verticies .addAll (verticies );
6759 this .edges .addAll (edges );
6860
6961 for (Edge <T > e : edges ) {
70- Vertex <T > from = e .from ;
71- Vertex <T > to = e .to ;
62+ final Vertex <T > from = e .from ;
63+ final Vertex <T > to = e .to ;
7264
7365 if (!this .verticies .contains (from ) || !this .verticies .contains (to ))
7466 continue ;
7567
76- int index = this .verticies .indexOf (from );
77- Vertex <T > fromVertex = this .verticies .get (index );
78- index = this .verticies .indexOf (to );
79- Vertex <T > toVertex = this .verticies .get (index );
80- fromVertex .addEdge (e );
68+ from .addEdge (e );
8169 if (this .type == TYPE .UNDIRECTED ) {
82- Edge <T > reciprical = new Edge <T >(e .cost , toVertex , fromVertex );
83- toVertex .addEdge (reciprical );
70+ Edge <T > reciprical = new Edge <T >(e .cost , to , from );
71+ to .addEdge (reciprical );
8472 this .edges .add (reciprical );
8573 }
8674 }
@@ -103,10 +91,9 @@ public List<Edge<T>> getEdges() {
10391 */
10492 @ Override
10593 public String toString () {
106- StringBuilder builder = new StringBuilder ();
107- for (Vertex <T > v : verticies ) {
94+ final StringBuilder builder = new StringBuilder ();
95+ for (Vertex <T > v : verticies )
10896 builder .append (v .toString ());
109- }
11097 return builder .toString ();
11198 }
11299
@@ -125,8 +112,10 @@ public Vertex(T value, int weight) {
125112 this .weight = weight ;
126113 }
127114
115+ /** Deep copies edges **/
128116 public Vertex (Vertex <T > vertex ) {
129117 this (vertex .value , vertex .weight );
118+
130119 this .edges = new ArrayList <Edge <T >>();
131120 for (Edge <T > e : vertex .edges )
132121 this .edges .add (new Edge <T >(e ));
@@ -165,7 +154,7 @@ public boolean pathTo(Vertex<T> v) {
165154 */
166155 @ Override
167156 public int hashCode () {
168- int code = this .value .hashCode () + this .weight ;
157+ final int code = this .value .hashCode () + this .weight + this . edges . size () ;
169158 return 31 * code ;
170159 }
171160
@@ -177,14 +166,18 @@ public boolean equals(Object v1) {
177166 if (!(v1 instanceof Vertex ))
178167 return false ;
179168
180- Vertex <T > v = (Vertex <T >) v1 ;
169+ final Vertex <T > v = (Vertex <T >) v1 ;
181170
182- boolean valuesEquals = this .value . equals ( v . value ) ;
183- if (!valuesEquals )
171+ final boolean weightEquals = this .weight == v . weight ;
172+ if (!weightEquals )
184173 return false ;
185174
186- boolean weightEquals = this .weight == v .weight ;
187- if (!weightEquals )
175+ final boolean edgesSizeEquals = this .edges .size () == v .edges .size ();
176+ if (!edgesSizeEquals )
177+ return false ;
178+
179+ final boolean valuesEquals = this .value .equals (v .value );
180+ if (!valuesEquals )
188181 return false ;
189182
190183 return true ;
@@ -205,11 +198,10 @@ public int compareTo(Vertex<T> v) {
205198 */
206199 @ Override
207200 public String toString () {
208- StringBuilder builder = new StringBuilder ();
209- builder .append ("vertex:" ). append ( " value =" ).append (value ).append (" weight=" ).append (weight ).append ("\n " );
210- for (Edge <T > e : edges ) {
201+ final StringBuilder builder = new StringBuilder ();
202+ builder .append ("Value =" ).append (value ).append (" weight=" ).append (weight ).append ("\n " );
203+ for (Edge <T > e : edges )
211204 builder .append ("\t " ).append (e .toString ());
212- }
213205 return builder .toString ();
214206 }
215207 }
@@ -223,6 +215,7 @@ public static class Edge<T extends Comparable<T>> implements Comparable<Edge<T>>
223215 public Edge (int cost , Vertex <T > from , Vertex <T > to ) {
224216 if (from == null || to == null )
225217 throw (new NullPointerException ("Both 'to' and 'from' Verticies need to be non-NULL." ));
218+
226219 this .cost = cost ;
227220 this .from = from ;
228221 this .to = to ;
@@ -253,7 +246,8 @@ public Vertex<T> getToVertex() {
253246 */
254247 @ Override
255248 public int hashCode () {
256- return 31 * (this .cost * (this .getFromVertex ().value .hashCode () * this .getToVertex ().value .hashCode ()));
249+ final int cost = (this .cost * (this .getFromVertex ().hashCode () * this .getToVertex ().hashCode ()));
250+ return 31 * cost ;
257251 }
258252
259253 /**
@@ -264,17 +258,17 @@ public boolean equals(Object e1) {
264258 if (!(e1 instanceof Edge ))
265259 return false ;
266260
267- Edge <T > e = (Edge <T >) e1 ;
261+ final Edge <T > e = (Edge <T >) e1 ;
268262
269- boolean costs = this .cost == e .cost ;
263+ final boolean costs = this .cost == e .cost ;
270264 if (!costs )
271265 return false ;
272266
273- boolean froms = this .from .equals (e .from );
267+ final boolean froms = this .from .equals (e .from );
274268 if (!froms )
275269 return false ;
276270
277- boolean tos = this .to .equals (e .to );
271+ final boolean tos = this .to .equals (e .to );
278272 if (!tos )
279273 return false ;
280274
@@ -299,8 +293,8 @@ public int compareTo(Edge<T> e) {
299293 @ Override
300294 public String toString () {
301295 StringBuilder builder = new StringBuilder ();
302- builder .append ("edge: " ).append (" [ " ).append (from .value ).append ("] " ).append (" -> " ).append ("[ " )
303- .append (to .value ).append ("]" ).append (" = " ).append (cost ).append ("\n " );
296+ builder .append ("[ " ).append (from . value ). append ( "( " ).append (from .weight ).append (") " ).append ("] " ).append (" -> " )
297+ .append ("[ " ). append ( to .value ). append ( "(" ). append ( to . weight ). append ( ") " ).append ("]" ).append (" = " ).append (cost ).append ("\n " );
304298 return builder .toString ();
305299 }
306300 }
@@ -335,7 +329,7 @@ public Vertex<T> getVertex() {
335329 */
336330 @ Override
337331 public int hashCode () {
338- return 31 * (this .cost * this .vertex . hashCode ());
332+ return 31 * (this .cost * (( this .vertex != null )? this . vertex . hashCode (): 1 ));
339333 }
340334
341335 /**
@@ -346,11 +340,11 @@ public boolean equals(Object e1) {
346340 if (!(e1 instanceof CostVertexPair ))
347341 return false ;
348342
349- CostVertexPair <?> pair = (CostVertexPair <?>)e1 ;
343+ final CostVertexPair <?> pair = (CostVertexPair <?>)e1 ;
350344 if (this .cost != pair .cost )
351345 return false ;
352346
353- if (!this .vertex .equals (pair ))
347+ if (!this .vertex .equals (pair . vertex ))
354348 return false ;
355349
356350 return true ;
@@ -363,6 +357,7 @@ public boolean equals(Object e1) {
363357 public int compareTo (CostVertexPair <T > p ) {
364358 if (p == null )
365359 throw new NullPointerException ("CostVertexPair 'p' must be non-NULL." );
360+
366361 if (this .cost < p .cost )
367362 return -1 ;
368363 if (this .cost > p .cost )
@@ -375,18 +370,18 @@ public int compareTo(CostVertexPair<T> p) {
375370 */
376371 @ Override
377372 public String toString () {
378- StringBuilder builder = new StringBuilder ();
379- builder .append ("Vertex= " ).append (vertex .getValue () ).append (" cost=" ).append (cost ).append ("\n " );
373+ final StringBuilder builder = new StringBuilder ();
374+ builder .append (vertex . getValue ()). append ( " ( " ).append (vertex .weight ). append ( ") " ).append (" cost=" ).append (cost ).append ("\n " );
380375 return builder .toString ();
381376 }
382377 }
383378
384379 public static class CostPathPair <T extends Comparable <T >> {
385380
386381 private int cost = 0 ;
387- private Set <Edge <T >> path = null ;
382+ private List <Edge <T >> path = null ;
388383
389- public CostPathPair (int cost , Set <Edge <T >> path ) {
384+ public CostPathPair (int cost , List <Edge <T >> path ) {
390385 if (path == null )
391386 throw (new NullPointerException ("path cannot be NULL." ));
392387
@@ -402,7 +397,7 @@ public void setCost(int cost) {
402397 this .cost = cost ;
403398 }
404399
405- public Set <Edge <T >> getPath () {
400+ public List <Edge <T >> getPath () {
406401 return path ;
407402 }
408403
@@ -425,14 +420,15 @@ public boolean equals(Object obj) {
425420 if (!(obj instanceof CostPathPair ))
426421 return false ;
427422
428- CostPathPair <?> pair = (CostPathPair <?>)obj ;
423+ final CostPathPair <?> pair = (CostPathPair <?>) obj ;
429424 if (this .cost != pair .cost )
430425 return false ;
431426
432- Object [] e = pair .path .toArray ();
433- int i =0 ;
434- for (Edge <T > e1 : path ) {
435- Edge <T > e2 = (Edge <T >) e [i ++];
427+ final Iterator <?> iter1 = this .getPath ().iterator ();
428+ final Iterator <?> iter2 = pair .getPath ().iterator ();
429+ while (iter1 .hasNext () && iter2 .hasNext ()) {
430+ Edge <T > e1 = (Edge <T >) iter1 .next ();
431+ Edge <T > e2 = (Edge <T >) iter2 .next ();
436432 if (!e1 .equals (e2 ))
437433 return false ;
438434 }
@@ -445,11 +441,10 @@ public boolean equals(Object obj) {
445441 */
446442 @ Override
447443 public String toString () {
448- StringBuilder builder = new StringBuilder ();
444+ final StringBuilder builder = new StringBuilder ();
449445 builder .append ("Cost = " ).append (cost ).append ("\n " );
450- for (Edge <T > e : path ) {
446+ for (Edge <T > e : path )
451447 builder .append ("\t " ).append (e );
452- }
453448 return builder .toString ();
454449 }
455450 }
0 commit comments