390 likes | 412 Views
A Test for the Consecutive Ones Property. Outline. Consecutive ones property PQ-trees Template operations Complexity Analysis The most time consuming part 2 children for each Q-node. Consecutive 1’s Property of matrices.
E N D
Outline • Consecutive ones property • PQ-trees • Template operations • Complexity Analysis • The most time consuming part • 2 children for each Q-node
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
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.
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
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.
Outline • Consecutive ones property • PQ-trees • Template operations • Complexity Analysis • The most time consuming part • 2 children for each Q-node
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
P-Q TreesAn Example Q P 1 2 3 4 L(T) = { all permutations generated by T } In the example, L(T) = { 1234,1243,4321,3421 }
Q Q P 4 5 6 3 1 2 PQ-trees and the COP 1 2 3 4 5 6 1 1 0 0 0 0 0 1 1 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 A matrix satisfies the COP iff there is a PQ-tree recording all feasible permutations of its column indices
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.
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.
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
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 upartial. • The first time all children of a node u become full label ufull.
Outline • Consecutive ones property • PQ-trees • Template operations • Complexity Analysis • The most time consuming part • 2 children for each Q-node
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.
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.
LCA is a full node: No tree modification necessary 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 LCA: The least common ancestor of full leaves
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
....... ....... ....... ....... Templates P0 & P1 P0 P1
... ... Template P2 for ROOT (T,S) when it is a P-node ... ...
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.
... ... 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
... ... ... ... Template P4 for ROOT(T,S) when it is a P-node with one partial child ... ... ... ...
... ... ... ... Template P5 for a singly partial P-node, other than ROOT(T,S), with one partial child ... ... ... ...
... ... ... ... ... Template P6 for ROOT(T,S) when it is a doubly partial P-node ... ... ... ... ... ...
... ... Templates Q0 & Q1 • Similar to P0 and P1 Q0 Q1 ... ...
... ... ... ... Template Q2 for a singly partial Q-node ... ... ...
... ... ... ... ... ... Template Q3 for a double partial Q-node ... ... ... ... ... ... ...
Outline • Consecutive ones property • PQ-trees • Template operations • Complexity Analysis • The most time consuming part • 2 children for each Q-node
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.
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)
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(nlogn) if you do the change in a smart way.
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.
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.
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)