Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HELP] how to get edges of a vertex? #151

Closed
lucasmsoares96 opened this issue Jun 29, 2022 · 7 comments
Closed

[HELP] how to get edges of a vertex? #151

lucasmsoares96 opened this issue Jun 29, 2022 · 7 comments

Comments

@lucasmsoares96
Copy link

I need to get all edges that come out of a given vertex. The edges(g) function returns a list of edges for the entire graph. Is there any way to get only the edges of a vertex? For example edges(g,1) to get all edges from vertex 1.

@gdalle
Copy link
Member

gdalle commented Jun 29, 2022

Hi @lucasmsoares96! Could you be more specific as to what you want to do exactly? What kind of graph are you working with?
The function you seek does not exist but you may be able to re-create it manually using outneighbors(g, v), which is documented here.
Note that access to edges is one of the topics we want to address when working on version 2.0 of Graphs.jl, so don't hesitate to pitch in on the other issues discussing it

@lucasmsoares96
Copy link
Author

Hi @gdalle! Thanks for the tip. I solved it as follows:

import Graphs: edges
edges(g::SimpleWeightedDiGraph{Int64, Int64},v::Int64) = [e for e in edges(g) if src(e) == v]

@gdalle
Copy link
Member

gdalle commented Jun 30, 2022

@lucasmsoares96 this works but it highly inefficient, because you have to enumerate every single edge in the whole graph when handling a single vertex. Can I ask what the application is? In particular, do you need to recover edge weights?

@gdalle
Copy link
Member

gdalle commented Jun 30, 2022

Cause I think you could do something better by looking at the implementation of edges for SimpleWeightedDiGraph and taking inspiration from it. Can you try the following to see if it works?

using Graphs, SimpleWeightedGraphs

function outedges(g::AbstractSimpleWeightedGraph, i::Integer)
    return (SimpleWeightedEdge(i, j, g.weights[j, i]) for j in outneighbors(g, i))
end

The inversion between i and j to index g.weights is not a typo

@etiennedeg
Copy link
Member

Currently, the only function in the API that return edges of a given graph is edges. So you only have choice to browse edges, or to use the graph structure to call the right edge constructor (but then you have not a reference to the edge, just a copy). I think this is a fundamental flaw, and it really pushes to implement get_edge(g, u, v) and outedges(g, u) (see #146)

@lucasmsoares96
Copy link
Author

I'm just doing a reconnaissance of the Julia language. Your solution solves the problem more efficiently. I solved it by going through the list of edges because it made more sense for me to actually return the edge than to return a copy.

@gdalle
Copy link
Member

gdalle commented Jul 2, 2022

No worries! I'm closing the issue if that's alright, feel free to reach out if you need more help

@gdalle gdalle closed this as completed Jul 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants