There are two direction for us to solve this problem.

(1) Recursion

Recursive step: T[0] conbines with findsubsets(T[1:])

Final step: if len(T)==0: return [[]]

Code:

#Recursive method
def findsubsets(T):
if len(T)==0:
return [[]] answer=[] for sets in findsubsets(T[1:]):
answer.append(sets)
new=sets[:]+[T[0]]
answer.append(new) return answer

  

(2) Noncursive method

In this method, we can use stack and queue to help us solve this problem.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Algorithm:

Input: an n-element set T

Output: all the subsets of T

1. define an empty stack S

2. define an empty queue Q

3. S.push([])

4. S.push([T[0]])

5. for all the elements a in T[1:]:

6.   while S is not empty:

7.    x=S.pop()

8.    Q.enqueue(x)

9.    x=x+[[a]]

10.       Q.enqueue(x)

11.  if a is not the final element in T:

12.    while Q is not empty:

13.      S.push(Q.dequeue())

14. return Q

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code:

def findsubsets2(T):
Q=ArrayQueue()
S=ArrayStack() S.push([])
S.push([T[0]]) for i in T[1:]:
while (len(S)!=0):
x=S.pop()
Q.enqueue(x)
x=x+[[i]]
Q.enqueue(x) if i!=T[-1]:
while (len(Q)!=0):
S.push(Q.dequeue())
return Q._data

  

[Algorithm] How to find all the subsets of an n-element set T?的更多相关文章

  1. [LeetCode] Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...

  2. Design and Analysis of Algorithms_Divide-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  3. Ehcache(2.9.x) - API Developer Guide, Cache Eviction Algorithms

    About Cache Eviction Algorithms A cache eviction algorithm is a way of deciding which element to evi ...

  4. Kth Smallest Element in Unsorted Array

    (referrence: GeeksforGeeks, Kth Largest Element in Array) This is a common algorithm problem appeari ...

  5. File Transfer

    本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> 代码的测试工具PTA File Transfer 1 Question 2 Explain First, ...

  6. [Swift]LeetCode147. 对链表进行插入排序 | Insertion Sort List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  7. LeetCode 147. Insertion Sort List 链表插入排序 C++/Java

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  8. STL 笔记(四) 迭代器 iterator

    stl 中迭代器能够理解为面向对象版本号的广义指针,提供了对容器中的对象的訪问方法,能够遍历容器全部元素.也能够訪问随意元素.stl 迭代器有下面五种: Input iterators   仅仅读,输 ...

  9. Lintcode: Majority Number 解题报告

    Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...

随机推荐

  1. JavaMail直接发送邮件

    一般用JavaMail发送邮件都需要先登录到外部smtp服务器(如smtp.163.com) 二次转发,其实只要得到域名的邮件交换服务器地址(MX)就可以直接将邮件发出 这里用到了dnsjava(下载 ...

  2. python json格式转xml格式

    import xmltodict #json转xml函数 def jsontoxml(jsonstr): #xmltodict库的unparse()json转xml xmlstr = xmltodic ...

  3. SetCommMask

    SetCommMask           用途:设置串口通信事件   原型:BOOL SetCommMask(HANDLE hFile, //标识通信端口的句柄   DWORD dwEvtMask ...

  4. vscode python3 配置生成任务

    一直用sublime,但是ubuntu下输入中文有问题,解决起来太麻烦: pycharm太重.虚拟机一开+Chrome打开10几个页面,然后再运行pycharm,静音轻薄笔记本CPU和8G内存基本都占 ...

  5. 绝对干货!!css3字体图标—丰富的阿里图标库iconfont的使用详解

    在移动端Web项目开发中,我们往往需要用到一些小图标,比如搜索,返回,小菜单,小箭头等等..这如果还用切图你就OUT了.. 而这时CSS3提供的字体图标无疑是我们最好的选择,它就像字体一样,可以设置大 ...

  6. python-day39--数据库

    1.什么是数据:描述事物的特征,提取对自己有用的信息  称之为数据 2..什么是数据库: 数据库即存放数据的仓库,只不过这个仓库是在计算机存储设备上,而且数据是按一定的格式存放的 为什么要用数据库: ...

  7. 51nod-1605-博弈

    1605 棋盘问题  基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 上帝创造了一个n*m棋盘,每一个格子都只有可能是黑色或者白色的. 亚当和夏娃在 ...

  8. XML文档的创建

    右键项目,添加,新建项,XML文件 XML文件的第一行有一个标题,标题描述了这个XML文件的版本和编码 XML文件必须有根节点且只能有一个根节点,如<Books></Books> ...

  9. 37. Sudoku Solver *HARD*

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  10. POJ 2499 A*求第K短路

    DES就是给你一个图.然后给你起点和终点.问你从起点到终点的第K短路. 第一次接触A*算法. 题目链接:Remmarguts' Date 转载:http://blog.csdn.net/mbxc816 ...