# Digraph edges notation

**URL:** <https://forum.elixirforum.com/t/digraph-edges-notation/60647>\
**Category:** Questions\
**Tags:** digraph\
**Created:** [December 30, 2023, 5:48pm UTC](https://forum.elixirforum.com/t/digraph-edges-notation/60647 "2023-12-30T17:48:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevensonmt](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/32/20503_2.png) [@stevensonmt](https://forum.elixirforum.com/u/stevensonmt)\
**Post date:** [December 30, 2023, 5:48pm UTC](https://forum.elixirforum.com/t/digraph-edges-notation/60647/1 "2023-12-30T17:48:13Z")

</div>

When you use the `:digraph` module and want to get a list of all edges, one would expect to see a list of vertex pairs, ideally with the “emanate from” / “incident to” order consistent. So something like `[{:a, :b}, {:b, :c}, {:c, :b}]` for a graph with vertices `[:a, :b, :c]` where `:a` has an edge incident to `:b` and `:b` and `:c` have a bidirectional edge. Instead you might see something inscrutable like `[[:"$e" | 0], [:"$e" | 1], [:"$e" | 2]]`. How are you supposed to interpret that list?

---

<div class="post-metadata">

**Author:** ![al2o3cr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/32/3457_2.png) [@al2o3cr](https://forum.elixirforum.com/u/al2o3cr)\
**Post date:** [December 30, 2023, 6:13pm UTC](https://forum.elixirforum.com/t/digraph-edges-notation/60647/2 "2023-12-30T18:13:24Z")

</div>

Definitely an odd design choice - you could get something closer to what you’re looking for by mapping `:digraph.edge` over the output of `:digraph.edges`, but that’s a lot of round-trips to ETS to get what could have been one call to `:ets.select` 🤔

[https://github.com/erlang/otp/blob/e78684732252a5b2b33de90adaf8f7ff80eb5a3c/lib/stdlib/src/digraph.erl#L307-L334](https://github.com/erlang/otp/blob/e78684732252a5b2b33de90adaf8f7ff80eb5a3c/lib/stdlib/src/digraph.erl#L307-L334)

---

<div class="post-metadata">

**Author:** ![bjorng](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bjorng/32/13187_2.png) [@bjorng](https://forum.elixirforum.com/u/bjorng)\
**Post date:** [December 31, 2023, 6:31am UTC](https://forum.elixirforum.com/t/digraph-edges-notation/60647/3 "2023-12-31T06:31:56Z")

</div>

> [@al2o3cr](#):
>
> you could get something closer to what you’re looking for by mapping `:digraph.edge` over the output of `:digraph.edges`

Alternatively, you could specify you our own edge names when creating the edges. As long as there is only a single edge in each direction between each pair of vertices, the following should work:

```elixir
:digraph.add_edge(g, {from, to}, from, to, [])

```

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/32/20503_2.png) [@stevensonmt](https://forum.elixirforum.com/u/stevensonmt)\
**Post date:** [December 31, 2023, 6:46am UTC](https://forum.elixirforum.com/t/digraph-edges-notation/60647/4 "2023-12-31T06:46:47Z")

</div>

So that’s brilliant. I’m curious why that’s not the default behavior of `add_edge/3` though? Implicitly naming an edge with `{from, to}` has to be more intuitive than whatever `[:"$e" | N]` is meant to convey.

I also think it’s difficult to understand that this is possible from the docs for `add_edge/5` because the `edge()` type is not documented. It’s also confusing to have edge “names” differ from edge “labels” and not document the “names” anywhere.

---

<div class="post-metadata">

**Author:** ![jhogberg](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jhogberg/32/24176_2.png) [@jhogberg](https://forum.elixirforum.com/u/jhogberg)\
**Post date:** [December 31, 2023, 11:12am UTC](https://forum.elixirforum.com/t/digraph-edges-notation/60647/5 "2023-12-31T11:12:56Z")

</div>

The default edge names aren’t meant to convey anything: not giving them a name implies that you don’t care what they’re called.

The funny-looking names were most likely chosen because they’re more compact in memory and are unlikely to clash with user-named edges.

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/32/20503_2.png) [@stevensonmt](https://forum.elixirforum.com/u/stevensonmt)\
**Post date:** [December 31, 2023, 3:48pm UTC](https://forum.elixirforum.com/t/digraph-edges-notation/60647/6 "2023-12-31T15:48:38Z")

</div>

Submitted pull request to hopefully clarify docs a bit.

---

<div class="post-metadata">

**Author:** ![bjorng](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bjorng/32/13187_2.png) [@bjorng](https://forum.elixirforum.com/u/bjorng)\
**Post date:** [January 1, 2024, 7:28am UTC](https://forum.elixirforum.com/t/digraph-edges-notation/60647/7 "2024-01-01T07:28:12Z")

</div>

> [@stevensonmt](#):
>
> I’m curious why that’s not the default behavior of `add_edge/3` though? Implicitly naming an edge with `{from, to}` has to be more intuitive than whatever `[:"$e" | N]` is meant to convey.

Using the `{from, to}` naming scheme will not work in general, because the `:digraph` module supports having any number of distinct edges between each pair of vertices.

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/32/20503_2.png) [@stevensonmt](https://forum.elixirforum.com/u/stevensonmt)\
**Post date:** [January 1, 2024, 3:03pm UTC](https://forum.elixirforum.com/t/digraph-edges-notation/60647/8 "2024-01-01T15:03:19Z")

</div>

> [@bjorng](#):
>
> having any number of distinct edges between each pair of vertices.

What is the utility of that feature? Is it to allow using labels to sort of classify connections in different schemas or something? Something like a graph of cars that includes vertices

```elixir
v1 = {:toyota, :yellow}
v2 = {:toyota, :red}
v3 = {:ford, :yellow}
v4 = {:also_ford, :yellow}

```

Could have edges `{v1, v2} label: :toyota, {v1, v3} label: :yellow, {v3, v4} label: :yellow, {v3, v4} label: :ford`?

---

<div class="post-metadata">

**Author:** ![bjorng](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bjorng/32/13187_2.png) [@bjorng](https://forum.elixirforum.com/u/bjorng)\
**Post date:** [January 1, 2024, 3:37pm UTC](https://forum.elixirforum.com/t/digraph-edges-notation/60647/9 "2024-01-01T15:37:38Z")

</div>

> [@stevensonmt](#):
>
> What is the utility of that feature?

I don’t know the original motivation for that feature, but one algorithm that needs it is [Karger’s algorithm](https://en.wikipedia.org/wiki/Karger%27s_algorithm).
