Skip to content

Commit 749118d

Browse files
committed
clone a graph
1 parent f77a88e commit 749118d

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
- [x] [Terminology and Representations of Graphs](http://www.techiedelight.com/terminology-and-representations-of-graphs/)
136136
- [x] Given a list of edges and tasked to build your own graph from the edges
137137
- [x] Implement Dijkstra’s algorithm (implemented Weighted Graph to showcase this)
138-
138+
- [x] [Clone graph](https://leetcode.com/problems/clone-graph/)
139139
### Matrix
140140

141141
### Trees
@@ -275,7 +275,6 @@
275275
- [ ] Implement Floyd-Warshall algorithm
276276
- [ ] Implement Prim’s algorithm
277277
- [ ] Implement Kruskal’s algorithm
278-
- [ ] [Clone graph](https://leetcode.com/problems/clone-graph/)
279278
- [ ] [Course Schedule](https://leetcode.com/problems/course-schedule/)
280279
- [ ] [Alien Dictionary](https://leetcode.com/problems/alien-dictionary/)
281280
- [ ] [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dataStructures.graphs;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
// LeetCode Node for examples
7+
public class Node {
8+
public int val;
9+
public List<Node> neighbors;
10+
11+
public Node() {
12+
val = 0;
13+
neighbors = new ArrayList<>();
14+
}
15+
16+
public Node(int _val) {
17+
val = _val;
18+
neighbors = new ArrayList<>();
19+
}
20+
21+
public Node(int _val, ArrayList<Node> _neighbors) {
22+
val = _val;
23+
neighbors = _neighbors;
24+
}
25+
26+
// My addition: toString()
27+
28+
@Override
29+
public String toString() {
30+
return "Node{" +
31+
"val=" + val +
32+
", neighbors=" + neighbors +
33+
'}';
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dataStructures.graphs.cloner;
2+
3+
import dataStructures.graphs.Node;
4+
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class GraphCloner {
10+
11+
public static void main(String[] args) {
12+
Node n1 = new Node(1);
13+
Node n2 = new Node(2);
14+
n2.neighbors = List.of(n1);
15+
Node root = new Node(0);
16+
root.neighbors = List.of(n1, n2);
17+
System.out.println("OG: " + root);
18+
System.out.println("Clone: " + cloneGraph(root));
19+
}
20+
21+
// DFS
22+
public static Node cloneGraph(Node node) {
23+
Map<Node, Node> map = new HashMap<>();
24+
25+
if (node == null) return null;
26+
27+
if (map.containsKey(node)) {
28+
return map.get(node); // already cloned, use the previously cloned one
29+
}
30+
31+
// Clone the node before recursion
32+
Node clone = new Node(node.val); // create new node with same value
33+
map.put(node, clone);
34+
35+
// Clone all the neighbors recursively
36+
for (Node neighbor : node.neighbors) {
37+
clone.neighbors.add(cloneGraph(neighbor));
38+
}
39+
40+
return clone;
41+
}
42+
}

0 commit comments

Comments
 (0)