Skip to content

Commit 995005c

Browse files
Merge pull request TheAlgorithms#99 from rei2hu/master
Add adjacency list graph implementation
2 parents c6887b8 + 9b566f9 commit 995005c

File tree

1 file changed

+120
-23
lines changed

1 file changed

+120
-23
lines changed

data_structures/Graphs/Graphs.java

Lines changed: 120 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,129 @@
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+
* this method removes an edge from the graph between two specified
46+
* verticies
47+
*
48+
* @param from the data of the vertex the edge is from
49+
* @param to the data of the vertex the edge is going to
50+
* @return returns false if the edge doesn't exist, returns true if the edge exists and is removed
51+
*/
52+
public boolean removeEdge(E from, E to) {
53+
Vertex fromV = null;
54+
for (Vertex v: verticies) {
55+
if (from.compareTo(v.data) == 0) {
56+
fromV = v;
57+
break;
58+
}
59+
}
60+
if (fromV == null) return false;
61+
return fromV.removeAdjacentVertex(to);
62+
}
63+
/**
64+
* this method adds an edge to the graph between two specified
65+
* verticies
66+
*
67+
* @param from the data of the vertex the edge is from
68+
* @param to the data of the vertex the edge is going to
69+
* @return returns true if the edge did not exist, return false if it already did
70+
*/
71+
public boolean addEdge(E from, E to) {
72+
Vertex fromV = null, toV = null;
73+
for (Vertex v: verticies) {
74+
if (from.compareTo(v.data) == 0) { // see if from vertex already exists
75+
fromV = v;
76+
} else if (to.compareTo(v.data) == 0) { // see if to vertex already exists
77+
toV = v;
78+
}
79+
if (fromV != null && toV != null) break; // both nodes exist so stop searching
80+
}
81+
if (fromV == null) {
82+
fromV = new Vertex(from);
83+
verticies.add(fromV);
84+
}
85+
if (toV == null) {
86+
toV = new Vertex(to);
87+
verticies.add(toV);
88+
}
89+
return fromV.addAdjacentVertex(toV);
90+
}
91+
92+
/**
93+
* this gives a list of verticies in the graph and their adjacencies
94+
*
95+
* @return returns a string describing this graph
96+
*/
97+
public String toString() {
98+
StringBuilder sb = new StringBuilder();
99+
for (Vertex v: verticies) {
100+
sb.append("Vertex: ");
101+
sb.append(v.data);
102+
sb.append("\n");
103+
sb.append("Adjacent verticies: ");
104+
for (Vertex v2: v.adjacentVerticies) {
105+
sb.append(v2.data);
106+
sb.append(" ");
107+
}
108+
sb.append("\n");
109+
}
110+
return sb.toString();
111+
}
112+
}
20113

21114
public class Graphs {
22115

23-
/**
24-
* Main method
25-
*
26-
* @param args Command line arguments
27-
*/
28116
public static void main(String args[]) {
29-
30-
}
117+
AdjacencyListGraph<Integer> graph = new AdjacencyListGraph<>();
118+
assert graph.addEdge(1, 2);
119+
assert graph.addEdge(1, 5);
120+
assert graph.addEdge(2, 5);
121+
assert !graph.addEdge(1, 2);
122+
assert graph.addEdge(2, 3);
123+
assert graph.addEdge(3, 4);
124+
assert graph.addEdge(4, 1);
125+
assert !graph.addEdge(2, 3);
126+
System.out.println(graph);
127+
}
31128

32129
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy