summaryrefslogtreecommitdiff
path: root/graph/havelHakimi.cpp
diff options
context:
space:
mode:
authormzuenni <michi.zuendorf@gmail.com>2022-06-27 17:19:28 +0200
committermzuenni <michi.zuendorf@gmail.com>2022-06-27 17:19:28 +0200
commit5ab8a5088b729a9953b8dff1b2a985dc8fb2098b (patch)
treeed40d6936c0e9eee40ba62751cbf99ecddbaddc2 /graph/havelHakimi.cpp
parentadabbad9c51cf7cd3874bfde8eac1fbcf84fec10 (diff)
updated tcr
Diffstat (limited to 'graph/havelHakimi.cpp')
-rw-r--r--graph/havelHakimi.cpp19
1 files changed, 19 insertions, 0 deletions
diff --git a/graph/havelHakimi.cpp b/graph/havelHakimi.cpp
new file mode 100644
index 0000000..9fb9846
--- /dev/null
+++ b/graph/havelHakimi.cpp
@@ -0,0 +1,19 @@
+vector<vector<int>> havelHakimi(const vector<int>& deg) {
+ priority_queue<pair<int, int>> pq;
+ for (int i = 0; i < sz(deg); i++) pq.push({deg[i], i});
+ vector<vector<int>> adj;
+ while (!pq.empty()) {
+ auto v = pq.top(); pq.pop();
+ if (sz(pq) < v.first) return {}; //ERROR
+ vector<pair<int, int>> todo;
+ for (int i = 0; i < v.first; i++) {
+ auto u = pq.top(); pq.pop();
+ adj[v.second].push_back(u.second);
+ adj[u.second].push_back(v.second);
+ u.first--;
+ if (u.first > 0) todo.push_back(u);
+ }
+ for (auto e : todo) pq.push(e);
+ }
+ return adj;
+}