Basically, I would summarize your algorithm for computing a new graph as:
- Create a node in the new graph for every edge in the original graph
- If a pair of nodes in the new graph shared a node in the original graph (aka they are mapped to adjacent edges in the original graph), add an edge between them of weight equal to the product of the mapped edge weights.
The approach I would take to this would be to loop through the nodes in the original graph, adding an edge to the new graph for each pair edges adjacent to that node:
# Sample graph (from comments on original question)
library(igraph)
g <- graph.data.frame(data.frame(x=1:3, y=2:4, weight=c(0.7, 0.5, 0.2)))
# Build new graph from edge list
g.edges <- get.edges(g, E(g))
enames <- paste(g.edges[,1], g.edges[,2], sep=",")
ewts <- E(g)$weight
(new.edges <- do.call(rbind, sapply(1:vcount(g), function(x) {
incident <- which(g.edges[,1] == x | g.edges[,2] == x)
if (length(incident) <= 1) {
return(NULL)
} else {
all.comb <- combn(incident, 2)
return(data.frame(x=enames[all.comb[1,]], y=enames[all.comb[2,]], weight=apply(all.comb, 2, function(x) prod(ewts[x]))))
}
})))
# x y weight
# 1 1,2 2,3 0.35
# 2 2,3 3,4 0.10
You can build the new graph with graph.data.frame(new.edges)
.