2つのパスが到達できるポイントの最大数


8

のリストが与えられたとしましょう んn ポイント、その バツx そして yy座標はすべて負ではありません。また、重複するポイントがないとします。ポイントからしか行けない(バツ私、y私)(xi,yi) ポイントへ (バツj、yj)(xj,yj) もし バツ私≤バツjxi≤xj そして y私≤yjyi≤yj。問題は次のとおりです。んnポイント、上記のルールを使用してポイントを接続する2つのパスを描画することが許可されている場合に到達できるポイントの最大数はいくつですか?パスは原点から開始する必要があり、繰り返しポイントを含めることができます。(0、0)(0,0) もちろん到達ポイントには含まれていません。

例:与えられた (2、0)、(2、1)、(1、2)、(0、3)、(1、3)、(2、3)、(3、3)、(2、4)、(1、5)、(1、6)(2,0),(2,1),(1,2),(0,3),(1,3),(2,3),(3,3),(2,4),(1,5),(1,6)、答えは 88 私たちは取ることができるので (0、0)→(2、0)→(2、1)→(2、3)→(2、4)(0,0)→(2,0)→(2,1)→(2,3)→(2,4) そして (0、0)→(1、2)→(1、3)→(1、5)→(1、6)(0,0)→(1,2)→(1,3)→(1,5)→(1,6)。

パスを1つだけ描画することが許可されている場合、実行される動的プログラミングによって問題を簡単に解決できます O(ん2)O(n2)。最初にポイントを降順に並べ替えますバツ私+y私xi+yi。しましょうD[私]D[i] コインからピックアップできるコインの最大数 11 に 私iソートされたリスト。その後D[1]=1D[1]=1 そして D[私]=最高1≤j<私、バツj≤バツ私、yj≤y私D[j]+1D[i]=max1≤j<i,xj≤xi,yj≤yiD[j]+1。そのときの答えは最高1≤私≤んD[私]+1max1≤i≤nD[i]+1。

しかし、私は2つの経路の繰り返し関係を思い付くことができません。このような再発関係について何かご存じの方がいらっしゃれば、ぜひお聞かせください。


私はポイントを辞書式にソートしますが、それは問題ではないと思います。あなたは間違いなく定着すべきですD[0]D[0]; 最良の道は最初のコインを使用しないかもしれません。また、結果の選択方法は、最良のパスが最後のポイントを使用する必要があることを示唆しています。さらに、厄介な構造のため、この問題はDPには適さないようです。ポイントによって暗示されるDAGの最長パスを見つけることは、はるかに理にかなっています。
— ラファエル

1つのパスでは、最後のポイントを含める必要はありません。ある点については私i その右上には意味がありません D[私]D[i] 単になります 11。私はそれをもっと明確にすべきだったと思います。
— アデンドン

単純にアルゴリズムを2回実行するのではなく、2番目のパスで、最初のパスで触れたすべてのポイントを削除しますか?または、単一の反復関係が必要ですか?
— edA-qa mort-ora-y 2012

回答:


4

問題、言い換えると一般化:有限集合が与えられた場合 SS部分注文を装備 ≤≤、チェーンを見つける C1、C2⊆SC1,C2⊆S 最大化 |C1∪C2||C1∪C2|。問題は、S⊆R2+S⊆R2+ そして (バツ、y)≤(z、w)⟺バツ≤z∧y≤w(x,y)≤(z,w)⟺x≤z∧y≤w。

単純に、単一の最良のチェーンを見つけようとするかもしれません S2S2、チェーンのコンポーネントが持つ明確な値の数によって最もよく測定されます。残念ながら、1つのコンポーネントは他のコンポーネントのステップをたどることができます。((0、0)、(0、0))<((1、0)、(0、0))<((2、0)、(0、0))<((2、0)、(1、0))、

((0,0),(0,0))<((1,0),(0,0))<((2,0),(0,0))<((2,0),(1,0)),
したがって、この最良の概念には最適な部分構造はありません。

Instead, we look for chains in the set T:={(x,y)∣(x,y)∈S2∧x≮y∧y≮x}T:={(x,y)∣(x,y)∈S2∧x≮y∧y≮x}. By requiring that the components be equal or incomparable, we prevent retracing but now need to argue that some best chain conforms to the new requirement.

Lemma 1 (no retracing). Let C⊆TC⊆T be a chain and define C1:={x∣(x,y)∈C}C1:={x∣(x,y)∈C} and C2:={y∣(x,y)∈C}C2:={y∣(x,y)∈C}. For all z∈Sz∈S, we have z∈C1∩C2z∈C1∩C2 if and only if (z,z)∈C(z,z)∈C.

Proof. The if direction is trivial. In the only if direction, for all z∈C1∩C2z∈C1∩C2, there exist x,y∈Sx,y∈S such that (x,z),(z,y)∈C(x,z),(z,y)∈C. Since CC is a chain, (x,z)≤(z,y)∨(z,y)≤(x,z)(x,z)≤(z,y)∨(z,y)≤(x,z). Assume symmetrically that (x,z)≤(z,y)(x,z)≤(z,y), which implies that x≤z≤yx≤z≤y. We know by the definition of TT that x≮z∧z≮yx≮z∧z≮y, so x=z=yx=z=y, and (z,z)∈C(z,z)∈C.

Lemma 2 (existence of restricted best chain). For all chains C1,C2⊆SC1,C2⊆S, there exists a chain C⊆TC⊆T such that C1⊆{x∣(x,y)∈C}⊆C1∪C2C1⊆{x∣(x,y)∈C}⊆C1∪C2 and C2⊆{y∣(x,y)∈C}⊆C1∪C2C2⊆{y∣(x,y)∈C}⊆C1∪C2.

