【问题标题】:Algorithm itemset matching pattern算法项集匹配模式
【发布时间】:2015-12-05 11:54:48
【问题描述】:

我有一组具有顺序关系的元素(可能很大):

[a,b,c,d,e,f] 

以及一组带有 ids 的频繁模式(可能很大):

[a]:1,[b]:2,[c]:3,[a,b]:4,[b,c]:5,[a,b,c]:6

我有一系列有序集合:

[a,b], [e], [c], [e,f], [a,b,c]

我想将序列中的每个集合与相应模式的 id 进行匹配:

[a,b]:{1,2,4}, [e]:{}, [c]:{3}, [a,b,c]:{1,2,3,4,5,6}

我的目标是限制序列的传递次数,因此我想构建一个可以在扫描期间使用的数据结构。 我在考虑前缀树:

──null
   ├──a : 1
   |  |
   |  └──b : 4
   |     |
   |     └──c : { 5, 6 }
   |
   ├──b : 2
   |  |
   |  └──c : 5
   |
   └──c : 3

我扫描序列中的一个集合,并通过树多次递归(set,set.tail,set.tail.tail ...),每次我到达一个节点时,我将对应的 id 添加到数组中。

我在推理中是否遗漏了任何特殊情况(刚刚意识到如果我不想错过 [a,c] 如果 [a,b,c] 存在于集)? 有没有更复杂的数据结构可以用来缩短处理时间?

编辑:事实上在深度 n,我的方法需要 2^(n-2) ids(考虑到我的树很密集)。我不确定这是一种有效的方法......

Edit2 :另一种方法,合并序列中每个元素的位图以构建每个模式(如 SPADE 算法中所用)。

a  : [1,0,0,0,1]
b  : [0,1,0,0,1]
ab : [0,0,0,0,1]

通过一些数组操作,我应该能够将它与我的初始数组的元素相匹配。

【问题讨论】:

  • 您可以构建一个 DFA(“字典引擎”)来识别流中的所有六种模式。 (这实际上是 fgrep 所做的)
  • @wildplasser,我可能有很多元素和模式(唯一的限制是元素按模式排序),dfa 仍然是一种有效的方法吗?你有什么实施参考吗?
  • dcs.kcl.ac.uk/staff/mac/TSP/http://www.dcs.kcl.ac.uk/staff/mac/…(第一章,第 47 页,IIRC)或者可能是龙书。
  • @wildplasser,我查看了字典匹配讲座并在此维基百科页面上结束 en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm 但看起来它只考虑完全匹配,所以在我的情况下它不会检测到 [a,c ] 在 [a,b,c] 中。
  • 您的实际问题是什么?如果你能有效地解决这个问题,你似乎遇到了一个可以解决的问题。但是您将为序列中的每个长集合放入指数数量的 id,因此没有有效的解决方案。但是,您最初的问题可能会更容易。

标签: algorithm


【解决方案1】:

如果您正在构建前缀树(也称为 trie),则所有节点都是唯一的,因此集合 {a,b,c} 的前缀树按字母顺序连续如下所示:

──null
   ├──a : 1
   |  |
   |  └──b : 4
   |     |
   |     └──c : 6
   |
   ├──b : 2
   |  |
   |  └──c : 5
   |
   └──c : 3

它映射到前缀集{ a, b, c, ab, bc, abc }

树空间复杂度为SUM k for k = 1..N ~ O(N^2)

Node.java

class Node
{
    public String str;
    public ArrayList<String> child;

    public Node (String str)
    {
        this.str = str;
        this.child = new ArrayList<String>();
    }
}

MyTree.java

class MyTree
{
    Node head;

    ..

    public void build_tree(String [] symbol)
    {
        this.head = new Node("");
        build(symbol,head,0,symbol.length-1);
    }

    // build the prefix tree through DFS
    private void build(String [] symbol, Node parent, int start, int end)
    {
        Node ch = null;
        for (int i=start; i<=end; i++)
        {
            ch = new Node(symbol[i]);
            parent.child.add(ch);

            if (end - start > 0)
            {
                build(symbol,ch,i,end);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多