问题
I need to find a way to find the path between two vertices that maximizes the product of edge attributes. In my case the edge attribute is a probability of connection. Let's assume I want to find the maximum probability path between the vertex 1 and 4 in the following example:
require(igraph)
G<-graph.data.frame(as.data.frame(cbind(id1=c(1,1,2,3,1,4),id2=c(2,3,4,4,5,5),weight=c(0.5,0.35,0.5,0.9,0.6,0.6))), directed=FALSE)
plot(G, edge.label=paste(E(G),"=",signif(E(G)$weight, digits=1)), vertex.size=10)
#weighted shortest path using connection probability
a<-get.shortest.paths(G,1,4, weights=E(G)$weight, output="epath")[[1]]
E(G)[a]
prod(E(G)$weight[a])
#weighted shortest path using the inverse of connection probability
b<-get.shortest.paths(G,1,4, weights=1-E(G)$weight, output="epath")[[1]]
E(G)[b]
prod(E(G)$weight[b])
In this example, the path which maximizes the connection is through the vertex 5, in fact the product of the probabilities is equal to 0.36 (0.6*0.6). Shortest path function seems to gives priority on the basis of the sum of the attributes, but not the product. In fact, in the example above, either I use probabilities or the inverse of probabilities, it suggests two paths whose probability of connection is lower (0.25 and 0.315).
Is there any way to find the path which maximizes the product??? thanks
回答1:
You are using shortest path algorithm to get longest path. So inverting weights is necessary. At the same time you want to maximize product and not sum. Combining -
x<-get.shortest.paths(G,1,4, weights=-log(E(G)$weight), output="epath")[[1]]
E(G)[x]
prod(E(G)$weight[x])
来源:https://stackoverflow.com/questions/16032499/igraph-in-r-find-the-path-between-vertices-that-maximizes-the-product-of-edge-a