Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions R/centrality.R
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ betweenness.estimate <- estimate_betweenness
#' @aliases betweenness.estimate
#' @aliases edge.betweenness.estimate
#' @param graph The graph to analyze.
#' @param v The vertices for which the vertex betweenness will be calculated.
#' @param vids The vertices for which the vertex betweenness will be calculated.
#' @param directed Logical, whether directed paths should be considered while
#' determining the shortest paths.
#' @param weights Optional positive weight vector for calculating weighted
Expand All @@ -411,7 +411,7 @@ betweenness.estimate <- estimate_betweenness
#' @param cutoff The maximum shortest path length to consider when calculating
#' betweenness. If negative, then there is no such limit.
#' @return A numeric vector with the betweenness score for each vertex in
#' `v` for `betweenness()`.
#' `vids` for `betweenness()`.
#'
#' A numeric vector with the edge betweenness score for each edge in `e`
#' for `edge_betweenness()`.
Expand All @@ -438,15 +438,15 @@ betweenness.estimate <- estimate_betweenness
#'
betweenness <- function(
graph,
v = V(graph),
vids = V(graph),
directed = TRUE,
weights = NULL,
normalized = FALSE,
cutoff = -1
) {
res <- betweenness_cutoff_impl(
graph = graph,
vids = v,
vids = vids,
directed = directed,
weights = weights,
cutoff = cutoff
Expand Down Expand Up @@ -1800,7 +1800,7 @@ harmonic_centrality <- function(

bonpow.dense <- function(
graph,
nodes = V(graph),
vids = V(graph),
loops = FALSE,
exponent = 1,
rescale = FALSE,
Expand All @@ -1823,12 +1823,12 @@ bonpow.dense <- function(
} else {
ev <- ev * sqrt(n / sum((ev)^2))
}
ev[as.numeric(nodes)]
ev[as.numeric(vids)]
}

bonpow.sparse <- function(
graph,
nodes = V(graph),
vids = V(graph),
loops = FALSE,
exponent = 1,
rescale = FALSE,
Expand Down Expand Up @@ -1856,7 +1856,7 @@ bonpow.sparse <- function(
ev <- ev * sqrt(vcount(graph) / sum((ev)^2))
}

ev[as.numeric(nodes)]
ev[as.numeric(vids)]
}


Expand Down Expand Up @@ -1914,7 +1914,7 @@ bonpow.sparse <- function(
#' is important to think about the edge direction and what it represents.
#'
#' @param graph the input graph.
#' @param nodes vertex sequence indicating which vertices are to be included in
#' @param vids vertex sequence indicating which vertices are to be included in
#' the calculation. By default, all vertices are included.
#' @param loops boolean indicating whether or not the diagonal should be
#' treated as valid data. Set this true if and only if the data can contain
Expand Down Expand Up @@ -1975,30 +1975,30 @@ bonpow.sparse <- function(
#'
power_centrality <- function(
graph,
nodes = V(graph),
vids = V(graph),
loops = FALSE,
exponent = 1,
rescale = FALSE,
tol = 1e-7,
sparse = TRUE
) {
nodes <- as_igraph_vs(graph, nodes)
vids <- as_igraph_vs(graph, vids)
if (sparse) {
res <- bonpow.sparse(graph, nodes, loops, exponent, rescale, tol)
res <- bonpow.sparse(graph, vids, loops, exponent, rescale, tol)
} else {
res <- bonpow.dense(graph, nodes, loops, exponent, rescale, tol)
res <- bonpow.dense(graph, vids, loops, exponent, rescale, tol)
}

if (igraph_opt("add.vertex.names") && is_named(graph)) {
names(res) <- vertex_attr(graph, "name", nodes)
names(res) <- vertex_attr(graph, "name", vids)
}

res
}

alpha.centrality.dense <- function(
graph,
nodes = V(graph),
vids = V(graph),
alpha = 1,
loops = FALSE,
exo = 1,
Expand Down Expand Up @@ -2037,12 +2037,12 @@ alpha.centrality.dense <- function(
diag(id) <- 1

ev <- solve(id - alpha * d, tol = tol) %*% exo
ev[as.numeric(nodes)]
ev[as.numeric(vids)]
}

alpha.centrality.sparse <- function(
graph,
nodes = V(graph),
vids = V(graph),
alpha = 1,
loops = FALSE,
exo = 1,
Expand Down Expand Up @@ -2092,7 +2092,7 @@ alpha.centrality.sparse <- function(
M3 <- M2 - alpha * M
r <- Matrix::solve(M3, tol = tol, exo)

r[as.numeric(nodes)]
r[as.numeric(vids)]
}


Expand All @@ -2114,7 +2114,7 @@ alpha.centrality.sparse <- function(
#'
#' @param graph The input graph, can be directed or undirected. In undirected
#' graphs, edges are treated as if they were reciprocal directed ones.
#' @param nodes Vertex sequence, the vertices for which the alpha centrality
#' @param vids Vertex sequence, the vertices for which the alpha centrality
#' values are returned. (For technical reasons they will be calculated for all
#' vertices, anyway.)
#' @param alpha Parameter specifying the relative importance of endogenous
Expand Down Expand Up @@ -2160,19 +2160,19 @@ alpha.centrality.sparse <- function(
#'
alpha_centrality <- function(
graph,
nodes = V(graph),
vids = V(graph),
alpha = 1,
loops = FALSE,
exo = 1,
weights = NULL,
tol = 1e-7,
sparse = TRUE
) {
nodes <- as_igraph_vs(graph, nodes)
vids <- as_igraph_vs(graph, vids)
if (sparse) {
res <- alpha.centrality.sparse(
graph,
nodes,
vids,
alpha,
loops,
exo,
Expand All @@ -2182,7 +2182,7 @@ alpha_centrality <- function(
} else {
res <- alpha.centrality.dense(
graph,
nodes,
vids,
alpha,
loops,
exo,
Expand All @@ -2191,7 +2191,7 @@ alpha_centrality <- function(
)
}
if (igraph_opt("add.vertex.names") && is_named(graph)) {
names(res) <- vertex_attr(graph, "name", nodes)
names(res) <- vertex_attr(graph, "name", vids)
}
res
}
26 changes: 13 additions & 13 deletions R/cocitation.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@
#' both cite, `bibcoupling()` calculates this.
#'
#' `cocitation()` calculates the cocitation counts for the vertices in the
#' `v` argument and all vertices in the graph.
#' `vids` argument and all vertices in the graph.
#'
#' `bibcoupling()` calculates the bibliographic coupling for vertices in
#' `v` and all vertices in the graph.
#' `vids` and all vertices in the graph.
#'
#' Calculating the cocitation or bibliographic coupling for only one vertex
#' costs the same amount of computation as for all vertices. This might change
#' in the future.
#'
#' @param graph The graph object to analyze
#' @param v Vertex sequence or numeric vector, the vertex ids for which the
#' @param vids Vertex sequence or numeric vector, the vertex ids for which the
#' cocitation or bibliographic coupling values we want to calculate. The
#' default is all vertices.
#' @return A numeric matrix with `length(v)` lines and
#' @return A numeric matrix with `length(vids)` lines and
#' `vcount(graph)` columns. Element `(i,j)` contains the cocitation
#' or bibliographic coupling for vertices `v[i]` and `j`.
#' or bibliographic coupling for vertices `vids[i]` and `j`.
#' @author Gabor Csardi \email{csardi.gabor@@gmail.com}
#' @family cocitation
#' @export
Expand All @@ -53,29 +53,29 @@
#' cocitation(g)
#' bibcoupling(g)
#'
cocitation <- function(graph, v = V(graph)) {
cocitation <- function(graph, vids = V(graph)) {
ensure_igraph(graph)

v <- as_igraph_vs(graph, v)
vids <- as_igraph_vs(graph, vids)
on.exit(.Call(R_igraph_finalizer))
res <- .Call(Rx_igraph_cocitation, graph, v - 1)
res <- .Call(Rx_igraph_cocitation, graph, vids - 1)
if (igraph_opt("add.vertex.names") && is_named(graph)) {
rownames(res) <- vertex_attr(graph, "name", v)
rownames(res) <- vertex_attr(graph, "name", vids)
colnames(res) <- vertex_attr(graph, "name")
}
res
}

#' @rdname cocitation
#' @export
bibcoupling <- function(graph, v = V(graph)) {
bibcoupling <- function(graph, vids = V(graph)) {
ensure_igraph(graph)

v <- as_igraph_vs(graph, v)
vids <- as_igraph_vs(graph, vids)
on.exit(.Call(R_igraph_finalizer))
res <- .Call(Rx_igraph_bibcoupling, graph, v - 1)
res <- .Call(Rx_igraph_bibcoupling, graph, vids - 1)
if (igraph_opt("add.vertex.names") && is_named(graph)) {
rownames(res) <- vertex_attr(graph, "name", v)
rownames(res) <- vertex_attr(graph, "name", vids)
colnames(res) <- vertex_attr(graph, "name")
}
res
Expand Down
Loading