Crates.io | pq-tree |
lib.rs | pq-tree |
version | 0.1.0 |
source | src |
created_at | 2022-06-15 14:50:33.180374 |
updated_at | 2022-06-15 14:50:33.180374 |
description | PQ-tree implementation for consecutive ones property (C1P) and graph planarity testing |
homepage | |
repository | https://github.com/khitrin/pq-tree-rs |
max_upload_size | |
id | 606552 |
size | 81,435 |
A PQ-tree is a data structure that represents all possible permutations of some elements limited by a set of constraints. Each constraint enforces consecutive ordering of a subset of elements in the leaf set of the tree.
PQ-tree based algorithms can be used for solving various problems, including consecutive ones property testing for matrices, graph planarity testing, interval graph recognition and so on.
A PQ-tree tree consists of three types of nodes: P-nodes, allowing arbitrary permutations of children, Q-nodes, allowing only reversions and L-nodes that represent tree leaves.
use pq_tree::PQTree;
// is this matrix C1P ?
let matrix = vec![
vec![1, 1, 0, 1, 1],
vec![0, 0, 0, 1, 0],
vec![1, 1, 1, 1, 0],
vec![1, 1, 1, 1, 1],
vec![1, 0, 1, 1, 0],
];
let mut tree = PQTree::from_leaves(&[1, 2, 3, 4, 5]).unwrap();
tree = tree.reduction(&[1, 3, 4, 5]).unwrap();
tree = tree.reduction(&[1, 3, 4]).unwrap();
tree = tree.reduction(&[3, 4, 5]).unwrap();
tree = tree.reduction(&[1, 2, 3, 4, 5]).unwrap();
tree = tree.reduction(&[1, 4]).unwrap();
tree.frontier().into_iter().for_each(|r| println!("{:?}", matrix[r - 1]));
// [1, 1, 0, 1, 1]
// [1, 1, 1, 1, 1]
// [1, 1, 1, 1, 0]
// [1, 0, 1, 1, 0]
// [0, 0, 0, 1, 0]
// Yes, it is!