图形同构的jar文件 [英] Graph isomorphism for jar files
问题描述
我正在处理* .jar文件和图同构。我想检查两个* .jar文件之间的图同构。是否有一个python或红宝石的库为此。我可以用igraph做什么或做什么?
谢谢。
这是一个努力使用 NetworkX同构检查作为做我的基础理解你问的问题......假设你使用两个jar文件的内容创建文本文件,例如 .sun.com / developer / Books / javaprogramming / JAR / basics / view.htmlrel =nofollow>这里的方法。
这段代码将加载两个jar文件并将图加载到NetworkX中。这里的例子是简化的,每个路径名只有两个级别,但总体原则保持不变。如果您发布了一些示例内容,我们可以调整 get_edges()
函数来处理更深层次的嵌套。
将networkx作为nx
从networkx.algorithms导入同构
#列出了两个jar文件的内容,如
#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('\\\
','/').split('/'))
nodes = dict(zip(nodes,range(len(nodes))))
edges = [edge.split('/')边缘在jar.split('\\\
')]
边缘= [(节点[边缘[0]],节点[边缘[1]])边缘边缘]
返回边缘
if __name__ =='__main__':
G1 = nx.Graph()
G1.add_edges_from(get_edges(jar1))
G2 = nx.Graph()
G2.add_edges_from(get_edges(jar2))
print'jar1:',G1.edges()
print'Edges from jar2:',G2.edges()
GM = isomorphism.GraphMatcher(G1,G2)
print'Isomorphic:',GM.is_isomorphic()
print'两个罐子之间的映射:',GM.mapping
这会打印:
来自jar1的边缘:[(0,4), (1,5),(0,6),(1,4),(1,5),(1,7),(2,4),(2,6),(2,7),(3) ,5),(3,6),(3,7)]
来自jar2的边缘:[(0,2),(0,3),(0,4),(1,2),( (1,5),(2,6),(3,6),(3,7),(4,7),(5,6),(5,7)]
同构:真
两个罐之间的映射:{0:0,1:1,2:6,3:7,4:2,5:4,6:3,7:5}
希望这有助于。
I'm working with *.jar files and on graph isomorphism. I want to check for graph isomorphism between two *.jar files. Is there a library for python or ruby for this. Can i do it with igraph or what ?
thanks.
Here is an effort at using NetworkX isomorphism checking as a basis for doing what I understand you to be asking...
Imagine you create text files with the contents of two jar files using, for example, the methods here.
This code would load the two jar files and load the graphs into NetworkX. The example here is simplified with only two levels in each pathname, but the general principle remains the same... If you post some example contents, we could tweak the get_edges()
function to deal with deeper levels of nesting.
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
This would print:
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}
Hope this helps.
这篇关于图形同构的jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!