-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClone Graph.cpp
121 lines (100 loc) · 4.32 KB
/
Clone Graph.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
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
// http://oj.leetcode.com/problems/clone-graph/
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
unordered_map<int, UndirectedGraphNode *> visited;
UndirectedGraphNode *find_neighbor(vector<UndirectedGraphNode *> &neighbors, const int label) {
for (int i = 0; i < neighbors.size(); i++) {
if (label == neighbors[i]->label) {
return neighbors[i];
}
}
return nullptr;
}
// using DFS solution
void dfs(UndirectedGraphNode *old_node, UndirectedGraphNode *new_node) {
if (old_node == nullptr || new_node == nullptr)
return;
unordered_set<int> visited_neighbors;
visited.emplace(old_node->label, new_node);
for (int i = 0; i < old_node->neighbors.size(); i++) {
UndirectedGraphNode *cur = old_node->neighbors[i];
// handle duplicate neighbors
if (visited_neighbors.count(cur->label) == 0) {
visited_neighbors.emplace(cur->label);
} else {
UndirectedGraphNode *dup = find_neighbor(new_node->neighbors, cur->label);
if (dup != nullptr) {
new_node->neighbors.push_back(dup);
}
continue;
}
// handle loop
if (visited.count(cur->label) == 0) {
UndirectedGraphNode *new_cur = new UndirectedGraphNode(cur->label);
new_node->neighbors.push_back(new_cur);
visited.emplace(cur->label, new_cur);
dfs(cur, new_cur);
} else {
UndirectedGraphNode *dup = visited[cur->label];
if (dup != nullptr) {
new_node->neighbors.push_back(dup);
}
}
}
}
// using BFS solution
void bfs(UndirectedGraphNode *old_node, UndirectedGraphNode *new_node) {
if (old_node == nullptr || new_node == nullptr)
return;
list<UndirectedGraphNode *> old_q, new_q;
old_q.push_back(old_node);
new_q.push_back(new_node);
while (!old_q.empty()) {
unordered_set<int> visited_neighbors;
UndirectedGraphNode *old_cur = old_q.front();
UndirectedGraphNode *new_cur = new_q.front();
old_q.pop_front();
new_q.pop_front();
visited.emplace(old_cur->label, new_cur);
for (int i = 0; i < old_cur->neighbors.size(); i++) {
UndirectedGraphNode *old_ncur = old_cur->neighbors[i];
if (visited_neighbors.count(old_ncur->label) == 0) {
visited_neighbors.emplace(old_ncur->label);
} else {
UndirectedGraphNode *dup = find_neighbor(new_cur->neighbors, old_ncur->label);
if (dup != nullptr) {
new_cur->neighbors.push_back(dup);
}
continue;
}
if (visited.count(old_ncur->label) == 0) {
UndirectedGraphNode *new_ncur = new UndirectedGraphNode(old_ncur->label);
visited.emplace(old_ncur->label, new_ncur);
new_cur->neighbors.push_back(new_ncur);
old_q.push_back(old_ncur);
new_q.push_back(new_ncur);
} else {
UndirectedGraphNode *dup = visited[old_ncur->label];
if (dup != nullptr) {
new_cur->neighbors.push_back(dup);
}
}
}
}
}
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (node == nullptr)
return nullptr;
UndirectedGraphNode *new_node = new UndirectedGraphNode(node->label);
dfs(node, new_node);
return new_node;
}
};