This repository was archived by the owner on Jun 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphDiameterAnalyzer.java
More file actions
296 lines (264 loc) · 9.72 KB
/
Copy pathGraphDiameterAnalyzer.java
File metadata and controls
296 lines (264 loc) · 9.72 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package gvisual;
import edu.uci.ics.jung.graph.Graph;
import java.util.*;
import java.util.Arrays;
/**
* Analyzes graph diameter, radius, eccentricity, center, and periphery.
*
* <p>For each vertex, computes the <b>eccentricity</b> — the maximum shortest-path
* distance (hop count) to any other reachable vertex. From eccentricities:</p>
* <ul>
* <li><b>Diameter</b> — maximum eccentricity (longest shortest path in the graph)</li>
* <li><b>Radius</b> — minimum eccentricity</li>
* <li><b>Center</b> — set of vertices whose eccentricity equals the radius</li>
* <li><b>Periphery</b> — set of vertices whose eccentricity equals the diameter</li>
* </ul>
*
* <p>Operates on the largest connected component when the graph is disconnected.
* Useful for understanding the overall spread and identifying structurally
* important nodes in social/IMEI networks.</p>
*
* @author zalenix
*/
public class GraphDiameterAnalyzer {
private final Graph<String, edge> graph;
private Map<String, Integer> eccentricities;
private int diameter;
private int radius;
private Set<String> centerVertices;
private Set<String> peripheryVertices;
private Set<String> largestComponent;
private boolean computed;
/**
* Creates a new GraphDiameterAnalyzer for the given graph.
*
* @param graph the JUNG graph to analyze
* @throws IllegalArgumentException if graph is null
*/
public GraphDiameterAnalyzer(Graph<String, edge> graph) {
if (graph == null) {
throw new IllegalArgumentException("Graph must not be null");
}
this.graph = graph;
this.computed = false;
}
/**
* Runs the analysis. Must be called before querying results.
*/
public void analyze() {
eccentricities = new LinkedHashMap<String, Integer>();
centerVertices = new LinkedHashSet<String>();
peripheryVertices = new LinkedHashSet<String>();
diameter = 0;
radius = Integer.MAX_VALUE;
if (graph.getVertexCount() == 0) {
radius = 0;
largestComponent = Collections.emptySet();
computed = true;
return;
}
// Find the largest connected component
largestComponent = findLargestComponent();
if (largestComponent.size() <= 1) {
for (String v : largestComponent) {
eccentricities.put(v, 0);
centerVertices.add(v);
peripheryVertices.add(v);
}
diameter = 0;
radius = 0;
computed = true;
return;
}
// Build vertex index and adjacency arrays ONCE for all BFS passes.
// This avoids per-source HashMap<String,Integer> allocations and
// String hashing inside the BFS hot loop — the same optimisation
// used in NodeCentralityAnalyzer and PageRankAnalyzer.
List<String> compList = new ArrayList<String>(largestComponent);
int compN = compList.size();
Map<String, Integer> idxMap = new HashMap<String, Integer>(compN * 2);
for (int i = 0; i < compN; i++) idxMap.put(compList.get(i), i);
// Pre-build int[][] adjacency for cache-friendly traversal
int[][] adj = new int[compN][];
{
@SuppressWarnings("unchecked")
List<Integer>[] tmp = new List[compN];
for (int i = 0; i < compN; i++) tmp[i] = new ArrayList<Integer>();
for (edge e : graph.getEdges()) {
Integer ui = idxMap.get(e.getVertex1());
Integer vi = idxMap.get(e.getVertex2());
if (ui != null && vi != null && !ui.equals(vi)) {
tmp[ui].add(vi);
tmp[vi].add(ui);
}
}
for (int i = 0; i < compN; i++) {
List<Integer> nb = tmp[i];
adj[i] = new int[nb.size()];
for (int j = 0; j < nb.size(); j++) adj[i][j] = nb.get(j);
}
}
// Reusable BFS arrays (allocated once, reset per source)
int[] dist = new int[compN];
int[] queue = new int[compN];
// Compute eccentricity for each vertex in the largest component
for (int s = 0; s < compN; s++) {
// Array-based BFS — no HashMap, no boxing, no per-source allocation
Arrays.fill(dist, -1);
dist[s] = 0;
int qStart = 0, qEnd = 0;
queue[qEnd++] = s;
int maxDist = 0;
while (qStart < qEnd) {
int v = queue[qStart++];
int d = dist[v];
for (int w : adj[v]) {
if (dist[w] < 0) {
dist[w] = d + 1;
if (dist[w] > maxDist) maxDist = dist[w];
queue[qEnd++] = w;
}
}
}
eccentricities.put(compList.get(s), maxDist);
if (maxDist > diameter) diameter = maxDist;
if (maxDist < radius) radius = maxDist;
}
// Identify center and periphery
for (Map.Entry<String, Integer> entry : eccentricities.entrySet()) {
if (entry.getValue() == radius) {
centerVertices.add(entry.getKey());
}
if (entry.getValue() == diameter) {
peripheryVertices.add(entry.getKey());
}
}
computed = true;
}
/**
* Returns the diameter (max eccentricity) of the graph.
*/
public int getDiameter() {
ensureComputed();
return diameter;
}
/**
* Returns the radius (min eccentricity) of the graph.
*/
public int getRadius() {
ensureComputed();
return radius;
}
/**
* Returns the eccentricity map for all vertices in the largest component.
*/
public Map<String, Integer> getEccentricities() {
ensureComputed();
return Collections.unmodifiableMap(eccentricities);
}
/**
* Returns the eccentricity of a specific vertex.
*
* @param vertex the vertex ID
* @return eccentricity value, or -1 if vertex is not in the largest component
*/
public int getEccentricity(String vertex) {
ensureComputed();
Integer ecc = eccentricities.get(vertex);
return ecc != null ? ecc : -1;
}
/**
* Returns the center vertices (eccentricity == radius).
*/
public Set<String> getCenterVertices() {
ensureComputed();
return Collections.unmodifiableSet(centerVertices);
}
/**
* Returns the periphery vertices (eccentricity == diameter).
*/
public Set<String> getPeripheryVertices() {
ensureComputed();
return Collections.unmodifiableSet(peripheryVertices);
}
/**
* Returns the size of the largest connected component analyzed.
*/
public int getLargestComponentSize() {
ensureComputed();
return largestComponent.size();
}
/**
* Returns the vertices in the largest connected component.
*/
public Set<String> getLargestComponent() {
ensureComputed();
return Collections.unmodifiableSet(largestComponent);
}
/**
* Returns vertices sorted by eccentricity (ascending — most central first).
*/
public List<Map.Entry<String, Integer>> getRankedByEccentricity() {
ensureComputed();
List<Map.Entry<String, Integer>> entries =
new ArrayList<Map.Entry<String, Integer>>(eccentricities.entrySet());
Collections.sort(entries, (Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) -> {
return Integer.compare(a.getValue(), b.getValue());
});
return entries;
}
/**
* Returns a human-readable summary of the diameter analysis.
*/
public String getSummary() {
ensureComputed();
StringBuilder sb = new StringBuilder();
sb.append("=== Graph Diameter Analysis ===\n");
sb.append(String.format("Largest component: %d vertices (of %d total)\n",
largestComponent.size(), graph.getVertexCount()));
sb.append(String.format("Diameter: %d\n", diameter));
sb.append(String.format("Radius: %d\n", radius));
sb.append(String.format("Center vertices (%d): %s\n",
centerVertices.size(), formatSet(centerVertices, 10)));
sb.append(String.format("Periphery vertices (%d): %s\n",
peripheryVertices.size(), formatSet(peripheryVertices, 10)));
return sb.toString();
}
// --- Private helpers ---
private int computeEccentricity(String source, Set<String> component) {
Map<String, Integer> distances = GraphUtils.bfsDistances(graph, source);
int maxDist = 0;
for (String v : component) {
Integer d = distances.get(v);
if (d != null && d > maxDist) {
maxDist = d;
}
}
return maxDist;
}
private Set<String> findLargestComponent() {
return GraphUtils.findLargestComponent(graph);
}
private String getOtherEnd(edge e, String current) {
return GraphUtils.getOtherEnd(e, current);
}
private String formatSet(Set<String> set, int max) {
StringBuilder sb = new StringBuilder();
int count = 0;
for (String s : set) {
if (count > 0) sb.append(", ");
if (count >= max) {
sb.append("... (").append(set.size() - max).append(" more)");
break;
}
sb.append(s);
count++;
}
return sb.toString();
}
private void ensureComputed() {
if (!computed) {
throw new IllegalStateException("Call analyze() before querying results");
}
}
}