Skip to content

renamed "verticies" as "vertices" #2973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@

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;
}
}
Expand All @@ -45,7 +45,7 @@ public boolean removeAdjacentVertex(E to) {

/**
* 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
Expand All @@ -54,7 +54,7 @@ public boolean removeAdjacentVertex(E to) {
*/
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;
Expand All @@ -67,7 +67,7 @@ public boolean removeEdge(E from, E to) {
}

/**
* 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
Expand All @@ -76,7 +76,7 @@ public boolean removeEdge(E from, E to) {
*/
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
Expand All @@ -88,29 +88,29 @@ public boolean addEdge(E from, E to) {
}
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(" ");
}
Expand Down