'How to get the vertex with the highest degree centrality in igraph

I want to get the vertex with the highest degree centrality using igraph.

I tried the following code, but it doesn't return what I am expecting:

V(g)[degree(g,V(g)$id) == which.max(degree(g))]


Solution 1:[1]

Try

library(igraph)
set.seed(5)
g <- ba.game(100)
V(g)$id <- paste0("id", 1:100)
(idx <- which(degree(g)==max(degree(g))) )
V(g)$id[idx]
# [1] "id1" "id2"

# optional plot
cols <- rep("blue", vcount(g)); cols[idx] <- "red"; plot(g, vertex.shape="none", vertex.label.color=cols, edge.arrow.size=.5)

Solution 2:[2]

V(g)$degree <- degree(g)

which.max(V(g)$degree)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lukeA
Solution 2 desertnaut