Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/graph_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ end
Base.eltype(::IGraph) = LibIGraph.igraph_int_t
Base.zero(::Type{IGraph}) = IGraph(0)
# Graphs.edges # TODO
function Graphs.edges(g::IGraph)
edge_list = Vector{Graphs.edgetype(g)}()
adjlist = IGAdjList()
LibIGraph.adjlist_init(g,adjlist,LibIGraph.IGRAPH_ALL,LibIGraph.IGRAPH_LOOPS_TWICE,LibIGraph.IGRAPH_MULTIPLE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find! This seems like a pretty expensive allocating operation. I would expect there to be a way to iterate over edges without having to create the adjlist.

I suspect this will be a better way: https://igraph.org/c/html/latest/igraph-Iterators.html#edge-iterators

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and we also need correctness tests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean by correctness tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new feature usually comes with additions to the test suite, so that one can attest that the feature is correctly implemented. More importantly, such tests will be executed for any change in the future, ensuring that the feature is not accidentally broken.

For instance, here you are implementing an edges function. A convenient correctness test would be some self-consistency check. E.g. create a graph by specifically adding some random set of edges. Then call the edges function on that graph and verify that it returns the same set as the initial set you started with. This type of "round trip" consistency checks are a powerful way to detect issues.

for i in Graphs.vertices(g)
neigh = IGVectorInt(Ref(unsafe_load(adjlist.objref[].adjs,i)))
for k in 1:LibIGraph.vector_int_size(neigh)
j = LibIGraph.vector_int_get(neigh,k-1)+1
if i < j
push!(edge_list,Graphs.SimpleGraphs.SimpleEdge(i,j))
end
end
end
return edge_list
end
Graphs.edgetype(g::IGraph) = Graphs.SimpleGraphs.SimpleEdge{eltype(g)} # TODO maybe expose the edge id information from IGraph
Graphs.has_edge(g::IGraph,s,d) = LibIGraph.get_eid(g,s,d,false,false)[1]!=-1
Graphs.has_vertex(g::IGraph,n::Integer) = 1≤n≤Graphs.nv(g)
Expand Down
Loading