Skip to content

Instantly share code, notes, and snippets.

@arimitramaiti
Created July 28, 2020 11:13
Show Gist options
  • Save arimitramaiti/93071cf856da39b061e9638dd2df7dc2 to your computer and use it in GitHub Desktop.
Save arimitramaiti/93071cf856da39b061e9638dd2df7dc2 to your computer and use it in GitHub Desktop.
Simulate Random Graph Model for Mumbai Local Rail Network
#install.packages("igraph")
library("igraph")
#Create empty list
degree_list = list()
closeness_list = list()
betweenness_list = list()
eigen_list = list()
for (i in 1:100) {
# ... Random graph with fixing n and probability
#Formula to calculate n and p are shown in project slides
g_np <- erdos.renyi.game(n=114, p=0.018, type = c("gnp"), directed=FALSE, loops=FALSE)
#No need to plot as of now because we need centrality measures for each iteration
#Calculate degree centrality measure
degree.cent_g_np <- centr_degree(g_np, mode = "all")
#Stores 1 iteration
degree_val <- data.frame(degree.cent_g_np$res)
#Calculate closeness centrality measure
closeness.cent_g_np <- closeness(g_np, mode = "all")
#Stores 1 iteration
closeness_val <- as.data.frame(closeness.cent_g_np)
#Calculate betweenness centrality measure
between.cent_g_np <- betweenness(g_np)
#Stores 1 iteration
betweenness_val <- as.data.frame(between.cent_g_np)
#Calculate eigen value centrality measure
eigen.cent_g_np <- eigen_centrality(g_np)
z_cent <- eigen.cent_g_np$vector
f_cent <- z_cent/sum(z_cent)
eigen_val <- as.data.frame(f_cent)
#Assign the value of i to every row
degree_val$i <- i
#Stores all iteration in the empty list
degree_list[[i]] <- degree_val # add it to list
#Assign the value of i to every row
closeness_val$i <- i
#Stores all iteration in the empty list
closeness_list[[i]] <- closeness_val # add it to list
#Assign the value of i to every row
betweenness_val$i <- i
#Stores all iteration in the empty list
betweenness_list[[i]] <- betweenness_val # add it to list
#Assign the value of i to every row
eigen_val$i <- i
#Stores all iteration in the empty list
eigen_list[[i]] <- eigen_val # add it to list
}
degree_all = do.call(cbind, degree_list)
degree_random_graph = degree_all[, seq(1,199,2)]
degree_random_graph = data.frame(degree_random_graph)
closeness_all = do.call(cbind, closeness_list)
closeness_random_graph = closeness_all[, seq(1,199,2)]
closeness_random_graph = data.frame(closeness_random_graph)
betweenness_all = do.call(cbind, betweenness_list)
betweenness_random_graph = betweenness_all[, seq(1,199,2)]
betweenness_random_graph = data.frame(betweenness_random_graph)
eigen_all = do.call(cbind, eigen_list)
eigen_random_graph = eigen_all[, seq(1,199,2)]
eigen_random_graph = data.frame(eigen_random_graph)
#install.packages("writexl")
library("writexl")
write_xlsx(degree_random_graph,"<your_directory>\\degree_random_graph.xlsx")
write_xlsx(closeness_random_graph,"<your_directory>\\closeness_random_graph.xlsx")
write_xlsx(betweenness_random_graph,"<your_directory>\\betweenness_random_graph.xlsx")
write_xlsx(eigen_random_graph,"<your_directory>\\eigen_random_graph.xlsx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment