获取指定长度得全部序列

通过事件来表述这个序列,即n重伯努利实验(二项分布)的全部可能结果。比如时间a表示为: a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 假设每次实验为从a中选择一个数字。那么进行n次实验,获得全部可能得序列。

比方,进行两次实验, n=2, 那么可能得结果有100个。这里由于每次实验都是相对独立的,所以每次实验的结果可能出现反复,也就是说在获得全部可能的序列中,能够存在反复得值。

递归实现,DFS(深度优先遍历)

def gen_all_sequence_dfs(outcomes, length):
"""
generate all sequence by dfs outcomes: all the possible event, a list
length: how many times does the sequence repeat, sequence length
""" res = []
seq = []
dfs_sequence(outcomes, length, seq, res) return res def dfs_sequence(outcomes, length, seq, res):
"""
deep first search
"""
if 0 == length:
res.append(tuple(seq[:]))
return for key in outcomes:
seq.append(key)
dfs_sequence(outcomes, length - 1, seq, res)
seq.pop() def run_dfs_example1():
"""
Example of all sequences
"""
print 'dfs gen all sequence'
outcomes = set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) length = 2
seq_outcomes = gen_all_sequence_dfs(outcomes, length)
print "Computed", len(seq_outcomes), "sequences of", str(length), "outcomes"
print "Sequences were", seq_outcomes run_dfs_example1()

执行输出结果:

dfs gen all sequence
Computed 100 sequences of 2 outcomes
Sequences were [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]

非递归实现

利用动态规划的原理(这里我也不太熟悉是不是动态规划,暂且这么叫。假设有错误,请大家帮忙更正),动态的计算第k次实验后获得得全部得序列。

依据第k-1次实验的全部得序列得结果,然后把每一次结果拿出来计算这一次结果再加上一次实验(即第k次实验)能够获得的结果。

def gen_all_sequences(outcomes, length):
"""
Iterative function that enumerates the set of all sequences of
outcomes of given length Permutation allow repeat
""" ans = set([()])
for dummy_idx in range(length):
temp = set()
for seq in ans:
for item in outcomes:
new_seq = list(seq)
new_seq.append(item)
temp.add(tuple(new_seq))
ans = temp
return ans # example for digits
def run_example1():
"""
Example of all sequences
"""
print 'gen all sequence'
outcomes = set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
#outcomes = set(["Red", "Green", "Blue"])
#outcomes = set(["Sunday", "Mondy", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]) length = 2
seq_outcomes = gen_all_sequences(outcomes, length)
print "Computed", len(seq_outcomes), "sequences of", str(length), "outcomes"
print "Sequences were", seq_outcomes print '#############################################'
run_example1()

代码执行结果:

gen all sequence
Computed 100 sequences of 2 outcomes
Sequences were set([(7, 3), (6, 9), (0, 7), (1, 6), (3, 7), (2, 5), (8, 5), (5, 8), (4, 0), (9, 0), (6, 7), (5, 5), (7, 6), (0, 4), (1, 1), (3, 2), (2, 6), (8, 2), (4, 5), (9, 3), (6, 0), (7, 5), (0, 1), (3, 1), (9, 9), (7, 8), (2, 1), (8, 9), (9, 4), (5, 1), (7, 2), (1, 5), (3, 6), (2, 2), (8, 6), (4, 1), (9, 7), (6, 4), (5, 4), (7, 1), (0, 5), (1, 0), (0, 8), (3, 5), (2, 7), (8, 3), (4, 6), (9, 2), (6, 1), (5, 7), (7, 4), (0, 2), (1, 3), (4, 8), (3, 0), (2, 8), (9, 8), (8, 0), (6, 2), (5, 0), (1, 4), (3, 9), (2, 3), (1, 9), (8, 7), (4, 2), (9, 6), (6, 5), (5, 3), (7, 0), (6, 8), (0, 6), (1, 7), (0, 9), (3, 4), (2, 4), (8, 4), (5, 9), (4, 7), (9, 1), (6, 6), (5, 6), (7, 7), (0, 3), (1, 2), (4, 9), (3, 3), (2, 9), (8, 1), (4, 4), (6, 3), (0, 0), (7, 9), (3, 8), (2, 0), (1, 8), (8, 8), (4, 3), (9, 5), (5, 2)])

