def refine_matrix(m): p = part.Partition(m.get_row_info(0)) C1P = True for i in xrange(1, m._height): if not p.refine(m.get_row_info(i)): return False #endif #endfor return True
def check_C1P(matrix): # create overlap graphs og = create_overlap_graph(matrix) # overlap graph (as a spanning tree) # traverse overlap graph and refine partitions for j in xrange(len(og)): p = part.Partition(og[j]._head) # current partition to refine for v in og[j]._rows: if not p.refine(v): return False #endif #endfor #endfor return True
def make_PQR_tree_from_graph(support, og): # add leaves for x in support: og.insert(0, c1p.OverlapComponent(bm.Row(set([x]), 'leaf', 0, [], False))) #endfor # create PQR-tree nodes nodes = [tree.TreeNode(g._support) for g in og] # the nodes of the PQR-tree # for each overlap graph make a node in the PQR-tree for g in xrange(len(og)): head = og[g]._head # the head of the current overlap graph # if there is only one node in the tree the component is a P node otherwise: if len(og[g]._rows) > 0: # refine to determine order or R'ness is_R = False # True if node is an R-node p = part.Partition(head) # partition of the # current overlap graph for row in og[g]._rows: if not p.refine(row): is_R = True break #endif #endfor if is_R: nodes[g]._value = 'R' else: nodes[g]._value = 'Q' nodes[g]._child = p._part #endif else: # P node if len(og[g]._support) == 1: nodes[g]._value = og[g]._support.__iter__().next() else: nodes[g]._value = 'P' #endif #endif #endfor return make_PQR_tree_from_partitions(support, nodes, og)
def test_row(row, p): found = False # True if first intersecting part is found failed = False # True if not C1P j = 0 # current index mem = [[], []] # altered rows # find overlaps while j < len(p): if len(p[j]._part._support & row._set) > 0: overlaps = False # True if current component overlaps with row for s in p[j]._list: if overlap(row._set, s._set): overlaps = True break #endif #endfor if overlaps: # refine the new found partition if not found: pNew = PartitionTree(p[j]._part.copy(), copy.copy(p[j]._list)) found = True failed = not pNew._part.refine(row) if not failed: pNew._list.append(row) mem[0].append(p[j]) mem[1].append(pNew) #endif else: # if partition already found join the two partitions # error (this should not happen) if len(pNew._part._part._value) == 0: print 'error1' print row return #endif pCopy = PartitionTree(p[j]._part.copy(), copy.copy(p[j]._list)) failed = not pNew._part.join(pCopy._part) if not failed: pNew._list.extend(p[j]._list) mem[0].append(p[j]) #endif #endif # error (this should not happen) if len(pNew._part._part._value) == 0: print 'error2' print row return #endif if failed: break #endif #endif j = j + 1 #endwhile # create new partition (new overlap component) if not found: par = PartitionTree(part.Partition(row), [row]) sort.insert(p, par, 0, len(p), comp_part) mem = [[], [par]] elif not failed: # remove refined rows for m in mem[0]: p.remove(m) #endfor # add refined row sort.insert(p, pNew, 0, found, comp_part) else: mem = [[], []] #endif return not failed, mem
def make_PQCR_tree_from_graph(support, og): # add leaves for x in support: og.insert(0, c1p.OverlapComponent(bm.Row(set([x]), 'leaf', 0, [], False))) #endfor # create PQR-tree nodes nodes = [tree.TreeNode(g._support) for g in og] # the nodes of the PQR-tree # for each overlap graph make a node in the PQR-tree for g in xrange(len(og)): head = og[g]._head # the head of the current overlap graph # if there is only one node in the tree the component is a P node otherwise: if len(og[g]._rows) > 0: # refine to determine order or R'ness is_R = False # True if node is an R-node p = part.Partition(head) # partition of the current overlap graph # print str(p) for row in og[g]._rows: if not p.refine(row): is_R = True break #endif #endfor if is_R: # check if node is circC1P for n in xrange(g+1, len(og) - 1): if og[g]._support < og[n]._support: if n != len(og) - 1: nodes[g]._value = 'R' #endif #endif #endfor if nodes[g]._value != 'R': m = bm.BinaryMatrix() for row in og[g]._all_rows: m.add_row_info(row) #endfor if cc1p.check_circC1P(m): p_comp = part.Partition(head) non_c1p = [] for row in og[g]._rows: if p_comp.test_refine(row._set) < 0: p_comp.refine(row) else: non_c1p.append(row) #endif #endif for row in non_c1p: p_comp.left_refine(row) p_comp.right_refine(row) if len(row._set - p_comp._support) > 0: p_comp.insert_after(tree.TreeNode(row._set - p_comp._support), p_comp._end) p_comp._support = p_comp._support | row._set #endif #endfor nodes[g]._value = 'C' nodes[g]._child = p_comp._part else: nodes[g]._value = 'R' #endfor else: nodes[g]._value = 'R' #endif else: nodes[g]._value = 'Q' nodes[g]._child = p._part #endif else: # P node if len(og[g]._support) == 1: nodes[g]._value = og[g]._support.__iter__().next() else: nodes[g]._value = 'P' #endif #endif #endfor return make_PQR_tree_from_partitions(support, nodes, og)