# Creating a Weighted Graph

**URL:** <https://discourse.julialang.org/t/creating-a-weighted-graph/41241>\
**Category:** Graphs\
**Tags:** lightgraphs, graphs\
**Created:** [June 12, 2020, 3:44am UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241 "2020-06-12T03:44:10Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [June 13, 2020, 11:14am UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/21 "2020-06-13T11:14:01Z")

</div>

That is great, that I am in the ballpark. So I am stopping the optimization process, although it might be possible to decrease memory allocation further with more sophisticated datastructures at the outset. A graph, even with 20 million edges should not tax my memory significantly. Your help is much appreciated, @anon94023334!

---

<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 13, 2020, 4:26pm UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/22 "2020-06-13T16:26:19Z")

</div>

A graph with 1 million nodes and 20 million edges weighted by `Float64`s is trivially represented by a SimpleWeightedGraph in ~ 618 MB. If you squash the vertex indices down to `UInt32` (using `squash()` on the SimpleGraph prior to creating the SWG), you get that down to 462 MB. If you restrict your weights to `Float32`, it’s 309MB.

So whatever’s happening in your data is likely not due directly to the storage that the graph is requiring.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 13, 2020, 6:38pm UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/23 "2020-06-13T18:38:45Z")

</div>

> [@anon94023334](#):
>
> Please don’t access internal fields of any JuliaGraphs structs. We can’t prevent it because there’s no such thing as access control in Julia, but it’s undefined behavior and your code is pretty much guaranteed to break on you at some point without warning if you do this.

I just wanna point two things:

1. [You (kinda) can have private fields in Julia if you really want.](https://discourse.julialang.org/t/private-properties-in-julia-0-7/10785/5)
2. Undefined behavior is a term often used by a language specification for code that should not be written because the language definition does not specify what to do in such case and, consequently, each compiler implementation can act differently, possibly with segmentation fault or silently corrupting data. Accessing such fields is probably not undefined behavior, with a specific version of Julia (language, not compiler implementation) and a specific version of the package in question (with source code available), it will behave in a predictably fashion, what is problematic is that you violated the package API and any version change can break the code (because the implementation detail you are accessing can change with minor changes), but if you know what you are doing and have no problem with fixing the package version, it can be a solution (while very restrictive).

---

<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 14, 2020, 12:42am UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/24 "2020-06-14T00:42:04Z")

</div>

That’s very cool. I think it’s a bit too magical right now, but if this turns out to be a bigger issue than it’s been to date perhaps we do it.

You’re right re: undefined behavior vs API contract. I should have chosen my words more carefully but the bottom line still is: don’t do this if you expect your code to work 🙂

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 14, 2020, 6:23am UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/25 "2020-06-14T06:23:18Z")

</div>

