|
| 1 | +import collections |
| 2 | +from typing import List, Optional, Dict |
| 3 | + |
| 4 | + |
| 5 | +class Node: |
| 6 | + def __init__(self, val=0, neighbors=None): |
| 7 | + self.val = val |
| 8 | + self.neighbors = neighbors if neighbors is not None else [] |
| 9 | + |
| 10 | + def __hash__(self) -> int: |
| 11 | + return self.val |
| 12 | + |
| 13 | + def __eq__(self, other: Optional["Node"]) -> bool: |
| 14 | + if self is None and other is None: |
| 15 | + return True |
| 16 | + elif self is None: |
| 17 | + return False |
| 18 | + elif other is None: |
| 19 | + return False |
| 20 | + adj1 = self.adj_list() |
| 21 | + adj2 = other.adj_list() |
| 22 | + return adj1 == adj2 |
| 23 | + |
| 24 | + def adj_list(self) -> Dict[int, List[int]]: |
| 25 | + """Return the adjacency list of the graph.""" |
| 26 | + adj = collections.defaultdict(list) |
| 27 | + queue = [self] |
| 28 | + visited = {self.val} |
| 29 | + while queue: |
| 30 | + node = queue.pop(0) |
| 31 | + for neighbor in node.neighbors: |
| 32 | + if neighbor.val not in visited: |
| 33 | + adj[node.val].append(neighbor.val) |
| 34 | + queue.append(neighbor) |
| 35 | + visited.add(neighbor.val) |
| 36 | + return adj |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def build(edges: List[List[int]]) -> Optional["Node"]: |
| 40 | + """Build a graph from an adjacency list.""" |
| 41 | + if not edges: |
| 42 | + return None |
| 43 | + |
| 44 | + nodes = {} |
| 45 | + for i in range(len(edges)): |
| 46 | + if i not in nodes: |
| 47 | + nodes[i + 1] = Node(i + 1) |
| 48 | + for j in edges[i]: |
| 49 | + if j not in nodes: |
| 50 | + nodes[j] = Node(j) |
| 51 | + nodes[i + 1].neighbors.append(nodes[j]) |
| 52 | + return nodes[1] |
| 53 | + |
| 54 | + |
| 55 | +class Solution: |
| 56 | + """Base class for all LeetCode Problems.""" |
| 57 | + |
| 58 | + def cloneGraph(self, node: Optional["Node"]) -> Optional["Node"]: |
| 59 | + """ |
| 60 | + Given a reference of a node in a connected undirected graph. |
| 61 | +
|
| 62 | + Return a deep copy (clone) of the graph. |
| 63 | +
|
| 64 | + Each node in the graph contains a value (int) and a list (List[Node]) of its |
| 65 | + neighbors. |
| 66 | +
|
| 67 | + class Node { |
| 68 | + public int val; |
| 69 | + public List<Node> neighbors; |
| 70 | + } |
| 71 | +
|
| 72 | +
|
| 73 | + Test case format: |
| 74 | +
|
| 75 | + For simplicity, each node's value is the same as the node's index (1-indexed). |
| 76 | + For example, the first node with val == 1, the second node with val == 2, and so |
| 77 | + on. The graph is represented in the test case using an adjacency list. |
| 78 | +
|
| 79 | + An adjacency list is a collection of unordered lists used to represent a finite |
| 80 | + graph. Each list describes the set of neighbors of a node in the graph. |
| 81 | +
|
| 82 | + The given node will always be the first node with val = 1. You must return the |
| 83 | + copy of the given node as a reference to the cloned graph. |
| 84 | + """ |
| 85 | + |
| 86 | + if node is None: |
| 87 | + return None |
| 88 | + |
| 89 | + # Create all new nodes (without neighbors) |
| 90 | + old_to_copy = {None: None} |
| 91 | + queue = [node] |
| 92 | + visited = {node} |
| 93 | + while queue: |
| 94 | + cur = queue.pop(0) |
| 95 | + copy = Node(cur.val) |
| 96 | + old_to_copy[cur] = copy |
| 97 | + for nxt in cur.neighbors: |
| 98 | + if nxt not in visited: |
| 99 | + queue.append(nxt) |
| 100 | + visited.add(nxt) |
| 101 | + |
| 102 | + # Add all neighbors to new nodes |
| 103 | + queue = [node] |
| 104 | + visited = {node} |
| 105 | + while queue: |
| 106 | + cur = queue.pop(0) |
| 107 | + copy = old_to_copy[cur] |
| 108 | + copy.neighbors = [old_to_copy[nxt] for nxt in cur.neighbors] |
| 109 | + for nxt in cur.neighbors: |
| 110 | + if nxt not in visited: |
| 111 | + queue.append(nxt) |
| 112 | + visited.add(nxt) |
| 113 | + |
| 114 | + return old_to_copy[node] |
0 commit comments