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

**URL:** <https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968>\
**Category:** Usage\
**Tags:** R\
**Created:** [13 November 2021 22:52 UTC](https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968 "2021-11-13T22:52:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexey\_Didorenko](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/alexey_didorenko/32/483_2.png) [@Alexey\_Didorenko](https://igraph.discourse.group/u/Alexey_Didorenko)\
**Post date:** [13 November 2021 22:52 UTC](https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968/1 "2021-11-13T22:52:39Z")

</div>

I try to use

```auto
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.](https://stackoverflow.com/q/69964447/)

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [14 November 2021 12:03 UTC](https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968/2 "2021-11-14T12:03:21Z")

</div>

For reference, the relevant issue is here:

> <https://github.com/igraph/igraph/issues/183>
>
> A -- B
> A -- C
> B -- C
> A -- D
> B -- D
> A -- A
> 
> The result for A should be 3, but igr…aph counts A itself twice, because it appears twice in the neighbor list.
> 
> \---
> 
> Imported from Launchpad using lp2gh.
> \- date created: 2010-04-08T07:56:45Z
> \- owner: gabor.csardi
> \- assignee: gabor.csardi
> \- the launchpad url was https://bugs.launchpad.net/bugs/557903

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [14 November 2021 16:25 UTC](https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968/3 "2021-11-14T16:25:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Alexey\_Didorenko](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/alexey_didorenko/32/483_2.png) [@Alexey\_Didorenko](https://igraph.discourse.group/u/Alexey_Didorenko)\
**Post date:** [14 November 2021 17:36 UTC](https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968/4 "2021-11-14T17:36:31Z")

</div>

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

```auto
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)

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [14 November 2021 21:05 UTC](https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968/5 "2021-11-14T21:05:09Z")

</div>

> [@Alexey\_Didorenko](#):
>
> I get non-simple graph with loops.

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`?

---

<div class="post-metadata">

**Author:** ![Alexey\_Didorenko](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/alexey_didorenko/32/483_2.png) [@Alexey\_Didorenko](https://igraph.discourse.group/u/Alexey_Didorenko)\
**Post date:** [15 November 2021 14:22 UTC](https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968/6 "2021-11-15T14:22:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [15 November 2021 15:32 UTC](https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968/7 "2021-11-15T15:32:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alexey\_Didorenko](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/alexey_didorenko/32/483_2.png) [@Alexey\_Didorenko](https://igraph.discourse.group/u/Alexey_Didorenko)\
**Post date:** [15 November 2021 16:15 UTC](https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968/8 "2021-11-15T16:15:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [30 October 2023 17:00 UTC](https://igraph.discourse.group/t/how-can-i-calculate-knn-k-of-not-simple-graph/968/9 "2023-10-30T17:00:04Z")

</div>

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