Permutation 获取全部排列

给定一个序列。比如上面给出得 a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 假设依照上面获取序列的算法,那么每次实验都作为独立得实现,序列中能够出现反复实验结果。

可是再获取排列的时候则不能依照n重伯努利实验得思想进行了。获取排列不同意有反复得结果,即一个元素仅仅能被选择一次。可是在排列中是存在元素得顺序因素得。也就是说相同得两个元素,不同得顺序为不同得排列。

非递归实现

在上面非递归算法得基础上。添加一个推断。推断该元素是否已经选择过就能够实现排列得获取。

def gen_permutations(outcomes, length):
"""
Iterative function that enumerates the set of all sequences of
outcomes of given length
Permutation not allow repeat
""" ans = set([()])
for dummy_idx in range(length):
temp = set()
for seq in ans:
for item in outcomes:
if item in seq:
continue
new_seq = list(seq)
new_seq.append(item)
temp.add(tuple(new_seq))
ans = temp
return ans # example for digits
def run_example1():
"""
Example of all sequences
"""
outcomes = set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
#outcomes = set(["Red", "Green", "Blue"])
#outcomes = set(["Sunday", "Mondy", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]) length = 2
seq_outcomes = gen_permutations(outcomes, length)
print "Computed", len(seq_outcomes), "sequences of", str(length), "outcomes"
print "Sequences were", seq_outcomes run_example1()

代码执行结果:

Computed 90 sequences of 2 outcomes
Sequences were set([(7, 3), (6, 9), (0, 7), (1, 6), (3, 7), (2, 5), (8, 5), (5, 8), (4, 0), (9, 0), (6, 7), (7, 6), (0, 4), (3, 2), (2, 6), (8, 2), (4, 5), (9, 3), (6, 0), (7, 5), (0, 1), (3, 1), (7, 8), (2, 1), (8, 9), (9, 4), (5, 1), (7, 2), (1, 5), (3, 6), (8, 6), (4, 1), (9, 7), (6, 4), (5, 4), (7, 1), (0, 5), (1, 0), (0, 8), (3, 5), (2, 7), (8, 3), (4, 6), (9, 2), (6, 1), (5, 7), (7, 4), (0, 2), (1, 3), (4, 8), (3, 0), (2, 8), (9, 8), (8, 0), (6, 2), (5, 0), (1, 4), (3, 9), (2, 3), (1, 9), (8, 7), (4, 2), (9, 6), (6, 5), (5, 3), (7, 0), (6, 8), (0, 6), (1, 7), (0, 9), (3, 4), (2, 4), (8, 4), (5, 9), (4, 7), (9, 1), (5, 6), (0, 3), (1, 2), (4, 9), (2, 9), (8, 1), (6, 3), (7, 9), (3, 8), (2, 0), (1, 8), (4, 3), (9, 5), (5, 2)])

递归实现

def gen_permutations_dfs(outcomes, length):
"""
Iterative function that enumerates the set of all sequences of
outcomes of given length
Permutation not allow repeat, DFS
""" res = []
seq = []
dfs_permutation(outcomes, length, seq, res) return res def dfs_permutation(outcomes, length, seq, res):
"""
deep first search
""" if 0 == length:
res.append(tuple(seq[:]))
return for key in outcomes:
if key in seq:
continue
seq.append(key)
dfs_permutation(outcomes, length - 1, seq, res)
seq.pop() # example for digits
def run_example2():
"""
Example of all sequences
"""
outcomes = set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
#outcomes = set(["Red", "Green", "Blue"])
#outcomes = set(["Sunday", "Mondy", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]) length = 2
seq_outcomes = gen_permutations_dfs(outcomes, length)
print "Computed", len(seq_outcomes), "sequences of", str(length), "outcomes"
print "Sequences were", seq_outcomes run_example2()

执行结果:

Computed 90 sequences of 2 outcomes
Sequences were [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 0), (3, 1), (3, 2), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 0), (4, 1), (4, 2), (4, 3), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 6), (5, 7), (5, 8), (5, 9), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 7), (6, 8), (6, 9), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 8), (7, 9), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 9), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8)]

