Skip to content

Commit 381d898

Browse files
committed
add adjacency list implementation for graphs
1 parent 230a005 commit 381d898

File tree

2 files changed

+116
-23
lines changed

2 files changed

+116
-23
lines changed

data_structures/Graphs/Graphs.java

Lines changed: 113 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,122 @@
1+
import java.util.ArrayList;
2+
import java.lang.StringBuilder;
13

2-
/**
3-
* This class implements the Graph data structure
4-
* using the classes Graph and Graphs.
5-
*
6-
* @author Zachary Jones
7-
*
8-
*/
9-
class Graph {
4+
class AdjacencyListGraph<E extends Comparable<E>> {
105

11-
}
6+
ArrayList<Vertex> verticies;
7+
8+
public AdjacencyListGraph() {
9+
verticies = new ArrayList<>();
10+
}
11+
12+
private class Vertex {
13+
E data;
14+
ArrayList<Vertex> adjacentVerticies;
15+
16+
public Vertex(E data) {
17+
adjacentVerticies = new ArrayList<>();
18+
this.data = data;
19+
}
1220

13-
/**
14-
* This class is used to test the Graph
15-
* class above.
16-
*
17-
* @author Zachary Jones
18-
*
19-
*/
21+
public boolean addAdjacentVertex(Vertex to) {
22+
for (Vertex v: adjacentVerticies) {
23+
if (v.data.compareTo(to.data) == 0) {
24+
return false; // the edge already exists
25+
}
26+
}
27+
return adjacentVerticies.add(to); // this will return true;
28+
}
29+
30+
public boolean removeAdjacentVertex(E to) {
31+
// use indexes here so it is possible to
32+
// remove easily without implementing
33+
// equals method that ArrayList.remove(Object o) uses
34+
for (int i = 0; i < adjacentVerticies.size(); i++) {
35+
if (adjacentVerticies.get(i).data.compareTo(to) == 0) {
36+
adjacentVerticies.remove(i);
37+
return true;
38+
}
39+
}
40+
return false;
41+
}
42+
}
43+
44+
/**
45+
* @param from the data of the vertex the edge is from
46+
* @param to the data of the vertex the edge is going to
47+
* @return returns false if the edge doesn't exist, returns true if the edge exists and is removed
48+
*/
49+
public boolean removeEdge(E from, E to) {
50+
Vertex fromV = null;
51+
for (Vertex v: verticies) {
52+
if (from.compareTo(v.data) == 0) {
53+
fromV = v;
54+
break;
55+
}
56+
}
57+
if (fromV == null) return false;
58+
return fromV.removeAdjacentVertex(to);
59+
}
60+
/**
61+
* @param from the data of the vertex the edge is from
62+
* @param to the data of the vertex the edge is going to
63+
* @return returns true if the edge did not exist, return false if it already did
64+
*/
65+
public boolean addEdge(E from, E to) {
66+
Vertex fromV = null, toV = null;
67+
for (Vertex v: verticies) {
68+
if (from.compareTo(v.data) == 0) { // see if from vertex already exists
69+
fromV = v;
70+
} else if (to.compareTo(v.data) == 0) { // see if to vertex already exists
71+
toV = v;
72+
}
73+
if (fromV != null && toV != null) break; // both nodes exist so stop searching
74+
}
75+
if (fromV == null) {
76+
fromV = new Vertex(from);
77+
verticies.add(fromV);
78+
}
79+
if (toV == null) {
80+
toV = new Vertex(to);
81+
verticies.add(toV);
82+
}
83+
return fromV.addAdjacentVertex(toV);
84+
}
85+
86+
/**
87+
*
88+
* @return returns a string describing this graph
89+
*/
90+
public String toString() {
91+
StringBuilder sb = new StringBuilder();
92+
for (Vertex v: verticies) {
93+
sb.append("Vertex: ");
94+
sb.append(v.data);
95+
sb.append("\n");
96+
sb.append("Adjacent verticies: ");
97+
for (Vertex v2: v.adjacentVerticies) {
98+
sb.append(v2.data);
99+
sb.append(" ");
100+
}
101+
sb.append("\n");
102+
}
103+
return sb.toString();
104+
}
105+
}
20106

21107
public class Graphs {
22108

23-
/**
24-
* Main method
25-
*
26-
* @param args Command line arguments
27-
*/
28109
public static void main(String args[]) {
29-
30-
}
110+
AdjacencyListGraph<Integer> graph = new AdjacencyListGraph<>();
111+
assert graph.addEdge(1, 2);
112+
assert graph.addEdge(1, 5);
113+
assert graph.addEdge(2, 5);
114+
assert !graph.addEdge(1, 2);
115+
assert graph.addEdge(2, 3);
116+
assert graph.addEdge(3, 4);
117+
assert graph.addEdge(4, 1);
118+
assert !graph.addEdge(2, 3);
119+
System.out.println(graph);
120+
}
31121

32122
}

data_structures/Graphs/makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
javac Graphs.java
3+
java -ea Graphs

0 commit comments

Comments
 (0)