Reference no: EM131076098
Experiment
library(igraph)
Just to make labels and scores for original graph and its done once for the comparison reasons
G.O<- read.graph("dolphins.gml",format = c("gml")) G.O is the original graph "network"
ResourceAllocationFunction <- matrix(0, nrow = length(V(G.O)), ncol = length(V(G.O)))
source('ResourceAllocation Function.R')
for(i in 1:length(V(G.O))){
for(j in 1:length(V(G.O))){
ResourceAllocationFunction[i,j] <-Resource.Allocation(G.O,i,j)
}
}
Create an edgelist
G.O.Simplfied <- simplify(G.O, remove.multiple = T, remove.loops = T )
G.O.edgeList <- get.edgelist(G.O.Simplfied)
N <- length(V(G.O)) #number of nodes for original graph
L <- nrow(G.O.edgeList) #number of links..original graph num.of.allpossibleLinks <- N *(N-1)/2
ScoresOfexistent.G.O <- NULL
for(i in 1:(N-1)) {
for(j in (i+1):N) { if(i>j)
{temp <- j j <- i
i<- temp}
l <- (i-1)*N-i*(1+i)/2 + j #l is the link label
if(G.O[i,j]==1){
ScoresOfexistent.G.O <- rbind(ScoresOfexistent.G.O,c(l, ResourceAllocationFunction [i,j],i,j))
}
}
}
GO.Labels <- NULL #for original graph
GO.Labels <- c(GO.Labels ,ScoresOfexistent.G.O[,1])#original
Select randomly 10% of the links, without repetitions
num <- round(0.1* NROW(G.O.edgeList))
MySeq <-seq(from=1 , to=length(G.O.edgeList)/2)
p <- NULL
Labes.ScoresExp2 <- list() #for thr Gr
ScoresOfNonexistentExp2 <- list() GTopNList <- list()
for(x in 1:1000){
#1-
Lc <- 0 #for jaccrd
Mysample <- NULL
Mysample <- sample(MySeq,num,replace =FALSE)
GraphSample <- MySeq[- Mysample] #the same as the original edgelist but with 10% of the links removed but its not an edgelist its integer (but its only a sequence of numbers)
res <- NULL
for(i in 1:length(GraphSample)){
res<-rbind(res, G.O.edgeList[GraphSample[i],])}
GR <- graph.edgelist(res,directed=FALSE)#the same as GraphSample but converted into a graph
#GR.Simplified <- simplify(GR, remove.multiple = T, remove.loops = T )
#GR.edgelist <- get.edgelist(GR.Simplified)
#2- Get the GR scores#################################################
GR.Scores<- matrix(0, nrow = length(V(GR)), ncol = length(V(GR))) for(i in 1:N){
for(j in 1:N){
GR.Scores[i,j] <- Resource.Allocation(GR,i,j) #scores for the removed graph GR that has 10% of links removed.
}
}
#3- refine the Removed graph to take only edges that don't exist (missing and nonexistent) with there link labels############
ScoresOfNonexistent <- NULL
for(i in 1:(N-1)) {
for(j in (i+1):N) {
if(i>j)
{temp <- j j <- i
i<- temp}
l <- (i-1)*N-i*(1+i)/2 + j
if(GR[i,j]== 0){
ScoresOfNonexistent <- rbind(ScoresOfNonexistent,c(l,GR.Scores[i,j]))
}
}
}
#4-sort or order the GR scores only for links that don't exist o.J<-NULL
o.J <- order(ScoresOfNonexistent[ , 2] , decreasing=TRUE)
ScoresOfNonexistent <- ScoresOfNonexistent[o.J,]###ordered scores
#4-take the top N scores and add them to GR (N is 10% of the existing links = num)
Labels.Of.GTopN <- NULL for(a in 1:num){
Labels.Of.GTopN <- rbind( Labels.Of.GTopN,ScoresOfNonexistent[a,])
}
#5- compare using link labeles method##############################################
GTopNLabels <- NULL comparison <- NULL
GTopNLabels <- c(GTopNLabels,Labels.Of.GTopN[,1]) comparison <- GTopNLabels %in% GO.Labels
Lc<- length(comparison[comparison==TRUE]) #number of top scored links that were predicted correctly
pr <- Lc/num #compute precision
p <-c(p,pr) #vector <- c(vector, v) vector of precesion for each iteration
}#end of for loop
histinfo.p <- hist(p,main="Histogram of precision in experiment 2 using Resource Allocation scores/Dolphins ",xlab="precision",border="blue", col="grey",xlim=c(0,1),las=1,breaks=5,prob =TRUE)
hist(p,main="Histogram of precision in experiment 2 using Resource Allocation scores",xlab="precision",border="blue", col="grey",xlim=c(0,1),breaks=5)
RandomScore <- function(g,i,j){
RandomScoreFunction <- runif(1,min = 0, max = 1)
RandomScoreFunction
}