-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.h
More file actions
66 lines (59 loc) · 1.56 KB
/
Graph.h
File metadata and controls
66 lines (59 loc) · 1.56 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
#ifndef GRAPH_H
#define GRAPH_H
#include <istream>
#include <vector>
using namespace std;
#define vi vector<int>
class Graph{
private:
int sodinh;
int socanh;
vi *danhsachke;
bool *visited;
public:
Graph(int V){
sodinh = V;
socanh = 0;
danhsachke = new vi [sodinh];
visited = new bool [sodinh];
}
Graph (istream &in){
int v, w;
in>>sodinh;
in>>socanh;
for (int i=0; i < socanh; i++){
in>>v>>w;
danhsachke[v].push_back(w);
danhsachke[w].push_back(v);
}
}
void addEdge(int v, int w){
socanh++;
danhsachke[v].push_back(w);
danhsachke[w].push_back(v);
}
vi getAdj(int v) const {
return danhsachke[v]; // trả ra danh sách kề tại mỗi đỉnh v
}
int getV(){
return sodinh;
}
int getE(){
return socanh;
}
int getDeg(int v) const { // Tính số đỉnh nút v
vi kev = getAdj(v);
return kev.size();
}
void explore(int v) {
vi danhsachke = getAdj(v);
visited[v] = true;
cout << v << " ";
for (int i = 0; i < danhsachke.size(); i++) {
if (visited[danhsachke[i]] == false) {
explore(danhsachke[i]);
}
}
}
};
#endif