jar文件的图形同构

时间:2012-06-06 14:22:03

标签: java python ruby graph isomorphism

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

感谢。

1 个答案:

答案 0 :(得分:1)

以下是努力使用NetworkX isomorphism checking作为理解我要理解的内容的基础......

想象一下,使用例如the methods here创建包含两个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}

希望这会有所帮助。