-
Notifications
You must be signed in to change notification settings - Fork 0
/
farthest_node.py
66 lines (50 loc) · 1.64 KB
/
farthest_node.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
# https://programmers.co.kr/learn/courses/30/lessons/49189?language=python3
from collections import defaultdict, deque
class Node:
def __init__(self, idx):
self.idx = idx
self.is_visited = False
self.adjcent = {}
def link(self, node):
self.adjcent[node.idx] = node
def is_linked_to(self, idx):
return idx in self.adjcent
def get_linked_nodes(self):
return list(self.adjcent.values())
class Graph:
def __init__(self, size):
self.nodes = {i+1: Node(i+1) for i in range(size)}
def link(self, i1, i2):
n1, n2 = self.nodes.get(i1), self.nodes.get(i2)
if (n1 is None) or (n2 is None):
return
n1.link(n2)
n2.link(n1)
def bfs(self):
lv = 1
lvs = {}
lvs[1] = {
1: self.nodes.get(1)
}
while True:
old = lvs[lv] # upper level
new = {} # current level
for (i, n) in old.items():
if not n.is_visited:
n.is_visited = True
adj_nodes = n.get_linked_nodes()
for adj in adj_nodes:
if (not adj.is_visited) and (not adj.idx in old):
new[adj.idx] = adj
if not new:
break
lv += 1
lvs[lv] = new
return lvs
def solution(n, edge):
graph = Graph(n)
for (i1, i2) in edge:
graph.link(i1, i2)
lvs = graph.bfs()
max_lv = max(lvs.keys())
return len(lvs.get(max_lv))