> [@anon94023334](#):
>
> undefined behavior vs API contract

“Consequences are undefined” is a very [common way of phrasing this in software documentation](http://www.lispworks.com/documentation/HyperSpec/Body/01_db.htm). In a library, it is implied that they are undefined _by the API_. So I think your way of saying it is fine.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 14, 2020, 3:51pm UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/26 "2020-06-14T15:51:14Z")

</div>

Fair enough. However, given any Julia compiler following the language specification and the internals of some open source valid code, you can predict its behavior, with undefined behavior the compiler implementation is free to not even be deterministic/consistent in the behavior.

Also, unless I am missing something, the “software documentation” you linked is a language specification, so…

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 14, 2020, 5:42pm UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/27 "2020-06-14T17:42:57Z")

</div>

> [@Henrique\_Becker](#):
>
> However, given any Julia compiler following the language specification and the internals of some open source valid code, you can predict its behavior

Sure, but that’s not the relevant question: given that you are talking about computers, you can pretty much always predict everything (except for random seeds, etc).

The key question is what behavior you can predict

1. given a Julia version,
2. package versions (mostly major, but minor if you are relying on that),
3. the API specs of packages which conform to SemVer.

So basically your `Project.toml`, including the `[compat]` bounds.

Version 10.3.1 of HypotheticalPackage.jl can define

```julia
struct Foo{T,S}
    a::T
    b::S
end

```

with a given interface, which _does not_ include accessing fields.

Then, if you are accessing fields, and version 10.3.2 is refactored so that the API is unchanged but the fields of `Foo` are named something else, you are in the “consequences are undefined” situation. It will mostly likely just throw a error in current Julia (so in that sense, it is predictable), it’s just that the HypotheticalPackage.jl API does not define/care what happens.

> [@Henrique\_Becker](#):
>
> Also, unless I am missing something, the “software documentation” you linked is a language specification, so…

Yes, you are missing the fact that this is only an example. This kind of language us used pervasively — it is pretty standard.

Finally, given that this is a topic where a package author is kindly giving user support, I am not sure I understand why you started this subdiscussion.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 14, 2020, 6:22pm UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/28 "2020-06-14T18:22:23Z")

</div>

I understand your point while, in my experience, I have only seen the exact term _undefined behavior_ in the context of language specifications.

But this bit:

> Finally, given that this is a topic where a package author is kindly giving user support, I am not sure I understand why you started this subdiscussion.

This is just hypocritical. I can as well ask why did you continue this subdiscussion. My first comment in this thread (and only comment that does not answer to you) pointed two things: (1) that you kinda can have private fields, that is a knowledge sbromberger did not had and found positive to know (so I aggregated something to the discussion); (2) criticized the term _undefined behavior_, in which sbromberger agreed with me. The discussion could have ended here, but was you that found relevant enough to dispute my terminology here. If you are now second-guessing the usefulness of this subdiscussion that was effectively started by you, then you can delete your posts that I will delete my posts answering you. But I think there is value to my initial reply to sbromberger.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [June 14, 2020, 7:29pm UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/29 "2020-06-14T19:29:40Z")

</div>

Hi Seth,

I think I found a bug in SimpleWeightedGraphs (which does not occur in LightGraphs):  
If I add a self-edge, in this case from node 1 to node 1, no error is thrown, the number of edges is not incremented (I do not know why one would purposefully impose this limitation), and yet, `has_edge` returns true.

```julia
graph = SimpleWeightedGraph(100)
add_edge!(graph, 1,1)
println("ne(graph): ", ne(graph))
println("has_edge(graph, 1, 1): ", has_edge(graph, 1,1))
rem_edge!(graph, 1, 1)
println("ne(graph): ", ne(graph))
println("has_edge(graph, 1, 1): ", has_edge(graph, 1,1))

```

---

<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 15, 2020, 4:54am UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/30 "2020-06-15T04:54:05Z")

</div>

This isn’t a bug; it’s just that SimpleWeightedGraphs doesn’t support self-loops (unlike SimpleGraphs, which “sort of” does - most things will work; some won’t.)

The definition of a simple graph is one with no multi-edges, no self-loops, and no weights, so if you stick to making your graphs confirm to that definition when you use Simple\*Graphs packages, you won’t run into any problems.

---

<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 15, 2020, 4:26pm UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/31 "2020-06-15T16:26:53Z")

</div>

Thinking about how to handle this since we’re coming up on v2.0, it’s worth a discussion as to whether to disallow self-loops entirely in these packages and instead, if self-loops are needed, have an implementation in a separate graph structure.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [June 15, 2020, 4:55pm UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/32 "2020-06-15T16:55:50Z")

</div>

I think that either self-loops should be allowed or they should not be allowed. I have opinion, except that if they are not allowed, a command such as `has_edge(g, i, i)` should always return `false`, contrary to the current implementation of `SimpleWeightedGraph`.

---

<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 16, 2020, 4:24pm UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/33 "2020-06-16T16:24:44Z")

</div>

> a command such as `has_edge(g, i, i)` should always return `false` ,

That’s the way it _used_ to work. We made the change at the request of the JuliaBio community and it worked for them, but I don’t know whether they still need it anymore.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [June 16, 2020, 9:50pm UTC](https://discourse.julialang.org/t/creating-a-weighted-graph/41241/34 "2020-06-16T21:50:46Z")

</div>

@anon94023334, here is what I’d like to do, and perhaps you can give me some advice. I’d like to implement the following. Consider two graphs (G1, E1) and (G2, E2), with the same nodes, but different edges. I would like both graphs to share the node structure but with different edge structures. So each graph would have its own adjacency matrix. Of course I could create two independent graphs. Perhaps that would work. But here is the problem. I want the node to have a state index. If the node is in state “S1”, graph G1 would be activated and only its connections available to the program, and if the node is in state “S2”, graph G2 would be activated and only its connections would be available. This is an example of a multiplex network with two layers. This can be generalized, but this is sufficient. Do you think it is easy to create a package based on LightGraphs that can do this? What do you think?

[Previous page](https://discourse.julialang.org/t/creating-a-weighted-graph/41241.md?page=1)
