How can I calculate knn(k) of not simple graph?

I try to use

knn(graph)$knnk

where “graph” is not simple graph. But igraph out error message.
How can I avoid simplifying of my graph and get knnk of it?


Cross-posted to StackOverflow.

For reference, the relevant issue is here:

I see you also posted the question on StackOverflow. This is fine, but please do cross-link your posts so that people won’t waste time answering an already-answered question (one of the cross-posts is bound to get an answer before the other).

Since you want to compute this for a non-simple graph, a few questions:

  • In what way is your graph non-simple? Does it have self-loops, multi-edges, or both?

  • Is your graph weighted?

  • How do you propose defining this measure for multigraphs? What result to you expect for the following graph?

    How about the following?

    What would you say the neighbour list of vertex 2 is here? Is it (1, 3), (1, 2, 3), (1, 1, 2, 3), (1, 1, 2, 2, 3)? All these interpretations are reasonable, and all of them give different results.

    This is meant to illustrate that before you go ahead with implementation the calculation of this measure, it is important to be clear about how we understand it.

Initially, I have undirected simple graph, but after using:

rewire(as.undirected(graph), with = keeping_degseq(loops = TRUE, niter=100000000))

I get non-simple graph with loops. So, I need to combine plots of original graph’s knn(k) and rewired graph’ knn(k).
(All graphs are not weighted)

Yes, this is because you explicitly allowed loop creation using loops = TRUE. Can you explain why you are doing this if the starting graph is simple? What is your goal with rewire?

My starting graph is Disassortative, but it is Structural Disassortativity. I use rewire() function for Degree Preserving Randomization for free of Structural Disassortativity.

In that case, would it not make more sense to keep the graph loop-free, as it was originally, i.e. use rewire with loops = FALSE?

Then you will not need to think about how to define the average neighbour degree in loopy graphs.

So, I just have rewired graph loop-free (use loop=FALSE early).
If that’s enough for free of structural disassortativity, then my problem is solved. Thank you.

FYI, k_{nn}(k) for non-simple graphs will be supported in the upcoming 0.10.8 version of the C core.