pc-trees & pq-trees. 2 table of contents review of pq-trees –template operations introducing...

Post on 08-Jan-2018

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

3 Order of Leaves in a Tree Usually we do not specify the child order in a (rooted) tree –When you lay down a tree on the plane there are many ways to do this so that the leaf orders are different How many different ways can you order the leaves of a tree? In some data structure it is important to specify certain child orders for certain nodes

TRANSCRIPT

PC-Trees & PQ-Trees

2

Table of contents

• Review of PQ-trees– Template operations

• Introducing PC-trees• The PC-tree algorithm

– Terminal nodes– Splitting the terminal path

• Observations on PC-trees

3

Order of Leaves in a Tree

• Usually we do not specify the child order in a (rooted) tree– When you lay down a tree on the plane there are

many ways to do this so that the leaf orders are different

• How many different ways can you order the leaves of a tree?

• In some data structure it is important to specify certain child orders for certain nodes

4

The consecutive ones property (COP)

• COP : Can one permute the columns of a (0,1)-matrix such that the 1’s in each row are consecutive?

• One application is in the representation of the matrix, e.g. when you send the matrix through the Internet– Need only to give the “start” and “end” positions of

the 1’s for each row, and the column order– O(m+n) space instead of O(mn) for a (m x n) –

matrix.

5

PQ-Trees

• There are many column permutations satisfying the COP

• One can use a PQ-tree to record all feasible permutations of the column indices– Children order of a P-node can be arbitrary– Children order of a Q-node can only be

inverted

6

Q P

1 2 3 5 6

Q

4

1 2 3 4 5 61 2 3 4 5 6

1 1 0 0 0 01 1 0 0 0 0

0 1 1 0 0 00 1 1 0 0 0

1 1 1 1 0 01 1 1 1 0 0

0 0 0 1 1 10 0 0 1 1 1

PQ-trees and the COP

7

Linear time algorithm on PQ-trees

• [1974] Booth and Lueker presented a linear time algorithm for the COP test based on PQ-trees

• PQ-tree can also be used to yield a linear time algorithm for interval graph recognition and planar graph recognition.

8

Operations on PQ-trees

• Initially, there is a root P-node with all columns as leaves. Rows are considered one by one.

• Every time a new row comes in, we need to modify the current PQ-trees so that the columns with 1’s in this row are consecutive.

• At the end of the iteration, obtain a PQ-tree representing all feasible permutations for rows considered so far.

9

Booth and Lueker’s PQ-Tree algorithm

• At each iteration consider a new row coming in.• It is a bottom-up approach consisting of two

stages:– 1. Node labeling

• The leaves of the incoming row are labeled full, all the other leaves are empty. the remaining nodes are labeled as follows.

• empty : all of its children are empty

• full : all of its children are full

• partial : neither full nor empty

– 2. Tree modification

10

1. Node labeling (bottom-up)

• The first time a node u becomes partial or full report to its parent.

• The first time a node u gets a partial or full child label u partial.

• The first time all children of a node u become full label u full.

11

2. Tree modification

• Need to modify the current tree so that all the incoming columns can be arranged consecutively.

• There is no need to do anything for full subtrees• Modify the subtree of every partial node

– At each iteration, modify the subtree T of a partial node starting from the lowest level of the tree

• The purpose is to ensure all full subtrees of T can be arranged consecutively. The subtree modification is based on 9 templates of subtree structures.

12

Table of contents

• Review of PQ-trees– Template operations

• Introducing PC-trees• The PC-tree algorithm

– Terminal nodes– Splitting the terminal path

• Observations on PC-trees

13

Template operations

• Change the children order so that full nodes become consecutive.

• Perform this in a bottom-up fashion• At each stage, there are 9 templates to

check altogether– These templates are, in some sense,

minimized.

14

Templates P0 & P1

.......

....... .......

.......

P0 P1

15

......

...

...

Template P2 for ROOT (T,S) when it is a P-node

16

Q-templates for partial nodes other than the root

• If the root is the only partial node, use template P2 in the last slide (so the root remains as a P-node).

• Otherwise, we use Q-node to represent a partial node during the operation so that the Q-templates can be adopted correctly.

17

......

......

Template P3 for a singly partial P-node which is not ROOT (T,S)

Even though this Q-node has only two children, we still

make it a Q-node to facilitate later Q-template operations

18

... ...

... ...

Template P4 for ROOT(T,S) when it is a P-node with one partial child

...... ...

...

19

Template P5 for a singly partial P-node, other than ROOT(T,S), with one partial

child

... ...

... ...

... ...

... ...

20

