【问题标题】:Optimizing Trie implementation优化 Trie 实现
【发布时间】:2010-07-20 14:08:17
【问题描述】:

我今天实现了一个Trie,只是为了好玩。目前它支持 add() 和 search(),remove() 也应该实现,但我认为这相当简单。

它功能齐全,但是用数据填充 Trie 对我来说有点太多了。我将此列表用作数据源:http://www.isc.ro/lists/twl06.zip(在 SO 的其他地方找到)。加载大约需要 11 秒。我最初的实现花了大约 15 秒,所以我已经给它一个很好的性能提升,但我仍然不满意 :)

我的问题是:还有什么可以(显着)提升我的性能?我不受这种设计的约束,彻底检修是可以接受的。

class Trie
{
    private $trie;
    public function __construct(TrieNode $trie = null)
    {
        if($trie !== null) $this->trie = $trie;
        else $this->trie = new TrieNode();
        $this->counter = 0;
    }
    public function add($value, $val = null)
    {
        $str = '';
        $trie_ref = $this->trie;
        foreach(str_split($value) as $char)
        {
            $str .= $char;
            $trie_ref = $trie_ref->addNode($str);
        }
        $trie_ref->value = $val;
        return true;
    }
    public function search($value, $only_words = false)
    {
        if($value === '') return $this->trie;
        $trie_ref = $this->trie;
        $str = '';
        foreach(str_split($value) as $char)
        {
            $str .= $char;
            if($trie_ref = $trie_ref->getNode($str))
            {
                if($str === $value) return ($only_words ? $this->extractWords($trie_ref) : new self($trie_ref));
                continue;
            }
            return false;
        }
        return false;
    }
    public function extractWords(TrieNode $trie)
    {
        $res = array();
        foreach($trie->getChildren() as $child)
        {
            if($child->value !== null) $res[] = $child->value;
            if($child->hasChildren()) $res = array_merge($res, $this->extractWords($child));
        }
        return $res;
    }
}
class TrieNode
{
    public $value;
    protected $children = array();
    public function addNode($index)
    {
        if(isset($this->children[$index])) return $this->children[$index];
        return $this->children[$index] = new self();
    }
    public function getNode($index)
    {
        return (isset($this->children[$index]) ? $this->children[$index] : false);
    }
    public function getChildren()
    {
        return $this->children;
    }
    public function hasChildren()
    {
        return count($this->children)>0;
    }
}

【问题讨论】:

  • 您是否已经使用xhprofxdebug 分析了代码?
  • 我还没有,好电话。我明天会的!

标签: php optimization data-structures trie


【解决方案1】:

不懂php,但是,

在以下方法中:

   public function add($value, $val = null) 
    { 
        $str = ''; 
        $trie_ref = $this->trie; 
        foreach(str_split($value) as $char) 
        { 
            $str .= $char; 
            $trie_ref = $trie_ref->addNode($str); 
        } 
        $trie_ref->value = $val; 
        return true; 
    } 
    public function search($value, $only_words = false) 
    { 
        if($value === '') return $this->trie; 
        $trie_ref = $this->trie; 
        $str = ''; 
        foreach(str_split($value) as $char) 
        { 
            $str .= $char; 
            if($trie_ref = $trie_ref->getNode($str)) 
            { 
                if($str === $value) return ($only_words ? $this->extractWords($trie_ref) : new self($trie_ref)); 
                continue; 
            } 
            return false; 
        } 
        return false; 
    } 

为什么你甚至需要$str .= $char(我想是附加的)?这本身会将您的 O(n) 时间添加/搜索更改为 Omega(n^2)(n 的长度为 $value)而不是 O(n)。

在 trie 中,您通常在遍历字符串的同时遍历 trie,即根据当前字符而不是当前前缀找到下一个节点。

【讨论】:

  • 好点,完全没有必要。但它会提高速度吗?不过,这肯定会更容易记忆。无论如何,我明天会实施并在这里发布结果。
  • @Dennis:是的,IMO。这实际上是为什么尝试如此之快的主要原因之一
  • @Dennis:我很好奇你要发布的结果 :-)
【解决方案2】:

我想这个实现是针对 Key|value 类型的插入和查找?这是处理 [English] 单词的一个。

class Trie {


static function insert_word(Node $root, $text) 
{
    $v = $root;
    foreach(str_split($text) as $char) {
    $next = $v->children[$char];
        if ($next === null)
        {
            $v->children[$char] = $next = new Node();
        }
        $v = $next;
    }

    $v->leaf = true;
}


static function get_words_sorted(Node $node, $text) 
{

    $res = array();  
    for($ch = 0; $ch < 128; $ch++) {
    $child = $node->children[chr($ch)];

        if ($child !== null)
        {
            $res = array_merge($res, Trie::get_words_sorted($child, $text . chr($ch)));

        }
    }
    if ($node->leaf === true) 
    {
        $res[] = $text;
    }
    return $res;

}

static function search(Node $root, $text) 
{
    $v = $root;
    while($v !== null)
    {
        foreach(str_split($text) as $char) {
            $next = $v->children[$char];
            if ($next === null)
            {
                return false;
            }
            else
            {
                $v = $next;
            }
        }

        if($v->leaf === true)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
    return false;

}

}


class Node {

    public $children;
    public $leaf;


    function __construct()
    {
        $children = Array();

    }
}

示例用法

    $root = new Node();
    $words = Array("an", "ant", "all", "allot", "alloy", "aloe", "are", "ate", "be");


    for ($i = 0; $i < sizeof($words); $i++)
    {

        Trie::insert_word($root, $words[$i]);
    }

    $search_words = array("alloy", "ant", "bee", "aren't", "allot");

    foreach($search_words as $word)
    {
        if(Trie::search($root, $word) === true)
        {
            echo $word . " IS in my dictionary<br/>";
        }
        else
        {
            echo $word . " is NOT in my dictionary <br/>";
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2014-06-30
    • 2010-10-15
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多