From 094d35ce4415854060119ce20ea5f3baad5548d5 Mon Sep 17 00:00:00 2001 From: Yui <2946723935@qq.com> Date: Mon, 12 May 2025 18:07:07 +0800 Subject: [PATCH 1/2] hw1 kaiwenjin --- hw1/kaiwenjin/9.6.jl | 44 ++++++++++++++++++++++++++++++++ hw1/kaiwenjin/9.7.jl | 61 ++++++++++++++++++++++++++++++++++++++++++++ hw1/kaiwenjin/hw1.md | 26 +++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 hw1/kaiwenjin/9.6.jl create mode 100644 hw1/kaiwenjin/9.7.jl create mode 100644 hw1/kaiwenjin/hw1.md diff --git a/hw1/kaiwenjin/9.6.jl b/hw1/kaiwenjin/9.6.jl new file mode 100644 index 0000000..c8f1f2b --- /dev/null +++ b/hw1/kaiwenjin/9.6.jl @@ -0,0 +1,44 @@ +using Test +function pp_dp(w::Vector{Int}, v::Vector{Int}, V::Int) + @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && V >= 0 + @assert length(w) == length(v) + n = length(w) + dp = zeros(Int, n+1, V+1) + for i in 1:n + for j in 0:V + 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, V+1] +end + +function pp(w::Vector{Int}, v::Vector{Int}, V::Int) + @assert length(w) == length(v) + @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && V >= 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) <= V + res2 = val + end + end + return res2 +end +@testset "package_problem" begin + w = [2,3,4,7] + v = [1,3,5,9] + V = 10 + res1 = pp_dp(w,v,V) + res2 = pp(w,v,V) + @test res1 == res2 +end diff --git a/hw1/kaiwenjin/9.7.jl b/hw1/kaiwenjin/9.7.jl new file mode 100644 index 0000000..a56096d --- /dev/null +++ b/hw1/kaiwenjin/9.7.jl @@ -0,0 +1,61 @@ +using Test,Random +function pp_dp(w::Vector{Int}, v::Vector{Int}, V::Int) + @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && V >= 0 + @assert length(w) == length(v) + n = length(w) + dp = zeros(Int, n+1, V+1) + for i in 1:n + for j in 0:V + 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, V+1] +end + +function pp(w::Vector{Int}, v::Vector{Int}, V::Int) + @assert length(w) == length(v) + @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && V >= 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) <= V + res2 = val + end + end + return res2 +end + +function pp_fptas(w::Vector{Int}, v::Vector{Int}, V::Int, ε::Float64) + @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && V >= 0 && 0 < ε < 1 + @assert length(w) == length(v) + n = length(w) + v_max = maximum(v) + c = ε * v_max / n + w_scaled = floor.(Int, w ./ c) + v_scaled = floor.(Int, v ./ c) + V_scaled = floor(Int, V / c) + return pp_dp(w_scaled, v_scaled, V_scaled) * 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) + V = sum((M÷2)*(N÷2)) + ε = 0.1 + res0 = pp(w, v, V) + res1 = pp_fptas(w, v, V, ε) + @test abs(res1 - res0) <= ε * res0 +end \ No newline at end of file diff --git a/hw1/kaiwenjin/hw1.md b/hw1/kaiwenjin/hw1.md new file mode 100644 index 0000000..273db3e --- /dev/null +++ b/hw1/kaiwenjin/hw1.md @@ -0,0 +1,26 @@ +### 9.6 +The algorithm and test refers 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 not "P=NP": this $T$ depends on $v_{max}$, which may be much larger than $n$. + +### 9.7 +The algorithm and test refers 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 (assume 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: +$$\max(c^Tx)\\ +[A,I]\left[\begin{array}{c}x\\s\end{array}\right] = b\\x \geq 0, s \geq 0$$ \ No newline at end of file From 91236171d6acd0a818f37bd35d303d245832eedc Mon Sep 17 00:00:00 2001 From: Yui <2946723935@qq.com> Date: Tue, 13 May 2025 15:21:44 +0800 Subject: [PATCH 2/2] hw1 update --- hw1/kaiwenjin/9.6.jl | 22 +++++++++++----------- hw1/kaiwenjin/9.7.jl | 30 ++++++++++++++---------------- hw1/kaiwenjin/hw1.md | 20 +++++++++++++------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/hw1/kaiwenjin/9.6.jl b/hw1/kaiwenjin/9.6.jl index c8f1f2b..8a77498 100644 --- a/hw1/kaiwenjin/9.6.jl +++ b/hw1/kaiwenjin/9.6.jl @@ -1,11 +1,11 @@ using Test -function pp_dp(w::Vector{Int}, v::Vector{Int}, V::Int) - @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && V >= 0 +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, V+1) + dp = zeros(Int, n+1, W+1) for i in 1:n - for j in 0:V + for j in 0:W if j < w[i] dp[i+1, j+1] = dp[i, j+1] else @@ -13,12 +13,12 @@ function pp_dp(w::Vector{Int}, v::Vector{Int}, V::Int) end end end - return dp[n+1, V+1] + return dp[n+1, W+1] end -function pp(w::Vector{Int}, v::Vector{Int}, V::Int) +function pp(w::Vector{Int}, v::Vector{Int}, W::Int) @assert length(w) == length(v) - @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && V >= 0 + @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && W >= 0 n = length(w) res2 = -1 for k = 0:2^n-1 @@ -28,7 +28,7 @@ function pp(w::Vector{Int}, v::Vector{Int}, V::Int) k >>= 1 end val = sum(chosen .* v) - if val > res2 && sum(chosen .* w) <= V + if val > res2 && sum(chosen .* w) <= W res2 = val end end @@ -37,8 +37,8 @@ end @testset "package_problem" begin w = [2,3,4,7] v = [1,3,5,9] - V = 10 - res1 = pp_dp(w,v,V) - res2 = pp(w,v,V) + W = 10 + res1 = pp_dp(w,v,W) + res2 = pp(w,v,W) @test res1 == res2 end diff --git a/hw1/kaiwenjin/9.7.jl b/hw1/kaiwenjin/9.7.jl index a56096d..fc73c53 100644 --- a/hw1/kaiwenjin/9.7.jl +++ b/hw1/kaiwenjin/9.7.jl @@ -1,11 +1,11 @@ using Test,Random -function pp_dp(w::Vector{Int}, v::Vector{Int}, V::Int) - @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && V >= 0 +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, V+1) + dp = zeros(Int, n+1, W+1) for i in 1:n - for j in 0:V + for j in 0:W if j < w[i] dp[i+1, j+1] = dp[i, j+1] else @@ -13,12 +13,12 @@ function pp_dp(w::Vector{Int}, v::Vector{Int}, V::Int) end end end - return dp[n+1, V+1] + return dp[n+1, W+1] end -function pp(w::Vector{Int}, v::Vector{Int}, V::Int) +function pp(w::Vector{Int}, v::Vector{Int}, W::Int) @assert length(w) == length(v) - @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && V >= 0 + @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && W >= 0 n = length(w) res2 = -1 for k = 0:2^n-1 @@ -28,23 +28,21 @@ function pp(w::Vector{Int}, v::Vector{Int}, V::Int) k >>= 1 end val = sum(chosen .* v) - if val > res2 && sum(chosen .* w) <= V + if val > res2 && sum(chosen .* w) <= W res2 = val end end return res2 end -function pp_fptas(w::Vector{Int}, v::Vector{Int}, V::Int, ε::Float64) - @assert findmin(w)[1] >= 0 && findmin(v)[1] >= 0 && V >= 0 && 0 < ε < 1 +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 - w_scaled = floor.(Int, w ./ c) v_scaled = floor.(Int, v ./ c) - V_scaled = floor(Int, V / c) - return pp_dp(w_scaled, v_scaled, V_scaled) * c + return pp_dp(w, v_scaled, W) * c end @testset "pp_fptas" begin @@ -53,9 +51,9 @@ end M = 100 w = rand(collect(1:M), N) v = rand(collect(1:M), N) - V = sum((M÷2)*(N÷2)) + W = sum((M÷2)*(N÷2)) ε = 0.1 - res0 = pp(w, v, V) - res1 = pp_fptas(w, v, V, ε) + res0 = pp(w, v, W) + res1 = pp_fptas(w, v, W, ε) @test abs(res1 - res0) <= ε * res0 end \ No newline at end of file diff --git a/hw1/kaiwenjin/hw1.md b/hw1/kaiwenjin/hw1.md index 273db3e..6ed9597 100644 --- a/hw1/kaiwenjin/hw1.md +++ b/hw1/kaiwenjin/hw1.md @@ -1,26 +1,32 @@ ### 9.6 -The algorithm and test refers to [algorithm 9.6](9.6.jl) +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 not "P=NP": this $T$ depends on $v_{max}$, which may be much larger than $n$. +Why "P≠NP": this $T$ depends on $v_{max}$, which may be much larger than $n$. ### 9.7 -The algorithm and test refers to [algorithm 9.7](9.7.jl) +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 (assume that $v_{max}\leq V$) +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: +Original form: $$\max(c^Tx)\\ Ax \leq b\\x \geq 0$$ -standard form: +Standard form $L$: $$\max(c^Tx)\\ -[A,I]\left[\begin{array}{c}x\\s\end{array}\right] = b\\x \geq 0, s \geq 0$$ \ No newline at end of file +[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$.