mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
Fix spelling (#2973)
This commit is contained in:
@ -4,38 +4,38 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
class AdjacencyListGraph<E extends Comparable<E>> {
|
class AdjacencyListGraph<E extends Comparable<E>> {
|
||||||
|
|
||||||
ArrayList<Vertex> verticies;
|
ArrayList<Vertex> vertices;
|
||||||
|
|
||||||
public AdjacencyListGraph() {
|
public AdjacencyListGraph() {
|
||||||
verticies = new ArrayList<>();
|
vertices = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Vertex {
|
private class Vertex {
|
||||||
|
|
||||||
E data;
|
E data;
|
||||||
ArrayList<Vertex> adjacentVerticies;
|
ArrayList<Vertex> adjacentVertices;
|
||||||
|
|
||||||
public Vertex(E data) {
|
public Vertex(E data) {
|
||||||
adjacentVerticies = new ArrayList<>();
|
adjacentVertices = new ArrayList<>();
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addAdjacentVertex(Vertex to) {
|
public boolean addAdjacentVertex(Vertex to) {
|
||||||
for (Vertex v : adjacentVerticies) {
|
for (Vertex v : adjacentVertices) {
|
||||||
if (v.data.compareTo(to.data) == 0) {
|
if (v.data.compareTo(to.data) == 0) {
|
||||||
return false; // the edge already exists
|
return false; // the edge already exists
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return adjacentVerticies.add(to); // this will return true;
|
return adjacentVertices.add(to); // this will return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeAdjacentVertex(E to) {
|
public boolean removeAdjacentVertex(E to) {
|
||||||
// use indexes here so it is possible to
|
// use indexes here so it is possible to
|
||||||
// remove easily without implementing
|
// remove easily without implementing
|
||||||
// equals method that ArrayList.remove(Object o) uses
|
// equals method that ArrayList.remove(Object o) uses
|
||||||
for (int i = 0; i < adjacentVerticies.size(); i++) {
|
for (int i = 0; i < adjacentVertices.size(); i++) {
|
||||||
if (adjacentVerticies.get(i).data.compareTo(to) == 0) {
|
if (adjacentVertices.get(i).data.compareTo(to) == 0) {
|
||||||
adjacentVerticies.remove(i);
|
adjacentVertices.remove(i);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ class AdjacencyListGraph<E extends Comparable<E>> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* this method removes an edge from the graph between two specified
|
* this method removes an edge from the graph between two specified
|
||||||
* verticies
|
* vertices
|
||||||
*
|
*
|
||||||
* @param from the data of the vertex the edge is from
|
* @param from the data of the vertex the edge is from
|
||||||
* @param to the data of the vertex the edge is going to
|
* @param to the data of the vertex the edge is going to
|
||||||
@ -54,7 +54,7 @@ class AdjacencyListGraph<E extends Comparable<E>> {
|
|||||||
*/
|
*/
|
||||||
public boolean removeEdge(E from, E to) {
|
public boolean removeEdge(E from, E to) {
|
||||||
Vertex fromV = null;
|
Vertex fromV = null;
|
||||||
for (Vertex v : verticies) {
|
for (Vertex v : vertices) {
|
||||||
if (from.compareTo(v.data) == 0) {
|
if (from.compareTo(v.data) == 0) {
|
||||||
fromV = v;
|
fromV = v;
|
||||||
break;
|
break;
|
||||||
@ -67,7 +67,7 @@ class AdjacencyListGraph<E extends Comparable<E>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* this method adds an edge to the graph between two specified verticies
|
* this method adds an edge to the graph between two specified vertices
|
||||||
*
|
*
|
||||||
* @param from the data of the vertex the edge is from
|
* @param from the data of the vertex the edge is from
|
||||||
* @param to the data of the vertex the edge is going to
|
* @param to the data of the vertex the edge is going to
|
||||||
@ -76,7 +76,7 @@ class AdjacencyListGraph<E extends Comparable<E>> {
|
|||||||
*/
|
*/
|
||||||
public boolean addEdge(E from, E to) {
|
public boolean addEdge(E from, E to) {
|
||||||
Vertex fromV = null, toV = null;
|
Vertex fromV = null, toV = null;
|
||||||
for (Vertex v : verticies) {
|
for (Vertex v : vertices) {
|
||||||
if (from.compareTo(v.data) == 0) { // see if from vertex already exists
|
if (from.compareTo(v.data) == 0) { // see if from vertex already exists
|
||||||
fromV = v;
|
fromV = v;
|
||||||
} else if (to.compareTo(v.data) == 0) { // see if to vertex already exists
|
} else if (to.compareTo(v.data) == 0) { // see if to vertex already exists
|
||||||
@ -88,29 +88,29 @@ class AdjacencyListGraph<E extends Comparable<E>> {
|
|||||||
}
|
}
|
||||||
if (fromV == null) {
|
if (fromV == null) {
|
||||||
fromV = new Vertex(from);
|
fromV = new Vertex(from);
|
||||||
verticies.add(fromV);
|
vertices.add(fromV);
|
||||||
}
|
}
|
||||||
if (toV == null) {
|
if (toV == null) {
|
||||||
toV = new Vertex(to);
|
toV = new Vertex(to);
|
||||||
verticies.add(toV);
|
vertices.add(toV);
|
||||||
}
|
}
|
||||||
return fromV.addAdjacentVertex(toV);
|
return fromV.addAdjacentVertex(toV);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* this gives a list of verticies in the graph and their adjacencies
|
* this gives a list of vertices in the graph and their adjacencies
|
||||||
*
|
*
|
||||||
* @return returns a string describing this graph
|
* @return returns a string describing this graph
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (Vertex v : verticies) {
|
for (Vertex v : vertices) {
|
||||||
sb.append("Vertex: ");
|
sb.append("Vertex: ");
|
||||||
sb.append(v.data);
|
sb.append(v.data);
|
||||||
sb.append("\n");
|
sb.append("\n");
|
||||||
sb.append("Adjacent verticies: ");
|
sb.append("Adjacent vertices: ");
|
||||||
for (Vertex v2 : v.adjacentVerticies) {
|
for (Vertex v2 : v.adjacentVertices) {
|
||||||
sb.append(v2.data);
|
sb.append(v2.data);
|
||||||
sb.append(" ");
|
sb.append(" ");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user