-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacencyList.java
More file actions
96 lines (83 loc) · 2.98 KB
/
AdjacencyList.java
File metadata and controls
96 lines (83 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.*;
// An AdjacencyList class can implement a graph
public class AdjacencyList<V, E> implements Graph<V, E> {
private Map<Vertex<V>, List<Edge<V, E>>> adj;
private final boolean isDirected;
// Constructs an empty adjacency list
public AdjacencyList(boolean directed) {
isDirected = directed;
adj = new HashMap<>();
}
// Returns an iterable collection containing all the vertices of the given graph
public Set<Vertex<V>> vertices() {
return new HashSet<>(adj.keySet());
}
// Returns an iterable collection containing all the edges of the given graph in ascending order by weight
public Iterable<Edge<V, E>> edges() {
Set<Edge<V, E>> e = new TreeSet<>();
for (List<Edge<V, E>> value : adj.values()) {
e.addAll(value);
}
return e;
}
// Returns an iterable collection containing all the edges of a vertex
public Iterable<Edge<V, E>> neighbors(Vertex<V> v) {
return new HashSet<>(adj.get(v));
}
// Adds given edge to graph between two existing vertices
public void addEdge(Vertex<V> source, Vertex<V> destination, E label, Double weight) {
if (!adj.containsKey(source)) {
addVertex(source);
}
adj.get(source).add(new Edge<>(source, destination, label, weight));
if (!adj.containsKey(destination)) {
addVertex(destination);
}
if (!isDirected) {
adj.get(destination).add(new Edge<>(destination, source, label, weight));
}
}
// Adds given vertex to graph
public void addVertex(Vertex<V> v){
if (!adj.containsKey(v)) {
adj.put(v, new LinkedList<>());
}
}
// Removes given vertex from graph
public void removeVertex(Vertex<V> v) {
adj.remove(v);
if (!isDirected) {
adj.values().remove(v);
}
}
// Removes given edge from graph
public void removeEdge(Vertex<V> source, Vertex<V> destination) {
adj.get(source).remove(destination);
if (!isDirected) {
adj.get(destination).remove(source);
}
}
// Checks if given vertex is in graph
// Returns true if found and false otherwise
public boolean containsVertex(Vertex<V> vertex) {
return adj.containsKey(vertex);
}
// Checks if given edge is in graph
// Returns true if found and false otherwise
public boolean containsEdge(Vertex<V> source, Vertex<V> destination) {
return adj.get(source).contains(destination);
}
// Returns a string representation of the adjacency list
public String toString() {
StringBuilder s = new StringBuilder();
String newline = "";
for (Map.Entry<Vertex<V>, List<Edge<V, E>>> entry : adj.entrySet()) {
s.append(newline)
.append(entry.getKey())
.append(": ")
.append(entry.getValue());
newline = "\n";
}
return (s.toString());
}
}