# How to extract weights from SimpleWeightedEdge (from kruskal\_mst)

**URL:** https://discourse.julialang.org/t/how-to-extract-weights-from-simpleweightededge-from-kruskal-mst/81192
**Category:** Graphs
**Tags:** lightgraphs, graphs
**Created:** [May 17, 2022, 12:07pm UTC](https://discourse.julialang.org/t/how-to-extract-weights-from-simpleweightededge-from-kruskal-mst/81192 "2022-05-17T12:07:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![JedPhillips](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@JedPhillips](https://discourse.julialang.org/u/JedPhillips)
#### Post date: [May 17, 2022, 12:07pm UTC](https://discourse.julialang.org/t/how-to-extract-weights-from-simpleweightededge-from-kruskal-mst/81192/1 "2022-05-17T12:07:41Z")

</div>

I’m trying to figure out how to interact with a minimal spanning tree object returned by Graphs.kruskal\_mst(). I just can’t figure out how to extract the resulting edge wights (or the edges).

Here’s a minimal code to show the issue:

```julia
using Distributions
using Graphs
using SimpleWeightedGraphs
using Distances

M = reshape(rand(Normal(), 100), 10, 10)
g = SimpleWeightedGraph(pairwise(Euclidean(), M, dims = 1))
my_mst = Graphs.kruskal_mst(g)

# when inspecting the object
julia> my_mst
9-element Vector{SimpleWeightedEdge{Int64, Float64}}:
 Edge 2 => 8 with weight 2.3668794009593306
 Edge 1 => 10 with weight 2.4514610105905223
 Edge 1 => 9 with weight 2.4717071330626306
 Edge 6 => 8 with weight 2.714304985226182
 ⋮
 Edge 3 => 9 with weight 3.507507419474672
 Edge 4 => 9 with weight 4.113153232605692
 Edge 1 => 7 with weight 4.1630221327329044

julia> my_mst[1]
Edge 2 => 8 with weight 2.3668794009593306

```

My goal here is just to get a vector of the edge weights. I’ve tried a bunch of thing, treating it as a dictionary, trying to convert it back to a graph, trying to extract the edges, but nothing works. I’m sure it’s a simple solution but I can’t track it down.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 17, 2022, 12:23pm UTC](https://discourse.julialang.org/t/how-to-extract-weights-from-simpleweightededge-from-kruskal-mst/81192/2 "2022-05-17T12:23:06Z")

</div>

Hi there!  
Don’t hesitate to ask the REPL for help, it is my best friend in such cases.

```julia
julia> typeof(my_mst[1])
SimpleWeightedEdge{Int64, Float64}

help?> SimpleWeightedEdge
search: SimpleWeightedEdge SimpleWeightedGraphEdge SimpleWeightedDiGraphEdge

  No documentation found.

  Summary
  ≡≡≡≡≡≡≡≡≡

  struct SimpleWeightedEdge{T<:Integer, U<:Real}

  Fields
  ≡≡≡≡≡≡≡≡

  src :: T<:Integer
  dst :: T<:Integer
  weight :: U<:Real

  Supertype Hierarchy
  ≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡

  SimpleWeightedEdge{T<:Integer, U<:Real} <: AbstractSimpleWeightedEdge{T<:Integer} <: AbstractEdge{T<:Integer} <: Any

julia> my_mst[1].weight
3.1273492392134963

```

---

<div class="post-metadata">

### Author: ![JedPhillips](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@JedPhillips](https://discourse.julialang.org/u/JedPhillips)
#### Post date: [May 17, 2022, 12:28pm UTC](https://discourse.julialang.org/t/how-to-extract-weights-from-simpleweightededge-from-kruskal-mst/81192/3 "2022-05-17T12:28:51Z")

</div>

> [@gdalle](#):
>
> `SimpleWeightedEdge`

Spectacular. I’m not sure I would have understand the syntax even looking at the REPL. But this definitely will help for future issues. Thanks!

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 17, 2022, 12:30pm UTC](https://discourse.julialang.org/t/how-to-extract-weights-from-simpleweightededge-from-kruskal-mst/81192/4 "2022-05-17T12:30:39Z")

</div>

Glad that it solved your problem, don’t hesitate to mark the answer as a solution so that others know they don’t need to pile on 😉  
I deduced the solution by looking at the fields of the `SimpleWeightedEdge` type, which can be accessed with the `object.field` syntax. The help mode of the REPL, which I used to do that, can be accessed by typing `?` as the first character

---

<div class="post-metadata">

### Author: ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)
#### Post date: [May 17, 2022, 12:35pm UTC](https://discourse.julialang.org/t/how-to-extract-weights-from-simpleweightededge-from-kruskal-mst/81192/5 "2022-05-17T12:35:26Z")

</div>

It’s generally considered bad practice to access internal fields directly, you should prefer the accessor `weight(e::AbstractSimpleWeightedEdge)`. The documentation can probably be improved in this regard.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 18, 2022, 7:19am UTC](https://discourse.julialang.org/t/how-to-extract-weights-from-simpleweightededge-from-kruskal-mst/81192/6 "2022-05-18T07:19:34Z")

</div>

Actually @JedPhillips maybe it would be better to mark the answer by @etienne_dg as the right solution
