2022-02-17 03:32:19 -06:00

54 lines
1.3 KiB
Plaintext

katz.cent <- function(A, alpha=NULL, beta=NULL){ #NULL sets the default value
g <- A
if (class(A) == 'igraph'){
#Error checking. Turn into adj matrix.
A <- get.adjacency(A)
}
lam.dom <- eigen(A)$values[1] #dom eigenvec
if (is.null(alpha)){
alpha <- 0.9 * (1/lam.dom) #Set alpha to 90% of max allowed
}
n <- nrow(A)
if (is.null(beta)){
beta <- matrix(rep(1/n, n),ncol=1)
}
#Katz scores
scores <- solve(diag(n) - alpha*A,beta)
names(scores) <- V(g)$name
return(scores)
}
sg.katz <- function(A, alpha=NULL, beta=NULL){
g <- A
if (class(A) == 'igraph'){
# Error checking. Turn into adj matrix if needed.
A <- get.adjacency(A)
}
lam.dom <- eigen(A)$values[1] #dom eigenvec
if (is.null(alpha)){
alpha <- 0.9 * (1/lam.dom) #Set alpha to 90% of max allowed
}
A.eigs <- eigen(A)
V <- A.eigs$vectors # where columns are the v_i terms
lams <- A.eigs$values
n <- length(lams)
# Create subfunction to compute centrality for one node, then use sapply
# for all nodes
subg.node.i <- function(i){sum(V[i,]^2/(1-alpha*lams))}
subg.all <- sapply(1:n, subg.node.i)
# Add names to output
names(subg.all) <- V(g)$name
return(subg.all)
}