mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Fix spelling (#2973)
This commit is contained in:
@ -4,38 +4,38 @@ import java.util.ArrayList;
|
||||
|
||||
class AdjacencyListGraph<E extends Comparable<E>> {
|
||||
|
||||
ArrayList<Vertex> verticies;
|
||||
ArrayList<Vertex> vertices;
|
||||
|
||||
public AdjacencyListGraph() {
|
||||
verticies = new ArrayList<>();
|
||||
vertices = new ArrayList<>();
|
||||
}
|
||||
|
||||
private class Vertex {
|
||||
|
||||
E data;
|
||||
ArrayList<Vertex> adjacentVerticies;
|
||||
ArrayList<Vertex> adjacentVertices;
|
||||
|
||||
public Vertex(E data) {
|
||||
adjacentVerticies = new ArrayList<>();
|
||||
adjacentVertices = new ArrayList<>();
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean addAdjacentVertex(Vertex to) {
|
||||
for (Vertex v : adjacentVerticies) {
|
||||
for (Vertex v : adjacentVertices) {
|
||||
if (v.data.compareTo(to.data) == 0) {
|
||||
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) {
|
||||
// use indexes here so it is possible to
|
||||
// remove easily without implementing
|
||||
// equals method that ArrayList.remove(Object o) uses
|
||||
for (int i = 0; i < adjacentVerticies.size(); i++) {
|
||||
if (adjacentVerticies.get(i).data.compareTo(to) == 0) {
|
||||
adjacentVerticies.remove(i);
|
||||
for (int i = 0; i < adjacentVertices.size(); i++) {
|
||||
if (adjacentVertices.get(i).data.compareTo(to) == 0) {
|
||||
adjacentVertices.remove(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -45,7 +45,7 @@ class AdjacencyListGraph<E extends Comparable<E>> {
|
||||
|
||||
/**
|
||||
* 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 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) {
|
||||
Vertex fromV = null;
|
||||
for (Vertex v : verticies) {
|
||||
for (Vertex v : vertices) {
|
||||
if (from.compareTo(v.data) == 0) {
|
||||
fromV = v;
|
||||
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 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) {
|
||||
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
|
||||
fromV = v;
|
||||
} 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) {
|
||||
fromV = new Vertex(from);
|
||||
verticies.add(fromV);
|
||||
vertices.add(fromV);
|
||||
}
|
||||
if (toV == null) {
|
||||
toV = new Vertex(to);
|
||||
verticies.add(toV);
|
||||
vertices.add(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
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Vertex v : verticies) {
|
||||
for (Vertex v : vertices) {
|
||||
sb.append("Vertex: ");
|
||||
sb.append(v.data);
|
||||
sb.append("\n");
|
||||
sb.append("Adjacent verticies: ");
|
||||
for (Vertex v2 : v.adjacentVerticies) {
|
||||
sb.append("Adjacent vertices: ");
|
||||
for (Vertex v2 : v.adjacentVertices) {
|
||||
sb.append(v2.data);
|
||||
sb.append(" ");
|
||||
}
|
||||
|
Reference in New Issue
Block a user