-
Notifications
You must be signed in to change notification settings - Fork 7
/
create_network.R
190 lines (169 loc) · 5.19 KB
/
create_network.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# require package igraph, if not used before please install it
# install.packages("igraph")
library(igraph)
# basic paramters
AiIB = 0.2
ER_network <- function(network_size, prob){
a = matrix(0, network_size, network_size)
#links = sample(c(1:network_size), size = network_size * network_size, replace = T)
prob = prob+1/(network_size-1)
links = sample(seq(1,network_size,0.1), size = network_size * network_size, replace = T)
links = matrix(links, ncol = network_size, byrow = T)
threshold = prob * network_size
for (i in 1:network_size){
for (j in 1:network_size){
if (i != j){
if (links[i,j] < threshold){
a[i,j] = 1
}
}
}
}
G = graph_from_adjacency_matrix(a, mode = 'directed')
return (G)
}
# test
# a = ER_network(100, 3/99)
# cat(length(E(a))/20)
#length(E(sample_gnp(n=1000, p=4/999, TRUE)))/1000
# if SBM = ER:
# z = p * (N - 1/2)
# so for a certain z the p is:
# p = z/(network_size - 1/2)
SBM_network <- function(network_size, average_degree, p, later = TRUE){
adjust = 1/(network_size-1)
core_size <- network_size/2
periphery_size <- network_size/2
if (later == TRUE){
p_cc = p
p_pp <- (average_degree - (network_size - 1) * p_cc / 4) * (4/(3 * network_size -1))
p_cp <- p_pp
} else {
p_pp = p
p_cc <- (average_degree - (network_size -1) * p_pp / 4) * (4/(3 * network_size -1))
p_cp <- p_cc
}
p_cc <- p_cc+adjust
p_cp <- p_cp+adjust
p_pp <- p_pp+adjust
a = matrix(0, network_size, network_size)
links = sample(seq(1,network_size,0.1), size = network_size * network_size, replace = T)
links = matrix(links, ncol = network_size, byrow = T)
threshold_ppp = p_pp * network_size
threshold_pcp = p_cp * network_size
threshold_pcc = p_cc * network_size
for (i in 1:network_size){
for (j in 1:network_size){
if (i != j){
if (i <= network_size/2){
# left region (Core) in field
if (j <= network_size/2){
# left and upper region -> cc
threshold <- threshold_pcc
} else {
# left and lower region -> cp
threshold <- threshold_pcp
}
} else {
# right reigion in field
if (j <= network_size/2){
# right upper region -> cp / pc
threshold <- threshold_pcp
} else {
# right lower rigion -> pp
threshold <- threshold_ppp
}
}
if (links[i,j] < threshold){
a[i,j] = 1
}
}
}
}
G = graph_from_adjacency_matrix(a, mode = 'directed')
return (G)
}
get_low_bound_cc <- function(average_degree, network_size = 1000){
# this bound happened at the pp = cc
pcc = average_degree/(network_size-0.5)
return(pcc)
}
get_up_bound_cc <- function(average_degree, network_size = 1000){
# this bound happened at the pp = 0
ppp = 0
pcc = (average_degree - ppp / (4*(3*network_size - 1)))*4/(network_size-1)
return (pcc)
}
get_up_bound_pp <- function(average_degree, network_size = 1000){
# this bound happened at the pp = cc
ppp = average_degree/(network_size-0.5)
return(ppp)
}
get_low_bound_pp <- function(average_degree, network_size = 1000){
# this bound happened at the cc = 0
pcc = 0
ppp = (average_degree - pcc / (4*(3*network_size - 1)))*4/(network_size-1)
return (ppp)
}
create_network <- function(network_size=1000, parameter, type , p = 0.003001501, print_out = FALSE){
# first creat the graph without weights
if (type == 'er'){
# G <- sample_gnp(n=network_size, p=prob, TRUE)
G <- ER_network(network_size, prob = parameter)
} else if (type == 'sbm later'){
G <- SBM_network(network_size, average_degree = parameter, p)
} else if (type == 'sbm fomer'){
G <- SBM_network(network_size, average_degree = parameter, p, later = FALSE)
} else if (type == 'ba'){
G <- sample_pa(n=network_size, m = parameter, power = 1, directed = TRUE)
} else if (type == 'read'){
G <- read_graph(file = parameter)
} else {
cat('The setting of the network type is wrong.')
}
M <- length(E(G))
edge_df <- ends(G, E(G))
if (print_out == TRUE) sprintf('The network have %s edges, hence the average degree is %s',
M, M/network_size)
# creat the list of weights
edge_weight <- list(rep(0,M))
#print('before')
#print(edge_weight)
for (i in 1:network_size) {
if (print_out == TRUE){
cat('node:')
cat(i)
cat('\n')
}
nodes_in <- neighbors(G, i, 'in')
if (print_out == TRUE){
cat('has neighbors:')
cat(nodes_in)
cat('\n')
}
number_of_in <- length(nodes_in)
if (number_of_in != 0){
this_weight <- AiIB / number_of_in
ENDS = as.vector(rbind(as.numeric(nodes_in), i))
SpecialEdges = get.edge.ids(G, ENDS)
if (print_out == TRUE){
cat('the edges need to change')
cat(SpecialEdges)
cat('\n')
}
E(G)$weight[SpecialEdges] = c(rep(this_weight, number_of_in))
if (print_out == TRUE){
cat('after')
cat(E(G)$weight[SpecialEdges])
cat('\n')
}
} else{
this_weight <- 0
}
}
if (print_out == TRUE){
cat(E(G)$weight)
cat('\n')
}
return (G)
}