# How to have condition on SimpleWeightedDiGraph function?

**URL:** <https://discourse.julialang.org/t/how-to-have-condition-on-simpleweighteddigraph-function/86735>\
**Category:** Graphs\
**Tags:** question, graphs\
**Created:** [September 3, 2022, 4:47pm UTC](https://discourse.julialang.org/t/how-to-have-condition-on-simpleweighteddigraph-function/86735 "2022-09-03T16:47:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_Green\_Leave](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@A\_Green\_Leave](https://discourse.julialang.org/u/A_Green_Leave)\
**Post date:** [September 3, 2022, 4:47pm UTC](https://discourse.julialang.org/t/how-to-have-condition-on-simpleweighteddigraph-function/86735/1 "2022-09-03T16:47:38Z")

</div>

Hi Guys

According to the documentation the way `SimpleWeightedDiGraph` works is something like a `g = SimpleWeightedDiGraph(heads, tails, weights)`. If so and if having a three list of requirements:

```julia
heads = [62, 17, 15, 22, 19,14,61, 21,51,35]
tails = [62, 57, 29, 20, 19, 31,61,13,51,35]
weights = [309, 310, 307,308,302,316,321,301,302]

```

Is it possible when creating graph, directly exclude those edges that has same head and tails (eliminating loops) ?

And it seems that when we create `g` with graph package it starts at `1` regardless of the smallest node ID. For instance, in this simple example, if we call ` vertices(g)` it return `1:62`. This creates lots of single nodes that has degree of zero and makes my computations difficult when I want to work with node IDs that starts at 1000. I mean I have 1000 nodes that are empty and makes my graph very spars. When I call density on `g` it shows a very small number although my graph should be very dense. Is there any way to force `SimpleWeightedDiGraph` to not start at `1` but at the smallest node ID?

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:** [September 3, 2022, 9:33pm UTC](https://discourse.julialang.org/t/how-to-have-condition-on-simpleweighteddigraph-function/86735/2 "2022-09-03T21:33:27Z")

</div>

Hi,  
The best answer to both of your questions at the moment is to take care of it manually outside of the graph structure.  
For self-edges, it is not hard to scan heads and tails and exclude pairs where both are identical:

```julia
new_heads = [h for (h, t, w) in zip(heads, tails, weights) if h != t]
new_tails = [t for (h, t, w) in zip(heads, tails, weights) if h != t]
new_weights = [w for (h, t, w) in zip(heads, tails, weights) if h != t]

```

Regarding vertex indices, the convention in the Graphs.jl ecosystem is that vertices should go from 1 to n. If you want to circumvent that, you can either

- Translate the indices manually by keeping a dict outside of the graph
- Use a more sophisticated structure for graphs with metadata, such as [MetaGraphsNext.jl](https://github.com/JuliaGraphs/MetaGraphsNext.jl)

---

<div class="post-metadata">

**Author:** ![A\_Green\_Leave](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@A\_Green\_Leave](https://discourse.julialang.org/u/A_Green_Leave)\
**Post date:** [September 3, 2022, 9:57pm UTC](https://discourse.julialang.org/t/how-to-have-condition-on-simpleweighteddigraph-function/86735/3 "2022-09-03T21:57:05Z")

</div>

Thanks @gdalle for the info 🙂

---

<div class="post-metadata">

**Author:** ![A\_Green\_Leave](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@A\_Green\_Leave](https://discourse.julialang.org/u/A_Green_Leave)\
**Post date:** [September 5, 2022, 4:55pm UTC](https://discourse.julialang.org/t/how-to-have-condition-on-simpleweighteddigraph-function/86735/4 "2022-09-05T16:55:17Z")

</div>

Hi again @gdalle I just wanted to ask what do you suggest for translating indices?  
The way I did it is in spreadsheet. But is there any direct way in julia to change IDs while keeping the relationship between nodes (how they form edges)?

---

<div class="post-metadata">

**Author:** ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)\
**Post date:** [September 5, 2022, 5:07pm UTC](https://discourse.julialang.org/t/how-to-have-condition-on-simpleweighteddigraph-function/86735/5 "2022-09-05T17:07:35Z")

</div>

For example, you have 15 nodes and suppose that we want to encode them.

```julia
node_id = union(heads, tails) |> unique! |> sort!
encode = Dict(node_id .=> 1:15)

replace!(heads, encode...)
replace!(tails, encode...)

```

and then just duplicate of @gdalle, we get below.

```julia
julia> new_heads = [h for (h, t, w) in zip(heads, tails, weights) if h != t]
5-element Vector{Int64}:
 4
 3
 8
 2
 7

julia> new_tails = [t for (h, t, w) in zip(heads, tails, weights) if h != t]
5-element Vector{Int64}:
 13
  9
  6
 10
  1

julia> new_weights = [w for (h, t, w) in zip(heads, tails, weights) if h != t]
5-element Vector{Int64}:
 310
 307
 308
 316
 301

```

---

<div class="post-metadata">

**Author:** ![A\_Green\_Leave](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@A\_Green\_Leave](https://discourse.julialang.org/u/A_Green_Leave)\
**Post date:** [September 5, 2022, 5:12pm UTC](https://discourse.julialang.org/t/how-to-have-condition-on-simpleweighteddigraph-function/86735/6 "2022-09-05T17:12:13Z")

</div>

Thanks @rmsmsgood very much!

---

<div class="post-metadata">

**Author:** ![A\_Green\_Leave](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@A\_Green\_Leave](https://discourse.julialang.org/u/A_Green_Leave)\
**Post date:** [September 5, 2022, 6:43pm UTC](https://discourse.julialang.org/t/how-to-have-condition-on-simpleweighteddigraph-function/86735/7 "2022-09-05T18:43:24Z")

</div>

Is there any command like factorize in julia?  
[Python | Pandas Series.factorize() - GeeksforGeeks](https://www.geeksforgeeks.org/python-pandas-series-factorize/#:~:text=factorize()%20function%20encode%20the,matters%20is%20identifying%20distinct%20values.&text=Parameter%20%3A,labels%20to%20maintain%20the%20relationship).

---

<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:** [September 5, 2022, 7:43pm UTC](https://discourse.julialang.org/t/how-to-have-condition-on-simpleweighteddigraph-function/86735/8 "2022-09-05T19:43:10Z")

</div>

There are packages like CategoricalArrays.jl but that shouldn’t be needed here since all vertex indices and labels must be unique anyway
