Tree Pattern Matching to Subset Matching in Linear
Tree Pattern Matching to Subset Matching in Linear Time R. Cole and R. Hariharan
Tree Pattern Matching v Input: An ordered binary tree T, |T| = n. An ordered binary tree P, |P| = m. v Output: All nodes in T where P matches. t p
Subset Matching v Input: A set-string T and a set-string P. v Output: All T= a b c P= a c occurrences of P in T. a c b c c b a c c e f b
History v v v v Hoffman and O’Donell, 1982, O(nm). Kosaraju, 1989, O(nm 0. 75 logm). Dubiner, Galil, and Magen, 1994, O(nm 0. 5 logm). Cole and Hariharan, 1997, randomized O(nlog 3 m). Indyk, 1998, randomized O(nlogn). Cole, Hariharan, and Indyk, 1999, O(nlog 3 m). Cole and Hariharan, 2002, O(nlog 2 m).
Period v Def : The period of a string s is the smallest number j such that s[i]=s[i+j]. S= 001001 j = 3
Period in linear time v Exercise 1: Design an algorithm to compute the period in linear time.
θ-Path v Def: A path p is a θ-path if its string representation has period θ. Let θ = 0 0 1. p s =0 0 1 p is a θ-path.
Maximalθ-Path v Def: A θ-path p is maximal if it can not be extended. θ = 0 0 1. not
Maximalθ-Paths in linear time v Exercise 2: Design an linear time algorithm to find all maximalθ-Paths in a tree.
Centroid of a tree m = 19 m/2 = 9
Spine of a Tree v Spine = centroid 加強版
Spine of a Tree 0 1 0 1 0 Link node = Centroid 的最後一個node = Spine上最後一個 ≥ m/2的點 < m/2 ≥ m/2
A Special Case of Tree Pattern Matching v v Input: An ordered binary tree T, |T| = n. An ordered binary tree P, |P| = m. Output: All nodes in T where P matches Additional constraint: T has only one maximal θ-path, where θ is the period of the spine of P.
A Special Case of Tree Pattern Matching T P
Reduce to Subset Matching (1) P B 2 B 6 0 0 1 0
Reduce to Subset Matching (2) B 6 B 2 a b c e d 0 0 a b 0 c e f 1 0 0 1 a b 1 d e f 0
Reduce to Subset Matching (3) T 肋2 0 0 1 0 0 肋6 肋5
Reduce to Subset Matching (4) 肋2 a b c b e d a f c 0 a b c 1 0 0 e f Time: O( min{ m, 肋2} )
Reduce to Subset Matching (5) v Total Time: ∑i O( min{m, 肋i} ) = O(n)
How about the general case? maxima θ-paths不只一條該 怎麼 reduce呢? v 暴力法: 對每一條maximal θ-paths都用 剛才的方法reduce成subset matching problem。 v Time? v 如果找出來的
Where is the intuition come from? the first |θ| edges of each maximal θ-paths are removed, then those truncated paths are disjoint。 v Truncation lemma: If
Truncation Lemma (2) v By Classical Lemma: p p’ < |θ|
Warm up is over! v 接下來要做的事: Part 1: 證明 truncated maximal θ-paths 可以在 linear time reduce 成 subset matching。 Part 2: 考慮被砍掉的部分該如何解決。
Step 1: Find all maximal θ-paths T P θ Link node
Step 2: Filtering(1) v 把不符合以下三個property的maximal θ- pahts 過濾掉。 v Property 1: ≥m
Step 2: Filtering (2) Propety 2: ∵ P ≥ m/2
Step 2: Filtering (3) v Propety 3: ∵ P ≥θ ≥θ ≥ m/2
Step 3: Truncation v 將過濾後每一條maximal edges去掉。 θ-paths開頭的θ條
Step 4: Filtering again truncated maximal θ-paths 再過濾一遍, 剩下的這些paths在之後將簡稱為truncated paths. v把
Step 5: 一條一條reduce成 subset matching Time: ∑truncated paths ∑i O( min{m, 肋i} )
Analysis of Step 5 (1) ∑truncated paths ∑i O( min{m, 肋i} ) = ∑O( min{m, 肋} )
Analysis of Step 5 (4) v Part <m 2: ∑O(小肋) = n ∵小肋骨 are disjoint 大 ≥m 小 小 大 大 小
Marked nodes v Def: A node in t is marked if its left and right subtrees both contain ≥ m nodes.
# marked nodes is O(n/m) m= 2 ≥m ≥m
# marked nodes is O(n/m) ≥m ≥m ≥m ∵(# external nodes) * m ≤ n ∴ # external nodes ≤ n/m ⇒ # marked nodes = # internal nodes ≤ n/m - 1 ≥m ≥m
Analysis of Step 5 (5) Part 1. (#大肋) = O( n/m ) 一條truncated path上如果有k > 1根大肋骨, 則有k-1 個maked nodes。 v 大 大 大
Analysis of Step 5 (7) v O(n/m) 條 小 小 ≥ m/2 小 小 大
An observation k > 1根大肋骨的truncated paths只有 O(n/m)條。 v 擁有 k = 0根大肋骨的truncated paths只有 O(n/m)條。 v 所以truncated paths只有 O(n/m)條。 v 擁有
Disjoint Lemma Let C be a set of disjoint θ-paths and these θ-paths satisfy property 1~3. Then there are O(n/m) θpaths in C. v Pf: v 擁有 k > 1根大肋骨的θ-paths只有O(n/m)條。 v 擁有 k = 0根大肋骨的θ-paths只有O(n/m)條。 v
Review v Step 1: Finding all maximal θ-paths v Step 2: Filtering v Step 3: Truncation v Step 4: Filtering again v Step 5: Reduce to subset mathching
How about the removed parts? P θ θ Time: O(m)
The Last Job v Step 1: Finding all maximal θ-paths v Step 2: Filtering only O(n/m) paths left. v Step 3: Truncation v Step 4: Filtering again v Step 5: Reduce to subset mathching
Tail Lemma v path的尾巴不會被其他path碰到。
Chains . . .
Chain Lemma 0 0 一條chain上 (1) 編號 1, 3, 5 , 7 , … 的paths會是disjoint。 1 1 (2) 編號 0, 2, 4, 6, 8, … 的paths會是disjoint。 2 2 3 3
Truncated-Chains Lemma(1) v Truncated-Chains lemma: If the first two paths of each chains are removed, then those truncated chains are disjoint。
Truncated-Chains Lemma(2) ρ ρ’
Truncated-Chains Lemma(3) ρ v Case ρ’ 1: ≥θ
Truncated-Chains Lemma(4) ρ v Case ρ’ 2: ≥θ
Truncated-Chains Lemma(5) ρ v Case ρ’ 3: ≥θ
Truncated-Chains Lemma(6) ρ v Case ρ’ 4: ≥θ
Almost Over (1) Chain lemma: 在一條chain上 (1) 編號 1, 3, 5, 7 , … 的paths會是disjoint。 (2) 編號 0, 2, 4, 6 , … 的paths會是 disjoint。 v Truncated-Chains lemma 去掉編號 0, 1的 paths 後的chains會是disjoint v ⇒ (1) 編號 3, 5, 7, … 的paths會是disjoint (2)編號 2, 4, 6 , … 的paths會是disjoint
Almost Over (2) (1)編號 3, 5, 7, … 的paths會是disjoint (2)編號 2, 4, 6 , … 的paths會是disjoint By Disjoint Lemma: (1)編號 3, 5, 7, … 的paths共 O(n/m)條 (2)編號 2, 4, 6, … 的paths共 O(n/m)條
Almost Over (3) v If #chains = O(n/m), then 編號 0與1的 paths共 O(2 n/m) = O(n/m)條。
Over (1) Maximal Connected chains
Over (2) v maximal conneted chains中如果有 k > 1條 chains則有 k – 1 個 marked nodes。
Over (3) k > 1條chains的maximal connected chains 的chains 全部加起來是O(n/m)。 v 擁有
Over(4) By Disjont Lemma: v 擁有 k = 1條chains的maximal connected chains 共O(n/m)個。
Unproved Lemma Maximal θ-paths in linear time. (Lemma 2. 2 in M. Dubiner, Z. Galil, and E. Magen. Fast Tree Pattern Matching, J. ACM, 1994. ) v Chain Lemma. (Lemma 5. 5 in R. Cole and R. Hariharan. Tree Pattern Matching to Subset Matching in Linear Time, SIAM J. Computing, 2003. ) v
- Slides: 66