[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 ...
随机推荐
- C#学习 day1 c#基础
C#是一门编程语言,为什么我今天开始学C#了,下学期有门C#的课,以及有一个经验丰富的老学长正在做C#项目,由于之前学过C++和C基础,所以,C#的基础部分我查看文档来尝试能否自学归纳,而不是一直依靠 ...
- Java实现邮箱激活验证2
SendEmail.java [java] view plaincopyprint? package com.app.tools; import java.util.Date; import ja ...
- [ios][swift]文本框UITextField用法
参考:http://www.hangge.com/blog/cache/detail_530.html
- HTTP URL 字符转义 字符编码 、 RFC 3986编码规范
一.为什么要编码转义 通常如果一样东西需要编码,说明这样东西并不适合传输.原因多种多样,如Size过大,包含隐私数据,对于Url来说,之所以要进行编码,是因为Url中有些字符会引起歧义. 例如Url参 ...
- 20170719xlVBASmartIndent
Public Sub SmartIndenterProcedure() Dim OneComp As VBComponent Dim StartLine As Long, EndLine As Lon ...
- Pavel and barbecue CodeForces - 756A (排列,水题)
大意: 给定排列p, 0/1序列b, 有n个烤串, 每秒钟第i串会移动到$p_i$, 若$p_i$为1则翻面, 可以修改b和p, 求最少修改次数使得每串在每个位置正反都被烤过. 显然只需要将置换群合并 ...
- gradle 编译 No such property: sonatypeUsername错误解决
No such property: sonatypeUsername for class: org.gradle.api.publication.maven.internal.ant.DefaultG ...
- thinkphp3.2分页
在ThinkPHP 3.1及之前,分页功能可能是放在/Lib/Org/Util中的,到了ThinkPHP 3.2后,分页功能已经整合到了Library/Think中了.而且ThinkPHP 3.2已经 ...
- JAVA System.arraycopy 和Arrays.copyof 效率比较
System.arraycopy()源码.可以看到是native方法: native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中. ...
- 以DefaultFuture为例,看类变量初始化顺序
https://stackoverflow.com/questions/8517121/java-what-is-the-difference-between-init-and-clinit# < ...