-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
98 lines (90 loc) · 2.81 KB
/
Copy pathsolution.java
File metadata and controls
98 lines (90 loc) · 2.81 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
import java.util.*;
class Solution {
// BFS that starts from start; returns farthest node and also list of component
// nodes
private Pair<Integer, List<Integer>> bfsCollect(int start, List<List<Integer>> adj) {
int n = adj.size();
int[] dist = new int[n];
Arrays.fill(dist, -1);
Queue<Integer> q = new LinkedList<>();
List<Integer> comp = new ArrayList<>();
q.add(start);
dist[start] = 0;
int farthest = start;
while (!q.isEmpty()) {
int u = q.poll();
comp.add(u);
if (dist[u] > dist[farthest])
farthest = u;
for (int v : adj.get(u)) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.add(v);
}
}
}
return new Pair<>(farthest, comp);
}
// BFS from src to compute maximum distance reachable from src
private int bfsMaxDist(int src, List<List<Integer>> adj) {
int n = adj.size();
int[] dist = new int[n];
Arrays.fill(dist, -1);
Queue<Integer> q = new LinkedList<>();
q.add(src);
dist[src] = 0;
int maxd = 0;
while (!q.isEmpty()) {
int u = q.poll();
maxd = Math.max(maxd, dist[u]);
for (int v : adj.get(u)) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.add(v);
}
}
}
return maxd;
}
public int diameter(int V, int[][] edges) {
if (V <= 1)
return 0;
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < V; ++i)
adj.add(new ArrayList<>());
for (int[] e : edges) {
int a = e[0], b = e[1];
adj.get(a).add(b);
adj.get(b).add(a);
}
boolean[] processed = new boolean[V];
int answer = 0;
for (int i = 0; i < V; ++i) {
if (processed[i])
continue;
Pair<Integer, List<Integer>> res = bfsCollect(i, adj);
int u = res.getKey();
List<Integer> compNodes = res.getValue();
for (int node : compNodes)
processed[node] = true;
int compDiam = bfsMaxDist(u, adj);
answer = Math.max(answer, compDiam);
}
return answer;
}
// Simple Pair implementation if not using javafx/util
static class Pair<K, V> {
private K key;
private V val;
Pair(K k, V v) {
key = k;
val = v;
}
K getKey() {
return key;
}
V getValue() {
return val;
}
}
}