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. 《剑指offer》第十七题(打印1到最大的n位数)

    // 面试题17:打印1到最大的n位数 // 题目:输入数字n,按顺序打印出从1最大的n位十进制数.比如输入3,则 // 打印出1.2.3一直到最大的3位数即999. #include <ios ...

  2. C语言专题-基本数据类和占位符

    C语言中常用的几种基本数据类型有 基本数据类型的长度 unsigned unsigned unsigned unsigned float没有unsigned double没有unsigned 占位符的 ...

  3. xinwenti

    angularjs  angular2脏检查机制和数据双向绑定远离 angular2 aot编译

  4. ZendFramework中实现自动加载models

    最近自学Zendframework中,写Controller的时候总要require model下的类文件,然后才能实例化,感觉非常不爽 Google了许久,找到个明白人写的方法不错,主要就是修改ap ...

  5. codeforces 484a//Bits// Codeforces Round #276(Div. 1)

    题意:给出区间[ll,rr],求中间一个数二进制表示时一的个数最多. 写出ll和rr的二进制,设出现第一个不同的位置为pos(从高位到低位),找的数为x,那么为了使x在[ll,rr]内,前pos-1个 ...

  6. H5基础知识(一)

    一.概述 HTML5  是html4.0 升级版 结构 Html5 .样式 css3 .行为: API  都有所增强 HTML5并不仅仅只是做为HTML标记语言的一个最新版本,更重要的是它制定了Web ...

  7. 批量kill Linux环境下的任务

    批量kill Linux服务器的进程可以通过ps  grep  aws  xargs 进行结合来完成  比如要kill 全部test.sh 的进程 ps -ef |grep "test.sh ...

  8. view_countInfo

    create view view_countInfo as SELECT     a.dwmch, b.dwbh, b.djbh, c.rq, c.shl, c.djbh AS Expr1, d.sp ...

  9. iOS-UI篇—简单的浏览器查看程序和Tomcat简单实现

    #import "ViewController.h" @interface ViewController () @property (retain, nonatomic) NSAr ...

  10. 学习浏览器缓存(http缓存)

    Q: 浏览器缓存是个什么东东,为什么要学习浏览器缓存涅? A: 浏览器缓存其实就是浏览器保存通过HTTP获取的所有资源,是浏览器将网络资源存储在本地的一种行为.浏览器缓存可以减少冗余数据的传输,减小服 ...