Template P6 for ROOT(T,S) when it is a doubly partial P-node

......

...

...

... ...

...... ...... ...

21

Templates Q0 & Q1

• Similar to P0 and P1

22

... ......

... ... ......

Template Q2 for a singly partial Q-node

23

Template Q3 for a double partial Q-node

... ...... .........

... ... ... ...

...... ...

24

Time complexity of the original PQ-tree operations

• Because of the frequent change of parent children relations, we can only keep parent pointers for two “end” nodes of each Q-node.

• This analysis of O(m+n) time is quite involved– Booth & Lueker used amortized analysis to argue

that it takes constant time at every iteration.• Details will be given later when we discuss

the complexity of PC-tree operations.

25

Table of contents

• Review of PQ-trees– Template operations

• Introducing PC-trees• The PC-tree algorithm

– Terminal nodes– Splitting the terminal path

• Observations on PC-trees

26

Circular Ones Property (CLOP)

• Does there exist a column order such that either – the 1’s in each row are consecutive or – the 0’s in each row are consecutive

• This would be easy to see if you wrap around the column indices in a circle.

27

PC-Trees

• A PC-tree is an un-rooted tree with two types of nodes P and C.

• Neighbors of a P-node can be permuted arbitrarily (Note that here we did not say “children” because there is no specific root).

• Neighbors of a C-node obey a clockwise order, which can only be inverted.

28

PQ-trees → PC-trees

Q1 P

1 2 3

Q2

5 64

1

2

3

5

6

4

Root

C1P

C2

If you consider the order of the parent and children of a Q-node in a cyclic fashion, its clockwise order can only be inverted, which is the spirit of a C-node

29

PC-trees and the circular ones property

PC-trees can record all feasible circular permutations of the column indices

2

P

1 3 5 64

1 2 3 4 5 61 2 3 4 5 6

1 1 0 0 0 01 1 0 0 0 0

0 1 1 0 0 00 1 1 0 0 0

1 1 1 1 0 01 1 1 1 0 0

0 0 0 1 1 10 0 0 1 1 1

1 0 0 0 1 11 0 0 0 1 1 C

C

1, 5, 6 are consecutive

30

Rotation of PC-trees

1 2 3 5 64

CRoot

C P

2 3 5 64

C

C

P

1

Root

The “root” concept is not important for PC-trees. It is there to maintain the parent-children relation for efficiency.

31

PC-trees and the consecutive ones property

PC-trees can also be used to record all feasible permutations for consecutive ones (fix the first column to be 1)

P

1 2 3 5 64

1 2 3 4 5 61 2 3 4 5 6

1 1 0 0 0 01 1 0 0 0 0

0 1 1 0 0 00 1 1 0 0 0

1 1 1 1 0 01 1 1 1 0 0

0 0 0 1 1 10 0 0 1 1 1

C

C

32

Table of contents

• Review of PQ-trees– Template operations

• Introducing PC-trees• The PC-tree algorithm

– Terminal nodes– Splitting the terminal path

• Observations on PC-trees

33

The PC-tree algorithm

1. Node labeling• The same as in PQ-tree

2. Tree splitting• Create a new C-node

• Same as PQ-tree, this is also an online algorithm (rows are processed as they are given), but without any templates

34

Table of contents

• Review of PQ-trees– Template operations

• Introducing PC-trees• The PC-tree algorithm

– Terminal nodes– Splitting the terminal path

• Observations on PC-trees

35

• Objective: Objective: Find a path that separates the full Find a path that separates the full subtrees from the empty onessubtrees from the empty ones– Sufficient to find the two Sufficient to find the two end nodesend nodes of this path of this path

• A A rootingrooting of a PC-tree is to assign a node as of a PC-tree is to assign a node as the root and redirect all parent-child relationsthe root and redirect all parent-child relations

• Terminal NodeTerminal Node– A partial node whose children are either empty or

full relative to some rooting

Our approach:Finding a separating path

empty full

36

Key observationsKey observations

• There are at most two terminal nodes– There is a unique tree path between these

two nodes• Full children of C-nodes on the path can

be correctly flipped to one side, and empty ones to the other side

37

The unique terminal path between The unique terminal path between two terminal nodes two terminal nodes u, u’ u, u’ (I)(I)

u m u'empty

fullFlippable C-node

RWhat if node u is a C-node?

Case 1. u, u’ and R are not on a path

38

The unique terminal path between The unique terminal path between two terminal nodes two terminal nodes u, u’ u, u’ (II)(II)

u u'

R

empty

full

Case 2. u, u’ and R are on a path

39

Forbidden structures on PC-trees

