Pixie
Loading...
Searching...
No Matches
utils.h
1#pragma once
2
3#include <pixie/louds_tree.h>
4
5#include <queue>
6#include <random>
7#include <vector>
8
10
11std::vector<std::vector<size_t>> generate_random_tree(size_t tree_size,
12 std::mt19937_64& rng) {
13 if (tree_size == 0) {
14 return {};
15 }
16 std::vector<std::vector<size_t>> adj(tree_size);
17 adj[0].push_back(0);
18 for (size_t i = 1; i < tree_size; i++) {
19 size_t parent = rng() % i;
20 adj[i].push_back(parent);
21 adj[parent].push_back(i);
22 }
23 return adj;
24}
25
26std::vector<std::vector<size_t>> bfs_order(
27 size_t tree_size,
28 const std::vector<std::vector<size_t>>& adj) {
29 std::vector<std::vector<size_t>> bfs_adj(tree_size);
30 std::queue<std::pair<size_t, size_t>> q;
31 bfs_adj[0].push_back(0);
32 q.push({0, 0});
33 int cnt = 1;
34 while (!q.empty()) {
35 size_t old_v = q.front().first;
36 size_t cur_v = q.front().second;
37 q.pop();
38 for (size_t i = 1; i < adj[old_v].size(); i++) {
39 size_t old_u = adj[old_v][i];
40 size_t cur_u = cnt++;
41 q.push({old_u, cur_u});
42 bfs_adj[cur_u].push_back(cur_v);
43 bfs_adj[cur_v].push_back(cur_u);
44 }
45 }
46 return bfs_adj;
47}
48
49std::vector<uint64_t> adj_to_louds(
50 size_t tree_size,
51 const std::vector<std::vector<size_t>>& adj) {
52 size_t louds_size = tree_size * 2 - 1;
53 std::vector<uint64_t> louds((louds_size + 63) / 64, 0);
54 size_t pos = 0;
55 for (size_t i = 0; i < tree_size; i++) {
56 louds[pos >> 6] = louds[pos >> 6] | (1ULL << (pos & 63));
57 pos += adj[i].size();
58 }
59 return louds;
60}
61
63 size_t number;
64};
65
66bool operator==(const AdjListNode& a, const LoudsNode& b) {
67 return a.number == b.number;
68}
69
70bool operator==(const LoudsNode& b, const AdjListNode& a) {
71 return a.number == b.number;
72}
73
75 private:
76 std::vector<std::vector<size_t>> adj;
77
78 public:
82 explicit AdjListTree(const std::vector<std::vector<size_t>>& adjacency_list)
83 : adj(adjacency_list) {}
84
88 AdjListNode root() const { return AdjListNode(0); }
89
93 bool is_leaf(const AdjListNode& node) const {
94 return adj[node.number].size() <= 1;
95 }
96
100 bool is_root(const AdjListNode& node) const { return node.number == 0; }
101
105 size_t degree(const AdjListNode& node) const {
106 return adj[node.number].size() - 1;
107 }
108
113 AdjListNode child(const AdjListNode& node, size_t i) const {
114 return AdjListNode(adj[node.number][i + 1]);
115 }
116
121 return AdjListNode(adj[node.number][1]);
122 }
123
128 AdjListNode parent(const AdjListNode& node) const {
129 return AdjListNode(adj[node.number][0]);
130 }
131
135 bool is_last_child(const AdjListNode& node) const {
136 size_t p = parent(node).number;
137 return adj[p].back() == node.number;
138 }
139
144 return AdjListNode(node.number + 1);
145 }
146};
size_t degree(const AdjListNode &node) const
Returns the number of children of node.
Definition utils.h:105
AdjListTree(const std::vector< std::vector< size_t > > &adjacency_list)
Constructor from adjacency list (root is 0)
Definition utils.h:82
AdjListNode next_sibling(const AdjListNode &node) const
Returns next sibling of a node.
Definition utils.h:143
AdjListNode first_child(const AdjListNode &node) const
Returns the first child of node.
Definition utils.h:120
AdjListNode parent(const AdjListNode &node) const
Returns the parent of a node if node is not root, else returns root.
Definition utils.h:128
AdjListNode root() const
Returns the root node.
Definition utils.h:88
AdjListNode child(const AdjListNode &node, size_t i) const
Returns the i-th child of node Indexing starts at 0.
Definition utils.h:113
bool is_root(const AdjListNode &node) const
Checks if node is a root.
Definition utils.h:100
bool is_leaf(const AdjListNode &node) const
Checks if node is a leaf.
Definition utils.h:93
bool is_last_child(const AdjListNode &node) const
Indicates if node is last child.
Definition utils.h:135
Definition utils.h:62
A node class of LOUDS tree.
Definition louds_tree.h:13