Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions hw1/kaiwenjin/9.6.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Test
function pp_dp(w::Vector{Int}, v::Vector{Int}, W::Int)
@assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && W >= 0
@assert length(w) == length(v)
n = length(w)
dp = zeros(Int, n+1, W+1)
for i in 1:n
for j in 0:W
if j < w[i]
dp[i+1, j+1] = dp[i, j+1]
else
dp[i+1, j+1] = max(dp[i, j+1], dp[i, j - w[i] + 1] + v[i])
end
end
end
return dp[n+1, W+1]
end

function pp(w::Vector{Int}, v::Vector{Int}, W::Int)
@assert length(w) == length(v)
@assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && W >= 0
n = length(w)
res2 = -1
for k = 0:2^n-1
chosen = []
for i in 1:n
push!(chosen, 1&k)
k >>= 1
end
val = sum(chosen .* v)
if val > res2 && sum(chosen .* w) <= W
res2 = val
end
end
return res2
end
@testset "package_problem" begin
w = [2,3,4,7]
v = [1,3,5,9]
W = 10
res1 = pp_dp(w,v,W)
res2 = pp(w,v,W)
@test res1 == res2
end
59 changes: 59 additions & 0 deletions hw1/kaiwenjin/9.7.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Test,Random
function pp_dp(w::Vector{Int}, v::Vector{Int}, W::Int)
@assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && W >= 0
@assert length(w) == length(v)
n = length(w)
dp = zeros(Int, n+1, W+1)
for i in 1:n
for j in 0:W
if j < w[i]
dp[i+1, j+1] = dp[i, j+1]
else
dp[i+1, j+1] = max(dp[i, j+1], dp[i, j - w[i] + 1] + v[i])
end
end
end
return dp[n+1, W+1]
end

function pp(w::Vector{Int}, v::Vector{Int}, W::Int)
@assert length(w) == length(v)
@assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && W >= 0
n = length(w)
res2 = -1
for k = 0:2^n-1
chosen = []
for i in 1:n
push!(chosen, 1&k)
k >>= 1
end
val = sum(chosen .* v)
if val > res2 && sum(chosen .* w) <= W
res2 = val
end
end
return res2
end

function pp_fptas(w::Vector{Int}, v::Vector{Int}, W::Int, ε::Float64)
@assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && W >= 0 && 0 < ε < 1
@assert length(w) == length(v)
n = length(w)
v_max = maximum(v)
c = ε * v_max / n
v_scaled = floor.(Int, v ./ c)
return pp_dp(w, v_scaled, W) * c
end

@testset "pp_fptas" begin
Random.seed!(6)
N = 20
M = 100
w = rand(collect(1:M), N)
v = rand(collect(1:M), N)
W = sum((M÷2)*(N÷2))
ε = 0.1
res0 = pp(w, v, W)
res1 = pp_fptas(w, v, W, ε)
@test abs(res1 - res0) <= ε * res0
end
32 changes: 32 additions & 0 deletions hw1/kaiwenjin/hw1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
### 9.6
The algorithm and test refer to [algorithm 9.6](9.6.jl)

$n$ objects, $v_{max}$ values and each entry needs $O(n)$ comparisons. So the time complexity is $T = O(n^2v_{max})$.

Why "P≠NP": this $T$ depends on $v_{max}$, which may be much larger than $n$.

### 9.7
The algorithm and test refer to [algorithm 9.7](9.7.jl)

Proof: consider the scaling factor $c = \frac{n}{\epsilon v_{max}}$
$$T = O(n^2v_{max}') = O(\frac{n^3}{\epsilon})$$

And the error is not more than (assuming that $v_{max}\leq V$)
$$1*1/c*n = \epsilon v_{max}\leq \epsilon V$$

QED.

### 9.16
Original form:
$$\max(c^Tx)\\
Ax \leq b\\x \geq 0$$

Standard form $L$:
$$\max(c^Tx)\\
[A,I]\left[\begin{array}{c}x\\s\end{array}\right] = b\\x \geq 0, s \geq 0$$

Without loss of generality, assume that $b \geq 0$. Then we get an LP with a trivial feasible solution whose optimal solution is feasible for $L$:
$$\min e^T s\\
[A,I]\left[\begin{array}{c}x\\s\end{array}\right] = b\\x \geq 0, s \geq 0$$

A feasible solution is $x = 0, s = b$. Its optimal solution must exist and is a feasible solution for $L$.