-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph_structures.R
81 lines (76 loc) · 2.89 KB
/
graph_structures.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#' @title Adjacency list
#' @description Create adjacency lists from a graph, either for adjacent edges or for neighboring vertices. This version is faster than the version of igraph but less general.
#' @param g An igraph object
#' @details The function does not have a mode parameter and only returns the adjacency list comparable to as_adj_list(g,mode="all)
#' @return A list of numeric vectors.
#' @author David Schoch
#' @examples
#' library(igraph)
#' g <- make_ring(10)
#' as_adj_list1(g)
#' @export
as_adj_list1 <- function(g){
n <- igraph::vcount(g)
lapply(1:n,function(i){
x <- g[[i]][[1]]
attr(x,"env") <- NULL
attr(x,"graph") <- NULL
class(x) <- NULL
x
})
}
#' @title weighted dense adjacency matrix
#' @description returns the weighted adjacency matrix in dense format
#' @param g An igraph object
#' @param attr Either NULL or a character string giving an edge attribute name. If NULL a traditional adjacency matrix is returned. If not NULL then the values of the given edge attribute are included in the adjacency matrix.
#' @details This method is faster than as_adj from igraph if you need the weighted adjacency matrix in dense format
#' @return Numeric matrix
#' @author David Schoch
#' @examples
#' library(igraph)
#' g <- sample_gnp(10, 0.2)
#' E(g)$weight <- runif(ecount(g))
#' as_adj_weighted(g, attr="weight")
#' @export
as_adj_weighted <- function(g,attr = NULL){
as.matrix(igraph::as_adj(g,attr=attr,type = "both",sparse=T))
}
#' @title Clique Vertex Matrix
#' @description Creates the clique vertex matrix with entries (i,j) equal to one if node j is in clique i
#' @param g An igraph object
#' @return Numeric matrix
#' @author David Schoch
#' @examples
#' library(igraph)
#' g <- sample_gnp(10,0.2)
#' clique_vertex_mat(g)
#' @export
clique_vertex_mat <- function(g){
if(!igraph::is.igraph(g)){
stop("g must be an igraph object")
}
if(igraph::is.directed(g)){
warning("g is directed. Underlying undirected graph is used")
g <- igraph::as.undirected(g)
}
mcl <- igraph::max_cliques(g)
M <- matrix(0,length(mcl),igraph::vcount(g))
for(i in 1:length(mcl)){
M[i,mcl[[i]]] <- 1
}
M
}
#' @title Convert a list of graphs to an adjacency matrices
#' @description Convenience function that turns a list of igraph objects into adjacency matrices.
#' @param g_lst A list of igraph object
#' @param attr Either NULL or a character string giving an edge attribute name. If NULL a binary adjacency matrix is returned.
#' @param sparse Logical scalar, whether to create a sparse matrix. The 'Matrix' package must be installed for creating sparse matrices.
#' @return List of numeric matrices
#' @author David Schoch
#' @export
as_multi_adj <- function(g_lst,attr = NULL,sparse = FALSE){
if(!all(unlist(lapply(g_lst,igraph::is.igraph)))){
stop("all entries of g_lst must be igraph objects")
}
lapply(g_lst,function(x) igraph::as_adj(x,"both",attr=attr,sparse=sparse))
}