-
Notifications
You must be signed in to change notification settings - Fork 2
/
l_graph.h
91 lines (74 loc) · 1.5 KB
/
l_graph.h
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
#ifndef _L_GRAPH
#define _L_GRAPH
#include "l.h"
struct Graph{
vector<vector<int> > adj;
Graph(int V) {
for (int i = 0; i < V; i++) {
adj.push_back(vector<int>());
}
}
Graph(const char* filename) {
FILE* fp = fopen(filename, "r");
assert(fp);
int V, E;
fscanf(fp, "%d", &V);
fscanf(fp, "%d", &E);
for (int i = 0; i < V; i++) {
adj.push_back(vector<int>());
}
int v, w;
for (int i = 0; i < E; ++i) {
fscanf(fp, "%d %d", &v, &w);
adj[v].push_back(w);
adj[w].push_back(v);
}
}
};
void p_graph(Graph* g) {
cout << "graph: " << endl;
int i = 0;
for (auto v : g->adj) {
cout << i++ << ": ";
for (auto i : v) cout << i << " ";
cout << endl;
}
}
void dfs(Graph* g, int v, vector<bool> &marked, vector<int> &result) {
marked[v] = true;
result.push_back(v);
for (auto w : g->adj[v]) {
if (!marked[w]) {
dfs(g, w, marked, result);
}
}
}
vector<int> dfs(Graph* g) {
vector<bool> marked(g->adj.size());
vector<int> result;
for (auto i : marked) i = false;
int s = 0;
dfs(g, s, marked, result);
return result;
}
vector<int> bfs(Graph* g) {
vector<int> result;
vector<bool> marked(g->adj.size());
int s = 0;
queue<int> Q;
marked[s] = true;
Q.push(s);
while (!Q.empty()) {
int v = Q.front();
Q.pop();
result.push_back(v);
for (int w : g->adj[v]) {
if (!marked[w]) {
marked[w] = true;
Q.push(w);
}
}
}
return result;
}
#endif // _L_GRAPH