# Delete multiple edges with specific property in a metagraph

**URL:** <https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745>\
**Category:** Graphs\
**Tags:** metagraphs, graphs\
**Created:** [February 19, 2022, 1:37pm UTC](https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745 "2022-02-19T13:37:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChristianStehr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christianstehr/32/33980_2.png) [@ChristianStehr](https://discourse.julialang.org/u/ChristianStehr)\
**Post date:** [February 19, 2022, 1:37pm UTC](https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745/1 "2022-02-19T13:37:55Z")

</div>

Hello everyone,

I want to delete multiple edges in a metagraph that have a specific property, here is my mwe:

```julia
using MetaGraphs, Graphs
g = erdos_renyi(1000,5000)
mg = MetaGraph(g)
for i in 1:nv(mg) #give all edges specific property
    for j in neighbors(mg, i)
        set_prop!(mg, Edge(i, j), :age, 5)
    end
end

for i in 1:nv(mg) #remove edges with specific property
    for j in neighbors(mg, i)    
        if get_prop(mg, i, j, :age) <= 5
            rem_edge!(mg, i, j)  
        end
    end
end
ne(mg)

```

This returns around 1000 edges and I always need to repeat the second for-loop 2 times to remove all edges.  
What am I missing?  
Thanks in advance

---

<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:** [February 19, 2022, 1:55pm UTC](https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745/2 "2022-02-19T13:55:58Z")

</div>

Are you visiting each edge two times, or `(i, j)` is distinct from `(j, i)`? This is the only strange thing I see.

---

<div class="post-metadata">

**Author:** ![ChristianStehr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christianstehr/32/33980_2.png) [@ChristianStehr](https://discourse.julialang.org/u/ChristianStehr)\
**Post date:** [February 19, 2022, 2:01pm UTC](https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745/3 "2022-02-19T14:01:18Z")

</div>

Adding `rem_edge!(mg, j, i)` in addition to `rem_edge!(mg, i, j)` to the for-loop doesn’t change anything, if this is what you mean

---

<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:** [February 19, 2022, 2:04pm UTC](https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745/4 "2022-02-19T14:04:37Z")

</div>

No, it shouldn’t. You are already visiting both `(i, j)` and `(j, i)`, because you visit all nodes, and from every node you visit all neighbors, so you visit `(1, 3)` when your node is `1` and `(3, 1)` when your node is `3`. This is what I was meaning.

---

<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:** [February 19, 2022, 2:18pm UTC](https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745/5 "2022-02-19T14:18:03Z")

</div>

Truly baffling.

```julia
using MetaGraphs, Graphs

let
g = erdos_renyi(1000,5000)#; is_directed = true, seed = 1)

mg = MetaGraph(g)
for i in 1:nv(mg) #give all edges specific property
    for j in neighbors(mg, i)
        set_prop!(mg, Edge(i, j), :age, 5)
    end
end
@show nv(mg)
@show ne(mg)

qt_removed = 0
for i in 1:nv(mg) #remove edges with specific property
    for j in neighbors(mg, i)
        if get_prop(mg, i, j, :age) <= 5
						qt_removed += 1
            rem_edge!(mg, i, j)
        end
    end
end

@show nv(mg)
@show ne(mg)
@show qt_removed
@show ne(mg) + qt_removed

for i in 1:nv(mg) #remove edges with specific property
    for j in neighbors(mg, i)
        if get_prop(mg, i, j, :age) <= 5
						qt_removed += 1
            rem_edge!(mg, i, j)
        end
    end
end

@show nv(mg)
@show ne(mg)
@show qt_removed
@show ne(mg) + qt_removed
end

```

outputs

```julia
nv(mg) = 1000
ne(mg) = 5000
nv(mg) = 1000
ne(mg) = 1020
qt_removed = 3980
ne(mg) + qt_removed = 5000
nv(mg) = 1000
ne(mg) = 81
qt_removed = 4919
ne(mg) + qt_removed = 5000
5000

```

---

<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:** [February 19, 2022, 2:24pm UTC](https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745/6 "2022-02-19T14:24:33Z")

</div>

Weird, you should report an issue…

---

<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:** [February 19, 2022, 2:30pm UTC](https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745/7 "2022-02-19T14:30:42Z")

</div>

Or maybe this is due to modifying the graph while iterating through neighbors…  
**Edit:** Yep, this is it, you can maybe store the edges you want to delete in each pass through a neighborhood, then delete all the edges at once.

---

<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:** [February 19, 2022, 2:34pm UTC](https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745/8 "2022-02-19T14:34:15Z")

</div>

Yes, I think this is the best line of investigation. I do not know how the graph is stored, my basic mental model of representation did not have this problem, but maybe if it is highly optimized it does not support this.

---

<div class="post-metadata">

**Author:** ![ChristianStehr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christianstehr/32/33980_2.png) [@ChristianStehr](https://discourse.julialang.org/u/ChristianStehr)\
**Post date:** [February 19, 2022, 2:53pm UTC](https://discourse.julialang.org/t/delete-multiple-edges-with-specific-property-in-a-metagraph/76745/9 "2022-02-19T14:53:34Z")

</div>

That does the trick. If I store all edges with the specific property in an edgelist and then remove all edges of that list, it only takes a single for-loop and works as expected.

Thank you both for your fast replies!
