-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloneGraph.java
More file actions
209 lines (147 loc) · 5.32 KB
/
CloneGraph.java
File metadata and controls
209 lines (147 loc) · 5.32 KB
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
package Algorithms.Graphs;
import java.util.*;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 18 Dec 2025
* @link 133. Clone Graph <a href="https://leetcode.com/problems/clone-graph">LeetCode Link</a>
* @topics Graph, Hash Table, DFS, BFS
* @companies Meta(10), Amazon(2), Google(2), Microsoft(2), Flexport(11), Bloomberg(7), Apple(3), Adobe(2), Oracle(2), Wix(2), Uber(2), Nvidia(2), Nutanix(2), Grammarly(2)
*/
public class CloneGraph {
public static class Node {
int val;
List<Node> neighbors;
Node() {
}
Node(int v) {
val = v;
neighbors = new ArrayList<>();
}
Node(int v, List<Node> nie) {
val = v;
neighbors = nie;
}
}
public static void main(String[] args) {
Node node = prepareGraph();
/*
1 -- 2
| |
3 -- 4
*/
System.out.println("cloneGraph using DFS: ");
printGraph(cloneGraphUsingDfs1(node));
System.out.println("cloneGraph using recursion: ");
printGraph(cloneGraphUsingRecursion(node));
System.out.println("cloneGraph using BFS: ");
printGraph(cloneGraphUsingBfs(node));
}
/**
* @TimeComplexity O(V + E)
* @SpaceComplexity O(V + E)
*/
public static Node cloneGraphUsingDfs1(Node node) {
if (node == null) return null;
Map<Integer, List<Integer>> adjList = new HashMap<>();
adjList.put(node.val, new ArrayList<>());
dfs(node, adjList);
Map<Integer, Node> valToNode = new HashMap<>();
for (Map.Entry<Integer, List<Integer>> entry : adjList.entrySet()) {
int val = entry.getKey();
if (!valToNode.containsKey(val)) valToNode.put(val, new Node(val));
for (int nei : entry.getValue()) {
if (!valToNode.containsKey(nei)) valToNode.put(nei, new Node(nei));
valToNode.get(val).neighbors.add(valToNode.get(nei));
}
}
return valToNode.get(node.val);
}
private static void dfs(Node node, Map<Integer, List<Integer>> adjList) {
for (Node nei : node.neighbors) {
adjList.get(node.val).add(nei.val);
if (!adjList.containsKey(nei.val)) {
adjList.put(nei.val, new ArrayList<>());
dfs(nei, adjList);
}
}
}
/**
* @TimeComplexity O(V + E)
* @SpaceComplexity O(V)
*/
private final static HashMap<Node, Node> oldToNew = new HashMap<>(); // visited
public static Node cloneGraphUsingRecursion(Node node) {
if (node == null) return null;
if (oldToNew.containsKey(node)) return oldToNew.get(node);
Node cloneNode = new Node(node.val);
oldToNew.put(node, cloneNode);
for (Node neighbor : node.neighbors) {
cloneNode.neighbors.add(cloneGraphUsingRecursion(neighbor));
}
return cloneNode;
}
/**
* @TimeComplexity O(V + E)
* @SpaceComplexity O(V)
*/
public static Node cloneGraphUsingDfs2(Node node) {
if (node == null) return null;
HashMap<Node, Node> oldToNew = new HashMap<>();
return dfs(node, oldToNew);
}
private static Node dfs(Node node, HashMap<Node, Node> oldToNew) {
if (oldToNew.containsKey(node)) return oldToNew.get(node);
Node cloneNode = new Node(node.val);
oldToNew.put(node, cloneNode);
for (Node neighbor : node.neighbors) {
cloneNode.neighbors.add(dfs(neighbor, oldToNew)); // if get nei node by oldToNew map or it'll created and calculate it's neighbors as well
}
return cloneNode;
}
/**
* @TimeComplexity O(V + E)
* @SpaceComplexity O(V)
*/
public static Node cloneGraphUsingBfs(Node node) {
if (node == null) return null;
HashMap<Node, Node> oldToNew = new HashMap<>(); // visited
bfs(node, oldToNew);
return oldToNew.get(node);
}
private static void bfs(Node node, HashMap<Node, Node> oldToNew) {
LinkedList<Node> queue = new LinkedList<>();
queue.add(node);
oldToNew.put(node, new Node(node.val, new ArrayList<>()));
while (!queue.isEmpty()) {
Node curr = queue.remove();
for (Node neighbor : curr.neighbors) {
if (!oldToNew.containsKey(neighbor)) {
oldToNew.put(neighbor, new Node(neighbor.val));
queue.add(neighbor);
}
oldToNew.get(curr).neighbors.add(oldToNew.get(neighbor));
}
}
}
private static Node prepareGraph() {
Node one = new Node(1);
Node two = new Node(2);
Node three = new Node(3);
Node four = new Node(4);
one.neighbors.add(two);
one.neighbors.add(four);
two.neighbors.add(one);
two.neighbors.add(three);
three.neighbors.add(two);
three.neighbors.add(four);
four.neighbors.add(one);
four.neighbors.add(three);
return one;
}
private static void printGraph(Node node) {
Map<Integer, List<Integer>> adjList = new HashMap<>();
adjList.put(node.val, new ArrayList<>());
dfs(node, adjList);
System.out.println(adjList);
}
}