Skip to content
Extraits de code Groupes Projets
Valider 63a8b5cc rédigé par François LEFOULON's avatar François LEFOULON
Parcourir les fichiers

Kruskal marche ?

parent b0b123b9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Held \& Karp algorithm # Held \& Karp algorithm
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
using LinearAlgebra using LinearAlgebra
using JuMP using JuMP
using Cbc using Cbc
using LightGraphs using LightGraphs
using GraphPlot using GraphPlot
println("Modules loaded") println("Modules loaded")
``` ```
%% Output %% Output
Modules loaded Modules loaded
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
function connect_v1!(X, W1, n) function connect_v1!(X, W1, n)
argmin1, argmin2 = 2, 3 argmin1, argmin2 = 2, 3
min1, min2 = W1[2], W1[3] min1, min2 = W1[2], W1[3]
for i in 4:n for i in 4:n
if W1[i] < min1 if W1[i] < min1
min2 = min1 min2 = min1
argmin2 = argmin1 argmin2 = argmin1
min1 = W1[i] min1 = W1[i]
argmin1 = i argmin1 = i
end end
end end
X[1, argmin1] = 1 X[1, argmin1] = 1
X[1, argmin2] = 1 X[1, argmin2] = 1
for u 2:n for u 2:n
X[u, 1] = X[1, u] X[u, 1] = X[1, u]
end end
end end
``` ```
%% Output %% Output
connect_v1! (generic function with 1 method) connect_v1! (generic function with 1 method)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
function adj_to_graph(adj, n) function adj_to_graph(adj, n)
t = SimpleGraph(n) t = SimpleGraph(n)
for i in 1:n for i in 1:n
for j in i:n for j in i:n
if adj[i, j] == 1 if adj[i, j] == 1
add_edge!(t, i, j) add_edge!(t, i, j)
end end
end end
end end
return t return t
end end
``` ```
%% Output %% Output
adj_to_graph (generic function with 1 method) adj_to_graph (generic function with 1 method)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
function hk2(W) function hk2(W)
# Initialization # Initialization
n = size(W, 1) n = size(W, 1)
if size(W, 2)!=n if size(W, 2)!=n
error("Weight matrice must be square") error("Weight matrice must be square")
end end
# #
λ = zeros(n) λ = zeros(n)
g = complete_graph(n-1) g = complete_graph(n-1)
z = -1 z = -1
S = zeros(n,n) S = zeros(n,n)
k = 0 k = 0
X = falses(n, n) X = falses(n, n)
converge = false converge = false
while !converge && k < 10 # While not a tour while !converge && k < 100 # While not a tour
k+=1 k+=1
print(z, " ") print(z, " ")
X = falses(n, n) X = falses(n, n)
W_updated = deepcopy(W) W_updated = deepcopy(W)
# Minimum spanning tree (span_tree : st) # Minimum spanning tree (span_tree : st)
for u in 1:n for u in 1:n
for v in 1:n for v in 1:n
W_updated[u,v] -= λ[u] + λ[v] W_updated[u,v] -= λ[u] + λ[v]
end end
end end
st = kruskal_mst(g, W_updated[2:n,2:n]) st = kruskal_mst(g, W_updated[2:n,2:n])
println(st) println(st)
for i 1:size(st, 1) for i 1:size(st, 1)
X[src(st[i])+1, dst(st[i])+1] = 1 X[src(st[i])+1, dst(st[i])+1] = 1
end end
for u 1:n for u 1:n
for v 1:(u-1) for v 1:(u-1)
X[u,v] = X[v,u] X[u,v] = X[v,u]
end end
end end
# Connect vertex 1 # Connect vertex 1
W1 = deepcopy(W[1, :]) W1 = deepcopy(W[1, :])
for v in 2:n for v in 2:n
W1[v] -= λ[v] W1[v] -= λ[v]
end end
connect_v1!(X, W1, n) connect_v1!(X, W1, n)
one_tree = adj_to_graph(X) one_tree = adj_to_graph(X, n)
z = 2*sum(λ[2:n]) + sum((W[u,v] - λ[u] - λ[v])*X[u,v] for u 1:n for v 1:u) z = 2*sum(λ[2:n]) + sum((W[u,v] - λ[u] - λ[v])*X[u,v] for u 1:n for v 1:u)
#@constraint(m, spanningTree[i ∈ 1:size(st,1)], X[src(st[i]), dst(st[i])] == 1) #@constraint(m, spanningTree[i ∈ 1:size(st,1)], X[src(st[i]), dst(st[i])] == 1)
D = [sum(X[u,:]) for u 1:n] D = [sum(X[u,:]) for u 1:n]
if is_cyclic(one_tree) # If not a tour, update if is_cyclic(one_tree) # If not a tour, update
converge = true converge = true
λ .= λ .+ 2 .* (2 .- D) # Experimenter d'autres règles λ .= λ .+ 2 .* (2 .- D) # Experimenter d'autres règles
end end
println("sommes des poids des arêtes :", sum(W .* X)/2) println("sommes des poids des arêtes :", sum(W .* X)/2)
end end
return (X, λ) return (X, λ)
end end
``` ```
%% Output %% Output
hk2 (generic function with 1 method) hk2 (generic function with 1 method)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
W = [ W = [
0 8 4 15 15 3 ; 0 8 4 15 15 3 ;
8 0 5 15 2 15 ; 8 0 5 15 2 15 ;
4 5 0 6 15 15 ; 4 5 0 6 15 15 ;
15 15 6 0 5 3 ; 15 15 6 0 5 3 ;
15 2 15 5 0 4 ; 15 2 15 5 0 4 ;
3 15 15 3 4 0 3 15 15 3 4 0
] ]
``` ```
%% Output %% Output
6×6 Array{Int64,2}: 6×6 Array{Int64,2}:
0 8 4 15 15 3 0 8 4 15 15 3
8 0 5 15 2 15 8 0 5 15 2 15
4 5 0 6 15 15 4 5 0 6 15 15
15 15 6 0 5 3 15 15 6 0 5 3
15 2 15 5 0 4 15 2 15 5 0 4
3 15 15 3 4 0 3 15 15 3 4 0
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
X, λ = hk2(W) X, λ = hk2(W)
``` ```
%% Output %% Output
-1 LightGraphs.SimpleGraphs.SimpleEdge{Int64}[Edge 1 => 4, Edge 3 => 5, Edge 4 => 5, Edge 1 => 2] -1 LightGraphs.SimpleGraphs.SimpleEdge{Int64}[Edge 1 => 4, Edge 3 => 5, Edge 4 => 5, Edge 1 => 2]
sommes des poids des arêtes :25.0
MethodError: no method matching adj_to_graph(::BitArray{2}) (Bool[0 1 … 0 1; 1 0 … 1 0; … ; 0 1 … 0 1; 1 0 … 1 0], [0.0, -2.0, 2.0, 2.0, 0.0, -2.0])
Closest candidates are:
adj_to_graph(::Any, !Matched::Any) at In[31]:1
Stacktrace:
[1] hk2(::Array{Int64,2}) at ./In[32]:56
[2] top-level scope at In[34]:1
[3] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
X X
``` ```
%% Output %% Output
6×6 BitArray{2}: 6×6 BitArray{2}:
0 1 0 0 0 1 0 1 0 0 0 1
1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 1
0 1 0 0 0 1 0 1 0 0 0 1
1 0 0 1 1 0 1 0 0 1 1 0
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
λ λ
``` ```
%% Output %% Output
6-element Array{Float64,1}: 6-element Array{Float64,1}:
0.0 0.0
0.0 -2.0
0.0 2.0
0.0 2.0
0.0 0.0
0.0 -2.0
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
g = complete_graph(5) g = complete_graph(5)
gplot(g) gplot(g)
k = kruskal_mst(g, rand(5, 5)) k = kruskal_mst(g, rand(5, 5))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
for e in k for e in k
println(src(e), dst(e)) println(src(e), dst(e))
end end
typeof(k) typeof(k)
for i 1:size(k,1) for i 1:size(k,1)
println(src(k[i]), " ", dst(k[i])) println(src(k[i]), " ", dst(k[i]))
end end
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
``` ```
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter