(我发表的第一次入侵的回答为保持问题整洁。这个技巧并不总是有效,因此出现故障,请参阅下面的第二个例子。
对于没有工作黑客攻击,请参阅我的第二个答案或其他人的答案!)
我找到标准的规范置换,然后是这个新的规范图的规范着色,然后我可以使用vf2。
我们的功能重新上色图为:
# Convert aaabbccdefaa -> 111223345611
canonical <- function(input){
labels <- unique(input)
match(input, labels)
}
现在回到企业:
g <- graph.empty()
g <- g + vertices(1,2,3)
g <- g + path(1,2,3)
g1 <- g
V(g1)$color = c(1,2,2)
g2 <- g
V(g2)$color = c(2,1,1)
# Find canonical topological labeling and then canonical coloring
g1 <- permute.vertices(g1, canonical.permutation(g1)$labeling)
g2 <- permute.vertices(g2, canonical.permutation(g2)$labeling)
V(g1)$color <- canonical(V(g1)$color)
V(g2)$color <- canonical(V(g2)$color)
par(mfrow=c(1,2))
palette(rainbow(3))
plot(g1)
plot(g2)
现在会isomorfic被检测:
#vf2 wants colors to be the same, not "up to a relabeling"
# this is why we use canonical colors
graph.isomorphic.vf2(g1, g2)$iso
TRUE
故障实例:
在这个例子中,这是行不通的:
g1 <- graph.empty()
g1 <- g1 + vertices(1,2)
g1 <- g1 + edge(1,2)
V(g1)$color = c(1,2)
g2 <- graph.empty()
g2 <- g2 + vertices(1,2)
g2 <- g2 + edge(2,1)
V(g2)$color = c(2,1)
# Find canonical topological labeling and then canonical coloring
g1 <- permute.vertices(g1, canonical.permutation(g1)$labeling)
g2 <- permute.vertices(g2, canonical.permutation(g2)$labeling)
V(g1)$color <- canonical(V(g1)$color)
V(g2)$color <- canonical(V(g2)$color)
par(mfrow=c(1,2))
palette(rainbow(3))
plot(g1)
plot(g2)
graph.isomorphic.vf2(g1,g2)$iso
# FALSE
看起来像'graph.subisomorphic(g1,g2)'返回TRUE?你有没有特别想使用'vf2'算法?您可能想要查看'?graph.subisomorphic.vf2'帮助页面上列出的参考资料,以了解该算法有任何已知的缺点。 – MrFlick
我使用'vf2'的原因是,看着文档,在我看来,唯一的算法是处理颜色。 – alberto