Skip to content

kruskal_mst and prim_mst return different types from SimpleWeightedGraph input #17

@wjholden

Description

@wjholden

The kruskal_mst function may contain some logic that the prim_mst function lacks. When I supply a SimpleWeightedGraph as input, the kruskal_mst function returns a vector of SimpleWeightedEdge. By contrast, the prim_mst function returns a vector of SimpleEdge instead. The behavior of kruskal_mst seems more beneficial, to me. Here is a small example:

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.7.1 (2021-12-22)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using Graphs, SimpleWeightedGraphs

julia> A=[0 7 0 5 0 0 0;0 0 8 0 7 0 0;0 0 0 0 5 0 0;0 0 0 0 18 6 0;0 0 0 0 0 8 9; 0 0 0 0 0 0 11;0 0 0 0 0 0 0]
7×7 Matrix{Int64}:
 0  7  0  5   0  0   0
 0  0  8  0   7  0   0
 0  0  0  0   5  0   0
 0  0  0  0  18  6   0
 0  0  0  0   0  8   9
 0  0  0  0   0  0  11
 0  0  0  0   0  0   0

julia> kruskal_mst(SimpleWeightedGraph(A + A'))
6-element Vector{SimpleWeightedEdge{Int64, Int64}}:
 Edge 1 => 4 with weight 5
 Edge 3 => 5 with weight 5
 Edge 4 => 6 with weight 6
 Edge 1 => 2 with weight 7
 Edge 2 => 5 with weight 7
 Edge 5 => 7 with weight 9

julia> prim_mst(SimpleWeightedGraph(A + A'))
6-element Vector{Graphs.SimpleGraphs.SimpleEdge{Int64}}:
 Edge 1 => 2
 Edge 5 => 3
 Edge 1 => 4
 Edge 2 => 5
 Edge 4 => 6
 Edge 5 => 7

Metadata

Metadata

Assignees

No one assigned

    Labels

    breakingFixing this would require a breaking change

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions