【问题标题】:Algorithm for forming adjacency list with nested neighbors用嵌套邻居形成邻接表的算法
【发布时间】:2020-08-12 16:30:16
【问题描述】:

当给定同义词列表时,我很难想出一个好的算法来形成邻接列表。

同义词列表以向量的向量形式提供。内部向量大小为 2,由 2 个同义词组成。

例如,

std::vector<std::vector<std::string>> synonyms{{"joy", "happy"}, {"happy", 
"ecstatic"}, {"cheerful, ecstatic"}, {"big", "giant"}, {"giant", "gigantic"}};

所以这里我们有两组同义词:{joy, happy, ecstatic, cheerful}{big, giant, gigantic}

我想使用std::unordered_map&lt;std::string, std::set&lt;std::string&gt;&gt; 从边缘列表中创建一个邻接列表。该值为set,因为需要对邻居进行排序。或者,该值可以是一个向量,然后我们将在最后对向量进行排序。 在给定边缘的情况下制作此邻接列表的最佳方法是什么?

对于这个邻接列表,我希望每个单词都有一个条目。所以在上面的例子中,我会有 7 个条目。对于每个条目,我想将它映射到它的同义词。比如:

{happy} -> {cheerful, ecstatic, joy}
{joy} -> {cheerful, ecstatic, happy}
{ecstatic} -> {cheerful, happy, joy}
{cheerful} -> {ecstatic, happy, joy}
{giant} -> {big, gigantic}
{big} -> {giant, gigantic}
{gigantic} -> {big, giant}

【问题讨论】:

  • @JohnFilleau 是的,对不起,我忘了包括那个。我只是在最后加了一段。这能说明问题吗?
  • 所以输入被指定为vector&lt;vector&lt;string&gt;&gt;,但实际上它是一对,因为内部向量只能有2个元素。
  • @JohnFilleau 实际上,如果这样更容易,您可以假设它是一对。比起事物的存储方式,我对算法更感兴趣。

标签: c++ graph language-agnostic adjacency-list


【解决方案1】:

假设两个孤立的词邻域,NaNb。邻域中的每个单词都知道邻域中的所有其他单词(同义词)。然后,我们指定来自 Na 的单词,称为 Wa,和来自 Nb 的单词,称为 Wb 被声明为同义词。我们必须有WaWa 的每个邻居(即Na 中的每个单词)添加WbWb 的每个邻居(即 中的每个单词Nb)添加到它的邻居列表(同义词)。并且我们必须让 Nb 中的每个单词将 Na 中的每个单词添加到其邻居列表(同义词)中。

在此操作结束时,所有单词都知道新组合邻域中的所有其他单词,这使得这个新邻域成为上述算法的另一次迭代的有效输入。

尚未添加同义词的单个单词的邻域为 1,这使其成为算法的有效输入。

不要考虑组合单词。考虑合并社区。每个单词都映射到其完整的邻域开始。这使得将社区挤在一起更容易。稍后我们可以从它自己的邻域中删除每个单词。

std::unordered_map<std::string, std::set<std::string>> al;

for (auto const & syns : synonyms)
  for (auto const & word : syns)
  {
    al[word].insert(word);
    for (auto const & syn : syns)
    {
      al[syn].insert(syn);
      if (word != syn)
      {
        // add the entire neighborhood of word to
        // the entire neighborhood of syn, and vice versa
        for (auto const & neighbor_word : al[word])
          for (auto const & neighbor_syn : al[syn])
          {
            al[child_word].insert(child_syn);
            al[child_syn].insert(child_word);
          }
      }
    }
  }

// remove each word's mapping to itself as a synonym:
for (auto & words : al)
  words.second.erase(words.first);

真是乱七八糟,我不想评估复杂性,但要完成工作。我想?

等待同行评审...

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 2016-04-06
    • 2012-04-27
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2011-05-09
    相关资源
    最近更新 更多