Upload
cory-montgomery
View
216
Download
2
Embed Size (px)
Citation preview
“On an Algorithm of Zemlyachenko
for Subtree Isomorphism”Yefim Dinitz, Alon Itai, Michael
Rodeh (1998)
Presented by: Masha Igra, Merav Bukra
Agenda
• Introduction• Definition of Canonical Order• Overview of the Algorithm• Time Complexity• Hopcroft and Tarjan’s Canonical
Order
Agenda
• Introduction• Definition of Canonical Order• Overview of the Algorithm• Time Complexity• Hopcroft and Tarjan’s Canonical
Order
4
Introduction
• A free tree is a connected, acyclic and undirected graph
Free Tree
• A rooted tree is a free tree in which one of vertices is distinguished from others as its root.
Rooted Tree
5
Reduction to rooted case
• A vertex is central if its greatest distance from it to any other vertex is as small as possible.
6
Reduction to rooted case
• A vertex is central if its greatest distance from any other vertex is as small as possible.
7
Reduction to rooted case
• A vertex is central if its greatest distance from any other vertex is as small as possible.
8
Reduction to rooted case
• A vertex is central if its greatest distance from any other vertex is as small as possible.
9
Reduction to rooted case
• A vertex is central if its greatest distance from any other vertex is as small as possible.
10
Reduction to rooted case
• In case of the central edge, a new vertex will be connected to the endpoints of the central edge.
11
Reduction to rooted case
• In case of the central edge, a new vertex will be connected to the endpoints of the central edge.
• Thus, we assign to any free tree a unique rooted tree.
12
Graphs G1 = {V1, E1} and G2 = {V2, E2} are isomorphic if:
1. There is a bijection f from V1 to V2 and
2. There is a bijection g from E1 to E2 that maps each edge (v, u) to (f(v), f(u)).
Isomorphic graphs
G H
13
• An ordered tree is a rooted tree in which the children of each node are ordered.
• Isomorphic rooted trees: ordered trees that differ only in the order of siblings.
6
7
4 5
1 32
4
7
65
321
6
7
4 5
1 32
Isomorphic trees
• Observation: free trees are isomorphic iff the rooted trees assigned to them are isomorphic.
14
Isomorphism equivalence class
• In any isomorphism equivalence class of rooted trees, a particular canonically ordered tree will be defined.
• For any tree T, let us call such a distinguished tree in the isomorphism class of T the canonical representation of T.
• Two rooted trees are isomorphic if and only if their canonical representations coincide.
2
6
3 7
1 45
2
6
37
154
2
6
37
1 54
…..
15
Isomorphism equivalence class
• Two trees in the same isomorphism equivalence class have the same canonical representation.
• There are many kinds of canonical representations, we are going to describe one of them.
2
6
3 7
1 45
2
6
37
154
…..
….
A
B
C
D
….
….
….
16
Preorder traversal
• Preorder traversal:– Visit root.– Traverse children from left to right recursively.
2
6
3 7
1 45
Traversal sequence: { 6, 3, 7, 1, 2, 5, 4 }
Agenda
• Introduction• Definition of Canonical Order• Overview of the Algorithm• Time Complexity• Hopcroft and Tarjan’s Canonical
Order
18
Subree isomorphism
• Let Tv, the subtree rooted at v, be the
subtree that contains v.
• u and v are subtree isomorphic when
Tu and Tv are isomorphic.
• Height of a vertex v is the length of the path from v to the farthest leaf of its subtree.
height(v) = 2, height(r) = 4
u
r
v
T
Tv
Tu
19
• To determine whether u v :1. Compare their heights:
if height(u) < height(v), then u v
• if height(u) == height(v), then sort the children of u and v to create two lists:
u1 u2 …. ud(u)
v1 v2 …. vd(v)
u v if and only if the sorted list of u’s children is
lexicographically less than the sorted list of v’s children.
Canonical Order of vertices
C
C
C
C
C
C
C
C
C
• The canonical order on vertices is defined from the leaves up. The leaves are the minimal elements of .
C
C
C
1 1 4 9
1 2
C
C
C
C
20
• For example:
Children of u: 1 2 Children of v: 1 2
=> v u
• Thus u v if and only if height(u) = height(v),
and d(u) = d(v) and ui vi for i = 1, ..., d(u).
Canonical Order
C
C
u
v
C
C
C
C
1
1 1
12
2
21
Canonical Index of subtree isomorphism classes
• Starts with leaves (height 0):All leaves are isomorphic to each other, and have the minimum index 1.
1 1 1 1 1 1 1
22
Canonical Index of subtree isomorphism classes
• Starts with leaves (height 0):All leaves are isomorphic to each other, and have the minimum index 1.
• Height = 1:
v z u w = = 2
= 3 = 4
wu zv
1 1 1 1 1 1 1
C
C
C
][v][u
][z
][w
2 3 2 4
23
Canonical Index ][w
• Lemma: Two vertices have the same canonical index (label) iff they are isomorphic.
• By induction: Assumption: Correct for any height less than h.Step (height h): u and v are isomorphic by the assumption and the definition.
u
2 3 3 9
v
2 3 3 9
24
Canonical Index (example)
][w
1 1 11 1 1
32
1
2 23
4 4
5
25
Checking isomorphism of two given trees
uk vk
26
• For each height h:– Sort lists consisting of the children of vertices of height h.
• Total time will be
• We wish time O(n)• Since the numbers to be sorted are in range 1…n,
bucket-sort can ensure O(ni +range) for each sort.
• In worst case:
where the ranges of indexes and heights are of order n, algorithm requires .
Straightforward implementation
)( 2n
1
4
3
2
11
1
1
5 1
6 1
7
)log()log( nnOnnO hh
h
Agenda
• Introduction• Definition of Canonical Order• Overview of the Algorithm• Time Complexity• Hopcroft and Tarjan’s Canonical
Order
28
• Array level[h] = all the vertices of height h.
• Array children[h] = all the children of all vertices at level[h], sorted by their index.
• Construct the canonical indexes level by level:
– level[0] – the leaves.
– level[h] – assume that we already found the indexes of all vertices at lower levels:
• Construct new auxiliary data structure: ordered tree Dh
Data structures of the Algorithm
Example
Example
h = 0
Example
h = 1
Example
h = 2
33
• The process of construction of Dh (example):
1. Single vertex – root, to which all vertices of level[h] are associated.
Auxiliary ordered tree Dh
{u1, u2, u3, u4}rootD
h = 2
34
Auxiliary ordered tree Dh
{u1, u2, u3, u4}rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to vertex d of Dh.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
35
Auxiliary ordered tree Dh
{u1, u2, u3, u4}rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d
36
Auxiliary ordered tree Dh
{u2, u3, u4}rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d
d’{u1}1
37
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d {u2, u3, u4}
{u1}1
38
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d {u2, u4}
{u1, u3}1
d’
39
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d
{u2, u4}
{u1, u3}1
40
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d
{u2, u4}
{u3}1
2 d’{u1}
41
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d {u2, u4}
{u3}1
2{u1}
42
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d {u4}
{u3}1
2{u1}
2{u2}
43
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d
{u4}
{u3}1
2{u1}
2{u2}
44
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d
{u4}
{u3}1
2{u1}
2
2{u2}
{}
d’
45
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v d
{u4}
{u3}1
2{u1}
2
2{u2}
{}
46
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v d
{u4}
{}1
2{u1, u3}
2
2{u2}
{}
d’
47
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d {u4}
{}1
2{u1, u3}
2
2{u2}
{}
48
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
u
v
d {}
{}1
2{u1, u3}
2
2{u2}
{u4}d’
49
Auxiliary ordered tree Dh
rootD
• Dh construction:
1. Single vertex – root, to which all vertices of level[h] are associated.
2. Scan the array children[h] of level[h] by non decreasing order:
Suppose v is a child of u, and u was associated to d.Then we move u down to the child d’ of d whose label is .
h = 2
[v]
1
2{u1, u3}
2
2{u2}
{u4}
Traversing in preorder, on arrival to each new vertex of T – increasing the current canonical index by 1.
Overview of the Algorithm (for height h)
2
22
1
{u1,u3} {u2}
{u4}
Dh
=3
=4
=5
= max+1
Example
Agenda
• Introduction• Definition of Canonical Order• Overview of the Algorithm• Time Complexity• Hopcroft and Tarjan’s Canonical
Order
Time Complexity
• The Time Complexity of the whole algorithm is O(n).
• Since we traverse the nodes by heights, we handle each node only once.
• For each height, we construct Dh from children[h]. Each creation of d’ takes O(1), since we do not need search of its place in Dh:
We always add the rightmost leaf of Dh.
• The number of vertices of Dh for all h=0,…,height(T) is O(n).
• We don’t need to sort Dh because of the efficient insertions.
• Traversing Dh is linear in the number of vertices.
Agenda
• Introduction• Definition of Canonical Order• Overview of the techniques of the
Algorithm• Time Complexity• Hopcroft and Tarjan’s Canonical
Order
Hopcroft and Tarjan’s Canonical Order
• Their work is aimed at tree isomorphism only, rather than isomorphism together with subtree isomorphism.• They based on the depth (the distance from the root), which also distinguishes between vertices.
• Index(u) = Index(v) iff depth(u) = depth(v) and Tu and Tv are isomorphic.
• Since the indexes are given in increasing order, the indexes of all the vertices of any fixed depth d form a set of consecutive integers.
• All children of depth d are at the depth d+1. Thus, the sets of children in distinct phases are disjoint.• Therefore, the sum of their sizes is at most n. (Using bucket-sort may be done in time O(n), not Omega(n^2), as at our height-based approach.)
• The lack of their algorithm: Given the subtree isomorphism classes, it is easy to construct their classes. We do not know how to perform the reverse direction.
Questions…?