# LightGraphs: inconsistency between weighted and non-weighted graphs?

**URL:** <https://discourse.julialang.org/t/lightgraphs-inconsistency-between-weighted-and-non-weighted-graphs/41223>\
**Category:** Graphs\
**Tags:** lightgraphs\
**Created:** [June 11, 2020, 7:22pm UTC](https://discourse.julialang.org/t/lightgraphs-inconsistency-between-weighted-and-non-weighted-graphs/41223 "2020-06-11T19:22:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [June 11, 2020, 7:22pm UTC](https://discourse.julialang.org/t/lightgraphs-inconsistency-between-weighted-and-non-weighted-graphs/41223/1 "2020-06-11T19:22:41Z")

</div>

I want to get the unweighted adjacency matrix from a weighted graph. I thought that `weighs` gives the weighted adjacency matrix and `adjacency_matrix` gives the unweighted adjacency matrix. I found out this is not true:

```julia
gw = SimpleWeightedDiGraph(2)
{2, 0} directed simple Int64 graph with Float64 weights

julia> add_edge!(gw,1,2,3)
true

julia> adjacency_matrix(gw)
2×2 SparseMatrixCSC{Float64,Int64} with 1 stored entry:
  [1, 2] = 3.0

```

Can this be done? Is there a function that will give me the unweighted adjacency matrix?

So I decided to cast my `SimpleWeightedDiGraph` to a `SimpleDiGraph` and then take the adjacency matrix:

```julia
julia> g = SimpleDiGraph(gw)
{2, 1} directed simple Int64 graph

julia> adjacency_matrix(g)
2×2 SparseMatrixCSC{Int64,Int64} with 1 stored entry:
  [2, 1] = 1

```

For some reason this gives me the transposed adjacency matrix! What is happening here? Is this how this should work?

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 11, 2020, 7:29pm UTC](https://discourse.julialang.org/t/lightgraphs-inconsistency-between-weighted-and-non-weighted-graphs/41223/2 "2020-06-11T19:29:30Z")

</div>

> Is there a function that will give me the unweighted adjacency matrix?

You can get the unweighted (Float64 ones) adjacency matrix just by coercing the weighted AM to 1s:

```julia
am = min.(adjacency_matrix(gw), 1.0)

```

> For some reason this gives me the transposed adjacency matrix! What is happening here? Is this how this should work?

This is probably a bug. ~~We’ve done some (in retrospect) silly things in an effort to improve performance of AM generation and this looks indicative of one of those things.~~

The bug is in the construction of the DiGraph from the SimpleWeightedDiGraph:

```julia
julia> zz = DiGraph(gw)
{2, 1} directed simple Int64 graph

julia> collect(edges(zz))
1-element Array{LightGraphs.SimpleGraphs.SimpleEdge{Int64},1}:
 Edge 2 => 1

```

---

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [June 11, 2020, 7:31pm UTC](https://discourse.julialang.org/t/lightgraphs-inconsistency-between-weighted-and-non-weighted-graphs/41223/3 "2020-06-11T19:31:50Z")

</div>

The workaround you propose assumes that the weights are larger than 1, which is not always the case for me.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 11, 2020, 7:34pm UTC](https://discourse.julialang.org/t/lightgraphs-inconsistency-between-weighted-and-non-weighted-graphs/41223/4 "2020-06-11T19:34:35Z")

</div>

Good point, but I only have your example to go on. Here’s a more general solution that gives you `Int`s:

```julia
am = (!iszero).(adjacency_matrix(gw))

```

---

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [June 11, 2020, 7:39pm UTC](https://discourse.julialang.org/t/lightgraphs-inconsistency-between-weighted-and-non-weighted-graphs/41223/5 "2020-06-11T19:39:18Z")

</div>

Thanks, that works.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 11, 2020, 7:43pm UTC](https://discourse.julialang.org/t/lightgraphs-inconsistency-between-weighted-and-non-weighted-graphs/41223/6 "2020-06-11T19:43:05Z")

</div>

Awesome. I’ll try to remember to file a bug report for the constructor issue. For now, if you need to do this, just create it using the adjacency matrix:

```julia
dg = DiGraph(adjacency_matrix(gw))

```

---

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [June 11, 2020, 8:08pm UTC](https://discourse.julialang.org/t/lightgraphs-inconsistency-between-weighted-and-non-weighted-graphs/41223/7 "2020-06-11T20:08:44Z")

</div>

Out of curiosity, is there any particular reason it was decided that `adjacency_matrix` and `weights` should return the same matrix? I mean what is the point of `weights` if it was decided that `adjacency_matrix` will return the weighted adjacency matrix?

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 11, 2020, 8:46pm UTC](https://discourse.julialang.org/t/lightgraphs-inconsistency-between-weighted-and-non-weighted-graphs/41223/8 "2020-06-11T20:46:30Z")

</div>

In the case of SimpleWeightedGraphs, it was primarily because this is an O(1) operation that does not allocate, and it met the needs of the author/user at the time.

> I mean what is the point of `weights` if it was decided that `adjacency_matrix` will return the weighted adjacency matrix?

I’m not sure that the fact that two functions return the same result in a particular implementation of a graph structure is a valid criticism. For SimpleGraphs `1:nv(g)` and `vertices(g)` return the same thing, but we encourage their use in different scenarios. `weights` is not necessarily guaranteed to return a “real” matrix (look at `DefaultDistance` in SimpleGraphs for the example); just something that can be indexed into to retrieve a weight value.
