def mkUdGraph(s): dic = {} node, neib = None, None root = None flag = 1 for ob in s.split('#'): ob = ob.strip() x = ob.split(',') for i in range(len(x)): if i == 0: if x[i] not in dic: node = UndirectedGraphNode(x[i]) dic[x[i]] = node else: node = dic[x[i]] if flag: root = node flag = 0 else: if x[i] not in dic: neib = UndirectedGraphNode(x[i]) dic[x[i]] = neib else: neib = dic[x[i]] node.neighbors.append(neib) return root
def test_0(): sol = Solution() assert sol.cloneGraph(None) is None g1 = UndirectedGraphNode(2) g2 = sol.cloneGraph(g1) assert graph_eq(g1, g2) g1.label = 3 assert not graph_eq(g1, g2)
def test_1(): sol = Solution() n1 = UndirectedGraphNode(1) n0 = UndirectedGraphNode(0) n2 = UndirectedGraphNode(2) n1.neighbors = [n0, n2] n0.neighbors = [n1, n2] n2.neighbors = [n0, n1, n2] c1 = sol.cloneGraph(n1) assert graph_eq(n1, c1) n0.label = 9 assert not graph_eq(n1, c1)