-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.hh
More file actions
190 lines (179 loc) · 5.57 KB
/
Graph.hh
File metadata and controls
190 lines (179 loc) · 5.57 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
#include <cassert>
#include <deque>
#include <format>
#include <iostream>
#include <random>
#include <type_traits>
struct Graph {
int V;
int E [[maybe_unused]];
std::deque<std::deque<int>> adj;
Graph(int v) : V{v}, E{0}, adj(v) {}
void addEdge(int v, int w) {
assert(0 <= v && v < V && 0 <= w && w < V);
adj[v].push_back(w), adj[w].push_back(v);
E++;
}
};
struct Edge {
int v, w, weight;
Edge() {}
Edge(int v, int w, int weight) : v{v}, w{w}, weight{weight} {}
int other(int x) const {
assert(x == v || x == w);
return x == v ? w : v;
}
bool operator>(const Edge &rhs) const { return weight > rhs.weight; }
bool operator<(const Edge &rhs) const { return weight < rhs.weight; }
bool operator<=(const Edge &rhs) const { return weight <= rhs.weight; }
bool operator>=(const Edge &rhs) const { return weight >= rhs.weight; }
bool operator!=(const Edge &rhs) const { return weight != rhs.weight; }
bool operator==(const Edge &rhs) const { return weight == rhs.weight; }
};
struct EdgeWeightedGraph {
int V;
int E [[maybe_unused]];
std::deque<std::deque<Edge>> adj;
EdgeWeightedGraph(int v) : V{v}, E{0}, adj(v) {}
void addEdge(Edge e) {
int v{e.v}, w{e.w};
assert(0 <= v && v < V && 0 <= w && w < V);
adj[v].push_back(e), adj[w].push_back(e);
E++;
}
auto edges() const {
std::deque<Edge> deck;
for (int v = 0; v < V; v++) {
for (const auto &e : adj[v])
if (e.other(v) > v) deck.push_back(e);
}
return deck;
}
};
struct Digraph {
int V;
int E [[maybe_unused]];
std::deque<std::deque<int>> adj;
Digraph(int v) : V{v}, E{0}, adj(v) {}
void addEdge(int v, int w) {
assert(0 <= v && v < V && 0 <= w && w < V);
adj[v].push_back(w);
E++;
}
Digraph reverse() const {
Digraph reverse(V);
for (int v = 0; v < V; v++) {
for (int w : adj[v]) reverse.addEdge(w, v);
}
return reverse;
}
};
struct DirectedEdge {
int from, to, weight;
DirectedEdge() {}
DirectedEdge(int from, int to, int weight)
: from{from}, to{to}, weight{weight} {}
bool operator>(const DirectedEdge &r) const { return weight > r.weight; }
bool operator<(const DirectedEdge &r) const { return weight < r.weight; }
bool operator<=(const DirectedEdge &r) const { return weight <= r.weight; }
bool operator>=(const DirectedEdge &r) const { return weight >= r.weight; }
bool operator!=(const DirectedEdge &r) const { return weight != r.weight; }
bool operator==(const DirectedEdge &r) const { return weight == r.weight; }
};
struct EdgeWeightedDigraph {
int V;
int E [[maybe_unused]];
std::deque<std::deque<DirectedEdge>> adj;
EdgeWeightedDigraph(int v) : V{v}, E{0}, adj(v) {}
void addEdge(DirectedEdge e) {
assert(0 <= e.from && e.from < V);
assert(0 <= e.to && e.to < V);
adj[e.from].push_back(e);
E++;
}
EdgeWeightedDigraph reverse() const {
EdgeWeightedDigraph reverse(V);
for (int v = 0; v < V; v++) {
for (const auto &e : adj[v]) reverse.addEdge({e.to, e.from, e.weight});
}
return reverse;
}
auto edges() const {
std::deque<DirectedEdge> deck;
for (int v = 0; v < V; v++) {
for (const auto &e : adj[v]) deck.push_back(e);
}
return deck;
}
};
template <typename... Args>
void print(std::format_string<Args...> fmt, Args &&...args) {
std::cout << std::vformat(fmt.get(), std::make_format_args(args...));
}
template <typename... Args>
void println(std::format_string<Args...> fmt, Args &&...args) {
std::cout << std::vformat(fmt.get(), std::make_format_args(args...));
std::cout << '\n';
}
// one generator to rule them all
#ifdef __cpp_lib_concepts
template <class T>
concept isGraphType = std::is_same_v<T, Graph> or std::is_same_v<T, Digraph> or
std::is_same_v<T, EdgeWeightedGraph> or
std::is_same_v<T, EdgeWeightedDigraph>;
#endif
template <class T>
void generateGraph(T &G, int e)
#ifdef __cpp_lib_concepts
requires isGraphType<T>
#endif
{
std::mt19937 mt(std::random_device{}());
std::uniform_int_distribution randV(0, G.V - 1), randE(0, 7);
std::deque<int> a(e, -1), b(e, -1);
int i{0};
while (i < e) {
a[i] = randV(mt), b[i] = randV(mt);
// no self cycle
if (a[i] == b[i]) continue;
int j{0};
for (; j < i; ++j) {
// no parallel edge
if (a[i] == a[j] && b[i] == b[j]) break;
// no cycle with two vertex
if (a[i] == b[j] && b[i] == a[j]) break;
}
if (i != j) continue;
if constexpr (std::is_same_v<T, Graph> || std::is_same_v<T, Digraph>)
G.addEdge(a[i], b[i]);
else if constexpr (std::is_same_v<T, EdgeWeightedGraph>)
G.addEdge({a[i], b[i], randE(mt)});
else if constexpr (std::is_same_v<T, EdgeWeightedDigraph>)
G.addEdge({a[i], b[i], randE(mt)});
++i;
}
}
// one printer to rule them all
template <class T>
void printGraph(T &&G)
#ifdef __cpp_lib_concepts
requires isGraphType<std::remove_reference_t<T>>
#endif
{
for (int i = 0; i < G.V; ++i) {
print("v{}\t", i);
for (const auto &e : G.adj[i]) {
if constexpr (std::is_same_v<std::remove_reference_t<T>, Graph> or
std::is_same_v<std::remove_reference_t<T>, Digraph>)
print("[{}<->{}]\t", i, e);
else if constexpr (std::is_same_v<std::remove_reference_t<T>,
EdgeWeightedGraph>)
print("[{}->{}|{:+}]\t", e.v, e.w, e.weight);
else if constexpr (std::is_same_v<std::remove_reference_t<T>,
EdgeWeightedDigraph>)
print("[{}->{}|{:+}]\t", e.from, e.to, e.weight);
}
std::cout << '\n';
}
}