-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone_graph.cpp
More file actions
179 lines (143 loc) · 4.02 KB
/
Copy pathclone_graph.cpp
File metadata and controls
179 lines (143 loc) · 4.02 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
/*
Clone Graph
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors.
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use # as a separator for each node, and , as a separator for node label
and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}.
The graph has a total of three nodes, and therefore contains three parts as separated by #.
First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
Second node is labeled as 1. Connect node 1 to node 2.
Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 --- 2
/ \
\_/
*/
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>
using namespace std;
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
};
typedef UndirectedGraphNode graphNode;
typedef UndirectedGraphNode* graphPtr;
// according to the problem, graph is connected obviously
// BFS
class Solution
{
graphPtr bfs(graphPtr head)
{
unordered_map<graphPtr, graphPtr> clone;
queue<graphPtr> q;
q.push(head);
clone[head] = new graphNode(head->label);
// traverse each node and then copy
// nodes in the queue have already been copied
while (!q.empty()) {
graphPtr cur = q.front();
q.pop();
for (auto e : cur->neighbors) {
if (clone.count(e) != 0) { // already copied
clone[cur]->neighbors.push_back(clone[e]);
} else { // clone e
graphPtr copy = new graphNode(e->label);
clone[e] = copy;
q.push(e);
clone[cur]->neighbors.push_back(copy);
}
}
}
return clone[head];
}
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node)
{
if (node == nullptr) return node;
return bfs(node);
}
};
// DFS
class Solution2
{
void dfs(graphPtr cur, unordered_map<graphPtr, graphPtr> &clone)
{
// clone current node
if (clone.count(cur) == 0) {
clone[cur] = new graphNode(cur->label);
}
// clone neighbors
for (auto e : cur->neighbors) {
if (clone.count(e) == 0) { // clone e
dfs(e, clone);
}
clone[cur]->neighbors.push_back(clone[e]);
}
}
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node)
{
if (node == nullptr) return node;
unordered_map<graphPtr, graphPtr> clone;
dfs(node, clone);
return clone[node];
}
};
graphPtr create_graph()
{
graphPtr p0 = new graphNode(0);
graphPtr p1 = new graphNode(1);
graphPtr p2 = new graphNode(2);
p0->neighbors.push_back(p1);
p0->neighbors.push_back(p2);
p1->neighbors.push_back(p0);
p1->neighbors.push_back(p2);
p2->neighbors.push_back(p0);
p2->neighbors.push_back(p1);
p2->neighbors.push_back(p2);
return p0;
}
void print_graph(graphPtr head)
{
queue<graphPtr> q;
unordered_set<graphPtr> visited;
q.push(head);
visited.insert(head);
graphPtr cur = nullptr;
while (!q.empty()) {
cur = q.front();
q.pop();
cout << cur->label << ' ';
cout << cur << ' ';
for (auto e : cur->neighbors) {
cout << e << ' ';
if (visited.count(e) == 0) {
q.push(e);
visited.insert(e);
}
}
cout << endl;
}
}
int main(int argc, char *argv[])
{
Solution sol;
Solution2 sol2;
graphPtr g = create_graph();
graphPtr clone = sol.cloneGraph(g);
graphPtr clone2 = sol2.cloneGraph(g);
print_graph(g);
print_graph(clone);
print_graph(clone2);
return 0;
}