[Algorithm] How to find all the subsets of an n-element set T?
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?的更多相关文章
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- Design and Analysis of Algorithms_Divide-and-Conquer
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- 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 ...
- Kth Smallest Element in Unsorted Array
(referrence: GeeksforGeeks, Kth Largest Element in Array) This is a common algorithm problem appeari ...
- File Transfer
本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> 代码的测试工具PTA File Transfer 1 Question 2 Explain First, ...
- [Swift]LeetCode147. 对链表进行插入排序 | Insertion Sort List
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- STL 笔记(四) 迭代器 iterator
stl 中迭代器能够理解为面向对象版本号的广义指针,提供了对容器中的对象的訪问方法,能够遍历容器全部元素.也能够訪问随意元素.stl 迭代器有下面五种: Input iterators 仅仅读,输 ...
- Lintcode: Majority Number 解题报告
Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...
随机推荐
- CKEditor5 基本使用
1.引入 <script type="text/javascript" src="/plugin/ckeditor5/ckeditor.js">&l ...
- C#复制数据到剪切板
C#复制数据到剪切板 1. 复制固定的数据到剪切板 Clipboard.SetText("123456"); 于是123456就已经复制到剪切板中了,无论在任何地方粘贴都会出现‘1 ...
- 雷林鹏分享:C# 事件(Event)
C# 事件(Event) 事件(Event) 基本上说是一个用户操作,如按键.点击.鼠标移动等等,或者是一些出现,如系统生成的通知.应用程序需要在事件发生时响应事件.例如,中断.事件是用于进程间通信. ...
- Python的url解析库--urlparse
一.urlparse解析url的query并构建字典 下面的方法主要的功能: 解析url的各个部分,并能够获取url的query部分,并把query部分构建成dict. 具体的代码实现: >&g ...
- 3-22 Ruby 编码规则(个人整理)
编码规则 https://github.com/thoughtbot/guides/tree/master/style/ruby *Use a trailing comma after each it ...
- 『Scipy』常用方法记录
优化器使用教程 J = lambda wb: self.get_cost_grad(wb, X, Y_one_hot) theta = self.wb_init(X,Y_one_hot) result ...
- SQL基础日期函数
--dateadd 将制定的数值添加到指定的日期部分后的日期 select dateadd(mm,4,'01/01/99') -- 返回:以当前的日期格式返回05/01/99 --datediff 二 ...
- learning docker steps(6) ----- docker 镜像加速
http://www.docker-cn.com/registry-mirror 通过 Docker 官方镜像加速,中国区用户能够快速访问最流行的 Docker 镜像.该镜像托管于中国大陆,本地用户现 ...
- cookie -- 添加删除
前段时间学到了cookie,之前的公司用的jquery插件,现在终于学到了原生的js <!doctype html> <html> <head> <meta ...
- UML(统一的建模语言)
1.软件开发与软件工程 任何事情都必须想清楚了,才能去做!这样才不会出现很多不必要的麻烦,软件开发亦是如此. 写代码前要想好:想要做什么?做成什么样?如何去做? 软件设计就是把软件开发想清楚的过程: ...