"Iterative deepening search" Egyptian score issues

Source: Internet
Author: User

Thank you, Aso ~http://blog.csdn.net/urecvbnkuhbh_54245df/article/details/5856756 .

"Iterative deepening search (id,iterative deepening)": from small to large enumeration upper Maxd, each execution only considers nodes with depths not exceeding maxd.

------for the problem that the depth of the solution tree can be solved by backtracking method , the ID algorithm can be considered.

------Advantages: It is mainly at the beginning of the recursive search function to determine whether the depth of the current search is greater than the predefined maximum search depth, if greater than, exit the search layer, if not greater than, continue to search. The final solution must be the optimal solution. It avoids the disadvantage that breadth-first search takes up too much search space, and also reduces the blindness of depth-first search.

"A * algorithm (heuristic search)": A * (A-star) algorithm is the most effective direct search method for solving the shortest path in a static road network. The closer the estimate is to the actual value, the better the valuation function gets.

The estimate function of a * algorithm can be expressed as: F ' (n) = G ' (n) + h ' (n)

Here, F ' (n) is the valuation function, and G ' (n) is the shortest path value from the starting point to the node N, and H ' (n) is the heuristic value of the shortest pathway from N to the target. Since this f ' (n) is actually not known in advance, we use the previous estimate function f (n) to approximate it. g (n) instead of G ' (n), but G (n) >=g ' (n) can (in most cases, be satisfied, without consideration), h (n) instead of H ' (n), but H (n) <=h ' (n) (this is particularly important). It can be proved that the application of such a valuation function can find the shortest path, that is, can be adopted. We say that the best first algorithm to use this valuation function is a * algorithm. For example, the breadth-first algorithm is a special case of a * algorithm. where g (n) is the number of layers where the node is located, h (n) = 0, this h (n) is definitely less than H ' (n), so the above-mentioned known breadth-first algorithm is a possible adoption. Actually, too. Of course it is one of the most smelly * algorithms.

"Ida* algorithm": Synthesis of A * algorithm of artificial intelligence and backtracking on the advantages of less space consumption, in some large-scale search problems will have an unexpected effect. Its specific name is iterative deepening A *, presented by Korf in 1985. The original purpose of the algorithm is to solve the spatial problem of breadth A * with the advantage of deep search, and the cost is to produce duplicate search. to sum up, the basic idea of ida* is: first set the H value of the initial state node to the threshold Maxh, then the depth first search, the search process ignores all the H value is greater than the MAXH node; If no solution is found, increase the threshold maxh and repeat the search until a solution is found. Under the requirement that the calculation of H value satisfies a A * algorithm, it is proved that the solution must be the optimal one. in the implementation of the program, the ida* is more convenient than a *, because there is no need to save the node, do not need to judge the repetition, do not need to according to the H value of the node ordering, occupy small space.
The ida* algorithm also uses the appropriate valuation function to evaluate the distance to the target state.

In general the problem is this use of the ida* algorithm, the current situation of the valuation function value + current search depth > pre-defined maximum search depth, the pruning.

There is no uniform standard for the estimation of the function, but it is not easy to find the appropriate function, but it can be roughly followed by this principle: in a certain range, the difference of the value of the heuristic function of each state is increased.

e.g. classic Egyptian score question:

To give you a real score, you need to reduce it to the sum of the least number of special true fractions, and you want to output this sequence (sequence in ascending order). If there are different scenarios, the maximum denominator is minimized with the same number of numbers. If the same, make the denominator of the second largest ... And so on such as: 2/3=1/2+1/6, but do not allow 2/3=1/3+1/3, because addend have the same. For a fraction A/b, there are many ways to express it, but which is best? First of all, addend less than addend more good, second, add a number of the same, the smallest score bigger the better. such as: 19/45=1/3 + 1/12 + 1/180 19/45=1/3 + 1/15 + 1/45 19/45=1/3 + 1/18 + 1/3019/45=1/4 + 1/6 + 1/18019/45=1/5 + 1/6 + 1/18 most OK is the last one, because 18:180, 45, 30, are small.

Enter integer A, b, and program to calculate the best expression.

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <Set>6 using namespacestd;7typedefLong LongLL;8 Const intMAXN =100010;9LL ANS[MAXN], V[MAXN], maxd;//Maximum Enumeration DepthTen ll Get_first (ll A, ll b) One { A     returnb/a+1; - } - ll GCD (ll A, ll b) the { -     returnb = =0? A:GCD (b, a%b); - } - BOOLBetter (LL D) + { -      for(LL i = D; I >=0; i--) +     { A         if(V[i]! =Ans[i]) at         { -             return(Ans[i] = =-1|| V[i] <ans[i]); -         } -     } -     return false; - } in BOOLDfs (ll D, LL C, ll A, ll b) - { to     //cout << a << "" << b << Endl; +     if(d = =Maxd) -     { the         if(b%a)return false;//when the maximum depth is reached, the last score cannot be converted into Egyptian fractions. Where the molecule is 1. *V[D] = b/a;//the best Egyptian score at depth of D is 1/(b/a); $         if(Better (d))Panax Notoginseng         { -memcpy (ans, V,sizeof(LL) * (d+1)); the         } +         return true; A     } the     BOOLOK =false; +c =Max (c, Get_first (A, b)); -      for(LL i = C;; i++) $     { $         if((maxd-d+1) * b <= A * i) Break; -V[D] =i; -LL A2 = a*i-b, B2 = b*i; theLL g = gcd (A2, B2);//Numerator -         if(Dfs (d+1, i+1, a2/g, b2/g)) OK =true;Wuyi     } the     returnOK; - } Wu intMain () - { About     intT; $scanf"%d", &T); -      for(intKase =1; Kase <= T; kase++) -     { - LL A, b; Ascanf"%lld%lld", &a, &b); +  the         /*Main code*/ -          for(Maxd =1; ; maxd++)//Maximum Enumeration Depth $         { thememset (ans,-1,sizeof(ans)); thememset (V,-1,sizeof(v)); theLL C =Get_first (A, b); the             if(Dfs (0, C, a, b)) -             { in                  Break; the             } the         } About         /*-------------------------------------*/ theprintf"Case %d:%lld/%lld=", Kase, A, b); the          for(inti =0; I <= Maxd; i++) the         { +             if(i) printf ("+"); -printf"1/%lld", Ans[i]); the         }Bayiprintf"\ n"); the  the     } -     return 0; -}
View Code

"Iterative deepening search" Egyptian score issues

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.