a test for the consecutive ones property 1/39. outline consecutive ones property pq-trees template...

39
A Test for the Consecutive Ones Property 1/39

Upload: conrad-asher-kelly

Post on 29-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

A Test for the Consecutive Ones Property

1/39

Page 2: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Outline

• Consecutive ones property

• PQ-trees

• Template operations

• Complexity Analysis– The most time consuming part – 2 children for each Q-node

2/39

Page 3: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Consecutive 1’s Property of matrices

Given a (0,1)- matrix M, does there exist a PERMUTATION of the COLUMINS of M such that the 1’s in the ROWS are consecutive?

1 2 3 4 1 1 0 0 1 0 0 1 0 1 1 0

3 2 1 40 1 1 00 0 1 11 1 0 0

3/39

Page 4: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Consecutive requirement on the rows

• Each row i of M can be viewed as a requirement that those columns with a 1 in row j must be consecutive.

• Booth and Lueker 1976 showed that the ﹝ ﹞consecutive ones property can be tested using P-Q trees in linear time.

• They process the consecutive requirement in a row by row fashion.

4/39

Page 5: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

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? – standard tree operation

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

5/39

Page 6: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

6/39

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.

Page 7: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Outline

• Consecutive ones property

• PQ-trees

• Template operations

• Complexity Analysis– The most time consuming part – 2 children for each Q-node

7/39

Page 8: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

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

8/39

Page 9: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

P-Q TreesAn Example

Q

P1 2

3 4L(T) = { all permutations generated by T }

In the example, L(T) = { 1234,1243,4321,3421 }9/39

Page 10: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

10

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

A matrix satisfies the COP iff there is a PQ-tree recording all feasible permutations of its column indices

Page 11: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

11

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.

Page 12: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

12

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-tree so that the columns with 1’s in this row are consecutive.– If there is any problem, COP is not satisfied

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

Page 13: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

13

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 : not all full or all empty

– 2. Tree modification

Page 14: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

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.

14/39

Page 15: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Outline

• Consecutive ones property

• PQ-trees

• Template operations

• Complexity Analysis– The most time consuming part – 2 children for each Q-node

15/39

Page 16: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

16

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 (bottom-up)

• 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.

Page 17: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

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.

17/39

Page 18: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

18

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

Page 19: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

19

Motivation behind the templates

• By the previous observation, if the LCA is a full node, there would be no tree modification.

• Otherwise, one would have a partial node. Each template guarantees that after the tree modification, the full subtrees would be arranged consecutively.– Template P2 is a special case where the LCA has

only one full child. In template P3, there are full nodes in other subtrees not shown.

– In all other cases, you would get a partial Q-node – You don’t have to remember all the templates

Page 20: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Templates P0 & P1

.......

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

.......

P0 P1

20

Page 21: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

21

......

...

...

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

Page 22: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

22

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.

Page 23: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

23

......

......

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

Note that, in this case, there could be full leaves in other subtrees not shown in the picture, different from P2

Page 24: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

24

... ...

... ...

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

...... ...

...

Page 25: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

25

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

child

... ...

... ...

... ...

... ...

Page 26: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

26

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

......

...

...

... ...

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

Page 27: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

27

Templates Q0 & Q1

• Similar to P0 and P1

...

......

...

Q0 Q1

Page 28: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

28

... ......

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

Template Q2 for a singly partial Q-node

Page 29: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

29

Template Q3 for a double partial Q-node

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

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

...... ...

Page 30: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

30/39

Page 31: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

31

Page 32: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

32

Page 33: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Outline

• Consecutive ones property

• PQ-trees

• Template operations

• Complexity Analysis– The most time consuming part – 2 children for each Q-node

33/39

Page 34: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

34

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.

Page 35: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Parent Change

• How often do we need to change the parent pointers?

• If the original parent is a P-node, then always change parents for the smaller subtree

• What about the Q-nodes?– In the worst case, could be O(n2)

35

Page 36: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Changing the parent for children of a Q-node to another Q-node

… …

… …

… …

… …… …

Every node in this Q-node needs to change parent pointer in every template operation, the total number of changes could be O(n2), or O(nlog n) if you do the change in a smart way.

36/39

Page 37: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Sufficient for each Q-node to keep two children with parent pointers

• Connect the children of a Q-node using a doubly linked list.

• Only the two end children have parent pointers.

• The other nodes must find their parents through list traversal.

37

Page 38: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Full children of a Q-node

• When a child of a Q-node becomes full, but does not have a parent pointer, how does it tell the parent?– Only tell its two neighbors in the list – Full children of a Q-node must be

consecutive in the list.

38

Page 39: A Test for the Consecutive Ones Property 1/39. Outline Consecutive ones property PQ-trees Template operations Complexity Analysis –The most time consuming

Merging two Q-nodes

• There are two ways to merge:– Left-right or– Bottom-top

• Sufficient to let the end nodes make new friends (extend the linked list)

39