迭代深度的深度优先搜索
来源:互联网 发布:启明星辰网络安全设备 编辑:程序博客网 时间:2024/05/22 12:39
代码:
def iddfs(G,s): yielded=set() def recurse(G,s,d,S=None): if s not in yielded: yield s yielded.add(s) if d==0:return if S is None:S=set() S.add(s) for u in G[s]: if u in S:continue for v in recurse(G,u,d-1,S): yield v n=len(G) for d in range(n): if len(yielded)==n:break for u in recurse(G,s,d): yield uif __name__=="__main__": a, b, c, d, e, f, g, h, i= range(9) N = [ {b, c, d}, # a {a, d}, # b {a,d}, # c {a,b,c}, # d {g,f}, # e {e,g}, # f {e,f}, # g {i}, # h {h} #i ] G=[{b,c,d,e,f},#a {c,e},#b {d},#c {e},#d {f},#e {c,g,h},#f {f,h},#g {f,g}#h ] p=list(iddfs(G,0)) print(p) m=list(iddfs(N,0)) print(m)
运行:
[0, 1, 2, 3, 4, 5, 6, 7][0, 1, 2, 3]
阅读全文
0 0
- 迭代深度的深度优先搜索
- 图搜索算法(深度优先搜索、迭代加深的深度优先搜索、广度优先搜索、代价一致搜索、A*搜索)
- 深度优先,广度优先和迭代加深搜索算法
- 深度优先搜索 迭代加深搜索 hdu 1560
- code[vs]1004四子连棋(迭代深度优先搜索)
- 深度优先搜索和深度优先搜索的区别
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 深度优先搜索的实现
- 图的深度优先搜索
- 图的深度优先搜索
- 邻接矩阵的深度优先搜索
- 图的深度优先搜索
- 图的深度优先搜索
- 深度优先搜索的学习
- 图的深度优先搜索
- android SeekBar自定义样式滑动条的使用
- Leetcode 157 Read N Characters Given Read4
- web前端之文件上传
- spring+jta+atomikos
- servlet3.1(tomcat8、9)开发中,Filter使用注解(Annotation)时,如何定义执行顺序
- 迭代深度的深度优先搜索
- SimpleDateFormat 线程安全的解决方案--DateTimeFormatter
- Token和Session
- CH554E USB单片机 10引脚小封装低成本USB方案
- ORACLE 自定义函数
- 编译过程简介
- STM32 IAP升级
- Java多线程synchronized几种方式
- cygwin git 设置代理