# Where is the underlying implementation code for community\_walktrap() of python\_igraph?

**URL:** <https://igraph.discourse.group/t/where-is-the-underlying-implementation-code-for-community-walktrap-of-python-igraph/1419>\
**Category:** Usage\
**Tags:** Python\
**Created:** [18 November 2022 13:04 UTC](https://igraph.discourse.group/t/where-is-the-underlying-implementation-code-for-community-walktrap-of-python-igraph/1419 "2022-11-18T13:04:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyou\_Hu](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/kyou_hu/32/877_2.png) [@kyou\_Hu](https://igraph.discourse.group/u/kyou_Hu)\
**Post date:** [18 November 2022 13:04 UTC](https://igraph.discourse.group/t/where-is-the-underlying-implementation-code-for-community-walktrap-of-python-igraph/1419/1 "2022-11-18T13:04:11Z")

</div>

When I was using community\_walktrap, there was a big difference between directed graph and undirected graph. I want to see the implementation code. Where can I see it?

---

<div class="post-metadata">

**Author:** ![GroteGnoom](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/grotegnoom/32/428_2.png) [@GroteGnoom](https://igraph.discourse.group/u/GroteGnoom)\
**Post date:** [19 November 2022 11:56 UTC](https://igraph.discourse.group/t/where-is-the-underlying-implementation-code-for-community-walktrap-of-python-igraph/1419/2 "2022-11-19T11:56:24Z")

</div>

I think this piece of python code

> <https://github.com/igraph/python-igraph/blob/0.10.2/src/igraph/community.py#L368>

calls this C++ code:

> <https://github.com/igraph/igraph/blob/0.10.2/src/community/walktrap/walktrap.cpp#L125>

Those links point to the latest releases, so it could be your version is different.

If you give us more information, maybe we can help you better, like what your expectations and outcomes were.

---

<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:** [19 November 2022 14:48 UTC](https://igraph.discourse.group/t/where-is-the-underlying-implementation-code-for-community-walktrap-of-python-igraph/1419/3 "2022-11-19T14:48:42Z")

</div>

`community_walktrap()` is supposed to ignore edge directions. If you have an example where this is not the case, please minimize it and post it here, so we can look at it.

Note that there are several way to convert a directed graph to undirected. Consider this graph:

 ![image](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/973769ba4d2ff960e877bdec8bf623ef809c3fb5.png)

If you simply ignore edge directions, you get:

 ![image](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/f2dc9dd0acf70ecbe767b0ce0ee3aabf02d24d0e.png)

This is what `community_walktrap()` effectively does, and this is what you get with the `each` mode of the `as_undirected()` method. However, the default mode used by `as_undirected()` is `collapse`, which gives us a very different graph:

 ![image](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/f8ff249277214267f904498438fbdc28e01c5175.png)

This may be the reason why you are getting different results.

Note that `community_walktrap()` effectively adds up the weights of parallel edges, so in an unweighted directed graph reciprocal directed edges end up interpreted as edges with weight 2.

---

<div class="post-metadata">

**Author:** ![kyou\_Hu](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/kyou_hu/32/877_2.png) [@kyou\_Hu](https://igraph.discourse.group/u/kyou_Hu)\
**Post date:** [21 November 2022 13:49 UTC](https://igraph.discourse.group/t/where-is-the-underlying-implementation-code-for-community-walktrap-of-python-igraph/1419/4 "2022-11-21T13:49:18Z")

</div>

I will read it carefully. Thank you very much for your help.

---

<div class="post-metadata">

**Author:** ![kyou\_Hu](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/kyou_hu/32/877_2.png) [@kyou\_Hu](https://igraph.discourse.group/u/kyou_Hu)\
**Post date:** [21 November 2022 14:19 UTC](https://igraph.discourse.group/t/where-is-the-underlying-implementation-code-for-community-walktrap-of-python-igraph/1419/5 "2022-11-21T14:19:14Z")

</div>

Thank you very much for your answer.

I use the same set of data, like [1, 2], [2, 3]… There may also be duplicate edges, but I’m not going to worry about weights for now.

The graph was constructed using `nx.Digraph()` and `nx.graph()` respectively,

```auto
G = nx.DiGraph(); G.add_edge(1, 2); G.add_edge(2, 3); ...
G = nx.Graph(); G.add_edge(1, 2); G.add_edge(2, 3); ...

```

converted to a “.gml” file and run `community_walktrap(steps=6)` , so my directed graph should look like Fig. 3, but my undirected graph looks like Fig. 3 and the result is quite different.

In addition, when I was testing the data set `nx.les_lesables_graph ()`, converting it to a bidirectional graph and run `community_walktrap(steps=6)` is very different from the undirected graph.

```auto
G = nx.DiGraph(); G.add_edge(1, 2); ...
G = nx.Graph(); G.add_edge(1, 2); G.add_edge(2, 1); ...

```

Looking forward to your prompt reply.

---

<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:** [21 November 2022 14:34 UTC](https://igraph.discourse.group/t/where-is-the-underlying-implementation-code-for-community-walktrap-of-python-igraph/1419/6 "2022-11-21T14:34:22Z")

</div>

Once again, if you have an example where `community_walktrap()` produces a different result for a directed graph than the equivalent undirected one (created using `.as_undirected('each')`), please show it. Follow [the guide here](https://stackoverflow.com/help/minimal-reproducible-example) and make sure that the example is _complete_ and _minimal_, i.e. remove all aspects of the code that are not necessary for demonstrating the issue (e.g. do not use NetworkX, construct the graph directly in igraph).

---

<div class="post-metadata">

**Author:** ![tamas](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/tamas/32/1261_2.png) [@tamas](https://igraph.discourse.group/u/tamas)\
**Post date:** [21 November 2022 19:10 UTC](https://igraph.discourse.group/t/where-is-the-underlying-implementation-code-for-community-walktrap-of-python-igraph/1419/7 "2022-11-21T19:10:22Z")

</div>

> [@szhorvat](#):
>
> e.g. do not use NetworkX, construct the graph directly in igraph

Alternatively, construct the graph in NetworkX, save it in a format that can be read by igraph, and then post us the igraph-related code only, along with the input file. This way we can reproduce the issue without getting NetworkX involved (which is crucial to confirm that any issues that we find are on igraph’s side).

---

<div class="post-metadata">

**Author:** ![kyou\_Hu](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/kyou_hu/32/877_2.png) [@kyou\_Hu](https://igraph.discourse.group/u/kyou_Hu)\
**Post date:** [22 November 2022 06:00 UTC](https://igraph.discourse.group/t/where-is-the-underlying-implementation-code-for-community-walktrap-of-python-igraph/1419/8 "2022-11-22T06:00:34Z")

</div>

I see, the default is just to get rid of the direction and the number of reserved edges, I made a mistake.

Thank you very much!
