東京大学 情報理工学系研究科 創造情報学専攻 2010年8月実施 筆記試験 第1問
Author
itsuitsuki
Description
出典:大学公式問題冊子の保存版。
Assume we have n different products G1,…,Gn (n≥2) whose prices are p1,…,pn respectively, and choose m products Gi1,…,Gim (2≤m≤n) from them so that there are no two identical ones. For given two positive integers qmin,qmax such that qmin<qmax and pi<qmin for each i=1,…,n, we want to make qmin<∑j=1mpij<qmax, that is, the sum of the prices is between qmin and qmax, by choosing an appropriate combination of products. The following Algorithm 1 implements the backtracking algorithm that is one of the solutions to this problem. In the descriptions of Algorithm 1, ϵ represents an empty sequence. In the descriptions of the procedure "back(⟨Gi1,…,Gik⟩,S0)", the first argument is a sequence of products consisting of the elements of the product set expected to be the solution eventually and the second argument is a set of products that are candidates to be added to the first argument. k is the length of the first argument of this "back" invocation. If k=0, the first argument is an empty sequence.
Algorithm 1: Invoke "back(ϵ,{G1,…,Gn})" where the procedure "back" is defined as follows.
Procedure back(⟨Gi1,…,Gik⟩,S0):
Step 1 Let S be a local variable representing a set of products. Initiate with S=S0 and proceed to Step 2.
Step 2 If ∑j=1kpij>qmin, output {Gi1,…,Gik} and terminate. Otherwise proceed to Step 3.
Step 3 If S is empty, output the information saying "no solutions" and terminate if k=0, and return to the invoking procedure if k>0. If S is not empty proceed to Step 4.
Step 4 Choose one element of S, remove it from S, and add this element Gik+1 to the end of the sequence ⟨Gi1,…,Gik⟩. Create the set of G′(∈S) whose price p′ satisfies 'p′+∑j=1k+1pij<qmax'. Denote this set by a variable S′ that is different from S. Invoke "back(⟨Gi1,…,Gik,Gik+1⟩,S′)" recursively and go back to Step 3.
Then solve the following questions.
(1) Suppose we execute Algorithm 1 for the four products G1,G2,G3,G4 whose prices are p1=1,p2=2,p3=3,p4=4 respectively and qmin=8,qmax=10. The following sequence represents an example of the arguments of the "back" procedure invocations.
(ϵ,{G1,G2,G3,G4})→(⟨G4⟩,{G1,G2,G3})→…
Write an example of the arguments of the "back" procedure invocations succeeding to the above ones in the same format in which the execution of a "back" procedure goes back from Step 4 to Step 3 at least one time.
(2) There are some techniques to execute Algorithm 1 efficiently by decreasing the number of invocations of the "back" procedure. One of them is to choose the element Gik+1 of S whose price is the highest in Step 4. However, in some cases, this technique does not make the number of invocations the smallest. Show an example of such cases by describing the values of n,pi(i=1,…,n),qmin,qmax and the arguments of the "back" procedures along the invocations sequence in the same way as (1).
(3) Assume that we invoke "back(⟨Gi1,…,Gik⟩,S0)" for a sequence of products ⟨Gi1,…,Gik⟩ and a set of products S0, the number of the elements of S0 is l, and tl is the maximum number of invocations of the "back" procedure during the execution of "back(⟨Gi1,…,Gik⟩,S0)" where the invocations include the invocation of "back(⟨Gi1,…,Gik⟩,S0)" itself. Then explain the reason why tl=1+∑i=0l−1ti if l≥1.
(4) Describe the maximum number of invocations of the "back" procedures during the execution of Algorithm 1 for n products G1,…,Gn (n≥2) where the invocations include the invocation of "back(ϵ,{G1,…,Gn})" at the beginning of the execution.
题目描述
有 n 种互不相同的商品 G1,…,Gn(n≥2),价格分别为 p1,…,pn。从中选择互不重复的 m 件商品 Gi1,…,Gim(2≤m≤n)。给定正整数 qmin<qmax,且每个 pi<qmin,目标是找到组合使
qmin<j=1∑mpij<qmax.
算法 1 用回溯法求解。记 ϵ 为空序列;过程
back(⟨Gi1,…,Gik⟩,S0)
的第一个参数是当前候选解序列,第二个参数是还可加入的商品集合,k 为当前序列长度,k=0 时第一个参数为 ϵ。
算法从 back(ϵ,{G1,…,Gn}) 开始。每次调用执行:
-
令局部集合 S=S0。
-
若当前价格和 ∑j=1kpij>qmin,输出当前商品集合并结束。
-
若 S 为空:当 k=0 时输出“无解”并结束;当 k>0 时返回调用者。否则进入下一步。
-
从 S 中选择并删除一个元素 Gik+1,把它追加到当前序列;再从剩余的 S 中取出所有满足
p′+j=1∑k+1pij<qmax
的商品 G′,组成不同于 S 的集合 S′。递归调用
back(⟨Gi1,…,Gik,Gik+1⟩,S′),返回后回到第 3 步。
回答下列问题。
-
对四件商品 p1=1,p2=2,p3=3,p4=4,取 qmin=8,qmax=10。已给调用序列开头
(ϵ,{G1,G2,G3,G4})→(⟨G4⟩,{G1,G2,G3})→⋯.
按相同格式补出一种后续调用序列,且其中至少发生一次某次 back 从第 4 步递归返回第 3 步。
-
第 4 步总选 S 中价格最高的商品通常可减少调用次数,但不一定达到最少。给出一个反例:明确写出 n、所有 pi、qmin,qmax,并像第 1 问一样列出 back 的调用参数序列。
-
若调用 back(⟨Gi1,…,Gik⟩,S0) 时 ∣S0∣=l,定义 tl 为本次调用及其递归过程中 back 调用次数的最大值。说明当 l≥1 时为何
tl=1+i=0∑l−1ti.
-
求算法 1 对 n 件商品执行时 back 的最大调用次数,其中包括最初对空序列的调用。
Kai
成功時の「停止」は探索全体の終了を意味する。一つの枝が失敗して戻るときは、親の列を元の長さに戻し、その親の局所集合 S に残った候補を試す。
(1)
例えば G4 の次に G3、その次に G1 を選ぶ。呼出しの列は
→→→→(ϵ,{G1,G2,G3,G4})(⟨G4⟩,{G1,G2,G3})(⟨G4,G3⟩,{G1,G2})(⟨G4,G3,G1⟩,∅)(⟨G4,G3,G2⟩,∅).
4番目の呼出しは合計8で qmin を超えず、追加候補もないので親へ戻る。このとき親がステップ4から3へ戻り、残った G2 を選んで5番目の呼出しを行う。最後の合計は 4+3+2=9 で 8<9<10 を満たす。4番目で G2 が候補から外れたのは、さらに加えると 4+3+1+2=10 となり上限の厳密不等式を満たさないためである。上の最後の矢印には親への復帰を挟んでおり、5番目が4番目の子という意味ではない。
(2)
n=3, (p1,p2,p3)=(6,5,4), qmin=8, qmax=10 とする。価格の高いものから選ぶと、
→→→(ϵ,{G1,G2,G3})(⟨G1⟩,∅)(⟨G2⟩,{G3})(⟨G2,G3⟩,∅)
となり、4回の呼出しが必要である。6+5=11 と 6+4=10 はどちらも上限未満ではないので、G1 の枝では候補がなく失敗する。一方、最初から G2 を選べば
(ϵ,{G1,G2,G3})→(⟨G2⟩,{G3})→(⟨G2,G3⟩,∅)
の3回で成功する。各価格は qmin 未満であり、成功には少なくとも2商品と根を含む3回の呼出しが要るため、後者はこの例で最少である。
(3)
t0=1。l 個の候補を持つ呼出しでは、自分自身を1回数える。親の局所集合から候補を一つずつ永久に取り除くので、各再帰呼出しへ渡せる候補数の上限は順に l−1,l−2,…,0 となる。したがって総数は
tl≤1+tl−1+tl−2+⋯+t0.
最大の場合には、価格上限による候補の除去も途中での成功停止も起こらず、各子が残った全候補を受け取る。この場合に等号を達成できるので、
tl=1+i=0∑l−1ti.
これは特定の入力で常に等号になるという主張ではなく、入力と探索の全場合についての最大回数である。
(4)
l≥2 で前の式を引き算すると tl−tl−1=tl−1、また t1=1+t0=2 だから、
tl=2l,tn=2n.
例えば相異なる n 商品の価格を全て1、qmin=n, qmax=n+1 とすれば、どの部分集合も総額が qmin を超えず、追加候補は除去されない。全 2n 個の部分集合(空集合を含む)を探索して無解となり、この上界を実際に達成する。商品が相異なることは、価格も相異なることを意味しない。