-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_133_Graph_CloneGraph.cpp
95 lines (70 loc) · 2.42 KB
/
LC_133_Graph_CloneGraph.cpp
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
/* https://leetcode.com/problems/clone-graph/
https://practice.geeksforgeeks.org/problems/clone-graph/0
* 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.
*/
#include<bits/stdc++.h>
using namespace std;
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
class Solution {
public:
Node* cloneGraph(Node* gnode) {
if(!gnode) return nullptr;
//USING BFS
// return bfsCloneGraph(gnode);
//USING DFS
unordered_map<Node*, Node*> cm;
dfsCloneGraph(gnode, cm);
return cm[gnode];
}//cloneGraph()
Node* bfsCloneGraph(Node *g){
// if(!g) return nullptr; // if graph is null then return;
unordered_map<Node *, Node*> cm; //cloned map (orig, cloned) unordered map (unique keys)
queue<Node *> q; // for BFS Traversal
Node *cg = new Node(g->val); //creating new clone node
cm.insert({g, cg}); // cm[g] = cg;
q.push(g);
while(!q.empty()){
Node *s = q.front(); q.pop();
// iterate over all children of the node s. Node *w =
for(auto w: s->neighbors){
// if node is not created ie not visited yet
if(!cm[w]){
cg = new Node(w->val);
cm[w] = cg;
q.push(w);
}
// add the nodes to the adj list of new node.
cm[s]->neighbors.push_back(cm[w]);
}//for w
}// while q
return cm[g]; // that is source cloned map
} //bfsCloneGraph
void dfsCloneGraph(Node *g, unordered_map<Node*, Node*> &cm){
// if node is created ie already visited
if(cm[g]) return;
cm[g] = new Node(g->val);
for (auto w: g->neighbors)
dfsCloneGraph(w, cm);
for (auto w: g->neighbors)
cm[g]->neighbors.push_back(cm[w]);
}//dfsCloneGraph
};