-1

我正在使用 *.jar 文件和图同构。我想检查两个 *.jar 文件之间的图形同构。是否有用于 python 或 ruby​​ 的库。我可以用 igraph 或什么来做吗?

谢谢。

4

1 回答 1

1

这是使用NetworkX 同构检查作为执行我理解您要问的事情的基础的努力...

想象一下,您使用例如此处的方法创建包含两个 jar 文件内容的文本文件。

此代码将加载两个 jar 文件并将图形加载到 NetworkX 中。这里的示例被简化为每个路径名中只有两个级别,但一般原则保持不变......如果您发布一些示例内容,我们可以调整get_edges()函数以处理更深层次的嵌套。

import networkx as nx
from networkx.algorithms import isomorphism

# Contents of two jar files listed, as in
# http://java.sun.com/developer/Books/javaprogramming/JAR/basics/view.html
jar1 = '''a/g
a/h
a/i
b/g
b/h
b/j
c/g
c/i
c/j
d/h
d/i
d/j'''

jar2 = '''1/2
2/3
3/4
4/1
5/6
6/7
7/8
8/5
1/5
2/6
3/7
4/8'''

def get_edges(jar):
    nodes = set( jar.replace('\n', '/').split('/') )
    nodes = dict( zip(nodes, range(len(nodes)) ) )
    edges = [ edge.split('/') for edge in jar.split('\n') ]
    edges = [ (nodes[ edge[0] ],nodes[ edge[1] ]) for edge in edges ]
    return edges

if __name__ == '__main__':
    G1 = nx.Graph()
    G1.add_edges_from( get_edges(jar1) )

    G2 = nx.Graph()
    G2.add_edges_from( get_edges(jar2) )
    print 'Edges from jar1: ', G1.edges()
    print 'Edges from jar2: ', G2.edges()

    GM = isomorphism.GraphMatcher(G1,G2)
    print 'Isomorphic: ', GM.is_isomorphic()
    print 'Mapping between the two jars: ', GM.mapping

这将打印:

Edges from jar1:  [(0, 4), (0, 5), (0, 6), (1, 4), (1, 5), (1, 7), (2, 4), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)]
Edges from jar2:  [(0, 2), (0, 3), (0, 4), (1, 2), (1, 4), (1, 5), (2, 6), (3, 6), (3, 7), (4, 7), (5, 6), (5, 7)]
Isomorphic:  True
Mapping between the two jars:  {0: 0, 1: 1, 2: 6, 3: 7, 4: 2, 5: 4, 6: 3, 7: 5}

希望这可以帮助。

于 2012-06-06T18:58:07.253 回答