组合

组合与排列最大得差别是组合不关心顺序。所以组合得数量要比排列少。

,组合能够表示为

cmn

。从n个元素中选择m个元素。而且不关心顺序。

组合能够简单得在排列得结果得基础上去除不考虑顺序得情况下反复得结果就可以获得。

组合的计算公式:

cmn=n!m!(n−m)!

非递归实现

def gen_combination(outcomes, length):
"""
Iterative function that enumerates the set of all sequences of
outcomes of given length
Permutation not allow repeat
""" ans = set([()])
for dummy_idx in range(length):
temp = set()
for seq in ans:
for item in outcomes:
if item in seq:
continue
new_seq = list(seq)
new_seq.append(item)
temp.add(tuple(sorted(new_seq)))
ans = temp
return ans def run_example():
"""
Examples of sorted sequences of outcomes
"""
# example for digits
outcomes = set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
#outcomes = set(["Red", "Green", "Blue"])
#outcomes = set(["Sunday", "Mondy", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]) length = 2
seq_outcomes = gen_combination(outcomes, length)
print "Computed", len(seq_outcomes), "sorted sequences of", str(length) ,"outcomes"
print "Sequences were", seq_outcomes run_example()

执行结果:

Computed 45 sorted sequences of 2 outcomes
Sequences were set([(5, 9), (6, 9), (1, 3), (4, 8), (5, 6), (2, 8), (4, 7), (0, 7), (4, 6), (8, 9), (1, 6), (3, 7), (2, 5), (0, 3), (5, 8), (1, 2), (6, 7), (2, 9), (1, 5), (3, 6), (0, 4), (3, 5), (2, 7), (2, 6), (4, 5), (1, 4), (3, 9), (2, 3), (1, 9), (4, 9), (0, 8), (7, 9), (0, 1), (6, 8), (3, 4), (5, 7), (2, 4), (3, 8), (0, 6), (1, 8), (1, 7), (0, 9), (0, 5), (7, 8), (0, 2)])

组合的递归实现

def gen_combination_dfs(outcomes, length):
"""
Iterative function that enumerates the set of all sequences of
outcomes of given length
Permutation not allow repeat, DFS
""" res = []
seq = []
idx = 0
dfs_combination(outcomes, length, idx, seq, res) return res def dfs_combination(outcomes, length, idx, seq, res):
"""
deep first search
"""
if idx + length > len(outcomes):
return if 0 == length:
res.append(tuple(seq[:]))
return for i in range(idx, len(outcomes) - length + 1):
seq.append(outcomes[i])
dfs_combination(outcomes, length - 1, i + 1, seq, res)
seq.pop() def run_example2():
"""
Examples of sorted sequences of outcomes
"""
# example for digits
print "dfs combination"
outcomes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#outcomes = set(["Red", "Green", "Blue"])
#outcomes = set(["Sunday", "Mondy", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]) length = 2
seq_outcomes = gen_combination_dfs(outcomes, length)
print "Computed", len(seq_outcomes), "sorted sequences of", str(length) ,"outcomes"
print "Sequences were", seq_outcomes run_example2()

执行结果:

Computed 45 sorted sequences of 2 outcomes
Sequences were [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 6), (5, 7), (5, 8), (5, 9), (6, 7), (6, 8), (6, 9), (7, 8), (7, 9), (8, 9)]