• Whenever you have such a collection of subtrees, the matrix does not satisfy the CLOP (and hence, does not satisfy the COP).– Why? 1, 3, 5 have to be next to each other.– WLOG, assume the order is 135 where 3 is

between 1 and 5. But then, there is no place for 4, which needs to be to the left or right of 3.

33 44 55 6611 22

Where 1, 3, 5 are distinct and

consecutive

40

Forbidden structures on PC-trees

33 44 55 66

1. Three terminal nodes 2. Non-flippable C-node

11 22Where 1, 3, 5 are distinct

When three parents are on a path

The following two cases are forbidden for matrices satisfying the CLOP

41

Table of contents

• Review of PQ-trees– Template operations

• Introducing PC-trees• The PC-tree algorithm

– Terminal nodes– Splitting the terminal path

• Observations on PC-trees

42

The unique terminal path The unique terminal path between two terminal nodesbetween two terminal nodes

u m u'

R

empty

full

43

The path splitting operation

m

i

m

Duplicate each P-node that has both a full subtree and an empty subtree

44

Connecting to the new C-node (I)- using the “full side” as an example

u m u'

• All P-nodes on the path connects directly to the new node– obeying the order on the path

• All C-nodes on the path are deleted. Their children connect directly to the new node– obeying their original order as well as path order

45

Connecting to the new C-node (II)

CP PC C

11 554422 77 88

63

P P

1 542 7 863

A Graphical Illustration of the PQ-Tree Operations vs.

the PC-Tree Operations

For this example, we only demonstrate the flipping process

(but not the merge operation)

47

Template Matching of PQ-trees (I)

P

Q

P

P P

QP

Q

48

Template Matching of PQ-trees (II)

P

Q

P

P P

QP

Q

V

49

What has been accomplished by the flipping ?

P

Q

P

P P

QP

Q

P

Q

P

P P

QP

Q

50

Another look at the result of the flipping operation

m

R

u u’

P

Q

P

P P

QP

Q

51

The corresponding PC-Tree Operation

P4

P1

P3P2

P5

52

LCA: The least common ancestor of full leaves

1. LCA is a full node:

No tree modification necessary

2. LCA is a partial node:A child Q-node is created with the full children arranged consecutively (there can be many cases, one example is shown below)

The PQ-tree after the modification

53

An example for a PC-tree operation equivalent to a template conversion

54

Complexity Analysis• Want to show the cost is proportional to the # of

1’s in the row (i.e. # of full leaves). • There is a catch here: for each C-node, we cannot

afford to have a parent pointer for each child due to frequent pointer change in path splitting. – Can only keep parent pointer for two “end” nodes– Keep a cycle for each C-node as a double linked list– Internal node of the cycle find parent through neighbors!

virtual real

55

The cost of node labeling

• Easy for P-node; for each internal node of a C-node, inform its two neighbors when it becomes full or partial.– Since full children of a C-node must be

consecutive, it is easy to check at the end whether this C-node is full or partial

– In case none of the two end nodes are full, this C-node is the LCA. Nothing else needs to be done (similar to the case that the LCA is full)

56

The cost of path splitting (I)• Instead of connecting the new C-node to

each of its child, go around the boundary of the C-node to form its cycle.

CP PC C

11 554422 77 88

63

P P

1 542 7 863

Each P-node that has both a full subtree and an empty subtree is duplicated

57

Path splitting and the new C-node

The terminal path

The new C-node

Cost = path traversal + # of P-node duplication

58

The cost of path splitting (II)

• Each P-node that has both full subtrees and empty subtrees need to be duplicated– The # of new P-nodes added at each

iteration # of full subtrees• Need to worry about the cost of

traversing through empty P-nodes

59

A potential problem: what if a cycle contains very few full

nodes?• The cost of forming this cycle

(traversing through empty P-nodes in the cycle) is not proportional to the number of full nodes.

u u'

60

Charging the Traversal of empty P-nodes separately

• Every time an empty P-node is traversed in forming a cycle, its two neighboring tree edges become cycle links

• Once a tree edge becomes a cycle link, it remains as a link.– Each tree edge can be converted to a link

at most once!

61

Table of contents

• Review of PQ-trees– Template operations

• Introducing PC-trees• The PC-tree algorithm

– Terminal nodes– Splitting the terminal path

• Observations on PC-trees

62

Final observations (I)

• At every iteration, we determine a unique path that separates the tree into “two parts” (for circular ones, the root is immaterial).

63

Final observations (II)• The two terminal nodes actually give the two “cuts”

in terms of the column partition (when the new row is placed into the current partition).

64

top related