-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathGraphBase.h
48 lines (35 loc) · 1.04 KB
/
GraphBase.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
#ifndef ALGS4_GRAPHBASE_H
#define ALGS4_GRAPHBASE_H
#include <iostream>
#include <list>
#include <vector>
template<typename T>
class GraphBase {
protected:
const int V_; // number of vertices
int E_; // number of edges
std::vector<std::list<T> > adj_; // adjacency lists
public:
explicit GraphBase(int V) : V_(V), E_(0), adj_(V) {}
explicit GraphBase(std::istream &in);
virtual ~GraphBase() = default;
int V() const { return V_; }
int E() const { return E_; }
std::list<T> adj(int v) const { return adj_[v]; }
};
template<typename T>
GraphBase<T>::GraphBase(std::istream &in) : GraphBase([&in] {
int i;
return in >> i, i;
}()) {}
template<typename T>
std::ostream &operator<<(std::ostream &os, const GraphBase<T> &G) {
os << G.V() << " vertices, " << G.E() << " edges" << std::endl;
for (int v = 0; v < G.V(); ++v) {
os << v << ": ";
for (T w: G.adj(v)) os << w << " ";
os << std::endl;
}
return os;
}
#endif //ALGS4_GRAPHBASE_H