允许通过子集的迭代和随机选择进行更改的数据结构 (C++)
Data Structure(s) Allowing For Alteration Through Iteration and Random Selection From Subset (C++)
给定一个固定大小的对象数组 A,假设这些对象中的一小部分满足特定条件 B。我想以大致相等的频率完成三个任务:
- 我希望能够在通过索引访问 A 中的对象时随时将当前不满足条件 B 的对象更改为满足条件 B。
- 我希望能够在按索引访问 A 中的对象时将当前满足条件 B 的对象更改为不再满足条件 B。
- 我还希望能够仅从满足标准 B 的对象中选择一个随机对象。
所有任务都应该能够在恒定时间内完成,或者尽可能接近恒定时间,不依赖于A中对象的数量并且不依赖于满足标准 B 的对象数量。如果恒定时间是不可能的(我怀疑是这种情况),那么我想尽快完成两者,同时考虑我之前提到的频率。如果这两项任务被重复多次,哪种数据结构适合于这两项任务?
以我下面的 C++ 实现为例。虽然计时部分(代码中重复多次的部分)与 A(alltiles)的整体大小无关,但时间复杂度线性取决于 B(bluetiles)(无论 alltiles 的数量是否增加或不),严重减慢代码速度。
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib>
#include <algorithm>
using namespace std;
enum color {RED, GREEN, BLUE};
const int NUM_ATTEMPTS = 10000;
const int INITIAL_NUM_BLUE_TILES = 1000;
const int TOTAL_TILES = 1000000;
struct tile
{
int color = RED;
};
struct room
{
vector<tile> alltiles;
vector<tile*> bluetiles;
room(vector<tile> v) : alltiles(v) {}
};
int main()
{
srand (time(NULL));
// set up the initial room, time complexity here is irrelevant
room myroom(vector<tile>(1*TOTAL_TILES));
for(int i = 0; i < INITIAL_NUM_BLUE_TILES; i++)
{
myroom.alltiles[i].color = BLUE;
myroom.bluetiles.push_back(&myroom.alltiles[i]);
}
auto begin = std::chrono::high_resolution_clock::now();
for(int attempt_num = 0; attempt_num < NUM_ATTEMPTS; attempt_num++)
{
// access a BLUE tile by index from alltiles to change its color to RED
myroom.alltiles[5].color = RED; // constant time
myroom.bluetiles.erase(std::remove(myroom.bluetiles.begin(), myroom.bluetiles.end(), &myroom.alltiles[5]), myroom.bluetiles.end()); // linear time, oh no!
// access a RED tile by index from alltiles to change its color to BLUE
myroom.alltiles[5].color = BLUE; // constant time
myroom.bluetiles.push_back(&myroom.alltiles[5]); // constant time
// randomly choose from ONLY the blue tiles
int rand_index = rand() % myroom.bluetiles.size(); // constant time
myroom.bluetiles[rand_index]->color = GREEN; // constant time
myroom.bluetiles[rand_index]->color = BLUE; // constant time
// so now I have constant time access to a random blue tile
}
auto end = std::chrono::high_resolution_clock::now();
double runtime = std::chrono::duration_cast<std::chrono::milliseconds>(end-begin).count();
cout << runtime << " ms" << endl;
return 0;
}
正在计时的部分是我有兴趣经常执行的操作;在实际程序中,选择要更改的图块背后的逻辑是不同的。希望更好的数据结构不需要任何概率分析,但我担心它仍然可能。
我怀疑,也许,通过在图块中保留一个指针(指向 bluetiles 向量中的元素)来使用双重间接寻址 class 可以让我在恒定时间内实现这一目标,但我不确定。我想它至少可以在不再需要搜索 bluetiles 的意义上加快速度,但是删除 bluetiles 中的元素仍然是线性时间(因为我使用的是矢量),所以我真的只是不知道在这里做什么。
您能否设计出最快的数据结构来实现此目标,并提供基于我的示例的 C++ 实现构建?或者我所拥有的就是最好的吗?
UPDATE:这类似于我为 SO 问题提出的解决方案 Random element from unordered_set in O(1)
您可以实现类似于以下 SubsetVector<T>
class 的内容,它允许您在 O(1) 中从子集(即标记它们)中提取 insert/remove 元素。然后它让您在 O(1) 中找到子集的大小,并在 O(1) 中访问该子集中的第 i 个项目。我想这就是你想要的。请注意,该子集不保证任何特定顺序,但这应该可以满足您的需要。
想法是维护两个向量。
m_entries
包含实际数据。如果元素在子集中,m_entries[i]
包含元素和 m_subset_indices
的索引,否则为 -1。
m_subset_indices
包含子集中 m_entries
个元素的所有索引。
这是代码(已编译但未测试):
template <class T>
class SubsetVector
{
private:
struct Entry
{
T element;
int index_in_subset = -1;
};
public:
explicit SubsetVector(unsigned size = 0) : m_entries(size)
{
m_subset_indices.reserve(size);
}
void push_back(const T & element)
{
m_entries.push_back(Entry{element, -1});
}
const T & operator[](unsigned index) const { return m_entries[index].element; }
T & operator[](unsigned index) { return m_entries[index].element; }
void insert_in_subset(unsigned index)
{
if (m_entries[index].index_in_subset < 0) {
m_entries[index].index_in_subset = m_subset_indices.size();
m_subset_indices.push_back(index);
}
}
void erase_from_subset(unsigned index)
{
if (m_entries[index].index_in_subset >= 0) {
auto subset_index = m_entries[index].index_in_subset;
auto & entry_to_fix = m_entries[m_subset_indices.back()];
std::swap(m_subset_indices[subset_index], m_subset_indices.back());
entry_to_fix.index_in_subset = subset_index;
m_subset_indices.pop_back();
m_entries[index].index_in_subset = -1;
}
}
unsigned subset_size() const
{
return m_subset_indices.size();
}
T & subset_at(unsigned subset_index)
{
auto index = m_subset_indices.at(subset_index);
return m_entries.at(index).element;
}
const T & subset_at(unsigned subset_index) const
{
auto index = m_subset_indices.at(subset_index);
return m_entries.at(index).element;
}
private:
std::vector<Entry> m_entries;
std::vector<unsigned> m_subset_indices;
};
给定一个固定大小的对象数组 A,假设这些对象中的一小部分满足特定条件 B。我想以大致相等的频率完成三个任务:
- 我希望能够在通过索引访问 A 中的对象时随时将当前不满足条件 B 的对象更改为满足条件 B。
- 我希望能够在按索引访问 A 中的对象时将当前满足条件 B 的对象更改为不再满足条件 B。
- 我还希望能够仅从满足标准 B 的对象中选择一个随机对象。
所有任务都应该能够在恒定时间内完成,或者尽可能接近恒定时间,不依赖于A中对象的数量并且不依赖于满足标准 B 的对象数量。如果恒定时间是不可能的(我怀疑是这种情况),那么我想尽快完成两者,同时考虑我之前提到的频率。如果这两项任务被重复多次,哪种数据结构适合于这两项任务?
以我下面的 C++ 实现为例。虽然计时部分(代码中重复多次的部分)与 A(alltiles)的整体大小无关,但时间复杂度线性取决于 B(bluetiles)(无论 alltiles 的数量是否增加或不),严重减慢代码速度。
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib>
#include <algorithm>
using namespace std;
enum color {RED, GREEN, BLUE};
const int NUM_ATTEMPTS = 10000;
const int INITIAL_NUM_BLUE_TILES = 1000;
const int TOTAL_TILES = 1000000;
struct tile
{
int color = RED;
};
struct room
{
vector<tile> alltiles;
vector<tile*> bluetiles;
room(vector<tile> v) : alltiles(v) {}
};
int main()
{
srand (time(NULL));
// set up the initial room, time complexity here is irrelevant
room myroom(vector<tile>(1*TOTAL_TILES));
for(int i = 0; i < INITIAL_NUM_BLUE_TILES; i++)
{
myroom.alltiles[i].color = BLUE;
myroom.bluetiles.push_back(&myroom.alltiles[i]);
}
auto begin = std::chrono::high_resolution_clock::now();
for(int attempt_num = 0; attempt_num < NUM_ATTEMPTS; attempt_num++)
{
// access a BLUE tile by index from alltiles to change its color to RED
myroom.alltiles[5].color = RED; // constant time
myroom.bluetiles.erase(std::remove(myroom.bluetiles.begin(), myroom.bluetiles.end(), &myroom.alltiles[5]), myroom.bluetiles.end()); // linear time, oh no!
// access a RED tile by index from alltiles to change its color to BLUE
myroom.alltiles[5].color = BLUE; // constant time
myroom.bluetiles.push_back(&myroom.alltiles[5]); // constant time
// randomly choose from ONLY the blue tiles
int rand_index = rand() % myroom.bluetiles.size(); // constant time
myroom.bluetiles[rand_index]->color = GREEN; // constant time
myroom.bluetiles[rand_index]->color = BLUE; // constant time
// so now I have constant time access to a random blue tile
}
auto end = std::chrono::high_resolution_clock::now();
double runtime = std::chrono::duration_cast<std::chrono::milliseconds>(end-begin).count();
cout << runtime << " ms" << endl;
return 0;
}
正在计时的部分是我有兴趣经常执行的操作;在实际程序中,选择要更改的图块背后的逻辑是不同的。希望更好的数据结构不需要任何概率分析,但我担心它仍然可能。
我怀疑,也许,通过在图块中保留一个指针(指向 bluetiles 向量中的元素)来使用双重间接寻址 class 可以让我在恒定时间内实现这一目标,但我不确定。我想它至少可以在不再需要搜索 bluetiles 的意义上加快速度,但是删除 bluetiles 中的元素仍然是线性时间(因为我使用的是矢量),所以我真的只是不知道在这里做什么。
您能否设计出最快的数据结构来实现此目标,并提供基于我的示例的 C++ 实现构建?或者我所拥有的就是最好的吗?
UPDATE:这类似于我为 SO 问题提出的解决方案 Random element from unordered_set in O(1)
您可以实现类似于以下 SubsetVector<T>
class 的内容,它允许您在 O(1) 中从子集(即标记它们)中提取 insert/remove 元素。然后它让您在 O(1) 中找到子集的大小,并在 O(1) 中访问该子集中的第 i 个项目。我想这就是你想要的。请注意,该子集不保证任何特定顺序,但这应该可以满足您的需要。
想法是维护两个向量。
m_entries
包含实际数据。如果元素在子集中,m_entries[i]
包含元素和m_subset_indices
的索引,否则为 -1。m_subset_indices
包含子集中m_entries
个元素的所有索引。
这是代码(已编译但未测试):
template <class T>
class SubsetVector
{
private:
struct Entry
{
T element;
int index_in_subset = -1;
};
public:
explicit SubsetVector(unsigned size = 0) : m_entries(size)
{
m_subset_indices.reserve(size);
}
void push_back(const T & element)
{
m_entries.push_back(Entry{element, -1});
}
const T & operator[](unsigned index) const { return m_entries[index].element; }
T & operator[](unsigned index) { return m_entries[index].element; }
void insert_in_subset(unsigned index)
{
if (m_entries[index].index_in_subset < 0) {
m_entries[index].index_in_subset = m_subset_indices.size();
m_subset_indices.push_back(index);
}
}
void erase_from_subset(unsigned index)
{
if (m_entries[index].index_in_subset >= 0) {
auto subset_index = m_entries[index].index_in_subset;
auto & entry_to_fix = m_entries[m_subset_indices.back()];
std::swap(m_subset_indices[subset_index], m_subset_indices.back());
entry_to_fix.index_in_subset = subset_index;
m_subset_indices.pop_back();
m_entries[index].index_in_subset = -1;
}
}
unsigned subset_size() const
{
return m_subset_indices.size();
}
T & subset_at(unsigned subset_index)
{
auto index = m_subset_indices.at(subset_index);
return m_entries.at(index).element;
}
const T & subset_at(unsigned subset_index) const
{
auto index = m_subset_indices.at(subset_index);
return m_entries.at(index).element;
}
private:
std::vector<Entry> m_entries;
std::vector<unsigned> m_subset_indices;
};