-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiameter.py
68 lines (58 loc) · 1.26 KB
/
diameter.py
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
import io
from collections import defaultdict
test1 = io.StringIO('''5
1 2
1 3
1 4
1 5''')
test2 = io.StringIO('''4
1 2
2 3
3 4''')
int BFS(G: (V, E), source: int, destination: int):
d = int[|V|]
fill(d, ∞)
d[source] = 0
Q = ∅
Q.push(source)
while Q ≠∅
u = Q.pop()
for v: (u, v) in E
if d[v] == ∞
d[v] = d[u] + 1
Q.push(v)
return d[destination]
def bfs(graph, node):
distances = []
queue = []
visited.append(node)
queue.append(node)
while queue:
s = queue.pop(0)
print(s, end = " ")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
return len(visited)
def diameterTree(g, graph_len):
v = u = w = 1
d = bfs(g, v)
for i in range(graph_len):
if d[i] > d[u]:
u = i
bfs(g, u)
for i in range(graph_len):
if d[i] > d[w]:
w = i
return d[w]
def main(str_buffer):
graph = defaultdict(list)
graph_len = int(next(str_buffer))
for i in range(graph_len-1):
node1, node2 = map(int, next(str_buffer).strip().split())
graph[node1].append(node2)
graph[node2].append(node1)
print(graph)
diameterTree(graph, graph_len)
main(test1)