排列组合相关算法 python的更多相关文章

  1. leetcode排列组合相关

    目录 78/90子集 39/40组合总和 77组合 46/47全排序,同颜色球不相邻的排序方法 78/90子集 输入: [1,2,2] 78输出: [[], [1], [2], [1 2], [2], ...

  2. w3cshool -- 排列组合去重算法挑战

    function permAlone(str) { if(str.length == 1) return str; var a = str.substr(0, 1), one = [a], count ...

  3. python自带的排列组合函数

    需求: 在你的面前有一个n阶的台阶,你一步只能上1级或者2级,请计算出你可以采用多少种不同的方法爬完这个楼梯?输入一个正整数表示这个台阶的级数,输出一个正整数表示有多少种方法爬完这个楼梯. 分析:提炼 ...

  4. 【CodeForces】889 C. Maximum Element 排列组合+动态规划

    [题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...

  5. .NET平台开源项目速览(11)KwCombinatorics排列组合使用案例(1)

    今年上半年,我在KwCombinatorics系列文章中,重点介绍了KwCombinatorics组件的使用情况,其实这个组件我5年前就开始用了,非常方便,麻雀虽小五脏俱全.所以一直非常喜欢,才写了几 ...

  6. C# 排列组合

    排列组合的概念 排列:从n个不同元素中取出m(m≤n)个元素,按照一定的顺序排成一列,叫做从n个元素中取出m个元素的一个排列(Arrangement). 组合:从m个不同的元素中,任取n(n≤m)个元 ...

  7. C#的排列组合类

    C#的排列组合类 //-----------------------------------------------------------------------------//// 算法:排列组合 ...

  8. python算法-排列组合

    排列组合 一.递归 1.自己调用自己 2.找到一个退出的条件 二.全排列:针对给定的一组数据,给出包含所有数据的排列的组合 1:1 1,2:[[1,2],[2,1]] 1,2,3:[[1,2,3],[ ...

  9. Python算法题(二)——国际象棋棋盘(排列组合问题,最小的K个数)

    题目一(输出国际象棋棋盘)  分析: 用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格.   主要代码: for i in range(8): for j in range(8 ...

随机推荐

  1. GNU GPL介绍

    怎样在程序中使用GNU许可证       不管使用哪种许可证,使用时须要在每一个程序的源文件里加入两个元素:一个版权声明和一个复制许可声明.说明该程序使用GNU许可证进行授权.另外在声明版权前应该说明 ...

  2. 虚拟机centOS中安装Redis,主机Redis Destop Manager不能访问虚拟机Redis server的解决方案

    今天在学些redis的时候碰到个问题,发现主机Redis Destop Manager不能访问虚拟机Redis server的解决方案,找了一些网上的资料,原因可能有两个,整理记录下来: 1. Red ...

  3. TForm.ShowModal只是接管消息循环,禁止外部键盘和鼠标输入到别的窗口,但并不封锁其它窗口继续获取消息(比如WM_TIMER消息仍可被发送到别的窗口上)

    窗体上放一个TTimer,然后双击输入: procedure TForm1.Timer1Timer(Sender: TObject); var cvs: TCanvas; Rect: TRect; S ...

  4. 如何制作python安装模块(setup.py)

    Python模块的安装方法: 1. 单文件模块:直接把文件拷贝到$python_dir/lib 2. 多文件模块,带setup.py:python setup.py install 3. egg文件, ...

  5. 关于NSArray的几种排序:

    #利用数组的sortedArrayUsingComparator调用 NSComparator  当中NSComparator事实上就是一个返回NSComparisonResult的block. ty ...

  6. 算法起步之Prim算法

    原文:算法起步之Prim算法 prim算法是另一种最小生成树算法.他的安全边选择策略跟kruskal略微不同,这点我们可以通过一张图先来了解一下. prim算法的安全边是从与当前生成树相连接的边中选择 ...

  7. String、StringBuffer与StringBuilder差分

    的位置不言而喻.那么他们究竟有什么优缺点,究竟什么时候该用谁呢?以下我们从以下几点说明一下 1.三者在运行速度方面的比較:StringBuilder >  StringBuffer  >  ...

  8. php设计模式——UML类图

    前言 用php开发两年多了,准备也写一下平时常用的设计模式,都是基于自己的实践经验,当然,用设计模式之前首先要看懂设计模式,因此这里首先讲解一下UML类图.通过UML类图,能更好的和大家交流,也能很容 ...

  9. 你真的了解mysql的varchar字段的长度有多少吗?

    今天在设计系统字段的时候, 发现自己对varchar还不够了解.我设了一个字段.类型为VARCHER,然后我就往里面测试性了写了东西.发现没有多少就满了.我觉得奇怪,5.5版本以上的MYSQL不是有6 ...

  10. The Official Preppy Handbook

    The Official Preppy Handbook: Lisa Birnbach: 9780894801402: Amazon.com: Books The Official Preppy Ha ...