"Egyptian score" on the understanding of iterative deepening search

Source: Internet
Author: User
Tags greatest common divisor

The idea of iterative deepening search (IDDFS)

Iterative deepening search is generally used to solve the problem that the state tree is "very deep", and even the depth may tend to be infinite, but the "target state is shallow". If the common Dfs to solve, often not high efficiency. At this point we can make some improvements to DFS. One of the most intuitive approaches is to increase the maximum depth limit for a search by Maxd, typically starting from 1. Each search is performed within the maxd depth, and if no solution is found, continue to increase maxd until the solution is successfully found and then break.


As shown, if you use DFS, you need 15 steps to find node 3, but with an iterative deepening search, you will soon find node 3.

When using iterations to deepen the search, it is often necessary to introduce a valuation function, H (), to predict at least how many steps from the current depth to reach the target state. Assuming that at the cur level, when cur+h (cur) >maxd, it is impossible to find the target state within the maxd limit, no matter how you go, you can "prune" the operation at this time. This and the iterative deepening search with the valuation function is the ida* algorithm. To better understand the algorithm, the following is a classic example of "Egyptian score" as a description.


Egyptian score question

In ancient Egypt, people used the 1/a,a of unit fractions (i.e., natural numbers) to represent all rational numbers, for example, 2/3=1/2+1/6, but did not allow 2/3=1/3+1/3 because the same is not allowed in Addend. For a fraction of a/b, there are many ways to represent, where addend is less good than addend, and if the number of numbers is the same, the smaller the smallest score, the better. For example, 19/45=1/5+1/6+1/18 is the best solution.

Enter two integers, a, B (0<a<b<500), and try to calculate the best expression.

Sample input:

495 499

Sample output:

Case 1:495/499=1/2+1/5+1/6+1/8+1/3992+1/14970


Problem analysis

First of all, according to test instructions can be found that the answer tree is very large, not only the depth of no obvious upper bound, and in theory the choice of Addend is also unlimited, that is, the number of nodes in each layer is also unlimited. In this case, if the use of ordinary BFS, the first layer will be expanded. However, to achieve two purposes: 1. The number of addend is as small as possible. 2. The smallest of the scores as large as possible, that is, the largest denominator as small as possible.


In order to achieve the first goal, we naturally think that we can enumerate the number of possible, set Maxd to indicate that there is a total of maxd+1 scores is exactly equal to A/b (subscript starting from 0), according to this way of thinking, you can find the least addend solution.


Next, consider how to achieve a second goal. The second goal is actually to tell us that if there are multiple solutions, we need to update the optimal solution we found. One of the easiest mistakes here is to return true immediately after finding out. This way of writing can only achieve Goal 1, and does not really achieve goal 2. The correct approach should be to find a set of solutions to return to the previous layer and continue to find out whether a new solution exists.


Then naturally ask: Now that we find the understanding is not the return condition, what is the correct return condition? We continue to analyze, assuming that the current is already in the CUR layer, that is, all the denominator of 0~cur has been determined, the remaining points need to aa/bb, and then assume that we need to start from the denominator i>=from the number of search, it is not difficult to find, if (maxd+1-d) * (1/i) ≤aa/ BB, that is, the rest of the scores are all 1/i time, their sum is just equal to aa/bb. The reality is that the denominator is not allowed to recur, that is, the actual and certainly less than (maxd+1-d) * (1/i), this time if I continue to increase, (maxd+1-d) * (1/i) will only be less than aa/bb, the situation is worse, so here should stop the enumeration. In this way, we analyze the correct return condition: break when there is a (maxd+1-d) * (1/i) ≤aa/bb.


Through the above analysis, we also identified the DFS when the entry parameters: Cur is currently in the CUR layer, the from represents the beginning of the denominator of the cur layer, AA represents the remaining fraction of the numerator, BB represents the denominator of the remaining fractions. That is, DFS (CUR,FROM,AA,BB). At this point, the overall framework of the algorithm has been analyzed, we successfully use the ida* algorithm to solve the classic example.


Code:

#define _crt_secure_no_warnings#include<iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <cctype> #include <functional>using namespace std; #define ME (s) Memset (s,0,sizeof (s)) #define PB push_backtypedef Long long ll;typedef unsigned int uint;typedef unsigned long long Ull;typ Edef pair <int, int> p;int maxd;const int N = 1000;int ans[n];int v[n];int get_first (int x, int y)//Find first unit fraction less than x/y denominator {int res = Y/x;return res*x >= y? res:res + 1;} ll GCD (ll A, ll b) {return b = = 0? a:gcd (b, a%b);} BOOL Better (int D)//update current solution {for (int i = d; I >= 0; i--)//Because the denominator is small to large storage, so reverse enumeration if (v[i]! = Ans[i]) return ans[i] = = 1 | | v [I]<ans[i];return false;} BOOL Dfs (int d, int from, LL AA, LL bb) {if (d = = Maxd) {if (BB%AA) return false;//if not divisible, search failed v[d] = bb/aa;if (better (d)) memcpy (ans, V, sizeof (LL) * (d + 1); return true;} bool OK = False;from = Max (from, Get_first (AA, BB));//update from this step is easy to ignore, assuming that the denominator of the first d-1 fraction is a, the denominator of the D fraction does not have to start with a+1, and 1/(A+1) is less than or equal to aa/bbfor (int i = from; i++) {if (bb* (Maxd + 1-d) <= I*aa) break;//Here is the right stop enumeration condition V[D] = i;//enumeration new Denominator ll b2 = bb*i;/ /calculate aa/bb-1/i numerator, denominator ll a2 = Aa*i-bb;ll g = gcd (A2, b2);//Calculate greatest common divisor, perform numerator if (DFS (d + 1, i + 1, a2/g, b2/g)) ok = true;//Note, Do not write return true, because finding a set of solutions is not a condition for stopping enumeration}return OK; int main () {int A, b;int rnd = 0;while (~scanf ("%d%d", &a, &b)) {int ok = 0;for (maxd = 1;; maxd++)//iteration deepens the main frame of the search, max D to be set as global variable {memset (ans,-1, sizeof (ANS)), if (Dfs (0, Get_first (A, b), A, B)) {OK = 1;break;}} printf ("Case%d:%d/%d=", ++rnd, A, b), for (int i = 0; I <= maxd; i++) {if (i) printf ("+");p rintf ("1/%d", Ans[i]);} printf ("\ n");}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Egyptian score" on the understanding of 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.