In my R code, while I generate a number with sample (1: edge_counter, 1) everything works fine but while I generate it with rbinom (1, i, p) the code doesn't work. What could be the problem?
Here is the code where i generate a graph following the Forrest Fire module, the random generation problem is when i generate a random x:
install.packages("igraph")
library(igraph)
par(mar=c(1,1,1,1))
#Number of node to add in the graph
num_nodes = 10
#Forward burning probability
p = 0.37
#Backward burning ratio
r = 0.32
#Parameters for the plot
node_count <- rep(0, times = num_nodes)
edge_count <- rep(0, times = num_nodes)
g1 <- make_empty_graph(1)
for(i in 2:num_nodes)
{
exit = 0
visited_node <- rep(FALSE, times = i)
g1 = add_vertices(g1,1)
#orphans extention
q = runif(1,0,1)
if(q > 0.2)
{
#chose an ambassador node
w <- sample(1:(vcount(g1)-1), 1)
# connecting the ambassador with the node
v = i;
g1 = add_edges(g1,c(v,w))
# count number of node linked to the ambassador
edge_counter <- 0
for(h in 1: i)
{
if(g1[w, h] == 1 || g1[h, w] == 1)
{
edge_counter = edge_counter + 1
}
}
selected_node <- rep(FALSE, times = i)
while(exit == 0)
{
x = sample(1:edge_counter, 1)
#x = rbinom(1, i, p)
# select x links incident to w choosing from among both out links and in links
j = 0
while(j != x)
{
prob <- runif(1,0,1)
random_node <- sample(1:i, 1)
if(selected_node[random_node] == FALSE)
{
#chose out-link
if(prob < r)
{
if(g1[w, random_node] == 1)
{
selected_node[random_node] = TRUE
j = j+1
}
}
#chose in-link
else
{
if (g1[random_node, w] == 1)
{
selected_node[random_node] = TRUE
j = j+1
}
}
}
}
#v forms out-links to w1,...wx
for(l in 1:i)
{
if((selected_node[l] == TRUE) && (l != v))
{
g1 = add_edges(g1,c(v,l))
}
}
old_v <- v
visited_node[v] = TRUE
for(j in 1:i)
{
if ((selected_node[j] == TRUE) && (visited_node[v] == FALSE))
{
v <- j
break
}
}
if(old_v == v)
{
exit = 1
}
}
}
#Node and edge count
node_count[i] = gorder(g1)
edge_count[i] = gsize(g1)
}
# number of nodes respect number of edges
plot(node_count, edge_count, xlab = "Number of nodes", ylab = "Number of edges")
#graph plot
plot(g1)
Aucun commentaire:
Enregistrer un commentaire