Computing loops using count_multiple

I have been trying to count the number of loops using count_multiple function for a particular vertex. But, I seem to get an error. The adjacency matrix is correct, so n_loop for the edges (0,0) should be 3. But, I get the answer to be 1. See the code below. Not sure what is going on.
Would appreciate comments. Thx, Sid

g = Graph([(0,1)],directed=True)
g.add_edges([(0,0)])
g.add_edges([(0,0)])
g.add_edges([(0,0)])
n_loop=g.count_multiple((0,0))
print(g.get_adjacency())
print("n_loop =", n_loop)

Output:

[[3, 1]
 [0, 0]]
n_loop = 1

@tamas I don’t get this either. If the behaviour is as expected, the docs could be clarified as bit …

OK, so it turns out that this comes from the C interface (not specific to Python as I assumed, sorry @tamas). As far as I’m concerned, it’s a bug in the handling in directed graphs. See what we get in R:

> g<-make_graph(c(1,2, 1,1, 1,1, 1,1))
> count_multiple(g)
[1] 1.0 1.5 1.5 1.5

Non-integer counts??

And here’s this:

Apparently the Python interface rounds to integers, which is why we don’t see the 1.5.

I’ll submit a PR soon.

@sid Thanks for bringing this to our attention!

@tamas This really shows why this was a useful doc update: https://github.com/igraph/igraph/commit/01a13a720ef69f2446e14a7f3ac81e3c4f983d5d

Thank you. While you are filing your PR, here is my temporary workaround for counting loops of a vertex v_id.:
n_loop=(g.degree(v_id,loops=True,mode=ALL)-g.degree(v_id,loops=False,mode=ALL))/2

For the record, this issue is now fixed in master by the PR kindly submitted by @szhorvat

Thanks Tamas: Do I need to reinstall igraph package to get the update? SId

Unfortunately you would need to recompile the entire R interface from scratch, from the codebase hosted on Github. Not sure if there is an easy way for that (@Gabor?). Alternatively, you should wait for the next release and use the workaround until then.

@tamas, Sid is using the Python interface. Could we maybe just update the submodule there and provide instructions for installing the development version?

Ah, sorry, I missed that. I have updated the python-igraph repo on GIthub. If the CI tests pass, then in theory one could install the patched version of python-igraph from Git as follows:

pip install git+https://github.com/igraph/python-igraph@master#egg=python-igraph

But this still needs a working C compiler and whatnot on the user’s machine.