网络图中节点u对节点v的影响的有效计算

时间:2018-04-30 02:09:53

标签: r igraph

正如标题所说,计算了从节点u开始到节点v结束的所有路径,我必须通过以下类型计算节点u对节点v“I(u,v)”的影响: # define matlab dir MDIR = /Applications/MATLAB_R2017b.app # compiles mex files using g++ #CC = gcc # compiler flags for g++ #CCFLAGS = -O3 -fpic # to use the intel compiler instead, uncomment CC and CCFLAGS below: # compiles mex file using the intel compiler CC = icc # compiler flags for intel compiler CCFLAGS = -O3 -fPIC -D__amd64 # Figure out which platform we're on UNAME = $(shell uname -s) # Linux ifeq ($(findstring Linux,${UNAME}), Linux) # define which files to be included CINCLUDE = -I$(MDIR)/extern/include -Ic++ -shared # define extension EXT = mexa64 endif # Mac OS X ifeq ($(findstring Darwin,${UNAME}), Darwin) # define which files to be included CINCLUDE = -L$(MDIR)/bin/maci64 -Ic++ -shared -lmx -lmex -lmat -lmwblas # define extension EXT = mexmaci64 # CCFLAGS += -std=c++11 endif SRC:=$(wildcard *.c) *.o: $(SRC) for i in $(SRC) ; do \ $(CC) $(CCFLAGS) -I$(MDIR)/extern/include -c -Ic++ $$i -o $${i%.c}.o; \ done OBJ0:=bwblkslv.o sdmauxFill.o sdmauxRdot.o OBJ1:=choltmpsiz.o all: $(CC) $(CCFLAGS) $(CINCLUDE) $(OBJ0) -o bwblkslv.$(EXT) $(CC) $(CCFLAGS) $(CINCLUDE) $(OBJ1) -o choltmpsiz.$(EXT) # clean up clean: rm -f *.o *.$(EXT)

这就是我这样做的方式:


I(u,v) = Sum(for all the paths from u to v)[ Product of all the weights of consecutive edges from u to v ]

其中Alpha [u,v]是I(u,v),Flist [i,j]是路径i,直到第j个节点前进到v的概率。 排队 Alpha<-array(0,dim = c(length(V(g)),length(V(g)))) if( u!=v ){ paths<-all_simple_paths(g, from = V(g)[u])#computing all paths from u UpathV<-paths[unlist(lapply(paths, function(p){p[length(p)] %in% v}))]#extracting those that end to v if(length(UpathV)!=0){#computing probability matrix from u to v Flist<-array(NA,dim = c(length(UpathV),max(lengths(UpathV)))) Flist[1:length(UpathV)]<-1 for(i in 1:length(UpathV)){ for(j in 2:length(UpathV[[i]])){ Flist[i,j]<-Flist[i,j-1]*E(g)[get.edge.ids(g,as.numeric(UpathV[[i]][(j-1):j]))]$weight } } }#if length(pspaths) if(length(UpathV)!=0){ for(i in 1:length(UpathV)){ Alpha[u,v]<-Alpha[u,v]+Flist[i,length(UpathV[[i]])] }#computing I(u,v) } }else{ Alpha[u,v]<-1 } 你可以看到,如果第三条路径是1-> 2-> 3-> 4则它的长度为4,然后Flist [3,4]是边缘w(1-> 2)的权重*瓦特(2→3)* W(3→4)。 我的解决方案正在运行,但是对于大型网络,它需要花费很多时间来计算矩阵。鉴于我必须从每个u到每个vi计算I(u,v)需要知道是否有更有效的方法来做我的计算没有改变行3-4。提前做了很多!

1 个答案:

答案 0 :(得分:0)

过了一段时间,我找到了一种方法,通过更改1行代码并添加另一行代码来加快计算速度。 当计算Flist数组而不是从E(g)$ weight获得每个权重值时,我们应该从邻接矩阵中获取它。我发现在R中使用矩阵比使用数据帧更快。所以添加: AjM<-as_adjacency_matrix(g,attr = "weight") 和: Flist[i,j]<-Flist[i,j-1]*AjM[as.numeric(UpathV[[i]][j-1]),as.numeric(UpathV[[i]][j])]