[R] vectorized code

zeynab jibril zeyn2307 at gmail.com
Fri May 29 16:49:28 CEST 2015


HI

I was working on online example, where virus is spread through a graph. The
example is sufficient for small graph i.e. small number of edges and nodes.
But I tried it on very large graph i.e. 10000 nodes and 20000 edges, but
the below function is not sufficient for large graph because its slow.

My question is how can the below function be converted to Vectorized code
can be optimized for large graphs?

spreadVirus <- function(G,Vinitial,Activation_probability){



# Precompute all outgoing graph adjacencies



G$AdjList = get.adjlist(G,mode="out")



# Initialize various graph attributes

V(G)$color    = "blue"

E(G)$color    = "black"

V(G)[Vinitial]$color    <- "yellow"



# List to store the incremental graphs (for plotting later)

Glist <- list(G)

count <- 1



# Spread the infection

active <- Vinitial



while(length(active)>0){

new_infected <- NULL

E(G)$color = "black"



for(v in active){

# spread through the daily contacts of vertex v



daily_contacts <- G$AdjList[[v]]



E(G)[v %->% daily_contacts]$color <- "red"



for(v1 in daily_contacts){



if(V(G)[v1]$color == "blue" & new_color=="red") {



V(G)[v1]$color <- "red"



new_infected <- c(new_infected,v1)



 }

}

}

# the next active set

#this needed for updating



active <- new_infected



# Add graph to list

# optional dependening on if i want to graph

count <- count + 1

Glist[[count]] <- G

}

return(Glist)

}

	[[alternative HTML version deleted]]



More information about the R-help mailing list