4
4
5
5
class AdjacencyListGraph <E extends Comparable <E >> {
6
6
7
- ArrayList <Vertex > verticies ;
7
+ ArrayList <Vertex > vertices ;
8
8
9
9
public AdjacencyListGraph () {
10
- verticies = new ArrayList <>();
10
+ vertices = new ArrayList <>();
11
11
}
12
12
13
13
private class Vertex {
14
14
15
15
E data ;
16
- ArrayList <Vertex > adjacentVerticies ;
16
+ ArrayList <Vertex > adjacentVertices ;
17
17
18
18
public Vertex (E data ) {
19
- adjacentVerticies = new ArrayList <>();
19
+ adjacentVertices = new ArrayList <>();
20
20
this .data = data ;
21
21
}
22
22
23
23
public boolean addAdjacentVertex (Vertex to ) {
24
- for (Vertex v : adjacentVerticies ) {
24
+ for (Vertex v : adjacentVertices ) {
25
25
if (v .data .compareTo (to .data ) == 0 ) {
26
26
return false ; // the edge already exists
27
27
}
28
28
}
29
- return adjacentVerticies .add (to ); // this will return true;
29
+ return adjacentVertices .add (to ); // this will return true;
30
30
}
31
31
32
32
public boolean removeAdjacentVertex (E to ) {
33
33
// use indexes here so it is possible to
34
34
// remove easily without implementing
35
35
// equals method that ArrayList.remove(Object o) uses
36
- for (int i = 0 ; i < adjacentVerticies .size (); i ++) {
37
- if (adjacentVerticies .get (i ).data .compareTo (to ) == 0 ) {
38
- adjacentVerticies .remove (i );
36
+ for (int i = 0 ; i < adjacentVertices .size (); i ++) {
37
+ if (adjacentVertices .get (i ).data .compareTo (to ) == 0 ) {
38
+ adjacentVertices .remove (i );
39
39
return true ;
40
40
}
41
41
}
@@ -45,7 +45,7 @@ public boolean removeAdjacentVertex(E to) {
45
45
46
46
/**
47
47
* this method removes an edge from the graph between two specified
48
- * verticies
48
+ * vertices
49
49
*
50
50
* @param from the data of the vertex the edge is from
51
51
* @param to the data of the vertex the edge is going to
@@ -54,7 +54,7 @@ public boolean removeAdjacentVertex(E to) {
54
54
*/
55
55
public boolean removeEdge (E from , E to ) {
56
56
Vertex fromV = null ;
57
- for (Vertex v : verticies ) {
57
+ for (Vertex v : vertices ) {
58
58
if (from .compareTo (v .data ) == 0 ) {
59
59
fromV = v ;
60
60
break ;
@@ -67,7 +67,7 @@ public boolean removeEdge(E from, E to) {
67
67
}
68
68
69
69
/**
70
- * this method adds an edge to the graph between two specified verticies
70
+ * this method adds an edge to the graph between two specified vertices
71
71
*
72
72
* @param from the data of the vertex the edge is from
73
73
* @param to the data of the vertex the edge is going to
@@ -76,7 +76,7 @@ public boolean removeEdge(E from, E to) {
76
76
*/
77
77
public boolean addEdge (E from , E to ) {
78
78
Vertex fromV = null , toV = null ;
79
- for (Vertex v : verticies ) {
79
+ for (Vertex v : vertices ) {
80
80
if (from .compareTo (v .data ) == 0 ) { // see if from vertex already exists
81
81
fromV = v ;
82
82
} else if (to .compareTo (v .data ) == 0 ) { // see if to vertex already exists
@@ -88,29 +88,29 @@ public boolean addEdge(E from, E to) {
88
88
}
89
89
if (fromV == null ) {
90
90
fromV = new Vertex (from );
91
- verticies .add (fromV );
91
+ vertices .add (fromV );
92
92
}
93
93
if (toV == null ) {
94
94
toV = new Vertex (to );
95
- verticies .add (toV );
95
+ vertices .add (toV );
96
96
}
97
97
return fromV .addAdjacentVertex (toV );
98
98
}
99
99
100
100
/**
101
- * this gives a list of verticies in the graph and their adjacencies
101
+ * this gives a list of vertices in the graph and their adjacencies
102
102
*
103
103
* @return returns a string describing this graph
104
104
*/
105
105
@ Override
106
106
public String toString () {
107
107
StringBuilder sb = new StringBuilder ();
108
- for (Vertex v : verticies ) {
108
+ for (Vertex v : vertices ) {
109
109
sb .append ("Vertex: " );
110
110
sb .append (v .data );
111
111
sb .append ("\n " );
112
- sb .append ("Adjacent verticies : " );
113
- for (Vertex v2 : v .adjacentVerticies ) {
112
+ sb .append ("Adjacent vertices : " );
113
+ for (Vertex v2 : v .adjacentVertices ) {
114
114
sb .append (v2 .data );
115
115
sb .append (" " );
116
116
}
0 commit comments