-
Notifications
You must be signed in to change notification settings - Fork 43
/
clone-graph.py
450 lines (384 loc) · 12.7 KB
/
clone-graph.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""
133. Clone Graph
Medium
Given a reference of a node in a connected undirected graph.
Return a deep copy (clone) of the graph.
Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.
class Node {
public int val;
public List<Node> neighbors;
}
Test case format:
For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on. The graph is represented in the test case using an adjacency list.
An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
The given node will always be the first node with val = 1. You must return the copy of the given node as a reference to the cloned graph.
Example 1:
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
Output: [[2,4],[1,3],[2,4],[1,3]]
Explanation: There are 4 nodes in the graph.
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
Example 2:
Input: adjList = [[]]
Output: [[]]
Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
Example 3:
Input: adjList = []
Output: []
Explanation: This an empty graph, it does not have any nodes.
Constraints:
The number of nodes in the graph is in the range [0, 100].
1 <= Node.val <= 100
Node.val is unique for each node.
There are no repeated edges and no self-loops in the graph.
The Graph is connected and all nodes can be visited starting from the given node.
"""
# V0
# IDEA : BFS
class Solution(object):
def cloneGraph(self, node):
if not node:
return
q = [node]
"""
NOTE !!! : we init res as Node(node.val, [])
-> since Node has structure as below :
class Node:
def __init__(self, val = 0, neighbors = None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
"""
res = Node(node.val, [])
"""
NOTE !!! : we use dict as visited,
and we use node as visited dict key
"""
visited = dict()
visited[node] = res
while q:
#t = q.pop(0) # this works as well
t = q.pop(-1)
if not t:
continue
for n in t.neighbors:
if n not in visited:
"""
NOTE !!! : we need to
-> use n as visited key
-> use Node(n.val, []) as visited value
"""
visited[n] = Node(n.val, [])
q.append(n)
"""
NOTE !!!
-> we need to append visited[n] to visited[t].neighbors
"""
visited[t].neighbors.append(visited[n])
return res
# V0
# IDEA : DFS
# NOTE :
# -> 1) we init node via : node_copy = Node(node.val, [])
# -> 2) we copy graph via dict
class Solution(object):
def cloneGraph(self, node):
"""
:type node: Node
:rtype: Node
"""
node_copy = self.dfs(node, dict())
return node_copy
def dfs(self, node, hashd):
if not node: return None
if node in hashd: return hashd[node]
node_copy = Node(node.val, [])
hashd[node] = node_copy
for n in node.neighbors:
n_copy = self.dfs(n, hashd)
if n_copy:
node_copy.neighbors.append(n_copy)
return node_copy
# V0'
# IDEA : BFS
class Solution(object):
def cloneGraph(self, node):
# edge case
if not node:
return
q = []
hashd = dict()
q.append(node)
node_copy = Node(node.val, [])
hashd[node] = node_copy
while q:
t = q.pop(0)
if t:
for n in t.neighbors:
if n not in hashd:
hashd[n] = Node(n.val, [])
q.append(n)
hashd[t].neighbors.append(hashd[n])
return node_copy
# V0''
# IDEA : BFS
class Solution:
def __init__(self):
self.dict = {}
"""
@param: node: A undirected graph node
@return: A undirected graph node
"""
def cloneGraph(self, node):
if node is None:
return None
if node.val in self.dict:
return self.dict[node.val]
root = Node(node.val, [])
self.dict[node.val] = root
for item in node.neighbors:
root.neighbors.append(self.cloneGraph(item))
return root
# V0''
# IDEA : BFS
class Solution(object):
def cloneGraph(self, node):
"""
:type node: Node
:rtype: Node
"""
if not node: return
que = collections.deque()
hashd = dict()
que.append(node)
node_copy = Node(node.val, [])
hashd[node] = node_copy
while que:
t = que.popleft()
if not t: continue
for n in t.neighbors:
if n not in hashd:
hashd[n] = Node(n.val, [])
que.append(n)
hashd[t].neighbors.append(hashd[n])
return node_copy
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/88363919
# IDEA : DFS
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, neighbors):
self.val = val
self.neighbors = neighbors
"""
class Solution(object):
def cloneGraph(self, node):
"""
:type node: Node
:rtype: Node
"""
node_copy = self.dfs(node, dict())
return node_copy
def dfs(self, node, hashd):
if not node: return None
if node in hashd: return hashd[node]
node_copy = Node(node.val, [])
hashd[node] = node_copy
for n in node.neighbors:
n_copy = self.dfs(n, hashd)
if n_copy:
node_copy.neighbors.append(n_copy)
return node_copy
### Test case : dev
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/88363919
# IDEA : BFS
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, neighbors):
self.val = val
self.neighbors = neighbors
"""
class Solution(object):
def cloneGraph(self, node):
"""
:type node: Node
:rtype: Node
"""
if not node: return
que = collections.deque()
hashd = dict()
que.append(node)
node_copy = Node(node.val, [])
hashd[node] = node_copy
while que:
t = que.popleft()
if not t: continue
for n in t.neighbors:
if n not in hashd:
hashd[n] = Node(n.val, [])
que.append(n)
hashd[t].neighbors.append(hashd[n])
return node_copy
# V1
# IDEA : DFS
# https://leetcode.com/problems/clone-graph/solution/
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, neighbors):
self.val = val
self.neighbors = neighbors
"""
class Solution(object):
def __init__(self):
# Dictionary to save the visited node and it's respective clone
# as key and value respectively. This helps to avoid cycles.
self.visited = {}
def cloneGraph(self, node):
"""
:type node: Node
:rtype: Node
"""
if not node:
return node
# If the node was already visited before.
# Return the clone from the visited dictionary.
if node in self.visited:
return self.visited[node]
# Create a clone for the given node.
# Note that we don't have cloned neighbors as of now, hence [].
clone_node = Node(node.val, [])
# The key is original node and value being the clone node.
self.visited[node] = clone_node
# Iterate through the neighbors to generate their clones
# and prepare a list of cloned neighbors to be added to the cloned node.
if node.neighbors:
clone_node.neighbors = [self.cloneGraph(n) for n in node.neighbors]
return clone_node
# V1
# IDEA : BFS
# https://leetcode.com/problems/clone-graph/solution/
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, neighbors):
self.val = val
self.neighbors = neighbors
"""
from collections import deque
class Solution(object):
def cloneGraph(self, node):
"""
:type node: Node
:rtype: Node
"""
if not node:
return node
# Dictionary to save the visited node and it's respective clone
# as key and value respectively. This helps to avoid cycles.
visited = {}
# Put the first node in the queue
queue = deque([node])
# Clone the node and put it in the visited dictionary.
visited[node] = Node(node.val, [])
# Start BFS traversal
while queue:
# Pop a node say "n" from the from the front of the queue.
n = queue.popleft()
# Iterate through all the neighbors of the node
for neighbor in n.neighbors:
if neighbor not in visited:
# Clone the neighbor and put in the visited, if not present already
visited[neighbor] = Node(neighbor.val, [])
# Add the newly encountered node to the queue.
queue.append(neighbor)
# Add the clone of the neighbor to the neighbors of the clone node "n".
visited[n].neighbors.append(visited[neighbor])
# Return the clone of the node from visited.
return visited[node]
# V1''
# https://www.jiuzhang.com/solution/clone-graph/#tag-highlight-lang-python
# IDEA : DFS
"""
Definition for a undirected graph node
class UndirectedGraphNode:
def __init__(self, x):
self.label = x
self.neighbors = []
"""
class Solution:
def __init__(self):
self.dict = {}
"""
@param: node: A undirected graph node
@return: A undirected graph node
"""
def cloneGraph(self, node):
if node is None:
return None
if node.label in self.dict:
return self.dict[node.label]
root = UndirectedGraphNode(node.label)
self.dict[node.label] = root
for item in node.neighbors:
root.neighbors.append(self.cloneGraph(item))
return root
# V1'''
# https://www.jiuzhang.com/solution/clone-graph/#tag-highlight-lang-python
# IDEA : BFS
class Solution:
def cloneGraph(self, node):
root = node
if node is None:
return node
# use bfs algorithm to traverse the graph and get all nodes.
nodes = self.getNodes(node)
# copy nodes, store the old->new mapping information in a hash map
mapping = {}
for node in nodes:
mapping[node] = UndirectedGraphNode(node.label)
# copy neighbors(edges)
for node in nodes:
new_node = mapping[node]
for neighbor in node.neighbors:
new_neighbor = mapping[neighbor]
new_node.neighbors.append(new_neighbor)
return mapping[root]
def getNodes(self, node):
q = collections.deque([node])
result = set([node])
while q:
head = q.popleft()
for neighbor in head.neighbors:
if neighbor not in result:
result.add(neighbor)
q.append(neighbor)
return result
# V2
# Time: O(n)
# Space: O(n)
class UndirectedGraphNode(object):
def __init__(self, x):
self.label = x
self.neighbors = []
class Solution(object):
# @param node, a undirected graph node
# @return a undirected graph node
def cloneGraph(self, node):
if node is None:
return None
cloned_node = UndirectedGraphNode(node.label)
cloned, queue = {node:cloned_node}, [node]
while queue:
current = queue.pop()
for neighbor in current.neighbors:
if neighbor not in cloned:
queue.append(neighbor)
cloned_neighbor = UndirectedGraphNode(neighbor.label)
cloned[neighbor] = cloned_neighbor
cloned[current].neighbors.append(cloned[neighbor])
return cloned[node]