Simon Knapp sleepingwell at gmail.com
Sun Sep 14 12:14:36 CEST 2008

```# bit hard to provide a simple conversion without definitions of the
class 'Node', the template 'DirectedGraph' and the function 'Writed'!
# I've used the package 'igraph' as a drop in - hope it is still clear.
#
# by the way:
# - your curly braces don't match,
# - not all elements of P are initialised before they are used.

#------------------------------------
# original code (cleaned to make comparison easier).
#------------------------------------
#Random r = new Random();
#DirectedGraph<SimpleNode> graph = GetGraph();
#decimal B = 0.1m;
#decimal D = 0.05m;
#int nodes = graph.NodesCount;
#decimal[] E = new decimal[nodes];
#decimal[] P = new decimal[nodes];
#
#for (int i = 7; i <= 10; ++i) P[i] = (decimal)r.NextDouble();
#
#for (int t = 0; t < 100; ++t){
#    Writed(P, "P");
#
#    foreach (SimpleNode n in graph.Nodes) {
#        int id = graph.index[n];
#
#        decimal product = 1;
#        foreach (var item in graph.GetAdjacentNodes(n)){
#            int j = graph.index[item];
#            product *= (1 - B * P[j]);
#        }
#
#        E[id] = product;
#    }
#
#    foreach (SimpleNode n in graph.Nodes){
#        int i = graph.index[n];
#        P[i] = 1 - ((1 - P[i]) * E[i] + D * (1 - P[i]) * E[i] + 0.5m
* D * P[i] * (1 - E[i]));
#        if (P[i] < 0) P[i] = 0;
#    }
#}
#
#}

#------------------------------------
# drop-in for your method getGraph (produces a 10 'random' node
directed graph). I only assign to temporary so I can use the same
'grph' and 'P' in both implementations.
#------------------------------------
library(igraph)
replace=T), nrow=10))
grph.t <- GetGraph()
P.t <- runif(nodes) # assume you meant to initialise all elements of P

#------------------------------------
# IMPLEMENTATON 1.
# A 'mirror' implementation. Some of the code relies
# on the specifics of package igraph, but I've tried to
# be as similar as possible. Hope it still makes sense!
#------------------------------------
B <- 0.1
D <- 0.05
grph <- grph.t
nodes <- vcount(grph)
E <- numeric(nodes)
P <- P.t

for(t in 0:99){
cat('P:', P, '\n')# is this equivalent to 'Writed(P, "P")' ???
graph.Nodes <- get.adjlist(grph) # returns a list of vectors,
where each vector is the nodes a node is connected to.
id <- 0 # we loop over the vectors and so must index separately
for(n in graph.Nodes){ # n is a vector containing the verticies
the vertex at index id+1 is connected to.
id <- id+1
product <- 1;
for(item in n){
product <- product * (1 - B * P[item+1]); # verticies are
indexed from 0. no operator*= in R.
}
E[id] <- product;
}

at <- 0
for(i in 1:nodes){
P[i] <- 1 - ((1 - P[i]) * E[i] + D * (1 - P[i]) * E[i] + 0.5 *
D * P[i] * (1 - E[i])); # we are accessing nodes in order so the
indexes are also ordered.
if (P[i] < 0) P[i] <- 0;
}
}

P # print the result

#------------------------------------
# IMPLEMENTATION 2.
# a more 'R-ish' implementation.
#------------------------------------
B <- 0.1
D <- 0.05
P <- P.t
grph <- grph.t

for(t in 0:99){