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:
# Held \& Karp algorithm
%% Cell type:code id: tags:
``` julia
using LinearAlgebra
using JuMP
using Cbc
using LightGraphs
using GraphPlot
println("Modules loaded")
```
%% Output
Modules loaded
%% Cell type:code id: tags:
``` julia
function connect_v1!(X, W1, n)
argmin1, argmin2 = 2, 3
min1, min2 = W1[2], W1[3]
for i in 4:n
if W1[i] < min1
min2 = min1
argmin2 = argmin1
min1 = W1[i]
argmin1 = i
end
end
X[1, argmin1] = 1
X[1, argmin2] = 1
for u 2:n
X[u, 1] = X[1, u]
end
end
```
%% Output
connect_v1! (generic function with 1 method)
%% Cell type:code id: tags:
``` julia
function adj_to_graph(adj, n)
t = SimpleGraph(n)
for i in 1:n
for j in i:n
if adj[i, j] == 1
add_edge!(t, i, j)
end
end
end
return t
end
```
%% Output
adj_to_graph (generic function with 1 method)
%% Cell type:code id: tags:
``` julia
function hk2(W)
# Initialization
n = size(W, 1)
if size(W, 2)!=n
error("Weight matrice must be square")
end
#
λ = zeros(n)
g = complete_graph(n-1)
z = -1
S = zeros(n,n)
k = 0
X = falses(n, n)
converge = false
while !converge && k < 10 # While not a tour
while !converge && k < 100 # While not a tour
k+=1
print(z, " ")
X = falses(n, n)
W_updated = deepcopy(W)
# Minimum spanning tree (span_tree : st)
for u in 1:n
for v in 1:n
W_updated[u,v] -= λ[u] + λ[v]
end
end
st = kruskal_mst(g, W_updated[2:n,2:n])
println(st)
for i 1:size(st, 1)
X[src(st[i])+1, dst(st[i])+1] = 1
end
for u 1:n
for v 1:(u-1)
X[u,v] = X[v,u]
end
end
# Connect vertex 1
W1 = deepcopy(W[1, :])
for v in 2:n
W1[v] -= λ[v]
end
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)
#@constraint(m, spanningTree[i ∈ 1:size(st,1)], X[src(st[i]), dst(st[i])] == 1)
D = [sum(X[u,:]) for u 1:n]
if is_cyclic(one_tree) # If not a tour, update
converge = true
λ .= λ .+ 2 .* (2 .- D) # Experimenter d'autres règles
end
println("sommes des poids des arêtes :", sum(W .* X)/2)
end
return (X, λ)
end
```
%% Output
hk2 (generic function with 1 method)
%% Cell type:code id: tags:
``` julia
W = [
0 8 4 15 15 3 ;
8 0 5 15 2 15 ;
4 5 0 6 15 15 ;
15 15 6 0 5 3 ;
15 2 15 5 0 4 ;
3 15 15 3 4 0
]
```
%% Output
6×6 Array{Int64,2}:
0 8 4 15 15 3
8 0 5 15 2 15
4 5 0 6 15 15
15 15 6 0 5 3
15 2 15 5 0 4
3 15 15 3 4 0
%% Cell type:code id: tags:
``` julia
X, λ = hk2(W)
```
%% Output
-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})
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
(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])
%% Cell type:code id: tags:
``` julia
X
```
%% Output
6×6 BitArray{2}:
0 1 0 0 0 1
1 0 1 0 1 0
0 1 0 0 0 0
0 0 0 0 0 1
0 1 0 0 0 1
1 0 0 1 1 0
%% Cell type:code id: tags:
``` julia
λ
```
%% Output
6-element Array{Float64,1}:
0.0
0.0
0.0
0.0
0.0
0.0
0.0
-2.0
2.0
2.0
0.0
-2.0
%% Cell type:code id: tags:
``` julia
g = complete_graph(5)
gplot(g)
k = kruskal_mst(g, rand(5, 5))
```
%% Cell type:code id: tags:
``` julia
for e in k
println(src(e), dst(e))
end
typeof(k)
for i 1:size(k,1)
println(src(k[i]), " ", dst(k[i]))
end
```
%% Cell type:code id: tags:
``` julia
```
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter