-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.py
More file actions
59 lines (55 loc) · 1.73 KB
/
Copy pathsolution.py
File metadata and controls
59 lines (55 loc) · 1.73 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
from collections import deque
class Solution:
# BFS from start: returns farthest node and list of nodes in the component
def bfs_collect(self, start, adj):
n = len(adj)
dist = [-1] * n
q = deque([start])
dist[start] = 0
farthest = start
comp = []
while q:
u = q.popleft()
comp.append(u)
if dist[u] > dist[farthest]:
farthest = u
for v in adj[u]:
if dist[v] == -1:
dist[v] = dist[u] + 1
q.append(v)
return farthest, comp
# BFS from src to compute maximum distance reachable (diameter endpoint distance)
def bfs_maxdist(self, src, adj):
n = len(adj)
dist = [-1] * n
q = deque([src])
dist[src] = 0
maxd = 0
while q:
u = q.popleft()
if dist[u] > maxd:
maxd = dist[u]
for v in adj[u]:
if dist[v] == -1:
dist[v] = dist[u] + 1
q.append(v)
return maxd
def diameter(self, V, edges):
if V <= 1:
return 0
adj = [[] for _ in range(V)]
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
processed = [False] * V
answer = 0
for i in range(V):
if processed[i]:
continue
u, comp_nodes = self.bfs_collect(i, adj)
for node in comp_nodes:
processed[node] = True
comp_diam = self.bfs_maxdist(u, adj)
if comp_diam > answer:
answer = comp_diam
return answer