-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepth_first_search.cpp
More file actions
164 lines (134 loc) · 3.49 KB
/
Copy pathdepth_first_search.cpp
File metadata and controls
164 lines (134 loc) · 3.49 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
/*
* depth_first_search
*/
#include <iostream>
#include <string>
#include <vector>
#define M 500
using namespace std;
struct Vertex {
int item;
int degree;
bool visited;
Vertex(int item) : item(item), degree(0), visited(false) {}
void increaseDegree() {
degree++;
}
void decreaseDegree() {
degree--;
}
void setVisited() {
visited = true;
}
bool getVisitied() {
return visited;
}
};
struct Edge {
Vertex *src;
Vertex *dst;
bool explored;
bool discovery;
bool back;
string item;
Edge(Vertex *src, Vertex *dst) : src(src), dst(dst), discovery(false), explored(false), back(false) {}
void setEdgeItem(string name) {
item = name;
}
void setDiscovery() {
discovery = true;
}
bool getDiscovery() {
return discovery;
}
void setExplored() {
explored = true;
}
bool getExplored() {
return explored;
}
void setBack() {
back = true;
}
bool getBack() {
return back;
}
};
class Graph {
private:
int v_sz;
int max_sz;
Vertex **v;
vector<Edge *> e;
Edge ***mat;
public:
Graph() {
v_sz = 0;
max_sz = M;
v = new Vertex *[M];
for (int i = 0; i < M; i++) {
v[i] = NULL;
}
mat = new Edge **[M];
for (int i = 0; i < M; i++) {
mat[i] = new Edge *[M];
for (int j = 0; j < M; j++) {
mat[i][j] = NULL;
}
}
}
void insertVertex(int data) {
if (v_sz + 1 > max_sz || v[data] != NULL) return;
v[data] = new Vertex(data);
v_sz++;
}
void insertEdge(int src, int dst) {
if (v[src] == NULL || v[dst] == NULL || mat[src][dst] != NULL || mat[dst][src] != NULL) return;
Edge *newEdge = new Edge(v[src], v[dst]);
e.push_back(newEdge);
v[src]->increaseDegree();
v[dst]->increaseDegree();
mat[src][dst] = newEdge;
mat[dst][src] = newEdge;
}
void removeEdge(int src, int dst) {
if (v[src] == NULL || v[dst] == NULL || mat[src][dst] == NULL || mat[dst][src] == NULL) return;
for (int i = 0; i < (int) e.size(); i++) {
if (e[i] == mat[src][dst] || e[i] == mat[dst][src]) {
e.erase(e.begin() + i);
mat[src][dst] = NULL;
mat[dst][src] = NULL;
v[src]->decreaseDegree();
v[dst]->decreaseDegree();
return;
}
}
}
void removeVertex(int data) {
if (v_sz - 1 < 0 || v[data] == NULL) return;
for (int i = 0; i < M; i++) {
removeEdge(data, i);
}
v[data] = NULL;
v_sz--;
}
void dfs(int start) {
if (v[start]->getVisitied() == true) return;
v[start]->setVisited();
for (int i = 0; i < M; i++) {
if (mat[start][i] != NULL) {
if (v[i]->getVisitied() == false) {
mat[start][i]->setExplored();
mat[start][i]->setDiscovery();
dfs(i);
} else {
if (mat[start][i]->getDiscovery() == true) continue;
mat[start][i]->setExplored();
mat[start][i]->setBack(); //���� ������ visited �̱� ������, Edge���� back ����
}
}
}
}
};
int main() {
}