def getCopNumber(rt: RootedTree): '''Algorithem 1 Compute the copnumber of a tree root is picked randomly if not given return the label of the root''' if rt.labels != None and rt.labels[rt.root] != None: logger.debug(f"the cop number is already knonwn for rt = {rt}") return rt.labels[rt.root] logger.debug(f'rt = {rt}') # 2 revNodes = rt.reverseList() if rt.labels == None: rt.labels = dict( (node, Label.noChild() if len(descendant(rt, node)) == 0 else None) for node in revNodes) logger.debug(f'rt = {rt}') # 3 while (rt.labels[rt.root] == None): getNextLabel(rt, revNodes) return rt.labels[rt.root]
def compute_label(rt, u): '''rt = T1 return T1u[u]''' if (len(rt.descendant(u)) == 0): rt.labels[u] = Label.noChild() return rt.labels[u] logger.debug("start") k = c1Star(subForest(rt, u)) # ^k_wb numKWb = numKWeakBranChild(rt, u, k) # ^k_pb numKPb = numKPreBranChild(rt, u, k) # ^k_c numKC = numKC1Child(rt, u, k) # h^k_w hkW = maxWeaklyCounter(rt, u, k) # h^k hk = maxInitialCounter(rt, u, k) logger.debug( f"u = {u}, k = {k}, numKWb = {numKWb}, numKPb = {numKPb}, numKC ={numKC}, hkW = {hkW}, hk={hk}" ) # 1 if numKWb > 1: rt.labels[u] = Label.make(k + 1, constant.PERPEN_SYM) logger.debug("#1 is triggered") # 3 elif numKWb == 1 and numKPb >= 1: rt.labels[u] = Label.make(k + 1, constant.PERPEN_SYM, 0, 0, 0, 0) logger.debug("#3 is triggered") # 5 elif (numKWb == 1 and numKPb == 0 and numKC >= 2): # 6 if hkW == 2: rt.labels[u] = Label.make(k + 1, constant.PERPEN_SYM, 0, 0, 0, 0) logger.debug("#6 is triggered") # 8 elif (hkW == 1 and hk >= 1): rt.labels[u] = Label.make(k + 1, constant.PERPEN_SYM, 0, 0, 0, 0) logger.debug("#8 is triggered") # 10 elif (hkW == 1 and hk == 0): rt.labels[u] = Label.make(k, constant.PERPEN_SYM, 1, 2, 0, 0) logger.debug("#10 is triggered") # 12 elif (hkW == 0 and hk == 2): rt.labels[u] = Label.make(k + 1, constant.PERPEN_SYM, 0, 0, 0, 0) logger.debug("#12 is triggered") # 14 elif (hkW == 0 and hk <= 1): rt.labels[u] = Label.make(k, constant.PERPEN_SYM, 1, 1, 0, 0) logger.debug("#14 is triggered") # 16 elif numKWb == 1 and numKC == 1: # 17 if hkW == 2: rt.labels[u] = Label.make(k, u) logger.debug("#17 is triggered") # 19 elif hkW == 1: rt.labels[u] = Label.make(k, constant.PERPEN_SYM, 1, 2, 0, 0) logger.debug("#19 is triggered") # 21 elif hkW == 0: rt.labels[u] = Label.make(k, constant.PERPEN_SYM, 1, 1, 0, 0) logger.debug("#21 is triggered") # 23 elif numKWb == 0: # 24 if numKPb >= 3: rt.labels[u] = Label.make(k + 1, constant.PERPEN_SYM) logger.debug("#24 is triggered") # 26 elif numKPb == 2: rt.labels[u] = Label.make(k, constant.PERPEN_SYM, 1, 0, 0, 0) logger.debug("#26 is triggered") # 28 elif numKPb == 1: rt.labels[u] = Label.make(k, constant.PERPEN_SYM, 0, 0, 1, 0) logger.debug("#28 is triggered") # 30 elif numKPb == 0: # 31 if hk == 2: rt.labels[u] = Label.make(k, constant.PERPEN_SYM, 0, 0, 1, 0) logger.debug("#31 is triggered") # 33 elif hk == 1: rt.labels[u] = Label.make(k, constant.PERPEN_SYM, 0, 0, 0, 2) logger.debug("#33 is triggered") # 35 elif hk == 0: rt.labels[u] = Label.make(k, constant.PERPEN_SYM, 0, 0, 0, 1) logger.debug("#35 is triggered") if rt.labels[u] == None: raise Exception("nothing is changed from compute-label") return rt.labels[u]