Backtracking.ppt
《Backtracking.ppt》由会员分享,可在线阅读,更多相关《Backtracking.ppt(20页珍藏版)》请在麦多课文档分享上搜索。
1、Backtracking,What is backtracking?General structure of Backtracking algorithmsBacktracking and Recursion,Backtracking,Problem:Find out all 3-bit binary numbers for which the sum of the 1s is greater than or equal to 2.The only way to solve this problem is to check all the possibilities: (000, 001, 0
2、10, ,111)The 8 possibilities are called the search space of the problem. They can be organized into a tree.,Backtracking: Illustration,Backtracking,For some problems, the only way to solve is to check all possibilities.Backtracking is a systematic way to go through all the possible configurations of
3、 a search space.We assume our solution is a vector (a(1),a(2), a(3), a(n) where each element a(i) is selected from a finite ordered set S.,Backtracking,We build a partial solution v = (a(1), a(2),., a(k), extend it and test it.If the partial solution is still a candidate solution, proceed. elsedelet
4、e a(k) and try another possible choice for a(k).If possible choices of a(k) are exhausted, backtrack and try the next choice for a(k-1).,Backtracking: Iterative Version,Compute S(1) ,k = 1 While (k 0) do While S(k) != emptySet do (*advance*) a(k) = an element in S(k)S(k)= S(k)- a(k)if ( a(1),a(2),.,
5、a(k) is a solution, print itk = k + 1 Compute S(k)Compute , the candidate k-th element given v. k = k - 1 (*backtrack*),Backtracking: Recursive Version,Backtrack(a, k) if a is a solutionprint(a) else k = k +1 compute S(k)while do S(k)!=emptySeta(k) = an element in S(k)S(k)=S(k)-a(k) ,Backtracking an
6、d Recursion,Backtracking is easily implemented with recursion because:The run-time stack takes care of keeping track of the choices that got us to a given point.Upon failure we can get to the previous choice simply by returning a failure code from the recursive call.,Improving Backtracking: Search P
7、runing,Search pruning will help us to reduce the search space and hence get a solution faster.The idea is to a void those paths that may not lead to a solutions as early as possible by finding contradictions so that we can backtrack immediately without the need to build a hopeless solution vector.,E
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- BACKTRACKINGPPT
