# eigen\_centrality for a directed graph (R)

**URL:** <https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925>\
**Category:** Usage\
**Tags:** R\
**Created:** [26 October 2021 12:46 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925 "2021-10-26T12:46:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilmondodieli](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/ilmondodieli/32/611_2.png) [@ilmondodieli](https://igraph.discourse.group/u/ilmondodieli)\
**Post date:** [26 October 2021 12:46 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/1 "2021-10-26T12:46:02Z")

</div>

Hi,

I am using the function eigen\_centrality() applied to a weighted graph. In particular, being w the weighted adjacency matrix, the graph g is defined as: `graph.adjacency(w,mode="directed",weighted=TRUE)` .

Then, I have run the command below:

```auto
eigen_centrality(g)$vector

```

… my question is: what do I really get with this command? If I run `eigen_centrality(g, directed = TRUE, weights = E(g)$weigh)` , I obtain a different result.

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:** [26 October 2021 12:48 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/2 "2021-10-26T12:48:06Z")

</div>

> [@ilmondodieli](#):
>
> I obtain a different result.

Can you show a complete but minimal example that demonstrates the difference?

> [@ilmondodieli](#):
>
> what do I really get with this command?

Regarding how eigencentrality is computed in igraph, you will find some information here as well as in links within:

> [@Documentation of the algorithm of eigen\_centrality](https://igraph.discourse.group/t/documentation-of-the-algorithm-of-eigen-centrality/879):
>
> Hi, I would like to use eigen\_centrality in R with a weighed network, setting weights = TRUE. Which formula / algorithm does eigen\_centrality use? Is there a reference paper? I couldn’t find the exact documentation for weighted networks. Thank you! Natalie

---

<div class="post-metadata">

**Author:** ![ilmondodieli](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/ilmondodieli/32/611_2.png) [@ilmondodieli](https://igraph.discourse.group/u/ilmondodieli)\
**Post date:** [26 October 2021 13:19 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/3 "2021-10-26T13:19:52Z")

</div>

Consider the network with adjacency matrix `n` given by the following matrix:

n =\left( \begin{array}{ccccccccc} 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 \\ 1 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 1 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 \\ \end{array} \right)

and consider the matrix `w = n/rowSums((n)) `. Now if  
`g = graph.adjacency(wnet,mode="directed",weighted=TRUE)`  
and you compute both `eigen_centrality(g)$vector` and `eigen_centrality(g, directed = TRUE, weights = E(g)$weigh)$vector`, you obtain different results.

I know which is the def. of eigenvector centrality, but I wanted to know what the function gives as result.

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [26 October 2021 13:32 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/4 "2021-10-26T13:32:38Z")

</div>

The default for the `directed` argument to `eigen_centrality` is `FALSE`, as you can see in the [documentation](https://igraph.org/r/doc/eigen_centrality.html). In the first call, hence `directed=FALSE` by default, while in the second call, you explicitly set `directed=TRUE`. The full reproducible example is below, showing that both calls produce identical results if you set `directed=TRUE` also in the first call.

```auto
library(igraph)

A = matrix(
    c(0, 1, 1, 1, 1, 1, 1, 0, 0,
      1, 0, 1, 1, 0, 0, 0, 0, 0,
      1, 1, 0, 0, 1, 0, 0, 0, 0,
      1, 1, 0, 0, 0, 1, 0, 0, 0,
      1, 0, 1, 0, 0, 0, 0, 1, 0,
      1, 0, 0, 1, 0, 0, 0, 0, 0,
      1, 0, 0, 0, 0, 0, 0, 0, 1,
      0, 0, 0, 0, 1, 0, 0, 0, 1,
      0, 0, 0, 0, 0, 0, 1, 1, 0),
    nrow=9, ncol=9)

w = A/rowSums((A))
g = graph.adjacency(w,mode="directed",weighted=TRUE)

v1 = eigen_centrality(g, directed = TRUE)$vector
v2 = eigen_centrality(g, directed = TRUE, weights = E(g)$weigh)$vector
print(v1)
print(v2)

```

output:

```auto
> print(v1)
[1] 1.0000000 0.5000000 0.5000000 0.5000000 0.5000000 0.3333333 0.3333333
[8] 0.3333333 0.3333333
> print(v2)
[1] 1.0000000 0.5000000 0.5000000 0.5000000 0.5000000 0.3333333 0.3333333
[8] 0.3333333 0.3333333

```

---

<div class="post-metadata">

**Author:** ![ilmondodieli](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/ilmondodieli/32/611_2.png) [@ilmondodieli](https://igraph.discourse.group/u/ilmondodieli)\
**Post date:** [26 October 2021 13:40 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/5 "2021-10-26T13:40:42Z")

</div>

> [@vtraag](#):
>
> ```auto
> library(igraph)
> 
> A = matrix(
> c(0, 1, 1, 1, 1, 1, 1, 0, 0,
> 1, 0, 1, 1, 0, 0, 0, 0, 0,
> 1, 1, 0, 0, 1, 0, 0, 0, 0,
> 1, 1, 0, 0, 0, 1, 0, 0, 0,
> 1, 0, 1, 0, 0, 0, 0, 1, 0,
> 1, 0, 0, 1, 0, 0, 0, 0, 0,
> 1, 0, 0, 0, 0, 0, 0, 0, 1,
> 0, 0, 0, 0, 1, 0, 0, 0, 1,
> 0, 0, 0, 0, 0, 0, 1, 1, 0),
> nrow=9, ncol=9)
> 
> w = A/rowSums((A))
> g = graph.adjacency(w,mode="directed",weighted=TRUE)
> 
> v1 = eigen_centrality(g, directed = TRUE)$vector
> v2 = eigen_centrality(g, directed = TRUE, weights = E(g)$weigh)$vector
> print(v1)
> print(v2)
> 
> ```

Thank you for the reply. So the result of the call `eigen_centrality(g, directed = TRUE, weights = E(g)$weigh)$vector` takes the weights from the graph.  
And what about the results of `eigen_centrality(g)$vector`? Since the default for the `directed` argument to `eigen_centrality` is `FALSE`, it would return the same of `eigen_centrality(graph.adjacency(adj, mode = "undirected"), directed = TRUE)$vector`. Actually it doesn’t. Where am I wrong?

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [26 October 2021 14:23 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/6 "2021-10-26T14:23:30Z")

</div>

> [@ilmondodieli](#):
>
> So the result of the call `eigen_centrality(g, directed = TRUE, weights = E(g)$weigh)$vector` takes the weights from the graph.

Yes, both `eigen_centrality(g, weights = E(g)$weight)` and `eigen_centrality(g)` use the weights that are provided in the graph. Note that this is only the case if the weights are in the edge attribute named `weight`.

> [@ilmondodieli](#):
>
> Since the default for the `directed` argument to `eigen_centrality` is `FALSE` , it would return the same of `eigen_centrality(graph.adjacency(adj, mode = "undirected"), directed = TRUE)$vector` . Actually it doesn’t. Where am I wrong?

Please note that `graph.adjacency(adj, mode = "undirected")` uses what is the same as `mode="max"`, meaning that it will take as undirected edge weight the maximum of the two entries `adj[i, j]` and `adj[j, i]`. The unnormalised adjacency matrix `A` that you included is symmetric, so then this does not matter much. If you are using the normalised adjacency matrix `w` this does matter of course, since it is no longer symmetric.

However, even then, creating an undirected graph is not the same as creating a directed graph and then treating is as undirected when calculating `eigen_centrality`. Treating a directed graph as undirected in `eigen_centrality` implies that it ignores the direction of the edges. Since edges are directed, there are two edges between each connected pair of nodes for the adjacency matrix you used, both of which have a different weight. Hence, this is different from creating an undirected graph from the start, which will have only one edge, with a different weight.

Concretely, if you do

```auto
g = graph.adjacency(w,mode="directed",weighted=TRUE)

```

you will see `ecount(g)` returning `26`, while if you do

```auto
g = graph.adjacency(w,mode="undirected",weighted=TRUE)

```

you will see `ecount(g)` returning `13`.

---

<div class="post-metadata">

**Author:** ![ilmondodieli](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/ilmondodieli/32/611_2.png) [@ilmondodieli](https://igraph.discourse.group/u/ilmondodieli)\
**Post date:** [26 October 2021 14:46 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/7 "2021-10-26T14:46:45Z")

</div>

So the results are different because are different the two graphs:

> g1 = graph.adjacency(w,mode=“directed”,weighted=TRUE)  
> g2 = graph.adjacency(w,mode=“undirected”,weighted=TRUE)

From the computational point of view, since the `eigen_centrality` computes the eigenvector corresponding to the largest eigenvalue of the “adjacency” matrix of the graph, in these two different cases, what is taken as “adjacency matrix”? In other terms, why would they differ?  
Thank you again

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [26 October 2021 15:17 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/8 "2021-10-26T15:17:47Z")

</div>

The adjacency matrix of the two graphs are also different. You can check that yourself:

```auto
get.adjacency(g1, attr = 'weight')

```

yields

```auto
9 x 9 sparse Matrix of class "dgCMatrix"
                                                                                         
 [1,] . 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 . .  
 [2,] 0.3333333 . 0.3333333 0.3333333 . . . . .  
 [3,] 0.3333333 0.3333333 . . 0.3333333 . . . .  
 [4,] 0.3333333 0.3333333 . . . 0.3333333 . . .  
 [5,] 0.3333333 . 0.3333333 . . . . 0.3333333 .  
 [6,] 0.5000000 . . 0.5000000 . . . . .  
 [7,] 0.5000000 . . . . . . . 0.5
 [8,] . . . . 0.5000000 . . . 0.5
 [9,] . . . . . . 0.5000000 0.5000000 .  

```

while

```auto
get.adjacency(g2, attr = 'weight')

```

yields

```auto
9 x 9 sparse Matrix of class "dgCMatrix"
                                                                       
 [1,] . 0.3333333 0.3333333 0.3333333 0.3333333 0.5 0.5 . .  
 [2,] 0.3333333 . 0.3333333 0.3333333 . . . . .  
 [3,] 0.3333333 0.3333333 . . 0.3333333 . . . .  
 [4,] 0.3333333 0.3333333 . . . 0.5 . . .  
 [5,] 0.3333333 . 0.3333333 . . . . 0.5 .  
 [6,] 0.5000000 . . 0.5000000 . . . . .  
 [7,] 0.5000000 . . . . . . . 0.5
 [8,] . . . . 0.5000000 . . . 0.5
 [9,] . . . . . . 0.5 0.5 .  

```

---

<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:** [26 October 2021 16:13 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/9 "2021-10-26T16:13:43Z")

</div>

Since you’re working with directed graphs, just a word of warning: eigenvector centrality is not really ideal for directed graphs, primarily because most real-world directed networks are not (strongly) connected.

Consider if hub/authority scores, or perhaps PageRank, are a better fit for your application.

---

<div class="post-metadata">

**Author:** ![ilmondodieli](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/ilmondodieli/32/611_2.png) [@ilmondodieli](https://igraph.discourse.group/u/ilmondodieli)\
**Post date:** [27 October 2021 07:24 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/10 "2021-10-27T07:24:48Z")

</div>

Thank you again.  
It comes out what you were saying, i.e. `get.adjacency(g1, attr = 'weight')` gives exatly w (except for the sparse/normal matrix), while `get.adjacency(g2, attr = 'weight')` gives as (i,j)-th entry \max \left\{w\_{ij},w\_{ji}\right\}.

> [@vtraag](#):
>
> Please note that `graph.adjacency(adj, mode = "undirected")` uses what is the same as `mode="max"` , meaning that it will take as undirected edge weight the maximum of the two entries `adj[i, j]` and `adj[j, i]` . The unnormalised adjacency matrix `A` that you included is symmetric, so then this does not matter much. If you are using the normalised adjacency matrix `w` this does matter of course, since it is no longer symmetric.

Thank you

---

<div class="post-metadata">

**Author:** ![astruck](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@astruck](https://igraph.discourse.group/u/astruck)\
**Post date:** [27 August 2022 15:48 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/11 "2022-08-27T15:48:29Z")

</div>

@szhorvat thank you for maintaining igraph and answering questions.

The function `eigen_centrality` contains a

> WARNING: eigen\_centrality will not symmetrize your data before extracting eigenvectors; don’t send this routine asymmetric matrices unless you really mean to do so.

Your warning above about being unsuitable for real-world graphs reads important. Should it be added to the manual (page 159)?

Thanks

---

<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:** [27 August 2022 16:01 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/12 "2022-08-27T16:01:25Z")

</div>

@astruck Can you please give us a short example that triggers this warning? I’m not quite sure where this may be coming from. Which version of igraph are you using?

---

<div class="post-metadata">

**Author:** ![astruck](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@astruck](https://igraph.discourse.group/u/astruck)\
**Post date:** [27 August 2022 16:08 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/13 "2022-08-27T16:08:43Z")

</div>

I’m referring to the igraph manual, published 2022-07-19 for igraph v1.3.4. I do not have triggered the warning mentioned in the manual. Your warning here in this thread about being unsuitable for weakly connected graphs reads important enough to be included in the manual?

---

<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:** [27 August 2022 16:11 UTC](https://igraph.discourse.group/t/eigen-centrality-for-a-directed-graph-r/925/14 "2022-08-27T16:11:36Z")

</div>

I just realized that. Thanks for the clarification. This warning is inaccurate and outdated, so I will remove it. Actually, edge directions are ignored by default in the R interface (see the default of `directed = FALSE`).

I’ll include a note about disconnected graphs.

Thanks for the feedback. Please let us know if you come across anything else that looks fishy. We are aware that the R/igraph documentation could use some editing, but we are short of hands (and short of R experts as well).
