-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMGraph_DFS.cpp
More file actions
55 lines (55 loc) · 1.06 KB
/
MGraph_DFS.cpp
File metadata and controls
55 lines (55 loc) · 1.06 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
#include<iostream>
#include<string>
using namespace std;
const int MAXSIZE = 10;
template<class T>
class MGraph {
public:
MGraph(T a[], int n, int e);
void dfs(int v);
void print();
private:
bool visited[MAXSIZE] = { false };
T vertex[MAXSIZE];
int arc[MAXSIZE][MAXSIZE];
int vnum, arcnum;
};
template<class T>
MGraph<T>::MGraph(T a[], int n, int e) {
int i, j, k;
vnum = n;
arcnum = e;
for (k = 0; k < n; k++) {
vertex[k] = a[k];
}
memset(arc, 0, sizeof(arc));
for (k = 0; k < e; k++) {
cin >> i >> j;
arc[i][j] = 1;
arc[j][i] = arc[i][j];
}
}
template<class T>
void MGraph<T>::dfs(int v){
cout << vertex[v] << endl;
visited[v] = true;
for (int j = 0; j < vnum; j++) {
if (arc[v][j] == 1 && !visited[j])
dfs(j);
}
}
template<class T>
void MGraph<T>::print(){
for (int i = 0; i < vnum; i++) {
for (int j = 0; j < vnum; j++) {
cout << arc[i][j] << ' ';
}
cout << endl;
}
}
int main() {
string a[6] = { "v1","v2","v3","v4","v5","v6" };
MGraph<string> A(a, 6, 6);
A.print();
A.dfs(0);
}