-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphClass.cpp
69 lines (60 loc) · 1.42 KB
/
GraphClass.cpp
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
#include <vector>
#include <map>
using namespace std;
#define pair_ii pair<int, int>
class Graph_Weighted_Directed
{
public:
map<int, vector<pair_ii>> graph;
void add_edge(int from, int to, int weight)
{
graph[from].push_back({to, weight});
}
vector<int> nodes()
{
vector<int> res;
// map<int, vector<pair_ii>>::iterator itr;
for (auto itr = graph.begin(); itr != graph.end(); itr++)
res.push_back(itr->first);
return res;
}
vector<pair_ii> neighbors(int src)
{
if (graph.find(src) == graph.end())
return vector<pair_ii>();
return graph.at(src);
}
};
class Graph_Weighted : public Graph_Weighted_Directed
{
public:
void add_edge(int a, int b, int weight)
{
Graph_Weighted_Directed::add_edge(a, b, weight);
Graph_Weighted_Directed::add_edge(b, a, weight);
}
};
class Graph_Directed : public Graph_Weighted_Directed
{
public:
void add_edge(int from, int to)
{
Graph_Weighted_Directed::add_edge(from, to, 0);
}
vector<int> neighbors(int src)
{
vector<int> res;
for (pair_ii p : Graph_Weighted_Directed::neighbors(src))
res.push_back(p.first);
return res;
}
};
class Graph : public Graph_Directed
{
public:
void add_edge(int a, int b)
{
Graph_Directed::add_edge(a, b);
Graph_Directed::add_edge(b, a);
}
};