Skip to content

Commit 48d0b6e

Browse files
committed
Improved Graph
1 parent 61a993d commit 48d0b6e

File tree

5 files changed

+63
-63
lines changed

5 files changed

+63
-63
lines changed

Graph/Graph.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ public Graph( int n ) {
4747
public int node( T node ) {
4848
int i = 0;
4949
for (GraphNode<T> curr : nodes) {
50-
if (node.compareTo( curr.getElement() ) == 0)
50+
if (node.compareTo( curr.setContent() ) == 0)
5151
return i;
5252
i++;
5353
}
5454
return INDEX_NOT_FOUND;
5555
}
5656

5757
public T elementAt( int index ) {
58-
return nodes.get( index ).getElement();
58+
return nodes.get( index ).setContent();
5959
}
6060

6161
/**
@@ -82,7 +82,7 @@ public void addNode( T element ) throws Exception {
8282
"The node cannot be added because there's no space." );
8383
}
8484
for (GraphNode<T> node : nodes) {
85-
if (node.getElement().equals( element ))
85+
if (node.setContent().equals( element ))
8686
throw new Exception( "Element is already in the graph." );
8787
}
8888
nodes.add( new GraphNode<T>( element ) );
@@ -170,7 +170,7 @@ private String DFPrint( int v ) {
170170
}
171171

172172
public void print() throws Exception {
173-
System.out.println( traverseGraph( nodes.get( 0 ).getElement() ) );
173+
System.out.println( traverseGraph( nodes.get( 0 ).setContent() ) );
174174
}
175175

176176
/**
@@ -386,7 +386,6 @@ public void Dijkstra( T departureNode ) {
386386

387387
}
388388

389-
/* ---------- 2015-16 EXAM METHOD ---------- */
390389
/**
391390
* It return the number of nodes n such that is possible to go from the
392391
* source to them and return to the source with no more cost than the one
@@ -414,7 +413,8 @@ public int getNumberOfReturnNodesWithinCost( T source, double cost )
414413
return result;
415414
}
416415

417-
/* ---------- SOME OWN EXAMPLES OF WORKING WITH GRAPHS ---------- */
416+
/* ---------- SOME EXAMPLES OF WORKING WITH GRAPHS ---------- */
417+
418418
/**
419419
* IMPLEMENTING FLOYD Checks whether a node is strongly connected, i.e.
420420
* there is a path from the node to every other node in the graph and at the
@@ -446,7 +446,7 @@ public boolean isStronglyConnected( T node ) {
446446
* @return the maximum distance from this node to every other node as a
447447
* double.
448448
*/
449-
public double excentricidad( T node ) {
449+
public double eccentricity( T node ) {
450450
int index = node( node );
451451
floyd( size() );
452452
double result = 0.0;
@@ -469,7 +469,7 @@ public double excentricidad( T node ) {
469469
* @return the maximum distance from this node to every other node as a
470470
* double.
471471
*/
472-
public double excentricidadDijkstra( T node ) {
472+
public double eccentricityDijkstra( T node ) {
473473
Dijkstra( node );
474474
double distances[][] = getD();
475475
double result = 0.0;
@@ -488,10 +488,10 @@ public double excentricidadDijkstra( T node ) {
488488
*
489489
* @return double diameter.
490490
*/
491-
public double diametro() {
491+
public double diameter() {
492492
double result = 0.0;
493493
for (GraphNode<T> node : nodes) {
494-
double ex = excentricidad( node.getElement() );
494+
double ex = eccentricity( node.setContent() );
495495
if (ex > result) {
496496
result = ex;
497497
}
@@ -506,10 +506,10 @@ public double diametro() {
506506
*
507507
* @return
508508
*/
509-
public double diametroDijkstra() {
509+
public double diameterDijkstra() {
510510
double result = 0.0;
511511
for (GraphNode<T> node : nodes) {
512-
double ex = excentricidadDijkstra( node.getElement() );
512+
double ex = eccentricityDijkstra( node.setContent() );
513513
if (ex > result) {
514514
result = ex;
515515
}
@@ -525,7 +525,7 @@ public double diametroDijkstra() {
525525
* @param node
526526
* @return integer value with the number of aristas.
527527
*/
528-
public int gradoNodo( T node ) {
528+
public int degreeOf( T node ) {
529529
int index = node( node );
530530
int result = 0;
531531
for (int i = 0; i < size(); i++) {
@@ -541,10 +541,10 @@ public int gradoNodo( T node ) {
541541
*
542542
* @return integer. Número mínimo de aristas incidentes
543543
*/
544-
public int minGradoNodo() {
544+
public int minDegree() {
545545
int result = Integer.MAX_VALUE;
546546
for (GraphNode<T> node : nodes) {
547-
int gn = gradoNodo( node.getElement() );
547+
int gn = degreeOf( node.setContent() );
548548
if (gn < result)
549549
result = gn;
550550
}
@@ -557,10 +557,10 @@ public int minGradoNodo() {
557557
*
558558
* @return integer. Número máximo de aristas que incide
559559
*/
560-
public int maxGradoNodo() {
560+
public int maxDegree() {
561561
int result = 0;
562562
for (GraphNode<T> node : nodes) {
563-
int gn = gradoNodo( node.getElement() );
563+
int gn = degreeOf( node.setContent() );
564564
if (gn > result)
565565
result = gn;
566566
}

Graph/GraphNode.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@
1010
*/
1111
public class GraphNode<T extends Comparable<T>> {
1212

13-
private T element;
13+
private T content;
1414
private boolean visited;
1515

16-
public GraphNode( T element ) {
17-
setElement( element );
16+
public GraphNode( T content ) {
17+
setContent( content );
1818
setVisited( false );
1919
}
2020

2121
/**
2222
* Sets the element of the node.
2323
*
24-
* @param element to be stored.
24+
* @param content to be stored.
2525
*/
26-
public void setElement( T element ) {
27-
if (element != null)
28-
this.element = element;
26+
public void setContent( T content ) {
27+
if (content != null)
28+
this.content = content;
2929
}
3030

3131
/**
3232
* Returns the element that is stored in the node.
3333
*
3434
* @return the element stored in the node.
3535
*/
36-
public T getElement() {
37-
return this.element;
36+
public T setContent() {
37+
return this.content;
3838
}
3939

4040
/**
@@ -60,7 +60,7 @@ public boolean isVisited() {
6060
*/
6161
public String toString() {
6262
StringBuilder toReturn = new StringBuilder();
63-
toReturn.append( "GN(N:" ).append( this.getElement().toString() )
63+
toReturn.append( "GN(N:" ).append( this.setContent().toString() )
6464
.append( "/V:" ).append( this.isVisited() ).append( ")" );
6565
return toReturn.toString();
6666
}

Graph/tests/GraphMartinTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ public class GraphMartinTest {
1515

1616
@Test public void GraphNodeTest() {
1717
GraphNode<Integer> numbers = new GraphNode<Integer>( 4 );
18-
assertEquals( 4, (int) numbers.getElement() );
18+
assertEquals( 4, (int) numbers.setContent() );
1919
assertEquals( false, numbers.isVisited() );
2020
numbers.setVisited( true );
2121
assertEquals( true, numbers.isVisited() );
2222
assertEquals( "GN(N:4/V:true)", numbers.toString() );
2323

2424
GraphNode<String> myString = new GraphNode<String>( "hello" );
25-
assertEquals( "hello", myString.getElement() );
25+
assertEquals( "hello", myString.setContent() );
2626
assertEquals( false, myString.isVisited() );
2727
assertEquals( "GN(N:hello/V:false)", myString.toString() );
2828

2929
GraphNode<Character> myChar = new GraphNode<Character>( 'a' );
30-
assertEquals( 'a', (char) myChar.getElement() );
30+
assertEquals( 'a', (char) myChar.setContent() );
3131
assertEquals( "GN(N:a/V:false)", myChar.toString() );
32-
myChar.setElement( 'b' );
32+
myChar.setContent( 'b' );
3333
assertEquals( "GN(N:b/V:false)", myChar.toString() );
3434
myChar.setVisited( true );
3535
assertEquals( "GN(N:b/V:true)", myChar.toString() );

Graph/tests/GraphNodeTest.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,39 @@ public class GraphNodeTest {
2626

2727
@Test public void setElementTest() {
2828
// Strings...
29-
StringGraphNode.setElement( "B" );
30-
assertEquals( "B", StringGraphNode.getElement() );
31-
StringGraphNode.setElement( null );
32-
assertEquals( "B", StringGraphNode.getElement() );
33-
StringGraphNode.setElement( "C" );
34-
assertEquals( "C", StringGraphNode.getElement() );
29+
StringGraphNode.setContent( "B" );
30+
assertEquals( "B", StringGraphNode.setContent() );
31+
StringGraphNode.setContent( null );
32+
assertEquals( "B", StringGraphNode.setContent() );
33+
StringGraphNode.setContent( "C" );
34+
assertEquals( "C", StringGraphNode.setContent() );
3535

3636
// Integers...
37-
IntegerGraphNode.setElement( 2 );
38-
assertEquals( 2, (int) IntegerGraphNode.getElement() );
39-
IntegerGraphNode.setElement( null );
40-
assertEquals( 2, (int) IntegerGraphNode.getElement() );
41-
IntegerGraphNode.setElement( 3 );
42-
assertEquals( 3, (int) IntegerGraphNode.getElement() );
37+
IntegerGraphNode.setContent( 2 );
38+
assertEquals( 2, (int) IntegerGraphNode.setContent() );
39+
IntegerGraphNode.setContent( null );
40+
assertEquals( 2, (int) IntegerGraphNode.setContent() );
41+
IntegerGraphNode.setContent( 3 );
42+
assertEquals( 3, (int) IntegerGraphNode.setContent() );
4343

4444
// Characters...
45-
CharGraphNode.setElement( 'B' );
46-
assertEquals( 'B', (char) CharGraphNode.getElement() );
47-
CharGraphNode.setElement( null );
48-
assertEquals( 'B', (char) CharGraphNode.getElement() );
49-
CharGraphNode.setElement( 'C' );
50-
assertEquals( 'C', (char) CharGraphNode.getElement() );
45+
CharGraphNode.setContent( 'B' );
46+
assertEquals( 'B', (char) CharGraphNode.setContent() );
47+
CharGraphNode.setContent( null );
48+
assertEquals( 'B', (char) CharGraphNode.setContent() );
49+
CharGraphNode.setContent( 'C' );
50+
assertEquals( 'C', (char) CharGraphNode.setContent() );
5151
}
5252

5353
@Test public void getElementTest() {
5454
// Strings...
55-
assertEquals( "A", StringGraphNode.getElement() );
55+
assertEquals( "A", StringGraphNode.setContent() );
5656

5757
// Integers...
58-
assertEquals( 1, (int) IntegerGraphNode.getElement() );
58+
assertEquals( 1, (int) IntegerGraphNode.setContent() );
5959

6060
// Characters...
61-
assertEquals( 'A', (char) CharGraphNode.getElement() );
61+
assertEquals( 'A', (char) CharGraphNode.setContent() );
6262
}
6363

6464
@Test public void setVisitedTest() {
@@ -100,30 +100,30 @@ public class GraphNodeTest {
100100

101101
@Test public void toStringTest() {
102102
// Strings...
103-
StringGraphNode.setElement( "B" );
103+
StringGraphNode.setContent( "B" );
104104
assertEquals( "GN(N:B/V:false)", StringGraphNode.toString() );
105105
StringGraphNode.setVisited( true );
106106
assertEquals( "GN(N:B/V:true)", StringGraphNode.toString() );
107-
StringGraphNode.setElement( "C" );
107+
StringGraphNode.setContent( "C" );
108108
StringGraphNode.setVisited( false );
109109
assertEquals( "GN(N:C/V:false)", StringGraphNode.toString() );
110110

111111
// Integers...
112-
IntegerGraphNode.setElement( 2 );
112+
IntegerGraphNode.setContent( 2 );
113113
assertEquals( "GN(N:2/V:false)", IntegerGraphNode.toString() );
114114
IntegerGraphNode.setVisited( true );
115115
assertEquals( "GN(N:2/V:true)", IntegerGraphNode.toString() );
116116
IntegerGraphNode.setVisited( false );
117-
IntegerGraphNode.setElement( 3 );
117+
IntegerGraphNode.setContent( 3 );
118118
assertEquals( "GN(N:3/V:false)", IntegerGraphNode.toString() );
119119

120120
// Characters...
121-
CharGraphNode.setElement( 'B' );
121+
CharGraphNode.setContent( 'B' );
122122
assertEquals( "GN(N:B/V:false)", CharGraphNode.toString() );
123123
CharGraphNode.setVisited( true );
124124
assertEquals( "GN(N:B/V:true)", CharGraphNode.toString() );
125125
CharGraphNode.setVisited( false );
126-
CharGraphNode.setElement( 'C' );
126+
CharGraphNode.setContent( 'C' );
127127
assertEquals( "GN(N:C/V:false)", CharGraphNode.toString() );
128128
}
129129

Graph/tests/GraphTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ public class GraphTest {
646646
} catch (Exception e) {
647647
System.out.println( e );
648648
}
649-
assertEquals( 7, (int) integerGraph.diametro() );
649+
assertEquals( 7, (int) integerGraph.diameter() );
650650
}
651651

652652
@Test public void diametroDijkstraTest() {
@@ -662,7 +662,7 @@ public class GraphTest {
662662
} catch (Exception e) {
663663
System.out.println( e );
664664
}
665-
assertEquals( 7, (int) integerGraph.diametroDijkstra() );
665+
assertEquals( 7, (int) integerGraph.diameterDijkstra() );
666666
}
667667

668668
@Test public void gradoNodoTest() {
@@ -678,7 +678,7 @@ public class GraphTest {
678678
} catch (Exception e) {
679679
System.out.println( e );
680680
}
681-
assertEquals( 2, integerGraph.gradoNodo( 3 ) );
681+
assertEquals( 2, integerGraph.degreeOf( 3 ) );
682682
}
683683

684684
@Test public void minGradoNodoTest() {
@@ -694,7 +694,7 @@ public class GraphTest {
694694
} catch (Exception e) {
695695
System.out.println( e );
696696
}
697-
assertEquals( 0, integerGraph.minGradoNodo() );
697+
assertEquals( 0, integerGraph.minDegree() );
698698
}
699699

700700
@Test public void maxGradoNodoTest() {
@@ -710,7 +710,7 @@ public class GraphTest {
710710
} catch (Exception e) {
711711
System.out.println( e );
712712
}
713-
assertEquals( 2, integerGraph.maxGradoNodo() );
713+
assertEquals( 2, integerGraph.maxDegree() );
714714
}
715715

716716
}

0 commit comments

Comments
 (0)