【问题标题】:Backtracking, Text Segmentation回溯,文本分割
【发布时间】:2020-05-27 22:24:31
【问题描述】:

我无法弄清楚这个头脑风暴算法问题!有人可以帮忙吗?

--------描述--------

假设您是一位正在寻找某种基因的科学家。你知道你是一个基因 寻找应该由以下块组成\

AT GG CTAC

我们将这些块称为“有效”。所有其他块都称为“无效”。现在给定一个由符号组成的基因

'A', 'G', 'C', 'T' 你应该确定给定的基因是否可以用有效的块形成。 可以使用有效块形成的基因示例:

GGATATCTAC CTACGGGG

无法使用有效块形成的基因示例:

CCC GGCAC

Question!
1. How many possible choice you have after each reduction?
2. Try to draw a recursion tree for this algorithm
3. What kind of data do you need to store after each reduction?
4. Try to write a recurrence relation for the running time of this algorithm.
5. Try to estimate the running time from the above recurrence relation. Is running time exponential or
polynomial in terms of n, the length of a gene?

【问题讨论】:

  • 什么是reduction?例子会很好。
  • 构建 DFA

标签: algorithm backtracking text-segmentation


【解决方案1】:

执行复杂度为 nnl 的回溯的 Python 代码,其中 n 是此处的块数 3,L 是基因长度。对于树,我打印当前文本呢?然后是我测试添加的元素。

def genes(chaine, elements, result):
  print()
  if(chaine == ""):
      return result
  ctn = 1
  for elem in elements:
      print(" ".join([" "]*len(chaine)*ctn), "".join(result) + "? " + elem, end = '')
      if(chaine.startswith(elem)):
          return genes(chaine[len(elem):], elements, result + [elem])
      ctn = ctn + 1

   return 0


result = genes("GGATGGATCTAC", ["GG", "AT", "CTAC"], [])
print(result)

输出:

                  ? GG
                GG? GG                                        GG? AT
            GGAT? GG
        GGATGG? GG                        GGATGG? AT
    GGATGGAT? GG                GGATGGAT? AT                        GGATGGAT? CTAC

['GG', 'AT', 'GG', 'AT', 'CTAC']

【讨论】:

  • 感谢您的回答,您能否为此算法绘制递归树,Question 2
  • 我更新以集成打印。这是一个快速而肮脏的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-06
  • 2012-12-09
  • 2011-11-01
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多