问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/10916159/graph-isomorphism-for-jar-files