[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 ...
随机推荐
- VcCallC#_01
1.C# 代码: using System; using System.Collections.Generic; //using System.Linq; using System.Text; //u ...
- VC6的VCVARS32.BAT所在位置
1. C:\Program Files (x86)\Microsoft Visual Studio\VC98\Bin\VCVARS32.BAT 2.
- 团队作业Beta冲刺-第三天
2018.06.26 各个成员完成任务 成员 今日完成任务 贡献小时数 龙正圆 后台程序完善 5h 杨环宇 后台程序完善 4h 马军.龚继恒 界面美化 2h 候燕.纪亚星 Beta冲刺博客的撰写 3h ...
- 会话技术及jsp概述
一.会话技术 1.概念:在一次会话中共享数据,在web中指,浏览器和服务器的一次通信.包含多次请求,和多次响应. 可以在一次会话的多次请求中共享数据. 2.客户端会话技术:Cookie 将数据保存在客 ...
- nginx反向代理和正向代理,优缺点
http://blog.csdn.net/lishaojun0115/article/details/53200629 nginx反向代理代理的是服务器,正向代理代理的是客户端, 反向代理是客户点发送 ...
- stats.js随时查看fps
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- XML和Schema
2017-11-03 19:33:56 XML:Extensible Markup Language,也就是可扩展标记语言.XML工具使处理和转化信息变得十分容易和方便. XML和HTML格式是古老的 ...
- Java基础八--构造函数
Java基础八--构造函数 一.子父类中构造函数的特点 1.1 为什么在子类构造对象时,发现,访问子类构造函数时,父类也运行了呢? 原因是:在子类的构造函数中第一行有一个默认的隐式语句. super( ...
- wpf窗口阴影
https://www.cnblogs.com/yiyan127/p/6362509.html
- java final修饰变量时的一种情况
有如下一种场景. 1.在文件PaymentConfig.java中存在如下变量public static final desc="描述" 2.类Test.java中使用了desc变 ...