Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 480cdbc

Browse files
Clone Graph
1 parent 5293690 commit 480cdbc

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

‎Graphs/133-Clone-Graph.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
'''
2+
Leetcode- https://leetcode.com/problems/clone-graph/
3+
'''
4+
5+
'''
6+
133. Clone Graph
7+
8+
Given a reference of a node in a connected undirected graph.
9+
10+
Return a deep copy (clone) of the graph.
11+
12+
Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.
13+
14+
class Node {
15+
public int val;
16+
public List<Node> neighbors;
17+
}
18+
19+
20+
Test case format:
21+
22+
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.
23+
24+
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.
25+
26+
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.
27+
28+
29+
30+
Example 1:
31+
32+
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
33+
Output: [[2,4],[1,3],[2,4],[1,3]]
34+
Explanation: There are 4 nodes in the graph.
35+
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
36+
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
37+
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
38+
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
39+
40+
'''
41+
42+
# Definition for a Node.
43+
class Node:
44+
def __init__(self, val = 0, neighbors = None):
45+
self.val = val
46+
self.neighbors = neighbors if neighbors is not None else []
47+
48+
class Solution:
49+
def cloneGraph(self, node: 'Node') -> 'Node':
50+
if not node:
51+
return None
52+
53+
node_map = {}
54+
55+
def clone(node):
56+
if node in node_map:
57+
return node_map[node]
58+
59+
clone_node = Node(node.val)
60+
node_map[node] = clone_node
61+
62+
63+
for neighbor in node.neighbors:
64+
clone_node.neighbors.append(clone(neighbor))
65+
66+
return clone_node
67+
68+
return clone(node) if node else None
69+
70+
# T: O(V + E), where V is the number of nodes and E is the number of edges in the input graph.
71+
72+
'''
73+
Explaination of nodes:
74+
75+
node1 = Node(1)
76+
node2 = Node(2)
77+
node3 = Node(3)
78+
node4 = Node(4)
79+
80+
node1.neighbors = [node2, node4]
81+
node2.neighbors = [node1, node3]
82+
node3.neighbors = [node2, node4]
83+
node4.neighbors = [node1, node3]
84+
85+
adjList = [node1, node2, node3, node4]
86+
'''
87+
88+
'''
89+
Exlanation(solution):
90+
91+
Example Graph---
92+
93+
1
94+
/ \
95+
2 3
96+
97+
1. The cloneGraph method is called with the node 1.
98+
2. node is not None, so we proceed.
99+
3. node_map is initialized as an empty dictionary.
100+
4. The clone function is defined.
101+
5. We enter the clone function with node as 1.
102+
6. node is not in node_map, so we create a clone clone_node with value 1.
103+
7. node_map now contains {1: clone_node}.
104+
8. We iterate through node.neighbors, which are 2 and 3.
105+
9. We recursively call clone on 2.
106+
10. node is not in node_map, so we create a clone clone_node with value 2.
107+
11. node_map now contains {1: clone_node_1, 2: clone_node_2}.
108+
12. We recursively call clone on 3.
109+
13. node is not in node_map, so we create a clone clone_node with value 3.
110+
14. node_map now contains {1: clone_node_1, 2: clone_node_2, 3: clone_node_3}.
111+
15. We finish cloning 1 and its neighbors, so we return clone_node_1 to the initial call.
112+
16. The cloneGraph method returns clone_node_1.
113+
114+
'''

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ Check the notes for the explaination - [Notes](https://stingy-shallot-4ea.notion
4141

4242
- [x] [Graphs](Graphs)
4343
- [x] [Number of Islands](Graphs/200-Number-of-Islands.py)
44+
- [x] [Clone Graph](Graphs/133-Clone-Graph.py)
4445

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /