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. C++中int、string等常见类型转换

    1.int型与string型的互相转换 最佳实践: int型转string型 void int2str(const int &int_temp,string &string_temp) ...

  2. 算法笔记--字符串hash

    概述: 主要用于字符串的匹配. 定义hash函数: H(c)=(c1bm-1 +c2bm-2 +...+cmb0)mod h 对于字符串c中l-r区间的hash值: H(l,r)=H(1,r)-H(1 ...

  3. 关于Dos命令中存在中文的解决方法

    问题:当我们直接打开dos窗口并在里面写入中文时是没有问题的,但是当我们把这些命令放在bat文件中执行时就出问题了解决:1.首先可以通过pause命令来查看错误原因2.记事本默认是UTF-8格式的,而 ...

  4. delphi文件名获取方法

    取文件名 ExtractFileName(FileName); 取文件扩展名: ExtractFileExt(filename); 取文件名,不带扩展名: 方法一:   Function Extrac ...

  5. [.NET开发] NPOI导出

    //导出全部 expertPara = GetExpetPara(); expertPara.BeginIndex = pager.CurrentPageIndex; expertPara.EndIn ...

  6. Linux : 密码正确不能正常登陆,日志提示Could not get shadow information for user

    今天,再玩Centos7的时候,尝试修改了下ssh的端口.因为默认开启了SELinux,如果没有修改这个文件配置就修改端口sshd服务就不能正常启动了. 但是,当我修改会22端口的时候还是不能正常登陆 ...

  7. 清空mailq 队列里面的邮件

    tmp_=`mailq | grep -E "root" | awk '{print $1}'` for i in $tmp_;do postsuper -d $i;done po ...

  8. IIS Web 服务器/ASP.NET 运行原理基本知识概念整理

     前言:      记录 IIS 相关的笔记还是从公司笔试考核题开始的,问 Application Pool 与 AppDomain 的区别?      促使我对进程池进了知识的学习,所以记录一下学习 ...

  9. UVA-10061 How many zero's and how many digits ? (数论)

    题目大意:让求n!在base进制下的位数以及末尾0的连续个数. 题目分析:一个m位的b进制数N,最小是b^(m-1),最大不超过b^m,即b^(m-1)≤N<b^m.解不等式,得log10(N) ...

  10. seo-摘自网友

    网页前端制作中的SEO 在SEO盛行的今天到处都在谈优化,对于网站前端制作人员来说,有几点是跟SEO相关的,也就是SEO站内优化中的一部分,下面总结几点: 1.title,<title>页 ...