Proof (revised). We give an algorithm to construct CC. For convenience, define sentinels ⊥,⊤⊥,⊤ such that ⊥<x<⊤⊥<x<⊤ for all x∈Sx∈S. Let C′1:=C1∪{⊤}C′1:=C1∪{⊤} and C′2:=C2∪{⊤}C′2:=C2∪{⊤}.

  1. Initialize C:=∅C:=∅ and x:=⊥x:=⊥ and y:=⊥y:=⊥. An invariant is that x≮y∧y≮xx≮y∧y≮x.

  2. Let x′x′ be the next element of C1C1, that is, x′:=inf{z∣z∈C′1∧x<z}x′:=inf{z∣z∈C′1∧x<z}. Let y′y′ be the next element of C2C2, that is, y′:=inf{w∣w∈C′2∧y<w}y′:=inf{w∣w∈C′2∧y<w}.

  3. If x′≮y′∧y′≮x′x′≮y′∧y′≮x′, set (x,y):=(x′,y′)(x,y):=(x′,y′) and go to step 9.

  4. If y<x′<y′y<x′<y′, set (x,y):=(x′,x′)(x,y):=(x′,x′) and go to step 9.

  5. If y≮x′<y′y≮x′<y′, set x:=x′x:=x′ and go to step 9. Note that x<x′∧x≮yx<x′∧x≮y implies that x′≮yx′≮y.

  6. If x<y′<x′x<y′<x′, set (x,y):=(y′,y′)(x,y):=(y′,y′) and go to step 9.

  7. If x≮y′<x′x≮y′<x′, set y:=y′y:=y′ and go to step 9. Note that y<y′∧y≮xy<y′∧y≮x implies that y′≮xy′≮x.

  8. This step is never reached, as the conditions for steps 3–7 are exhaustive.

  9. If x≠⊤x≠⊤ (equivalently, y≠⊤y≠⊤), set C:=C∪{(x,y)}C:=C∪{(x,y)} and go to step 2.

Dynamic Program. For all (x,y)∈T(x,y)∈T, compute D[x,y]:=sup({D[z,w]+[x≠z]+[y≠w]−[x=y]|(z,w)∈T∧(z,w)<(x,y)}∪{2−[x=y]}),

D[x,y]:=sup({D[z,w]+[x≠z]+[y≠w]−[x=y]∣∣(z,w)∈T∧(z,w)<(x,y)}∪{2−[x=y]}),
where [condition]=1[condition]=1 if conditioncondition is true and [condition]=0[condition]=0 if conditioncondition is false. By Lemma 1, it follows that the bracket expressions correctly count the number of new elements. By Lemma 2, the optimal solution to the original problem is found.

Nice. I have not checked every detail, though. Welcome to cs.SE!
— Raphael

0

Let P=p1…pn to sorted list of points.


Following your recurrence for one path, the first thing to note is that you have to keep track of which points have been visited by the paths; otherwise you can not count correctly. The second thing is that you have now four possibilities for every point: neither path may use it, one of them or both. So, we have to find maximising combinations for all three cases.

Formally, let d:[0…n]→(2[n]×2[n])3 with d(i) the pair of (sets of) visited nodes of the two paths that maximise the number of visited points from the input set up to the ith one, with the first component the maximising pair of paths for which the first one uses pi, the second component similar for the second path and the third component with both paths using pi. d is given by the recurrence

d(0)=((∅,∅),(∅,∅),(∅,∅))d(i)=( argmax(L,R)∈(L′×R)i|L∪R|,=( argmax(L,R)∈(L×R′)i|L∪R|,=( argmax(L,R)∈(L′×R′)i|L∪R| )

with

(L′×R)i={(L∪{i},R)∣(L,R)∈i−1⋃j=0d(j),xi≥maxj∈Lxj,yi≥maxj∈Lyj},

(L′×R)i similar with extending R and (L′×R′)i similar with extending both L and R.

Needless to say, this is not very nice. This is because the problem does not lend itself to dynamic programming very well: you can not combine many partial solutions because there is no nice total ordering on the points, and you can not discard intermediate results for the same reason.


A nicer view on the problem is to model the set of points as weighted directed acyclic graph G=(V,E,w) with

  • V={(0,0),p1,…,pn,(X,Y)} with X=maxxi, Y=maxyi, and
  • E={((x1,y1),(x2,y2))∈V2∣xi≤xj,yi≤yj} and
  • w(v1,v2)={0,v2=(X,Y)1, else.

Note that you can keep the graph smaller if you remove redundant edges, that is remove (v1,v2) if there is a path (v1,…,v2), because taking such "shortcuts" is never better advantageous.

For one path, the solution is clearly the length of the longest path P∗ from (0,0) to (X,Y). Now, if we change w so that all edges leading to points on P∗ also have weight 0 and compute the longest path in this modified graph, we get a path P+ so that P∗ and P+ together cover as many points as two paths can. This leaves us with a runtime in O(|V|+|E|)⊆O(n2) (see here).


I may have misunderstood what you wrote, but for the weighted directed acyclic graph, does that mean we can simply find the longest path first, then delete all the edges in the longest path and find the longest path in the remaining graph?
— Aden Dong

@AdenDong: No, not delete; the second path is allowed to reuse edges the first path took. We assign them weight 0 so their target nodes are not counted again -- we want the second path to take a new route if profitable, after all.
— Raphael
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.