のリストが与えられたとしましょう ん
例:与えられた (2、0)、(2、1)、(1、2)、(0、3)、(1、3)、(2、3)、(3、3)、(2、4)、(1、5)、(1、6)
パスを1つだけ描画することが許可されている場合、実行される動的プログラミングによって問題を簡単に解決できます O(ん2)
しかし、私は2つの経路の繰り返し関係を思い付くことができません。このような再発関係について何かご存じの方がいらっしゃれば、ぜひお聞かせください。
のリストが与えられたとしましょう ん
例:与えられた (2、0)、(2、1)、(1、2)、(0、3)、(1、3)、(2、3)、(3、3)、(2、4)、(1、5)、(1、6)
パスを1つだけ描画することが許可されている場合、実行される動的プログラミングによって問題を簡単に解決できます O(ん2)
しかし、私は2つの経路の繰り返し関係を思い付くことができません。このような再発関係について何かご存じの方がいらっしゃれば、ぜひお聞かせください。
回答:
問題、言い換えると一般化:有限集合が与えられた場合 S
単純に、単一の最良のチェーンを見つけようとするかもしれません S2
Instead, we look for chains in the set T:={(x,y)∣(x,y)∈S2∧x≮y∧y≮x}
Lemma 1 (no retracing). Let C⊆T
Proof. The if direction is trivial. In the only if direction, for all z∈C1∩C2
Lemma 2 (existence of restricted best chain). For all chains C1,C2⊆S
Proof (revised). We give an algorithm to construct C
Initialize C:=∅
Let x′
If x′≮y′∧y′≮x′
If y<x′<y′
If y≮x′<y′
If x<y′<x′
If x≮y′<x′
This step is never reached, as the conditions for steps 3–7 are exhaustive.
If x≠⊤
Dynamic Program. For all (x,y)∈T
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
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).