Last active
January 3, 2017 20:54
-
-
Save dmarx/8981ad329506b250498f1f52c3050a9c to your computer and use it in GitHub Desktop.
Novel (?) technique for inferring a directed bipartite projection from an undirected bipartite graph. Code is for pedagogical demonstration to accompany the article here: http://dmarx.github.io/map-of-reddit-by-active-users/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(igraph) | |
# Experiment parameters | |
n=10 # Primary class (i.e. subreddits) | |
m=100 # Secondary class (i.e. users) | |
threshold = .5 # edge threshold | |
###################################### | |
seed(123) | |
g = sample_bipartite(n,m, p=.5) # this will generate a random graph, rather than scale free. | |
plot(g) | |
mat = as_adj(g) | |
#dim(mat) # NB: "(n+m) x (n+m)" form (block off-diagonal adjacency), rather than n x m (bipartite adjacency) | |
mat_bipart_sp = mat[1:n, 1:m +n] # Extract n x m form | |
mat_bipart = as.matrix(mat_bipart_sp) # sparse matrix strangely doesn't support transpose operation | |
# calculate node degree | |
n_deg = apply(mat_bipart_sp, 1, sum) | |
# Perform projection operation relative to first class | |
bipart_proj = as.matrix(mat_bipart_sp %*% t(mat_bipart)) | |
# Recalibrate edge weights according to target degree | |
dir_bipat = t(t(as.matrix(bipart_proj)) / n_deg) # need to take transpose to weight by target degree | |
diag(dir_bipat) = 0 | |
dir_bipat_trsh = dir_bipat | |
dir_bipat_trsh[dir_bipat<threshold] = 0 | |
# visualize | |
g2 = graph_from_adjacency_matrix(dir_bipat_trsh, weighted=TRUE) | |
plot(g2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment