Search Topic Summary: Iterative deepening Search

Source: Internet
Author: User
Tags greatest common divisor

Iterative deepened search

Iterative deepened search (iterative deepening Depth-first search, Iddfs) is often used to theoretically answer questions that have no upper bounds on tree depths, which often require solutions that meet certain conditions. For example, in the "Egyptian score" problem requires the decomposition of a fractional A/b into a number of shapes such as 1/d addend, and the less addend the better, if the number of numbers is the same, the smallest score, the better. The general flow of the method is summarized below:

(1) Overview: Iterative deepening search is done by limiting the maximum depth of each DFS search. So that Maxd represents the largest search depth, then Dfs can only be between 0~maxd, if found in this range to understand, exit the big loop, otherwise maxd++, expand the search scope. But it can be imagined that if there is no efficient and timely exit without solution, then the cost of time will be relatively large. At this point, we need to do "pruning" operation, in time to determine whether the solution can be found. For iterative deepening search, it is often possible to determine whether pruning can be done by designing an appropriate "optimistic valuation function". The depth of the current search is cur, and the optimistic valuation function is H (), then pruning is required when cur+h () >maxd.


So what is an optimistic valuation function? It simply means how many steps it takes to find the final Solution "at least" from the current depth, or how many layers need to be extended to find the final solution. If the depth maxd of the current limit is exceeded, it is impossible to find the solution at the maximum depth of the current limit and exit directly. For example, like the previous "Egyptian score" problem, to split 19/45 such a score, assuming that the current search to the third layer, get 19/45=1/5+1/100 ... then according to test instructions at this time the maximum score is 1/101, and if need to gather enough 19/45, need (19/45-1/ 5-1/100) *101=23 a 1/101. That is, it is possible to find all the solutions from the 3rd level and at least a depth greater than 23 layers. So if the maxd<23 at this time, it can be directly pruning. So (A/B-C/D)/(1/e) is the optimistic valuation function of the subject.


Note that when you use iterations to deepen your search, you must be sure you can find the solution, otherwise you will loop indefinitely.

The following is a code for the "Egyptian fractions" question to better understand the process of iterative deepening search:

#define _crt_secure_no_warnings#include<iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath>using Namespace Std;typedef Long Long Ll;int maxd;int A, b;const int maxn = 1000;int ANS[MAXN], v[maxn];int gcd (int a, int b) {RE Turn b = = 0? A:GCD (b, a%b);} int Get_first (int a, int b)///1/c≤a/b minimum C{int c = 1;while (b > A*c) C++;return C;} BOOL Better (int D)//When the depth is D, the solution found now is not better {for (int i = d; I >= 0; i--) if (v[i]! = Ans[i]) {return ans[i] = = 1 | | v[i] &L T ans[i];//in two cases the current is better: (1) The solution has not been found at this time, (2) The current denominator is less than the original denominator, indicating that the current score is larger than the original, in line with test instructions requirements}return false;} BOOL Dfs (int d, int from, LL AA, LL BB)//Current depth is D, the denominator cannot be less than from, the sum of the fractions is exactly aa/bb{if (d = = Maxd)//arrives at the last layer {if (BB%AA) return false;//not Divisible, stating that the last item does not conform to the definition of the Egyptian score, failure to exit v[d] = Bb/aa;if (better (d)) memcpy (ans, V, sizeof (LL) * (d + 1));//The solution currently found is better, update ANSREturn true;} bool OK = False;from = Max (from, Get_first (AA, BB)),//update fromfor (int i = from;; i++)//enumeration Denominator {if (bb* (Maxd + 1-d) <= i*a A) break;//uses the optimistic valuation function to prune, from the current depth d arrives maxd altogether has the maxd-d+1 item, if (maxd-d+1) * (1/i) also not enough aa/bb, needs pruning v[d] = i; LL B2 = bb*i;//calculates aa/bb-1/i,-pass, denominator is bb*i, denominator is aa*i-bbll a2 = AA*I-BB; LL g = gcd (A2, b2);//compute numerator, denominator of greatest common divisor, easy numerator if (Dfs (d + 1, i + 1, a2/g, b2/g)) ok = true;} return OK;} int main () {int ok = 1;while (scanf ("%d%d", &a, &b)//Input fraction a/b{for (maxd = 1;; maxd++) {memset (ans,-1, sizeof (ans)); if (Dfs (0, Get_first (A, b), A, B)) {OK = 1; break;}} printf ("%d/%d=", A, b), for (int i = 0;; i++) if (ans[i]>0) printf ("%s1/%d", i = = 0?) "": "+", ans[i]); else {printf ("\ n"); Break }}return 0;}

Search Topic Summary: Iterative deepening Search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.