-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeighted_Graph.cc
More file actions
330 lines (252 loc) · 6.57 KB
/
Copy pathWeighted_Graph.cc
File metadata and controls
330 lines (252 loc) · 6.57 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
class WeightedGraph {
int numVertex; //vertex의 수
// pair = 두 개의 값을 묶는 구조체(struct로 표현가능)
vector<vector<pair<int, int>>> List; // 인접
public:
// 생성자
WeightedGraph(int nv) : numVertex(nv) {
List.resize(numVertex);
}
// Edge 추가
void addEdge(int u, int v, int weight);
// Graph 내용 출력
void printGraph();
//
// 과제 (구현하여야 하는 멤버 함수)
//
/* bool isConnected();
설명: Connected Graph 인지 조사
Connected Graph 이면 true 아니면 false 반환 */
bool isConnected()
{
vector<bool> visited(numVertex, false);
queue<int> q;
q.push(0);
visited[0] = true;
int visitedCount = 1;
while (!q.empty())
{
int i = q.front();
q.pop();
for (auto& neighbor : List[i])
{
int v = neighbor.first;
if (!visited[v])
{
visited[v] = true;
visitedCount++;
q.push(v);
}
}
}
return visitedCount == numVertex;
}
/* bool isAdjacent(int u, int v);
설명 vertex u와 vertex v 가 인접한지 조사
인접한 vertex 이면 true, 아니면 false 반환 */
bool isAdjacent(int u, int v)
{
for (auto& neighbor : List[u])
{
int s = neighbor.first;
if (s == v)
{
return true;
}
}
return false;
}
/* void BFS(int s);
설명: s(시작 vertex 번호)로 부터 너비우선 탐색한 후
방문한 vertex 번호를 출력하는 함수 */
void BFS(int s)
{
vector<bool> visited(numVertex, false);
queue<int> q;
q.push(s);
visited[s] = true;
while (!q.empty())
{
int u = q.front();
q.pop();
cout << u << " ";
for (auto& neighbor : List[u])
{
int v = neighbor.first;
if (!visited[v])
{
visited[v] = true;
q.push(v);
}
}
}
cout << endl;
}
/* void DFS(int s);
설명: s(시작 vertex 번호)로 부터 깊이우선 탐색한 후
방문한 vertex 번호를 출력하는 함수 */
void DFS(int s)
{
vector<bool> visited(numVertex, false);
stack<int> st;
st.push(s);
while (!st.empty())
{
int u = st.top();
st.pop();
if (!visited[u])
{
cout << u << " ";
visited[u] = true;
//역순으로 push
for (auto it = List[u].rbegin(); it != List[u].rend(); ++it)
{
if (!visited[it->first])
{
st.push(it->first);
}
}
}
}
cout << endl;
}
/* void minSpanningTree();
설명: prime 알고리즘을 이용하여 minimum Spanning Tree
찾아 Tree의 에지를 출력하는 함수*/
void minSpanningTree()
{
vector<bool> inMST(numVertex, false);
// 우선순위 큐: (가중치, (출발 노드, 도착 노드))
// greater<> = 기본적으로 최대 힙을 가지는 우선순위 큐를 최소 힙으로 정렬하는 객체
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<>> pq;
inMST[0] = true;
for (auto& [v, w] : List[0])
{
pq.push({ w, {0, v} });
}
while (!pq.empty())
{
auto [weight, edge] = pq.top();
pq.pop();
auto [u, v] = edge;
if (inMST[v])
continue;
inMST[v] = true;
cout << u << " - " << v << " (weight " << weight << ")\n";
for (auto& [next, w] : List[v])
{
if (!inMST[next])
{
pq.push({ w, {v, next} });
}
}
}
}
/* void dijkstra(int s);
설명: s(시작 vertex 번호)로 다른 vertex까지의 distance 값을
출력하는 함수 */
void dijkstra(int s)
{
// 한 정점에서 다른 정점까지의 초기거리를 최대로 초기화 (int가 표현가능한 최대값)
vector<int> dist(numVertex, numeric_limits<int>::max());
dist[s] = 0;
// greater<> = 기본적으로 최대 힙을 가지는 우선순위 큐를 최소 힙으로 정렬하는 객체
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
pq.push({ 0, s });
while (!pq.empty())
{
auto [d, u] = pq.top();
pq.pop();
if (d > dist[u])
continue;
for (auto& [v, w] : List[u])
{
if (dist[v] > dist[u] + w)
{
dist[v] = dist[u] + w;
pq.push({ dist[v], v });
}
}
}
for (int i = 0; i < numVertex; i++)
{
cout << "Distance from " << s << " to " << i << ": ";
if (dist[i] == numeric_limits<int>::max())
cout << "INF\n";
else
cout << dist[i] << "\n";
}
}
};
void WeightedGraph::addEdge(int u, int v, int weight) {
List[u].push_back({v, weight});
List[v].push_back({u, weight});
}
void WeightedGraph::printGraph()
{
cout << "Graph adjecent list with weight" << endl;
for(int i = 0; i < numVertex; i++) {
cout << i << ": ";
for(auto& neighbor : List[i])
cout << "[" << neighbor.first << ", " << neighbor.second << "]";
cout << endl;
}
}
int main(int argc, char const *argv[])
{
// 8개의 정점을 가지므로 9를 넣음
WeightedGraph g(9);
//자료구조_마지막_과제 상에 있는 그래프 구현 후 테스트
g.addEdge(0, 1, 15);
g.addEdge(0, 2, 25);
g.addEdge(1, 4, 10);
g.addEdge(1, 7, 5);
g.addEdge(1, 8, 25);
g.addEdge(2, 3, 10);
g.addEdge(2, 4, 20);
g.addEdge(4, 5, 10);
g.addEdge(4, 6, 5);
g.addEdge(7, 8, 15);
g.printGraph();
cout << "\nIs graph connected? " << (g.isConnected() ? "Yes" : "No") << endl;
cout << "Is 0 adjacent to 3? " << (g.isAdjacent(0, 3) ? "Yes" : "No") << endl;
cout << "Is 1 adjacent to 2? " << (g.isAdjacent(1, 2) ? "Yes" : "No") << endl;
cout << "\nBFS from 0:";
g.BFS(0);
cout << "DFS from 0:";
g.DFS(0);
cout << "\nMinimum Spanning Tree:\n";
g.minSpanningTree();
cout << "\nDijkstra from node 0:\n";
g.dijkstra(0);
// Not Connected Graph 구현 및 테스트
WeightedGraph ng(5);
ng.addEdge(0, 1, 3);
ng.addEdge(0, 2, 5);
ng.addEdge(0, 3, 6);
ng.addEdge(1, 2, 2);
ng.addEdge(1, 3, 7);
ng.addEdge(2, 3, 3);
ng.printGraph();
cout << "\nIs graph connected? " << (ng.isConnected() ? "Yes" : "No") << endl;
cout << "Is 0 adjacent to 3? " << (ng.isAdjacent(0, 3) ? "Yes" : "No") << endl;
cout << "Is 1 adjacent to 2? " << (ng.isAdjacent(1, 2) ? "Yes" : "No") << endl;
cout << "\nIs 4 adjacent to 0? " << (ng.isAdjacent(4, 0) ? "Yes" : "No") << endl;
cout << "Is 4 adjacent to 1? " << (ng.isAdjacent(4, 1) ? "Yes" : "No") << endl;
cout << "Is 4 adjacent to 2? " << (ng.isAdjacent(4, 2) ? "Yes" : "No") << endl;
cout << "Is 4 adjacent to 3? " << (ng.isAdjacent(4, 3) ? "Yes" : "No") << endl;
cout << "\nBFS from 0:";
ng.BFS(0);
cout << "DFS from 0:";
ng.DFS(0);
cout << "\nMinimum Spanning Tree:\n" << endl;;
ng.minSpanningTree();
cout << "\nDijkstra from node 0:\n" << endl;;
ng.dijkstra(0);
return 0;
}