File tree Expand file tree Collapse file tree 3 files changed +78
-2
lines changed
src/dataStructures/graphs Expand file tree Collapse file tree 3 files changed +78
-2
lines changed Original file line number Diff line number Diff line change 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
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/ )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments