gpt4 book ai didi

c++ - 迭代合并 std::unordered_map

转载 作者:太空狗 更新时间:2023-10-29 20:50:51 36 4
gpt4 key购买 nike

我有一个节点列表,每个节点都分解成更多节点。例如

  • 节点 0 = w01 * 节点 1 + w02 * 节点 2 + w03 * 节点 3
  • 节点 1 = w12 * 节点 2 + w14 * 节点 4

因此,我们有 Node0 = w01*w12 * Node2 + w03 * Node3 + w01*w14 Node4。


为一组给定的权重分解执行上述聚合/分解/合并的 C++ 代码如下所示。但是,我觉得还有很多优化要做。仅举一个例子,我正在遍历 topWeights 的键并将它们收集到 topNodeNames 中,这似乎非常低效。

是否有任何 STL 算法可以帮助我加快速度,并可能避免不必要的复制?

#include <string>
#include <unordered_map>

template<class T, class U> using umap = std::unordered_map<T, U>;


umap<std::string, double> getWeights(const std::string& nodeName, const umap<std::string, umap<std::string, double>>& weightTrees)
{
const auto it = weightTrees.find(nodeName);
if (it == weightTrees.end())
return umap<std::string, double>();

umap<std::string, double> topWeights = it->second;
std::vector<std::string> topNodeNames;

for (const auto& kv : topWeights)
topNodeNames.push_back(kv.first);

for (const std::string& topNodeName : topNodeNames)
{
umap<std::string, double> subWeights = getWeights(topNodeName, weightTrees);
if (subWeights.size() > 0)
{
const double topWeight = topWeights[topNodeName];
topWeights.erase(topNodeName);
for (const auto& subWeight : subWeights)
{
const auto it = topWeights.find(subWeight.first);
if (it == topWeights.end())
topWeights[subWeight.first] = topWeight * subWeight.second;
else
it->second += topWeight * subWeight.second;
}
}
}

return topWeights;
}


int main()
{
umap<std::string, umap<std::string, double>> weightTrees = {{ "Node0", {{ "Node1",0.5 },{ "Node2",0.3 },{ "Node3",0.2 }} },
{ "Node1", {{ "Node2",0.1 },{ "Node4",0.9 }} }};

umap<std::string, double> w = getWeights("Node0", weightTrees); // gives {Node2: 0.35, Node3: 0.20, Node4: 0.45}
}

最佳答案

主要问题是您将每个 节点递归到每个 子节点,这通常是高度冗余的。避免这种情况的一种方法是在节点名称上引入一个顺序,其中“较高”节点仅依赖于“较低”节点,然后以相反的顺序计算它们(对于每个节点,您已经确切地知道所有子权重)。但是,我不认为有 std 算法可以为您找到此顺序,因为您无法廉价地暂时确定节点依赖性(“节点 X 是否依赖于节点 Y?如果不是直接的,我们可能必须搜索整棵树...").

因此,您可以走动态规划路线,将已完全计算的节点存储在某处。或者甚至更好 - 你可以在遍历它时将整棵树压扁到只有叶子的权重。只要在整个递归过程中保持扁平化,这在递归形式中实际上是相当优雅的:

using NodeWeights = std::unordered_map<std::string, double>;
using NonLeaves = std::unordered_map<std::string, NodeWeights>;

// Modifies the tree so that the given root has no non-leaf children.
void flattenTree(std::string root, NonLeaves& toFlatten)
{
auto rootIt = toFlatten.find(root);
if (rootIt == toFlatten.end())
return;

NodeWeights& rootWeights = rootIt->second;

NodeWeights leafOnlyWeights;

for (auto kvp : rootWeights)
{
const std::string& childRoot = kvp.first;
double childWeight = kvp.second;

std::cout << "Checking child " << childRoot << std::endl;

// If the graph is indeed acyclic, then the root kvp here is untouched
// by this call (and thus references to it are not invalidated).
flattenTree(childRoot, toFlatten);

auto childIt = toFlatten.find(childRoot);

// The child is a leaf after flattening: Do not modify anything.
if (childIt == toFlatten.end())
{
leafOnlyWeights[childRoot] = childWeight;
continue;
}

// Child is still not a leaf (but all its children are now leaves):
// Redistribute its weight among our other child weights.
const NodeWeights& leafWeights = childIt->second;
for (auto leafKvp : leafWeights)
leafOnlyWeights[leafKvp.first] += childWeight * leafKvp.second;
}

rootWeights = leafOnlyWeights;
}

int main()
{
umap<std::string, umap<std::string, double>> weightTrees = {{ "Node0", {{ "Node1",0.5 },{ "Node2",0.3 },{ "Node3",0.2 }} },
{ "Node1", {{ "Node2",0.1 },{ "Node4",0.9 }} }};

auto flattenedTree = weightTrees;
flattenTree("Node0", flattenedTree);

umap<std::string, double> w = flattenedTree["Node0"]; // Should give {Node2: 0.35, Node3: 0.20, Node4: 0.45}

for (auto kvp : w)
std::cout << kvp.first << ": " << kvp.second << std::endl;
}

Demo

由于每个节点最多被展平一次,因此您不会遇到原始算法所具有的指数运行时间。

关于c++ - 迭代合并 std::unordered_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52932680/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com