前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >生物信息学算法之 Python 实现|Rosalind 刷题笔记:007 兔子问题和递推

生物信息学算法之 Python 实现|Rosalind 刷题笔记:007 兔子问题和递推

作者头像
简说基因
发布2020-12-14 14:29:35
5700
发布2020-12-14 14:29:35
举报
文章被收录于专栏:简说基因简说基因

序列是一组有序对象的集合,它有如下特征:

  1. 序列内元素可以重复;
  2. 序列可以是有限的,也可以是无限的。

递推关系是一种用来定义序列的方法,可以通过前面的项,推导出后面的项。如斐波那契兔子问题,某月的兔子数,等于上一个月的兔子数,加上新生的兔子数;一个关键的现象是,因为新生的兔子要隔一代才有繁殖能力,所以某月新生的兔子数等于两个月前的兔子数。因此某月的兔子数可以通过下面的公式描述:

Fn = Fn-1 + Fn-2(F1=F2=1)

这便是递推。由递推启发的动态规划思想,是生物信息学中许多比对软件的算法基础。

给定:正整数 n 和 k。

需得:经过 n 个月后总共有多少对兔子。假定从一对兔子开始,每一代每对成年兔子生 k 对小兔子(不是一对)。

示例数据

代码语言:javascript
复制
5 3

示例结果

代码语言:javascript
复制
19

Python 实现

Rabbits_and_Recurrence_Relations.py

代码语言:javascript
复制
import sys

def fib(n, k):
    f = [1] * n
    for i in range(2, n):
        f[i] = f[i-1] + f[i-2] * k
    return f[n-1]

def fib2(n, k):
    return 1 if n < 3 else fib(n-1, k) + fib(n-2, k)*k

def test():
    return fib(5, 3) == 19 and fib2(5, 3) == 19

if __name__ == '__main__':
    if not test():
        print("fib: Failed")
        sys.exit(1)
    print(fib(35, 3))

解本题的关键是要理解:

  1. 本月的兔子数 = 上月的兔子数 + 新生的兔子数
  2. 由于兔子要隔一代才能繁殖,因此上月的新生兔子,在本月不繁殖,有繁殖能力的是上上月的兔子,并且要乘以繁殖系数 k。

本题用了两种方式解决,递推与递归,两者的区别是:

  1. 递推:从已知到未知,从小到大;就本题来说,就是以第 1,2 个月为基础,逐步推导出后续每一个月的兔子数。
  2. 递归:从未知到已知,从大到小。就本题来说,要计算某月的兔子数,先计算前面两个月的兔子数,如此循环,直到把任务都分解成计算第 1,2 个月的兔子数,而这两个月的兔子数是已知的。

Problem

A sequence is an ordered collection of objects (usually numbers), which are allowed to repeat. Sequences can be finite or infinite. Two examples are the finite sequence and the infinite sequence of odd numbers . We use the notation to represent the -th term of a sequence.

A recurrence relation is a way of defining the terms of a sequence with respect to the values of previous terms. In the case of Fibonacci's rabbits from the introduction, any given month will contain the rabbits that were alive the previous month, plus any new offspring. A key observation is that the number of offspring in any month is equal to the number of rabbits that were alive two months prior. As a result, if represents the number of rabbit pairs alive after the -th month, then we obtain the Fibonacci sequence having terms that are defined by the recurrence relation (with to initiate the sequence). Although the sequence bears Fibonacci's name, it was known to Indian mathematicians over two millennia ago.

When finding the -th term of a sequence defined by a recurrence relation, we can simply use the recurrence relation to generate terms for progressively larger values of . This problem introduces us to the computational technique of dynamic programming, which successively builds up solutions by using the answers to smaller cases.

Given: Positive integers and .

Return: The total number of rabbit pairs that will be present after months, if we begin with 1 pair and in each generation, every pair of reproduction-age rabbits produces a litter of rabbit pairs (instead of only 1 pair).

Sample Dataset

代码语言:javascript
复制
5 3

Sample Output

代码语言:javascript
复制
19

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-12-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 简说基因 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 示例数据
  • 示例结果
  • Python 实现
  • Problem
  • Sample Dataset
  • Sample Output
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档