我是R的新手,目前正致力于实施一种算法来修剪多图中的所有叶子,然后在结果图中完成每个循环,以便根据某些规则进一步简化它们。我目前正在测试几个数据集的算法,到目前为止它一直工作得很好,除了一个给我错误的特定数据集"不能使用另一个图的边缘序列,"我发现它很奇怪,因为它适用于更大的数据集。我花了最后几个小时试图检测错误无济于事。以下是我的代码:
pruneLeaf<-function(g){
members<-membership(clusters(g,mode="strong"))
strongly_connected=lapply(unique(members),function (x)
induced.subgraph(g,which(members==x)))
return (strongly_connected)
}
getNetflow<-function(g,node){
#precondition: g is a graph and node is a vertex in graph g
#postcondition: return the netflow from vertex node in graph g.
inflow_list=incident_edges(g,node,mode="in")
outflow_list=incident_edges(g,node,mode="out")
inflow<-0
outflow<-0
for(item in edge_attr(g,"Quantity",inflow_list[[1]])){
inflow<-inflow+item
}
for(item in edge_attr(g,"Quantity",outflow_list[[1]])){
outflow<-outflow+item
}
return (inflow-outflow)
}
modifyCycle<-function(g,node,netflow){
#pre-condition:node of graph g has negative netflow
#post-condition: eliminate transactions with the least priorities to
outflow_list=incident_edges(g,node,mode="out")
if(length(outflow_list[[1]])==1){
delete.edges(g,outflow_list[[1]])
}
else{
priority_list=outflow_list[[1]][order(outflow_list[[1]]$Priority)]
while(netflow<0){
count=1
edge=priority_list[count] #we begin with the lowest priority edge
flow=edge$Quantity
if(flow<= -netflow){
netflow<-netflow+flow
g<-delete.edges(g,edge)
}
else{ #otherwise, we allow partial transfer to balance the netflow
g<-set.edge.attribute(g,"Quantity",edge,flow+netflow)
netflow<-0
}
count<-count+1
}
return (g)
}
}
modifyGraph<-function(g){
#precondition: g is an igraph object containing only cycles
#postcondition: return an igraph with balanced flow at every cycle
isDone=FALSE
while(!isDone){
vertices=V(g)
isDone=TRUE
for(node in vertices){
netflow=getNetflow(g,node)
if(netflow<0){
g<-modifyCycle(g,node,netflow)
isDone=FALSE
}
}
}
return (g)
}
settlementGridlock<-function(g){
#precondition: g is an igraph object
#post-condition: return a list of graphs comprises of cyc
cyclic_graphs<-pruneLeaf(g)
result<-lapply(cyclic_graphs,function(x) modifyGraph(x))
return(result)
}
对于相当长的帖子感到抱歉,但基本上有几个函数:
pruneLeaf:清除图表中的所有叶子并返回图表中强连接组件的列表
getNetflow:计算图表中给定节点的净流量
错误似乎是由错误日志中给出的函数modifyCycle产生的:
Error in as.igraph.es(graph, index) :
Cannot use an edge sequence from another graph.
9 stop("Cannot use an edge sequence from another graph.")
8 as.igraph.es(graph, index)
7 i_set_edge_attr(graph = graph, name = name, index = index, value = value)
6 set.edge.attribute(g, "Quantity", edge, flow + netflow)
5 modifyCycle(g, node, netflow)
4 modifyGraph(x)
3 FUN(X[[i]], ...)
2 lapply(cyclic_graphs, function(x) modifyGraph(x))
1 settlementGridlock(graph3)