Skip to content

Commit fd83df7

Browse files
committed
solve(::MstProblem) now applies on the distance matrices as well as list of Connections
1 parent 2e04e9d commit fd83df7

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 0.2.13 (Upcoming Release)
22

3+
- Minimum Spanning Tree accepts distance matrix encapsulated by MstProblem as input.
34

45
### 0.2.12
56

src/mst.jl

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import ..Network: Connection, nodes
44

55
import ..OperationsResearchModels: solve
66

7+
import LinearAlgebra: issymmetric
8+
79
export hasloop
810
export MstResult
911
export MstProblem
@@ -17,15 +19,16 @@ export MstProblem
1719
Defines the minimum spanning tree problem.
1820
1921
# Fields
20-
- `connections::Vector{Connection}`: The connections (edges) in the network.
22+
- `data::Union{Vector{Connection}, AbstractMatrix{<:Real}}`: The data representing the network.
23+
It can be either a vector of connections or a distance matrix.
2124
2225
!!! info "Interpreting the Connection object"
2326
2427
The Connection object defines a directed edge, but for the minimum spanning tree problem,
2528
the edges are considered undirected.
2629
"""
2730
struct MstProblem
28-
connections::Vector{Connection}
31+
data::Union{Vector{Connection}, AbstractMatrix{<:Real}}
2932
end
3033

3134

@@ -120,6 +123,27 @@ function makedistancematrix(conns::Vector{Connection})::Matrix
120123
end
121124

122125

126+
127+
function makeconnections(mat::Matrix)::Vector{Connection}
128+
129+
n, _ = size(mat)
130+
131+
!issymmetric(mat) && throw(AssertionError("The distance matrix must be symmetric."))
132+
133+
conns = Vector{Connection}(undef, n * (n-1) ÷ 2)
134+
idx = 1
135+
for i = 1:n
136+
for j = (i+1):n
137+
conns[idx] = Connection(i, j, mat[i, j])
138+
idx += 1
139+
end
140+
end
141+
142+
return conns
143+
144+
end
145+
146+
123147
"""
124148
solve(problem::MstProblem)
125149
@@ -160,11 +184,19 @@ println(result.connections)
160184
"""
161185
function solve(problem::MstProblem)::MstResult
162186

163-
conns = problem.connections
187+
if problem.data isa AbstractMatrix
188+
conns = makeconnections(problem.data)
189+
distmat = problem.data
190+
else
191+
conns = problem.data
192+
distmat = makedistancematrix(conns)
193+
end
194+
195+
#conns = problem.connections
164196

165-
totaldist = 0.0
197+
#distmat = makedistancematrix(conns)
166198

167-
distmat = makedistancematrix(conns)
199+
totaldist = 0.0
168200

169201
assigned = Set{Int64}()
170202
unassigned = nodes(conns)

test/testmst.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,25 @@
173173
@test !hasloop(result.connections)
174174
@test length(result.connections) == length(allnodes) - 1
175175
end
176+
177+
@testset "MST with distance symmetric matrix" begin
178+
m = [0 4 2 5 6 7;
179+
4 0 3 8 9 10;
180+
2 3 0 1 2 3;
181+
5 8 1 0 4 5;
182+
6 9 2 4 0 6;
183+
7 10 3 5 6 0
184+
]
185+
result = solve(MstProblem(m))
186+
@test result.distance == 11.0
187+
end
188+
189+
@testset "MST with distance asymmetric matrix" begin
190+
m = [0 4 3;
191+
2 0 1;
192+
5 8 0
193+
]
194+
@test_throws AssertionError solve(MstProblem(m))
195+
196+
end
176197
end

0 commit comments

Comments
 (0)