zoukankan      html  css  js  c++  java
  • 高效率的排列组合算法《编程珠矶》python实现

    组合算法   
      本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标  
      代表的数被选中,为0则没选中。     
      首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。     
      然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为   
      “01”组合,同时将其左边的所有“1”全部移动到数组的最左端。     
      当第一个“1”移动到数组的m-n的位置,即n个“1”全部移动到最右端时,就得   
      到了最后一个组合。     
      例如求5中选3的组合:     
      1   1   1   0   0   //1,2,3     
      1   1   0   1   0   //1,2,4     
      1   0   1   1   0   //1,3,4     
      0   1   1   1   0   //2,3,4     
      1   1   0   0   1   //1,2,5     
      1   0   1   0   1   //1,3,5     
      0   1   1   0   1   //2,3,5     
      1   0   0   1   1   //1,4,5     
      0   1   0   1   1   //2,4,5     
      0   0   1   1   1   //3,4,5  

    使用python实现:

    方法一:


    group = [1, 1, 1, 0, 0, 0] group_len = len(group) #计算次数 ret = [group] ret_num = (group_len * (group_len - 1) * (group_len - 2)) / 6 for i in xrange(ret_num - 1): '第一步:先把10换成01' number1_loc = group.index(1) number0_loc = group.index(0)

    #替换位置从第一个0的位置开始 location
    = number0_loc

          #判断第一个0和第一个1的位置哪个在前,
          #如果第一个0的位置小于第一个1的位置,
          #那么替换位置从第一个1位置后面找起

        if number0_loc < number1_loc:
            location = group[number1_loc:].index(0) + number1_loc
    
        group[location] = 1
        group[location - 1] = 0
    
        '第二步:把第一个10前面的所有1放在数组的最左边'
        if location - 3 >= 0:
            if group[location - 3] == 1 and group[location - 2] == 1:
                group[location - 3] = 0
                group[location - 2] = 0
                group[0] = 1
                group[1] = 1
            elif group[location - 3] == 1:
                group[location - 3] = 0
                group[0] = 1
            elif group[location - 2] == 1:
                group[location - 2] = 0
                group[0] = 1
    
        print group
        ret.append(group)

     方法二:

    charters = ['A', 'B', 'C', 'D', 'E', 'F']
    s4 = time.time()
    group = [1, 1, 1, 0, 0, 0]
    group_len = len(group)
    ret_num = (group_len * (group_len - 1) * (group_len - 2)) / 6
    #记录 group 的1位置的容器
    indexes = [0, 1, 2]
    for i in xrange(ret_num - 1):
    
        '''
    
        第一步:先把10换成01
        第二步:把第一个10前面的所有1放在数组的最左边
    
        '''
        tmp = []
        #location:第一个10的起始位置
        #如果 indexes 的第一个元素与第二个元素值不连续,那么说明 group 的第一个10在最左边
        #[tmp.append(charters[i]) for i in indexes]
        tmp.append(charters[indexes[0]])
        tmp.append(charters[indexes[1]])
        tmp.append(charters[indexes[2]])
        print tmp
        
        if indexes[0] + 1 < indexes[1]:
            location = indexes[0]
            indexes[0] = location + 1
    
        #如果 indexes 的第二个元素与第三个元素值不连续,那么说明 group 的第一个10在中间
        elif indexes[1] + 1 < indexes[2]:
            location = indexes[1]
            indexes[1] = location + 1
            # indexes 中间的1进位之后,把左边的1的位置记录为0,同时修改 group 相应位置的值
            group[indexes[1] - 1] = 0
            group[0] = 1
            indexes[0] = 0
    
        #如果 indexes 的三个元素值都是连续的,那么说明 group 的第一个10在最右边
        elif indexes[0] + 1 == indexes[1] and indexes[1] + 1 == indexes[2]:
            location = indexes[2]
            indexes[2] = location + 1
            group[indexes[0]] = 0
            group[indexes[1]] = 0
            group[0] = 1
            group[1] = 1
            indexes[0] = 0
            indexes[1] = 1
    
        if location < 5:
            group[location] = 0
            group[location + 1] = 1
        else:
            group[location] = 1
    
    
    print time.time() - s4,'*************'
    #print ret,'**********'

     第二种方法经过优化,效率相当高。测试了1600多亿的循环计算,方法一要22分钟,而方法二只需要1分钟。

    全排列算法   
        
      从1到N,输出全排列,共N!条。   
      分析:用N进制的方法吧。设一个N个单元的数组,对第一个单元做加一操作,满N进   
      一。每加一次一就判断一下各位数组单元有无重复,有则再转回去做加一操作,没   
      有则说明得到了一个排列方案。

    python,go,redis,mongodb,.net,C#,F#,服务器架构
  • 相关阅读:
    左侧列固定的表格
    百度地图上添加多个标记,标记上添加信息展示窗口、跳转到导航界面
    vue-cli4版本解决eslint问题
    使用脚手架搭建项目
    正则表达式学习
    函数参数:
    装饰器(重点)
    列表生成式、生成器、迭代器
    logging 日志模块
    json 、 pickle 、shelve序列化
  • 原文地址:https://www.cnblogs.com/descusr/p/3118589.html
Copyright © 2011-2022 走看看