-
Notifications
You must be signed in to change notification settings - Fork 1
/
DirectedGraph.java
200 lines (178 loc) 路 6.03 KB
/
DirectedGraph.java
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DirectedGraph implements Iterable<DirectedGraph.Vertex> {
private final Map<Integer, Vertex> vertices = new HashMap<>();
private int numberOfVertices = 0;
private int degree = 0;
private int batchIndex = 0;
public static DirectedGraph example1() {
DirectedGraph graph = new DirectedGraph();
graph.addVertices(4);
graph.addEdge(0, 1, 3);
graph.addEdge(0, 3, 7);
graph.addEdge(1, 0, 8);
graph.addEdge(1, 2, 2);
graph.addEdge(2, 0, 5);
graph.addEdge(2, 3, 1);
graph.addEdge(3, 0, 2);
return graph;
}
public static DirectedGraph example2() {
DirectedGraph graph = new DirectedGraph();
graph.addVertices(7);
graph.addEdge(0, 1, 6);
graph.addEdge(0, 2, 5);
graph.addEdge(0, 3, 5);
graph.addEdge(1, 4, -1);
graph.addEdge(2, 1, -2);
graph.addEdge(2, 4, 1);
graph.addEdge(3, 2, -2);
graph.addEdge(3, 5, -1);
graph.addEdge(4, 6, 3);
graph.addEdge(5,6, 3);
return graph;
}
@Override
public String toString() {
return "Graph {" +
"vertices=" + vertices.keySet() + ", " +
"numberOfVertices=" + numberOfVertices +
", degree=" + degree +
'}';
}
@Override
public Iterator<Vertex> iterator() {
return vertices.values().iterator();
}
public static class Vertex implements Iterable<Edge> {
private final int data;
private final Map<Vertex, List<Edge>> edges = new HashMap<>();
Vertex(int data) {
this.data = data;
}
private void addEdge(Edge edge) {
List<Edge> edgeList = edges.getOrDefault(edge.to, new ArrayList<>());
edgeList.add(edge);
edges.put(edge.to, edgeList);
}
@Override
public Iterator<Edge> iterator() {
Map<Vertex, Edge> shortestEdges = getShortestEdges();
return shortestEdges.values().iterator();
}
private Map<Vertex, Edge> getShortestEdges() {
Map<Vertex, Edge> result = new HashMap<>();
for (Map.Entry<Vertex, List<Edge>> entry : edges.entrySet()) {
Edge smallestEdge = entry.getValue().stream().min(Edge::compareTo).get();
result.put(entry.getKey(), smallestEdge);
}
return result;
}
}
private static class Edge implements Comparable<Edge> {
private final Vertex from;
private final Vertex to;
private final int weight;
Edge(Vertex from, Vertex to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
@Override
public int compareTo(Edge other) {
return Integer.compare(weight, other.weight);
}
}
public void addVertex(int data) {
if (!vertices.containsKey(data)) {
vertices.put(batchIndex++, new Vertex(data));
numberOfVertices++;
}
}
public void addVertices(int batch) {
while (batch-- > 0) {
addVertex(batchIndex);
}
}
public void addEdge(int from, int to) {
addEdge(from, to, 1);
}
public void addEdge(int from, int to, int weight) {
Vertex fromVertex = vertices.get(from);
Vertex toVertex = vertices.get(to);
Edge edge = new Edge(fromVertex, toVertex, weight);
fromVertex.addEdge(edge);
degree++;
}
/**
* Time Complexity: O(V^3)
* Space Complexity: O(V^2)
* @return the minimum distance matrix between all vertices of the graph
*/
public long[][] floydWarshallMinDistances() {
long[][] adjacencyMatrix = getAdjacencyMatrix();
for (int k = 0 ; k < adjacencyMatrix.length ; k++) {
for (int i = 0 ; i < adjacencyMatrix.length ; i++) {
for (int j = 0 ; j < adjacencyMatrix.length ; j++) {
adjacencyMatrix[i][j] = Math.min(
adjacencyMatrix[i][j],
adjacencyMatrix[i][k] + adjacencyMatrix[k][j]
);
}
}
}
return adjacencyMatrix;
}
private long[][] getAdjacencyMatrix() {
long[][] matrix = new long[numberOfVertices][numberOfVertices];
assignAllInfinity(matrix);
for (Vertex vertex : this) {
matrix[vertex.data][vertex.data] = 0;
for (Edge edge : vertex) {
matrix[vertex.data][edge.to.data] = edge.weight;
}
}
return matrix;
}
private void assignAllInfinity(long[][] matrix) {
for (int i = 0 ; i < matrix.length ; i++) {
for (int j = 0 ; j < matrix.length ; j++) {
matrix[i][j] = Integer.MAX_VALUE;
}
}
}
/**
* Time Complexity: O(V * E)
* Space Complexity: O(V)
* @param source the source
* @return the distance from the source vertex to all other vertices
*/
public Map<Integer, Long> bellmanFord(int source) {
Map<Integer, Long> distances = new HashMap<>();
distances.put(source, 0L);
Set<Edge> edges = edgeSet();
for (int i = 0 ; i < numberOfVertices - 1 ; i++) {
for (Edge edge : edges) {
distances.put(edge.to.data, Math.min(
distances.getOrDefault(edge.to.data, (long) Integer.MAX_VALUE),
distances.getOrDefault(edge.from.data, (long) Integer.MAX_VALUE) + edge.weight
));
}
}
return distances;
}
private Set<Edge> edgeSet() {
Set<Edge> edges = new HashSet<>();
for (Vertex vertex : this) {
for (Edge edge : vertex) {
edges.add(edge);
}
}
return edges;
}
}