Laplace Spectral Clustering

This commit is contained in:
Noah L. Schrick 2022-03-21 19:45:18 -05:00
parent 122acae34e
commit 1bd92a734d

View File

@ -17,6 +17,35 @@ g2 <- yeast
g2.netname <- "Yeast" g2.netname <- "Yeast"
##################### Part 1: Laplace Spectral Clustering ##################### ##################### Part 1: Laplace Spectral Clustering #####################
g1.adj <- get.adjacency(g1) # get adjacency
g2.adj <- get.adjacency(g2)
g1.deg <- rowSums(as.matrix(g1.adj)) # get degrees
g2.deg <- rowSums(as.matrix(g2.adj))
g1.Lap <- diag(g1.deg) - g1.adj # L = D-A
g2.Lap <- diag(g2.deg) - g2.adj
n1 <- length(V(g1)) # number of nodes
n2 <- length(V(g2))
# get eigvals and vecs
x1 <- eigen(g1.Lap)$vectors[,n1-1]
x2 <- eigen(g2.Lap)$vectors[,n2-1]
x1_val <- eigen(g1.Lap)$values[n1-1]
x2_val <- eigen(g2.Lap)$values[n2-1]
names(x1) <- names(V(g1))
names(x2) <- names(V(g2))
x1
x2
x1_clusters <- ifelse(x1>0,1,-1)
x2_clusters <- ifelse(x2>0,1,-1)
# Plotting
V(g1)$color <- ifelse(x1_clusters>0,"green","yellow")
V(g1)$size <- 50*abs(x1)
V(g2)$color <- ifelse(x2_clusters>0,"green","yellow")
V(g2)$size <- 50*abs(x2)
plot(g1, main=paste(g1.netname, " Laplace Spectral Clustering"))
plot(g2, main=paste(g2.netname, " Laplace Spectral Clustering"),
vertex.label=NA)
########################## Part 2: Newman Modularity ########################## ########################## Part 2: Newman Modularity ##########################
@ -42,7 +71,9 @@ V(karate)$color[karate.modularity$membership==1] <- "green"
V(karate)$color[karate.modularity$membership==2] <- "red" V(karate)$color[karate.modularity$membership==2] <- "red"
V(karate)$color[karate.modularity$membership==3] <- "blue" V(karate)$color[karate.modularity$membership==3] <- "blue"
plot(karate,vertex.size=10,vertex.label=V(karate)$label,vertex.color=V(karate)$color) plot(karate,vertex.size=10,
vertex.label=V(karate)$label,vertex.color=V(karate)$color,
main=paste("Karate Recursive Newman Modularity"))
###################### Part 4: TOM and Dynamic Tree Cut ###################### ###################### Part 4: TOM and Dynamic Tree Cut ######################