简体   繁体   English

从节点数组和边缘向量获取所有子树的最有效方法是什么?

[英]What is the most efficient way to get all subtrees from node array and edge vector?

Assume that there are nodes as array and undirected edges as vector like this: 假设像这样的数组有节点,向量的无向边是这样的:

int nodes[n] = {1, 2, 3, ... ,n };
vector<pair<int, int>> edges;

edges.push_back(std::make_pair(0, 2));
edges.push_back(std::make_pair(2, 4));

where each element of array is value and n is the number of array. 其中数组的每个元素都是值, n是数组的数量。 Following above code, there are two edges. 在上面的代码之后,有两个边缘。 One is from 0 to 2. The other one is from 2 to 4. These numbers indicate index of array. 一个从0到2。另一个从2到4。这些数字表示数组的索引。 In this case, size of largest sub-tree is 3 that 0-2-4 and size of smallest sub-tree is 1 obviously. 在这种情况下,最大子树的大小显然是0-2-4的3,最小子树的大小是1。

I solved this like below: 我如下解决了这个问题:

  1. Sort edges vector 排序edges矢量
  2. Choose one permutation in edges 选择edges一种排列
  3. Repeat 2 until exploring all possible cases 重复2,直到探究所有可能的情况

However I am not sure this is efficient way. 但是,我不确定这是否有效。 How can I get all sub-trees in the problem domain like this? 我如何获得像这样的问题域中的所有子树? is there any general and efficient way? 有什么通用有效的方法吗?

I solved this problem using BFS (Breadth First Search) based on edge information. 我使用基于边缘信息的BFS(广度优先搜索)解决了这个问题。 To avoid making cyclic graph and keep nodes as tree, I use set . 为了避免制作循环图并将节点保留为树,我使用set And I also apply sort before searching. 在搜索之前,我也会应用sort It is useful to reduce time complexity. 减少时间复杂度很有用。

void BFS(set<int> nodes, vector<pair<int,int>>& edges, vector<set<int>>& result) {
    result.push_back(nodes);
    int lastNode = *max_element(nodes.begin(), nodes.end());
    auto findIt = find_if(edges.begin(), edges.end(), [](const pair<int, int>& element){ return element.first == lastNode});
    if(findIt != edges.end()) {
        nodes.insert((*findIt).second);
        BFS(nodes, edges, result);
    }
}

sort(edges.begin(), edges.end());

vector<set<int>> result;
for(auto it : edges) {
    set<int> nodes;
    nodes.insert((*it).first);
    nodes.insert((*it).second);
    BFS(nodes, edges, result);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 ProtoBuf RepeatedField 获取所有元素的最有效方法是什么? - What is the most efficient way to get all elements from a ProtoBuf RepeatedField? 使用 Eigen 重复向量中的元素并在所有元素上应用一组不同的函数的最有效方法是什么? - What is the most efficient way to repeat elements in a vector and apply a set of different functions across all elements using Eigen? 什么是以升序打印矢量的所有元素的最有效方法,直到它没有重复的空? - What's the most efficient way to print all elements of vector in ascending order till it's empty without duplicates? 填充 2d std::array 和 std::vector 的最有效方法是什么? - what's the most efficient way to fill 2d std::array and std::vector? 从向量中提取min,max和median的最有效方法是什么 - What's the most efficient way to extract min, max & median from a vector 从长(合理)稀疏向量中选择随机元素的最有效方法是什么? - What is the most efficient way of selecting a random element from a long (and reasonably) sparse vector? 删除重复项和对向量进行排序的最有效方法是什么? - What's the most efficient way to erase duplicates and sort a vector? 将结构化数据投影到std :: vector的最有效方法是什么? - What is the most efficient way to project structured data to a std::vector? 将一个std :: vector附加到另一个stnd :: vector的最有效方法是什么? - What is the most efficient way to append one std::vector to the end of another? 在两个向量之间找到相同元素的最有效方法是什么 - what is the most efficient way to found the same element between two vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM